CodeSamples
Code examples for avr-gcc
CRC-Check of Programm Memory
Changes to the makefile, to append a calculated CRC at the end of the flash memory. The calculation is done with the srecord program, included in the winavr distribution.
More infos: http://srecord.sourceforge.net/
# Output format. (can be srec, ihex, binary)
FORMAT = srec
# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
mv $@ $(TARGET).org.hex
srec_cat $(TARGET).org.hex -Little_Endian_CRC16 -max $(TARGET).org.hex -Cyclic_Redundancy_Check_16_XMODEM -Fill 0 -OVER $(TARGET).org.hex -Output $(TARGET).hex
# The following lines are for debugging only
srec_cat $(TARGET).org.hex -O $(TARGET).org.bin -Binary
srec_cat $(TARGET).hex -O $(TARGET).bin -Binary
# Target: clean project.
clean: begin clean_list finished end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).org.hex
$(REMOVE) $(TARGET).bin
$(REMOVE) $(TARGET).org.binRemarks:
I use the motorola srecord format for the hex-files. This is the default
file format of the srec_cat program, that I use for postprocessing the .hex fileI create binary files in addition to the .hex files.
This is only needed, because my .hex editor cannot read the motorola srecord format
The following code was written for use with FreeRTOS.
#include <util/crc16.h>
uint8_t err_bError;
uint16_t err_crcCalc;
uint16_t err_crcStored;
void err_checkcrc(void)
{
uint16_t wCrc, i;
uint16_t wFlashEnd;
uint8_t bData = 0;
wFlashEnd = (uint16_t) &(__data_load_end[0]);
PORTB |= (1 << PB2);
wCrc = 0;
for (i = 0; i < wFlashEnd; i++){
bData = pgm_read_byte(i);
wCrc = _crc_xmodem_update(wCrc, bData);
}
PORTB &= ~(1 << PB2);
// uncomment the following line, if you don't use FreeRTOS
portENTER_CRITICAL();
err_crcStored = pgm_read_word(wFlashEnd);
err_crcCalc = wCrc;
// uncomment the following line, if you don't use FreeRTOS
portEXIT_CRITICAL();
// set error no 1, if crc error detected
if (err_crcStored != err_crcCalc) err_bError=1;
return;
}