Dear ttom,
I have found that the operation of the clock trimming in the various HCS08 devices rather confusing too!
Some comments:
===========================
For ICSTRM & FTRIM bit in ICSSC
===========================
For SOME devices (e.g. -SV16 -SE8) these locations are loaded from a hidden Flash location upon power-on-reset. The location contains a factory-trimmed value.
For SOME devices (e.g. -SG16) these locations are reset to 0x80 & 0 upon power-on-reset. It is up to your program to copy the values from regular flash locations (NVTRIM:NVFTRIM). This is usually done in the reset code.
For SOME devices (e.g. -LG32) these locations are automatically loaded from the regular flash locations (NVTRIM:NVFTRIM) upon power-on-reset.
To further confuse - Some devices have the (NVTRIM:NVFTRIM) locations pre-programmed with factory trimmed values. Others are programmed to zero (according to the manual!).
Note also that the value in ICSTRM & FTRIM bit in ICSSC are NOT affected by normal resets - only POR.
Finally - If operating in debug mode using a BDM then the above automatic initialisation from Flash locations doesn't happen and they are reset to default values of $80/0 only (I think!).
The outcome - very Carefully read the manual for the actual chip you are using to determine what happens. The app notes may not apply directly to your chip! The only safe alternative that works in all three cases is to assume the second case, do your own trimming & copying.
The above is further confused by whether the programmer you are using will:
Determine trim values.
Preserve the factory trim values.
Comments on your specific questions:
So I don't understand 3 things:
Why is the trim value == 0 (in flash) on my hardware with the S08SG16?
This sound like your BDM is not trimming the chip correctly OR
The program you are loading has a value for the trim location which is being used instead.
And why is this value not transferred to the working register ICSTRM?
See above - The transfer is not automatic for the -SG32. In any case, the transfer wouldn't happen when using a BDM anyway.
Why is the trim value on the demo board (with 9S08SG32) in flash different to the value in the working register? (0xb4 and 0xb3).
Differ by 1 - No idea! I don't even know why its getting initialised at all.
I hope the above helps rather than confuses.
bye
Example code to manually copy the trim values from Flash to clock registers:
// These locations _may not_ be copied automatically
// The volatile is needed to stop optimisation.
// The initialisation is needed to prevent the automatic creation of
// default variables with zero values.
volatile const byte NVFTRIM_INIT @0x0000FFAE = 0xFF;
volatile const byte NVICSTRM_INIT @0x0000FFAF = 0xFF;
// This location is automatically copied to registers
// Alter to modify the security
const byte NVOPT_INIT @0x0000FFBF = NVOPT_SEC01_MASK;
void init(void) {
SOPT1 = SOPT1_BKGDPE_MASK; // Disable COP, enable BKGD pin
if (NVFTRIM_INIT != 0xFF) {
// Only done if trim locations have been programmed
ICSSC = NVFTRIM_INIT;
ICSTRM = NVICSTRM_INIT;
// FLL Engaged Internal, BDIV=/1,
ICSC1 = ICSC1_IREFS_MASK;
ICSC2 = 0x00;//|ICSC2_BDIV0_MASK;
}
}