Dear Champs,
Our factory request the checksum should be send out via UART. So I implement a checksum calculation code as show below, the code works good if no pass word programmed into device. But it can not work after program password programmed.
Uint16 flash_checksum_calculate(Uint16 *StartAddr,Uint16 size)
{
volatile Uint16 checksum = 0;
volatile Uint16 i = 0;
for(i = 0; i < size; i++)
{
checksum += (Uint16)(*StartAddr++);
checksum = checksum&0xFFFF;
}
return checksum;
}
Uint16 flash_checksum;
void main(void)
{
init_system_and_device();
//flash start address = 0x3F0000
//flash size = 0x8000, 32KB
flash_checksum = flash_checksum_calculate((Uint16 *)0x3F0000, (Uint16)0x8000);
... ...
}
After go through system ref manual, use the C code example of secure and unsecure flash as show below, but it still can't work, and it looks to me that CPU are stuck in somewhere after using unsecure_flash and secure_flash. Can you please help me on that? What my expected is that the firmware should support to calculate flash checksum under both with and without password. Thanks...
unsecure_flash();
//flash start address = 0x3F0000
//flash size = 0x8000, 32KB
flash_checksum = flash_checksum_calculate((Uint16 *)0x3F0000, (Uint16)0x8000);
secure_flash();
My unsecure code:
void unsecure_flash(void)
{
volatile int *CSM = (volatile int *)0x000AE0; //CSM register file
volatile int *PWL = (volatile int *)0x003F7FF8; //Password location
volatile int tmp[8];
int I = 0;
// Read the 128-bits of the password locations (PWL)
// in flash at address 0x3F 7FF8 - 0x3F 7FFF
// If the device is secure, then the values read will
// not actually be loaded into the temp variable, so
// this is called a dummy read.
for (I=0; I<8; I++) tmp[I] = *PWL++;
//if(secure_flag == 1)
//If the password is not all ones (0xFFFF), then the code below is required
// to unsecure the CSM.
// Write the 128-bit password to the KEY registers
// If this password matches that stored in the
// PWL then the CSM will become unsecure. If it does not
// match, then the device will remain secure.
// An example password of:
// 0x11112222333344445555666677778888 is used.
asm(" EALLOW"); // Key registers are EALLOW protected
*CSM++ = 0xFFFF; // Register KEY0 at 0xAE0
*CSM++ = 0xFFFF; // Register KEY1 at 0xAE1
*CSM++ = 0xFFFF; // Register KEY2 at 0xAE2
*CSM++ = 0xFFFF; // Register KEY3 at 0xAE3
*CSM++ = 0xEBEE; // Register KEY4 at 0xAE4
*CSM++ = 0xD8FF; // Register KEY5 at 0xAE5
*CSM++ = 0xE9D3; // Register KEY6 at 0xAE6
*CSM++ = 0xAB38; // Register KEY7 at 0xAE7
asm(" EDIS");
}
void resecure_flash(void)
{
volatile int *CSMSCR = (volatile int *)0x00AEF; //CSMSCR register
//Set FORCESEC bit
asm(" EALLOW"); //CSMSCR register is EALLOW protected.
*CSMSCR = 0x8000;
asm("EDIS");
}