The Datasheet Archive - 100 Million Datasheets from 7500 Manufacturers.    


Datasheet Search Engine   
 
Part # or Description: • 5V RS232 Driver • 2SC5066* • "Real Time Clock" • "USB connector" • "blue led" 5mm • 10 watt zener diode • 2N3055* motorola
 
Search Tip: Try entering the part number only. Include a wildcard (eg. lm317* or 1n4148*)

 

 

Generation Checking Program Memory Supports AVR® Controllers


Datasheet Thumbnail

  

Download PDF



Top Searches for this datasheet



AVR236: Check Program Memory
Generation Checking
Program Memory
Supports AVR® Controllers with
Instruction
Compact Code Size, words
(CRC Generation Checking)
Requirement Checksum Stored EEPROM Execution time:
(AT90S8515 MHz)
bits Implementation, Easily Modified
bits
Supports CRC-16 Standard, Easily
Modified CRC-CCITT, CRC-32
Introduction
This application note describes (Cyclic Redundancy Check) theory
implementation detect errors program memory Atmel microcontroller. widely used method detecting errors messages transmitted over noisy channels. standards secure microcontroller applications introduced method detecting errors program memory microcontrollers. preferable implement calculation compact code with requirement data storage memory since frees resources actual application. implementation used this application note optimized minimum code size register usage.
AVR236 Application Note
Figure checking program memory using 16-bit divisor.
BYTE ADDRESS CODE 0011011 10011100 10000101 1011 ADDRESS 1FFF
DIVISOR
10000000 00000101
Rev. 1143A-10/98
Theory Operation
Checksums originally used communication through noisy channels. number (the checksum) computed function transmitted message. receiver uses same function compute checksum, compares computed value with value received from transmitting side. this application note checksum constructed function code, stored internal EEPROM. microcontroller later same function calculate checksum code compare with appended checksum. Example: Checksum calculated summing numbers code:
Code: Original code with checksum:04 Code with error: 39-> Correct
Code with checksum:
This checksum simply numbers code. second byte code corrupted from error will detected when original checksum compared with computed checksum.
Original code with checksum:04 Code with error: Wrong
problem with this checksum that simple. detect errors multiple bytes code detect errors checksum itself. This example shows that addition sufficient detect errors. calculations division instead addition calculate checksum code. principles similar, using division multiple errors burst errors will detected. algorithm treat program memory enormous binary number, which divided another fixed binary number. remainder this division checksum. microcontroller will later perform same division compare remainder with calculated checksum. Note that division uses polynomial (modulo-2) arithmetic, which similar regular binary arithmetic, except uses carry. addition numbers with polynomial arithmetic simply XOR'ing data. Example: Addition polynomial arithmetic: 1011 0110 1101 0011 0110 0101
first byte code corrupted from second byte corrupted from checksum will detect errors.
addition equal XOR'ing numbers.
AVR236
AVR236
Lets define some properties polynomial arithmetic: M(x) k-bit number (the code checked). G(x) (n+1) number (the divisor polynom). R(x) n-bit number such that (the remainder checksum). R(x) M(x)*2 Q(x) Where Q(x) quotient G(x) G(x)
Q(x) described
M(x)*2 R(x) Q(x) M(x) equals adding zeros code G(x)
M(x)*2 replaced last equation G(x)
R(x) R(x) M(x)*2 +R(x) -=Q(x) Q(x)+ -G(x) G(x) G(x) Which equal Q(x) since divisor remainder same number, adding itself same XORing which results zero.
Example Division
hexadecimal number which binary number 0110 1010, divided with divisor 1001 hex). checksum will remainder operation 0110 1010 divided with 1001.
011010100000 1001 01100 0000 1101 1001 1000 1001 0011 0000 0110 0000 1100 1001 1010 1001 0110 0000 1100 1001 0101 Remainder Checksum Quotient Ignored
First append zeros original message (where width divisor).
checksum added original code. resulting code will 6A5. When this code checked, code checksum divided divisor. remainder this division zero errors occurred, non-zero otherwise.
CRC-16 1000 0000 0000 0101= 8005(hex) CRC-CCITT 0001 0000 0010 0001= 1021 (hex) CRC-32
Several standards used today detection. characteristics divisor vary from bits, ability detect errors varies with width divisor used. Some commonly used divisors are:
0000 0100 1100 0001 0001 1101 1011 0111 04C11DB7 (hex)
Observe that bits divisors, actual numbers bits bits divisor number bits always
AVR236
AVR236
Software Description
Main Program
main program supplied show operation both generation checking. checksum generated stored internal EEPROM, read back before checking performed. most applications, checksum will generated programmer placed last address program memory. Figure Flowchart Main Program
START
main program call routine CRC_gen with status register 0x00 after reset generate checksum code. generated checksum stored EEPROM. check checksum routine CRC_gen called with status register 0xFF, value different from 0x00.
Checksum Generation
operation based principle rotating entire program memory bit. shifted into carry flag. carry flag (one), word XOR'ed with divisor. Note that program memory which shifted into carry flag also XOR'ed with divisor. Since they both result will always zero division ignored. program memory zeros appended code. checksum resulting value complete operation.
INITIALIZATION UNIT STACK POINTER PORTS
STATUS
CRC_GEN GENERATE CHECKSUM STORE CHECKSUM EEPROM
STATUS
LOAD CHECKSUM FROM EEPROM
CRC_GEN CHECK CHECKSUM
OUTPUT CHECKSUM
Checksum Checking
same principles applied generation, generated checksum appended code, replacing zeros. result calculation including appended checksum zero errors occurred, nonzero otherwise. checksum included program code, only checking part computation needs done program code. same routine used both generation checking. global register status loaded with 0x00 function call perform generation. status register loaded with value different from 0x00 function call, function performs checksum checking. flowchart shows flow crc_gen routine which includes both generation checking. flowcharts Figure Figure describes operation crc_gen subroutine. Figure CRC_gen Subroutine
START
Figure Rotate Subroutine
START
LOAD COUNTER (HEX)
DECREMENT COUNTER
COUNTER
SHIFT DATA LEFT
CARRY CODE DIVISOR
LOAD DIVISOR VALUE
LOAD INITIAL VALUE
LOAD BYTES
RETURN
CODE
ROTATE WORD ROUTINE
STATUS
APPEND ZEROS
APPEND CHECKSUM
ROUTINE WORD ROUTINE
RETURN
AVR236
AVR236
Modifications
code example implements 16-bit checksum CRC16 computation. code easily modified support 32bit checksum increasing size code buffer from bits, increasing size divisor from bits. checksum generated programmer placed last memory location, only code checking checksum needs included program. code "end" section routine removed. Please comments code. Some CRC-algorithms requires data register have initial value different from 0x00. other values used, initial values loaded into registers, replacing first instructions. comments code more information. algorithm reflected, which means that bytes shifted first instead MSB, routine support this replacing (Logical shift left) (Rotate left) instructions with (Logical shift right) (Rotate right) instructions. Other implementations computation exists with higher speed, most them lookup table increase speed operation. requirements such application makes them suitable more complex systems. C-code examples table driven implementation obtained from avr@atmel.com.
Resources
Table Memory Usage
Function main CRC_gen EEwrite EERread TOTAL Code Size words words words words words Cycles 700.000 (approx.) cycles cycles Register Usage R16, R22, R23, R24, R17, R18, R19, R20, R21, R22, R30, R16, R23, R24, R16, R23, R24, Interrupt Description Initialization example program Generate check checksum Write checksum EEPROM Read checksum from EEPROM
Table Peripheral Usage
Peripheral bytes EEPROM pins Description Storing value Output byte LEDs
Code Listing
;**** Title: Version: Last updated: Target: Support E-mail: NOTE: Always check Atmel site, www.atmel.com latest ;*updated version this software. DESCRIPTION This application note describes perform computation code memory contents using simple algorithm. generate checksum load register "status" with call ;*routine "crc_gen". resulting checksum placed registers byte2(low byte) byte3(high byte). check checksum load register "status" with call ;*routine "crc_gen". resulting checksum placed registers byte2(low byte) byte3(high byte). checksum program code ;*is correct, checksum non-zero error been introduced ;*program code avr@atmel.com check program memory 98.06.15 AT90Sxxxx (All Devices with instr) R236 ************************
.include "8515def.inc" .device AT90S8515
;***** Constants .equ .equ PROGSIZE 0x1FFF 0x8005 Size program memory(bytes) divisor value
.cseg PROGRAM START EXECUTION STARTS HERE
.org $0000 rjmp RESET ;Reset handle
AVR236
AVR236
"crc_gen" Generation checking checksum This subroutine generates checksum program code. bits loaded into register, upper bits XORed with divisor value each time shifted into carry flag from MSB. status byte 0x00,the routine will generate checksum: After computing code zeros appended code checksum calculated. status byte different from 0X00, routine will check current checksum valid. After computing code original checksum appended code calculated. result zero errors occurs result placed registers byte2 byte3 Number words Number cycles registers used High registers used return :program memory size(word)*175(depending memory content) (byte0,byte1,byte2,byte3)
;***** Subroutine Register Variables .def .def .def .def .def .def .def .def .def .def .def .def byte0 byte1 byte2 byte3 crch sizel sizeh divisor register Lower byte lower word Upper byte lower word Lower byte upper word Upper byte upper word checksum byte checksum high byte Program code size register
crdivl crdivh count
counter Status byte: generate(0) check(1)
status
crc_gen:ldi adiw
sizel,low(PROGSIZE) ;Load program memory address sizeh,high(PROGSIZE) crdivh,high(CR) crdivl,low(CR) ;Load first memory location byte3,byte0 zl,0x01 byte2,byte0 ;Move highest byte ;Increment pointer ;Load second memory location ;Load divisor value ;Clear pointer
new_word:cp brge adiw adiw rcall rjmp
zl,sizel zh,sizeh zl,0x01
;Loop starts here ;Check code ;Jump code
;Load high byte byte1,byte0 zl,0x01 ;Move upper byte ;Increment pointer ;Load program memory location rot_word new_word ;Call rotate routine
end: brne rjmp check:mov gen:rcall
;ret count,0x11 status,0x00 check byte0 byte1 byte0,crc byte1,crch rot_word crc,byte2 crch,byte3
;uncomment this line checksum last flash memory address.
;Append bits(0x0000) ;the code generation ;Append original checksum ;the code checking ;Call rotate routine
;Return main prog
rot_word:ldi count,0x11 rot_loop:dec count breq brcc rjmp stop:ret stop byte0 byte1 byte2 byte3 rot_loop byte2,crdivl byte3,crdivh rot_loop ;XOR high word ;Loop ;Decrement counter ;Break counter ;Shift zero into lowest ;Shift carry from previous byte ;Preceed shift
EERead_seq This routine reads EEPROM into global register variable "temp". Number words Number cycles return return
High Registers used (temp,eeadr,eeadrh,eedata)
AVR236
AVR236
.def .def .def .def temp eeadr eeadrh eedata
;***** Code
eeread: EEARH,eeadrh EEARL,eeadr EECR,EERE eedata,EEDR ;output address high byte ;output address byte ;set EEPROM Read strobe ;get data
EEWrite This subroutine waits until EEPROM ready programmed, then programs EEPROM with register variable "EEdwr" address "EEawr" Number words Number cycles Registers used return return EEPROM ready) :None
High Registers used: (temp,eeadr,eeadr,eedata)
.def .def .def .def
temp eeadr eeadrh eedata
EEWE clear Wait more
eewrite:sbic EECR,EEWE rjmp EEWrite EEARL,eeadr EEDR,eedata EECR,EEMWE EECR,EEWE
EEARH,eeadrh ;Output address high byte ;Output address byte ;Output data ;Set EEPROM Write strobe
.cseg .def .def .def crch temp ;Low byte checksum returned ;High byte checksum returned Start Main Program
.def .def .def .def
status eeadr eeadrh eedata
;Status byte: generate(0) check(1)
RESET:ldi
r16,high(RAMEND) SPH,r16 r16,low(RAMEND) SPL,r16
;Initialize stack pointer ;High byte only required ;RAM bigger than Bytes
temp,0xff DDRB,temp PORTB,temp status ;Set PORTB output ;Write 0xFF PORTB ;Clear status register,ready generation
rcall crc_gen
eeadr,0x01 eeadrh,0x00 eedata,crc
;Set address byte EEPROM write ;Set address high byte EEPROM write ;Set byte EEPROM data ;Write EEPROM ;Set address byte EEPROM write ;Set address high byte EEPROM write ;Set high byte EEPROM data ;Write EEPROM
rcall eewrite eeadr,0x02 eeadrh,0x00 eedata,crch
rcall eewrite
PORTB,crc
;Output value PORTB
mainloop: sbic EECR,EEWE rjmp mainloop EEWE clear Wait more
;********** Insert program code here *************
eeadr,0x01 eeadrh,0x00
;Set address byte EEPROM read ;Set address high byte EEPROM read ;Read EEPROM ;Read byte from EEPROM ;Set address byte EEPROM read ;Set address high byte EEPROM read ;Read EEPROM ;Read byte from EEPROM ;Set status register, prepare checking
rcall eeread crc,eedata eeadr,0x02 eeadrh,0x00
rcall eeread crch,eedata status
rcall crc_gen loop: PORTB,crc rjmp loop .exit ;Output value PORTB
AVR236
AVR236
References
Fred Halsall "Data Communication, Computer Networks Open Systems" 1992 Addison-Wesley Publishers "The Painless Guide Error Detection Algorithms"
Ross Williams
AVR236
AVR236
Atmel Headquarters
Corporate Headquarters
2325 Orchard Parkway Jose, 95131 (408) 441-0311 (408) 487-2600
Atmel Operations
Atmel Colorado Springs
1150 Cheyenne Mtn. Blvd. Colorado Springs, 80906 (719) 576-3300 (719) 540-1759
Europe
Atmel U.K., Ltd. Coliseum Business Centre Riverside Camberley, Surrey GU15 England (44) 1276-686677 (44) 1276-686697
Atmel Rousset
Zone Industrielle 13106 Rousset Cedex, France (33) (33)
Asia
Atmel Asia, Ltd. Room 1219 Chinachem Golden Plaza Mody Road Tsimshatsui East Kowloon, Hong Kong (852) 27219778 (852) 27221369
Japan
Atmel Japan K.K. Tonetsu Shinkawa Bldg., 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan (81) 3-3523-3551 (81) 3-3523-7581
Fax-on-Demand
North America: 1-(800) 292-8635 International: 1-(408) 441-0732
e-mail
literature@atmel.com
Site
http://www.atmel.com
1-(408) 436-4309
Atmel Corporation 1998. Atmel poration makes warranty products, other than those expressly contained Company's standard warranty which detailed Atmel's Terms Conditions located Company's website. Company assumes responsibility errors which appear this document, reserves right change devices specifications detailed herein time without notice, does make commitment update information contained herein. licenses patents other intellectual proper Atmel granted Company connection with sale Atmel products, expressly implication. Atmel's products authorized critical components life suppor devices systems. Marks bearing
and/or
registered trademarks trademarks Atmel Corporation. Printed recycled paper.
1143A-10/98/xM
Terms product names this document trademarks others.

Other recent searches


LY88330 - LY88330   LY88330 Datasheet
JTOS-1910+ - JTOS-1910+   JTOS-1910+ Datasheet
HI7190 - HI7190   HI7190 Datasheet
AN9532 - AN9532   AN9532 Datasheet
HAT2040R - HAT2040R   HAT2040R Datasheet
FLM6472-25F - FLM6472-25F   FLM6472-25F Datasheet
CY23S08 - CY23S08   CY23S08 Datasheet

 

Privacy Policy | Disclaimer
© 2012 Datasheet Archive