SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By SSK
#83771
Hello Forum members,

Has anyone worked on CHECKSUM section from CrossStudio compiler? How do you implement the CHECKSUM for required sections?

How to access these checksum value from code?
How do you verify these values externally, any application?

Any type of help will be highly appreciated. Thanks in advance.
By gm
#83800
You need to set the options in the 'Linker Options' in the Project properties. Set "Checksum Algorithm" to "CRC16" and set the "Checksum Sections" to "CONST;CODE".

Below is what I use to verify the checksums:
Code: Select all
// Symbols generated by the linker for the CHECKSUM section
extern CHECKSUM_INFO_t __begin_CHECKSUM[];

************************************************
*  Main program entry
*
*************************************************/
int main(void)
{
  uint16_t	                 calculated_checksum;
  CHECKSUM_INFO_t	*p;

  // Verify the checksum of the flash memory
  for (p = __begin_CHECKSUM; p->begin || p->end; ++p)
  {
	calculated_checksum =  __checksum_crc16(p->begin, p->end);
		 
	if ( calculated_checksum != p->checksum )
	{
	    // your checksum failure procedure here
	}
  }

  // more code....
}

/* Function to compute a CRC16 checksum over the supplied memory range */
unsigned int __checksum_crc16(unsigned char *begin, unsigned char *end)
{
  unsigned short crc = 0;
  while (begin < end)
    {
      crc  = (unsigned char)(crc >> 8) | (crc << 8);
      crc ^= *begin++;
      crc ^= (unsigned char)(crc & 0xff) >> 4;
      crc ^= (crc << 8) << 4;
      crc ^= ((crc & 0xff) << 4) << 1;
    }
  return crc;
}
By SSK
#84492
Thanks gm!!
By SSK
#84906
Hello gm,

Thanks for your reply.

I tried the implementation but while checking the calculated CRC and algorithm CRC, it is failing. I even observed that the start addresses for CONST and CODE blocks are defferring from that defined in section placement file. If I have my CONST section starting from 0x2100 to 0x2FFF then the p->begin=0x2100 but p->end=0x0000. This is bit confusing. Do I need to make any other changes to CrossStudio 2?

Thanks in advance.
By jradomski
#84918
I use the following in my code.. I checksum the individual sections.. Be aware that the "X" architecture can create a few issues so care has to be taken what the address space is.. there is also another section that the compiler creates for the stubs needed to access code across the 64k boundary..

In the project under checksum you need to enable checksumming of these sections. (CODE,ISR,CONST)


/* Symbols generated by the linker for the CHECKSUM section */
extern checksum_info __begin_CHECKSUM[];
/* Symbols generated by the linker to delimit the CODE and CONST sections */
extern unsigned char __begin_CODE[], __end_CODE[]; /* crossworks documentation had variable incorrectly as __start_CODE */
extern unsigned char __begin_ISR[], __end_ISR[]; /* crossworks documentation had variable incorrectly as __start_ISR */
extern unsigned char __begin_CONST[], __end_CONST[]; /* crossworks documentation had variable incorrectly as __start_CONST */

/* Symbols generated by the linker for the CODE and CONST checksums */
extern unsigned int __checksum_CODE, __checksum_CONST, __checksum_ISR;

unsigned int Calc_checksum_CODE;
unsigned int Calc_checksum_ISR;
unsigned int Calc_checksum_CONST;

Calc_checksum_CODE = __checksum_crc16(__begin_CODE, __end_CODE);

Calc_checksum_ISR = __checksum_crc16(__begin_ISR, __end_ISR);

Calc_checksum_CONST = __checksum_crc16(__begin_CONST, __end_CONST);
By gm
#84922
Oops! Forgot to give you the struct that is needed. I was also having a problem with p->end being null. After I looked at the disassembly it appeared that the pointers were 32-bit rather than the expected 16-bit ones. I compensated for those in a modified struct.

Here's the struct that I use:
Code: Select all
/* Checksum structure */
typedef struct {
  unsigned char *begin;
	unsigned int	pad1;		// upper word of begin
  unsigned char *end;
	unsigned int	pad2;		// upper word of end
  unsigned int	checksum;
} CHECKSUM_INFO_t;
Never did get an answer from Rowley about this but since it was resolved I just went on.
Last edited by gm on Thu Nov 12, 2009 12:22 pm, edited 1 time in total.
By SSK
#84932
Thanks gm and jradomski. I will implement your suggestions and let you know.

Thanks again.
By SSK
#85164
Thanks gm and jradomski.