Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crc/UserApplication.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
2 /**
3  * \file
4  *
5  * \brief
6  * Demo application for the CRC tests.
7  *
8  * This is an example that shows how the Flash and EEPROM CRC tests can be
9  * embedded in an user application. The CRC checksums of the Flash and a data
10  * section in EEPROM are pre-calculated and stored in EEPROM. During normal
11  * execution an LED is lighted while the system runs without errors. Pressing
12  * a button leads to CRC test of the Flash and the data section in EEPROM:
13  * new checksums are computed and compared with the ones stored in EEPROM.
14  * If any of the pairs were different, the error flag would be set (within
15  * the test) and the LED would be switched off. This can be tested for example
16  * by declaring the symbols \ref MODIFY_FLASH and \ref MODIFY_EEPROM.
17  *
18  * Note that if the program is modified, the stored CRC needs to be computed
19  * again since the content in Flash will change. This can be done by debugging
20  * the program and placing a watch on the following variables:
21  * - \c checksum_test_flash
22  * - \c checksum_test_eeprom
23  * .
24  * Those values can be assigned then to
25  * - \ref classb_precalculated_flash_crc
26  * - \ref classb_precalculated_eeprom_crc
27  * .
28  *
29  * \par Application note:
30  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
31  *
32  * \par Documentation
33  * For comprehensive code documentation, supported compilers, compiler
34  * settings and supported devices see readme.html
35  *
36  * \author
37  * Atmel Corporation: http://www.atmel.com \n
38  * Support email: avr@atmel.com
39  *
40  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
41  *
42  * \page License
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright notice,
48  * this list of conditions and the following disclaimer.
49  *
50  * 2. Redistributions in binary form must reproduce the above copyright notice,
51  * this list of conditions and the following disclaimer in the documentation
52  * and/or other materials provided with the distribution.
53  *
54  * 3. The name of Atmel may not be used to endorse or promote products derived
55  * from this software without specific prior written permission.
56  *
57  * 4. This software may only be redistributed and used in connection with an
58  * Atmel AVR product.
59  *
60  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
63  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
64  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
70  * DAMAGE.
71  */
72 
73 #include "avr_compiler.h"
74 #include "classb_crc.h"
75 
76 
77 #ifdef __DOXYGEN__
78  //! \brief If declared the content of the flash is modified.
79  //!
80  //! If declared an endless loop is compiled at the end of the main application.
81  //! This changes the program code and thus the flash content. The previously stored
82  //! checksum will then be different than the new one.
83  #define MODIFY_FLASH
84 #else
85  #define MODIFY_FLASH
86 #endif
87 
88 #ifdef __DOXYGEN__
89  //! \brief If declared the content of the EEPROM data section is modified.
90  //!
91  //! If declared an endless loop is compiled at the end of the main application.
92  //! This changes the program code and thus the flash content. The previously stored
93  //! checksum will then be different than the new one.
94  #define MODIFY_EEPROM
95 #else
96  //#define MODIFY_EEPROM
97 #endif
98 
99 #if __AVR_ATxmega256A3BU__ | __ATxmega256A3BU__
100 # define LEDPORT PORTR
101 # define SWITCHPORT PORTE
102 # define SWITCH_INT0_vect PORTE_INT0_vect
103 # define XPLAIN_PULLUP 0x00
104 #elif defined(__AVR_ATxmega128A1__) | defined(__ATxmega128A1__)
105 # define LEDPORT PORTE
106 # define SWITCHPORT PORTF
107 # define SWITCH_INT0_vect PORTF_INT0_vect
108 # define XPLAIN_PULLUP PORT_OPC_PULLUP_gc
109 #endif
110 
111 //! \brief This function sets up the LED and switch.
112 //! The LED is switched on to show that the system is working correctly.
113 //! The switch is configured to generate an interrupt after being pressed.
115 
116  // Set direction and state of LED pins
117  LEDPORT.DIRSET = PIN0_bm | PIN1_bm;
118  PORTCFG.MPCMASK = PIN0_bm | PIN1_bm;
119  LEDPORT.PIN0CTRL |= PORT_INVEN_bm;
120 
121  // Set sensitivity, polarity and pull-up for button PIN
122  SWITCHPORT.PIN5CTRL = PORT_ISC_FALLING_gc | PORT_INVEN_bm | XPLAIN_PULLUP;
123  SWITCHPORT.INT0MASK |= PIN5_bm;
124  SWITCHPORT.INTCTRL |= PORT_INT0LVL_LO_gc;
125 
126  // Enable LOW level interrupts in the INT controller
127  PMIC.CTRL |= PMIC_LOLVLEN_bm;
128 
129  // Turn on LED that signals correct operation.
130  LEDPORT.OUTSET = PIN0_bm;
131 }
132 
133 
134 //!\brief This variable contains the CRC checksum for the flash and is stored in EEPROM.
135 #if defined(__GNUC__) || defined(__DOXYGEN__)
136 uint32_t EEPROM_DECLARE( classb_precalculated_flash_crc ) = 0xe7138241;
137 #elif defined(__ICCAVR__)
138 uint32_t EEPROM_DECLARE( classb_precalculated_flash_crc ) = 0x4E4C04C6;
139 #endif
140 
141 //!\brief This variable contains the CRC checksum for the data section in EEPROM. It is stored in EEPROM.
142 uint16_t EEPROM_DECLARE( classb_precalculated_eeprom_crc ) = 0xcd4b;
143 
144 #if !defined(MODIFY_EEPROM) | defined(__DOXYGEN__)
145  //! \brief Data stored in EEPROM.
146  uint8_t EEPROM_DECLARE( data_eeprom[] ) = {0,1,2,3,4,5,6,7,8,9,10};
147 #else
148  uint8_t EEPROM_DECLARE( data_eeprom[] ) = {0,0,0,0,0,0,0,0,0,0,0};
149 #endif
150 
151 //! \brief Global error flag
152 NO_INIT volatile uint8_t classb_error;
153 
154 //The current toolchain version would not show correct values unless they were declared globally.
155 volatile uint32_t checksum_test_flash_2;
156 volatile uint32_t checksum_test_flash_3;
157 volatile uint16_t checksum_test_flash_4;
158 volatile uint16_t checksum_test_flash_5;
159 int main(void)
160 {
161 
163  // Enable interrupts:
164  sei();
165 
166  while(!classb_error) {
167  // Do nothing
168  };
169 
170  // If this is executed there has been an error.
171  // Disable interrupts
172  cli();
173  // Turn off LED to indicate error.
174  LEDPORT.OUTCLR = PIN0_bm;
175 
176 #ifdef MODIFY_FLASH
177  while(1);
178 #endif
179 
180 }
181 
182 
183 //! \brief Interrupt service routine for button press.
184 //!
185 //! This will compute the checksums of the Flash and data section in EEPROM and compare
186 //! them with the ones stored in EEPROM. There some code lines that could be uncommented
187 //! in order to check the other CRC implementations.
188 ISR(SWITCH_INT0_vect) {
189 
190  // Compute the checksum of the Flash
191  volatile uint32_t checksum_test_flash = CLASSB_CRC32_Flash_HW (CRC_APP, 0, 0, &classb_precalculated_flash_crc);
192  //// These lines should produce the same checksum
193  //checksum_test_flash_2 = CLASSB_CRC32_Flash_HW (CRC_FLASH_RANGE, APP_SECTION_START, APP_SECTION_SIZE, &classb_precalculated_flash_crc);
194  //checksum_test_flash_3 = CLASSB_CRC32_Flash_SW (APP_SECTION_START, APP_SECTION_SIZE, &classb_precalculated_flash_crc);
195  //// These lines should produce the same checksum (different than the CRC16 checksum)
196  //checksum_test_flash_4 = CLASSB_CRC16_Flash_HW (APP_SECTION_START, APP_SECTION_SIZE, &classb_precalculated_flash_crc);
197  //checksum_test_flash_5 = CLASSB_CRC16_Flash_SW (APP_SECTION_START, APP_SECTION_SIZE, &classb_precalculated_flash_crc);
198 
199  // Compute the checksum of the data section in EEPROM
200  volatile uint16_t checksum_test_eeprom = CLASSB_CRC16_EEPROM_HW(data_eeprom, sizeof(data_eeprom), &classb_precalculated_eeprom_crc);
201  //// This line should produce the same checksum
202  //volatile uint16_t checksum_test_eeprom_2 = CLASSB_CRC16_EEPROM_SW(data_eeprom, sizeof(data_eeprom), &classb_precalculated_eeprom_crc);
203  //// These lines should produce the same checksum (different than the CRC16 checksum)
204  //volatile uint32_t checksum_test_eeprom_3 = CLASSB_CRC32_EEPROM_HW(data_eeprom, sizeof(data_eeprom), &classb_precalculated_eeprom_crc);
205  //volatile uint32_t checksum_test_eeprom_4 = CLASSB_CRC32_EEPROM_SW(data_eeprom, sizeof(data_eeprom), &classb_precalculated_eeprom_crc);
206 
207 }
208 
209