forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp_driver.h
293 lines (263 loc) · 9.74 KB
/
sp_driver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief XMEGA Self-programming driver header file.
*
* This file contains the function prototypes for the
* XMEGA Self-programming driver.
* If any SPM instructions are used, the linker file must define
* a segment named BOOT which must be located in the device boot section.
*
*
* None of these functions clean up the NVM Command Register after use.
* It is therefore important to write NVMCMD_NO_OPERATION (0x00) to this
* register when you are finished using any of the functions in this driver.
*
* For all functions, it is important that no interrupt handlers
* perform any NVM operations. The user must implement a scheme for mutually
* exclusive access to the NVM. However, the 4-cycle timeout will work fine,
* since writing to the Configuration Change Protection register (CCP)
* automatically disables interrupts for 4 instruction cycles.
*
* \par Application note:
* AVR1316: XMEGA Self-programming
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: [email protected]
*
* $Revision: 1691 $
* $Date: 2008-07-29 13:25:40 +0200 (ti, 29 jul 2008) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef SP_DRIVER_H
#define SP_DRIVER_H
//#include "avr_compiler.h"
//#include "Flash_Defines.h"
/* Define the size of the flash page if not defined in the header files. */
#ifndef APP_SECTION_PAGE_SIZE
#error APP_SECTION_PAGE_SIZE must be defined if not defined in header files.
//#define APP_SECTION_PAGE_SIZE 512
#endif /*FLASH_PAGE_SIZE*/
/* Define the Start of the application table if not defined in the header files. */
#ifndef APPTABLE_SECTION_START
#error APPTABLE_SECTION_START must be defined if not defined in header files.
//#define APPTABLE_SECTION_START 0x01E000 //APPTABLE address for ATxmega128A1
#endif /*APPTABLE_SECTION_START*/
/*! \brief Read a byte from flash.
*
* This function reads one byte from the flash.
*
* \note Both IAR and GCC have functions to do this, but
* we include the fucntions for easier use.
*
* \param address Address to the location of the byte to read.
*
* \retval Byte read from flash.
*/
uint8_t SP_ReadByte(uint32_t address);
/*! \brief Read a word from flash.
*
* This function reads one word from the flash.
*
* \note Both IAR and GCC have functions to do this automatically, but
* we include the fucntions for easier use.
*
* \param address Address to the location of the word to read.
*
* \retval word read from flash.
*/
uint16_t SP_ReadWord(uint32_t address);
/*! \brief Read calibration byte at given index.
*
* This function reads one calibration byte from the Calibration signature row.
*
* \param index Index of the byte in the calibration signature row.
*
* \retval Calibration byte
*/
uint8_t SP_ReadCalibrationByte(uint8_t index);
/*! \brief Read fuse byte from given index.
*
* This function reads the fuse byte at the given index.
*
* \param index Index of the fuse byte.
*
* \retval Fuse byte
*/
uint8_t SP_ReadFuseByte(uint8_t index);
/*! \brief Write lock bits.
*
* This function changes the lock bits.
*
* \note It is only possible to change the lock bits to a higher security level.
*
* \param data The new value of the lock bits.
*/
void SP_WriteLockBits(uint8_t data);
/*! \brief Read lock bits.
*
* This function reads the lock bits.
*
* \retval Lock bits
*/
uint8_t SP_ReadLockBits(void);
/*! \brief Read user signature at given index.
*
* This function reads one byte from the user signature row.
*
* \param index Index of the byte in the user signature row.
*
* \retval User signature byte
*/
uint8_t SP_ReadUserSignatureByte(uint16_t index);
/*! \brief Erase user signature row.
*
* This function erase the entire user signature row.
*/
void SP_EraseUserSignatureRow(void);
/*! \brief Write user signature row.
*
* This function write the flash buffer in the user signature row.
*/
void SP_WriteUserSignatureRow(void);
/*! \brief Erase entire application section.
*
* This function erases the entire application and application table section
*
* \note If the lock bits is set to not allow spm in the application or
* application table section the erase is not done.
*/
void SP_EraseApplicationSection(void);
/*! \brief Erase page at byte address in application or application table section.
*
* This function erase one page given by the byte address.
*
* \param address Byte address for flash page.
*/
void SP_EraseApplicationPage(uint32_t address);
/*! \brief Erase and write page buffer to application or application table section at byte address.
*
* This function does a combined erase and write to a flash page in the application
* or application table section.
*
* \param address Byte address for flash page.
*/
void SP_EraseWriteApplicationPage(uint32_t address);
/*! \brief Write page buffer to application or application table section at byte address.
*
* This function writes the Flash page buffer to a page in the application or
* application table section given by the byte address.
*
* \note The page that is written to must be erased before it is written to.
*
* \param address Byte address for flash page.
*/
void SP_WriteApplicationPage(uint32_t address);
/*! \brief Load one word into Flash page buffer.
*
* This function Loads one word into the Flash page buffer.
*
* \param address Position in inside the flash page buffer.
* \param data Value to be put into the buffer.
*/
void SP_LoadFlashWord(uint16_t address, uint16_t data);
/*! \brief Load entire page from SRAM buffer into Flash page buffer.
*
* This function load an entire page from SRAM.
*
* \param data Pointer to the data to put in buffer.
*
* \note The __near keyword limits the pointer to two bytes which means that
* only data up to 64K (internal SRAM) can be used.
*/
void SP_LoadFlashPage(const uint8_t * data);
/*! \brief Read entire Flash page into SRAM buffer.
*
* This function reads an entire flash page and puts it to SRAM.
*
* \param data Pointer to where to store the data.
* \param address Address to page to read from flash.
*/
void SP_ReadFlashPage(const uint8_t * data, uint32_t address);
/*! \brief Flush Flash page buffer.
*
* This function flush the Flash page buffer.
*/
void SP_EraseFlashBuffer(void);
/*! \brief Erase page at byte address in boot section.
*
* This function erase one page given by the byte address.
*
* \param address Byte address for flash page.
*/
void SP_EraseBootPage(uint32_t address);
/*! \brief Erase and write page buffer to boot section at byte address.
*
* This function does a combined erase and write to a flash page in the boot
* section.
*
* \param address Byte address for flash page.
*/
void SP_EraseWriteBootPage(uint32_t address);
/*! \brief Write page buffer to boot section at byte address.
*
* This function writes the Flash page buffer to a page in the boot section
* given by the byte address.
*
* \note The page that is written to must be erased before it is written to.
*
* \param address Byte address for flash page.
*/
void SP_WriteBootPage(uint32_t address);
/*! \brief Generate CRC from application section.
*
* \retval 24-bit CRC value
*/
uint32_t SP_ApplicationCRC(void);
/*! \brief Generate CRC from boot section.
*
* \retval 24-bit CRC value
*/
uint32_t SP_BootCRC(void);
/*! \brief Lock SPM instruction.
*
* This function locks the SPM instruction, and will disable the use of
* SPM until the next reset occurs.
*/
void SP_LockSPM(void);
/*! \brief Wait for SPM to finish.
*
* This routine waits for the SPM to finish and clears the command register.
*/
void SP_WaitForSPM(void);
#endif /* SP_DRIVER_H */