Checksum Overview

Checksum is basically used to determine the data integrity whether it is data
transferred from one system to other using some communication protocol or from
hex file to microcontroller using a programmer.


When we talk about checksum in reference to programming a Microchip
microcontroller, it is essentially calculated based on the algorithm defined in
the programming specification of the particular microcontroller. User can
determine the integrity of the data which is being programmed by verifying the
checksum. User can read the device using the programmer and verify the checksum
displayed in the MPLAB IDE or PM3 programmer
display.


Checksum also helps to keep track of the files associated with a particular
product in the production houses or development center. Generally it is
believed that the checksum is stored inside the device automatically but this
is not the case.


Checksum is basically calculated using the algorithms provided in the
programming specifications. Generally it is calculated by summing up contents
of the program memory, configuration memory and/or user id locations.


Generally there are two cases to calculate the checksum of each Microchip
microcontroller


1.) Un-code protected checksum
This is calculated by summing up all the program memory locations and
configuration word. The algorithm can be found in the programming specification
to calculate program memory sum and configuration. They are two different
methods altogether to sum the contents of program memory and configuration
memory. Configuration memory bytes are masked with the mask values provided in
the programming specification before summing up.


2.) Code protected checksum  
This is calculated when the device is code protected for the program memory by
setting the configuration bits. In this case the program memory is not included
into the calculation rather user ids are added if they are available. For some
devices for which configurations are part of the flash memory, code protect
checksum is zero. In this case all the configuration words read as zero when
the device is code protected.


Code Guard Checksum
There are some devices where we can code protect different segments of
program memory. In this case the checksum is calculated by summing up all the
contents of the all other program memory segments along with the configurations
applying their appropriate masks.