Skip to end of banner
Go to start of banner

9. Check Sum Options

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

9.1 Default Checksum and PSA Calculation Used to Verify Flash Programming

The Main GUI has three values for check sum (CS) calculation for programming purposes (Figure 9.1). These values are always calculated and the type of checksum algorithm cannot be changed. These are the Source, Code, and Memory values.

  • The first, Source value, is calculated on the input code file contents that fit within the MCU's memory space.

  • The second, Code value, is calculated based on SetupMemory Options settings. If the Memory Options include the entire code file then the Source and Code values should be identical. The only exception to that is if the user configures serialization in Serialization→Serialization Setup, or custom CS in Setup→Check Sum Options (not related to programming) to be stored in a location occupied by data from the code file. Overlapping words are excluded for purposes of CS calculation and verification. For example, a constantly changing serialization number saved within code contents will not affect the Code and Memory CS values.

  • Lastly, the third, Memory value, is calculated based on MCU memory contents. The PSA for both code and memory contents is also calculated, but is not shown in the GUI. Both the Code and Memory CS and PSA values must match for verification to pass.

The checksum displayed in the Main GUI is the arithmetic sum of the code contents. Arithmetic sum is calculated as the sum of 16-bit unsigned words - result is 32-bit unsigned. Only programmed words are taken for calculation. All other not used words are ignored. All bytes are converted to 16-bits words using Little-Endian byte order when read:

//data[] is a byte array of MCU memory contents, pseudo code omits showing cast of data[] to unsigned short for brevity
unsigned short word = data[address] + (data[address + 1] << 8) 

where address is even and incremented by 2. The PSA value is also calculated on words. Odd byte boundaries in the code file are filled in with default MCU empty values (0xFF or 0x00 depending on MCU family).

Figure 9.1: Check Sum information compared between the file and MCU memory.

9.2 Custom Checksum Setup for Verification by User Code on Boot

Custom Checksums can be generated and programmed by the flasher to be used by user firmware for code verification on boot. Up to four different CS blocks can be specified and CS results can be saved in the flash for verification. Size of each CS block and CS result location in flash are defined by the user.
The Check Sum Options dialog (Figure 9.2) is selected from following pull down menu:
Setup→Checksum Options

There are several items of note:

  • Start Address should be even, and the Stop Address should be odd. This is because data at both addresses is used inclusively.

  • CS result address in the flash should be even. Make sure that the CS result is saved outside of the code block being counted, otherwise the CS result will modify the contents of the code block itself and further verification will fail.

  • The CS result is displayed in the report window shown in Figure 9.3.

  • The type of algorithm used to calculated the CS can be selected from the following list shown in Figure 9.4. Initial value for CS calculation can be selected as zero, all 0xFFs or as the Start Address from pull down menu shown in Figure 9.5.

  • CS result can be used As Is or can be inverted as shown in Figure 9.6.

  • Data size (byte or 16 bits word) used for calculation and CS result size is displayed in the dialog screen as “Data IN word size” and “CS Result size” (Figure 9.2). Polynomial contents (if required) can be specified in the POLY edit line in HEX format ( eg. 0x1234 ).

Figure 9.2: Check Sum Options.

Figure 9.3: Check Sum Report.

Figure 9.4: Check Sum Type

.

Figure 9.5: Check Sum Initial Value.

Figure 9.6: Check Sum Inverted Option.


The following checksum algorithms are implemented (as shown in Figure 9.4).

9.2.1 Arithmetic Sum (8b/16b)


Checksum is calculated as modulo 16-bits sum of all bytes (unsigned) from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr++ )
{
  CS = CS + data[addr];
}
CS = 0xFFFF & CS;
if( cs_inverted )
  CS = 0xFFFF ^ CS; 

9.2.2 Arithmetic Sum (8b/32b)


Checksum is calculated as modulo 32-bits sum of all bytes (unsigned) from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr++ )
{
  CS = CS + data[addr];
}
CS = 0xFFFFFFFF & CS;
if( cs_inverted )
  CS = 0xFFFFFFFF ^ CS; 

9.2.3 Arithmetic Sum (16b/16b)


Checksum is calculated as modulo 16-bits sum of all 2-byte words (unsigned) from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr=addr+2 )
{
  CS = CS + data[addr] + data[addr+1];
}
CS = 0xFFFF & CS;
if( cs_inverted )
  CS = 0xFFFF ^ CS; 

9.2.4 Arithmetic Sum (16b/32b)


Checksum is calculated as modulo 32-bits sum of all 2-byte words (unsigned) from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr=addr+2 )
{
  CS = CS + data[addr] + data[addr+1];
}
CS = 0xFFFFFFFF & CS;
if( cs_inverted )
  CS = 0xFFFFFFFF ^ CS; 

9.2.5 CRC16 (Poly 0x11201) - (8b/16b) (Named as CRCCCITT) and CRC16 defined polynomial - (8b/16b)


Checksum is calculated as CRC16 from each bytes from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr++ )
{
  CS = CS_CRC16_8to16( data[addr], CS, crc_tab32 );
}
CS = 0xFFFF & CS;
if( cs_inverted )
  CS = 0xFFFF ^ CS; 

where:

unsigned int  CS_CRC16_8to16( unsigned int data, unsigned int crc, unsigned int *crc_tab32 )
{
  unsigned int tmp;
  tmp = 0xFF & ((crc >> 8) ^ data );
  crc = (crc << 8) ^ crc_tab32[tmp];
  return( 0xFFFF & crc );
}

The CRC table is generated first as follows:
CS_init_crc16_tab( 0x1021 ); for CRC CCITT
CS_init_crc16_tab( CRC_def_POLY ); for CRC16 defined polynomial
where:

void	CS_init_crc16_tab( unsigned short poly ) 
{
	int i, j;
	unsigned short crc, c;

	for (i=0; i<256; i++) 
	{
		crc = 0;
		c   = ((unsigned short) i) << 8;

		for (j=0; j<8; j++) 
		{
			if ( (crc ^ c) & 0x8000 ) 
				crc = ( crc << 1 ) ^ poly;
			else                      
				crc =   crc << 1;
			c = c << 1;
		}
		crc_tab32[i] = (unsigned int)(0xFFFF & crc);
	}
}

9.2.6 CRC32 (Poly 0x04C11DB7) - (8b/32b) (Named as IEEE 802-3) and CRC32 defined polynomial - (8b/32b)


Checksum is calculated as CRC32 from each bytes from Start Address to the End Address as follows:

unsigned int CS = CS_initial_value;
for ( addr = StartAddress; addr <= EndAddress; addr++ )
{
  CS = CS_CRC32_8to32( data[addr], CS, crc_tab32 );
}
CS = 0xFFFFFFFF & CS;
if( cs_inverted )
  CS = 0xFFFFFFFF ^ CS; 

where:

unsigned int CS_CRC32_8to32( unsigned int data, unsigned int crc, unsigned int *crc_tab32 )
{
	crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab32[0xFF & (crc ^ data ) ];
	return(crc);	
}

The CRC table is generated first as follows:
CS_init_crc32_tab( 0x04C11DB7 ) for IEEE 802-3
a polynomial of:
x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
and
CS_init_crc32_tab( CRC_def_POLY ) for CRC32 defined polynomial
where:

void	CS_init_crc32_tab( unsigned int poly_in ) 
{
	unsigned int c, poly;            /* polynomial exclusive-or pattern */

	poly = 0L;
	for (unsigned int n = 0; n < 32; n++)
	{
		poly <<= 1;
		poly |= 1L & poly_in;	
		poly_in >>= 1;
	}

	for (unsigned int n = 0; n < 256; n++)
	{
		c = (UINT32 )n;
		for (unsigned int k = 0; k < 8; k++)
			c = c & 1 ? poly ^ (c >> 1) : c >> 1;
		crc_tab32[n] = c;
	}
}
  • No labels