1 | /* Copyright (c) 2007, Atmel Corporation |
---|
2 | All rights reserved. |
---|
3 | |
---|
4 | Redistribution and use in source and binary forms, with or without |
---|
5 | modification, are permitted provided that the following conditions are met: |
---|
6 | |
---|
7 | * Redistributions of source code must retain the above copyright |
---|
8 | notice, this list of conditions and the following disclaimer. |
---|
9 | |
---|
10 | * Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in |
---|
12 | the documentation and/or other materials provided with the |
---|
13 | distribution. |
---|
14 | |
---|
15 | * Neither the name of the copyright holders nor the names of |
---|
16 | contributors may be used to endorse or promote products derived |
---|
17 | from this software without specific prior written permission. |
---|
18 | |
---|
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
29 | POSSIBILITY OF SUCH DAMAGE. */ |
---|
30 | |
---|
31 | |
---|
32 | /* avr/lock.h - Lock Bits API */ |
---|
33 | |
---|
34 | #ifndef _AVR_LOCK_H_ |
---|
35 | #define _AVR_LOCK_H_ 1 |
---|
36 | |
---|
37 | |
---|
38 | /** \file */ |
---|
39 | /** \defgroup avr_lock <avr/lock.h>: Lockbit Support |
---|
40 | |
---|
41 | \par Introduction |
---|
42 | |
---|
43 | The Lockbit API allows a user to specify the lockbit settings for the |
---|
44 | specific AVR device they are compiling for. These lockbit settings will be |
---|
45 | placed in a special section in the ELF output file, after linking. |
---|
46 | |
---|
47 | Programming tools can take advantage of the lockbit information embedded in |
---|
48 | the ELF file, by extracting this information and determining if the lockbits |
---|
49 | need to be programmed after programming the Flash and EEPROM memories. |
---|
50 | This also allows a single ELF file to contain all the |
---|
51 | information needed to program an AVR. |
---|
52 | |
---|
53 | To use the Lockbit API, include the <avr/io.h> header file, which in turn |
---|
54 | automatically includes the individual I/O header file and the <avr/lock.h> |
---|
55 | file. These other two files provides everything necessary to set the AVR |
---|
56 | lockbits. |
---|
57 | |
---|
58 | \par Lockbit API |
---|
59 | |
---|
60 | Each I/O header file may define up to 3 macros that controls what kinds |
---|
61 | of lockbits are available to the user. |
---|
62 | |
---|
63 | If __LOCK_BITS_EXIST is defined, then two lock bits are available to the |
---|
64 | user and 3 mode settings are defined for these two bits. |
---|
65 | |
---|
66 | If __BOOT_LOCK_BITS_0_EXIST is defined, then the two BLB0 lock bits are |
---|
67 | available to the user and 4 mode settings are defined for these two bits. |
---|
68 | |
---|
69 | If __BOOT_LOCK_BITS_1_EXIST is defined, then the two BLB1 lock bits are |
---|
70 | available to the user and 4 mode settings are defined for these two bits. |
---|
71 | |
---|
72 | If __BOOT_LOCK_APPLICATION_TABLE_BITS_EXIST is defined then two lock bits |
---|
73 | are available to set the locking mode for the Application Table Section |
---|
74 | (which is used in the XMEGA family). |
---|
75 | |
---|
76 | If __BOOT_LOCK_APPLICATION_BITS_EXIST is defined then two lock bits are |
---|
77 | available to set the locking mode for the Application Section (which is used |
---|
78 | in the XMEGA family). |
---|
79 | |
---|
80 | If __BOOT_LOCK_BOOT_BITS_EXIST is defined then two lock bits are available |
---|
81 | to set the locking mode for the Boot Loader Section (which is used in the |
---|
82 | XMEGA family). |
---|
83 | |
---|
84 | The AVR lockbit modes have inverted values, logical 1 for an unprogrammed |
---|
85 | (disabled) bit and logical 0 for a programmed (enabled) bit. The defined |
---|
86 | macros for each individual lock bit represent this in their definition by a |
---|
87 | bit-wise inversion of a mask. For example, the LB_MODE_3 macro is defined |
---|
88 | as: |
---|
89 | \code |
---|
90 | #define LB_MODE_3 (0xFC) |
---|
91 | ` \endcode |
---|
92 | |
---|
93 | To combine the lockbit mode macros together to represent a whole byte, |
---|
94 | use the bitwise AND operator, like so: |
---|
95 | \code |
---|
96 | (LB_MODE_3 & BLB0_MODE_2) |
---|
97 | \endcode |
---|
98 | |
---|
99 | <avr/lock.h> also defines a macro that provides a default lockbit value: |
---|
100 | LOCKBITS_DEFAULT which is defined to be 0xFF. |
---|
101 | |
---|
102 | See the AVR device specific datasheet for more details about these |
---|
103 | lock bits and the available mode settings. |
---|
104 | |
---|
105 | A convenience macro, LOCKMEM, is defined as a GCC attribute for a |
---|
106 | custom-named section of ".lock". |
---|
107 | |
---|
108 | A convenience macro, LOCKBITS, is defined that declares a variable, __lock, |
---|
109 | of type unsigned char with the attribute defined by LOCKMEM. This variable |
---|
110 | allows the end user to easily set the lockbit data. |
---|
111 | |
---|
112 | \note If a device-specific I/O header file has previously defined LOCKMEM, |
---|
113 | then LOCKMEM is not redefined. If a device-specific I/O header file has |
---|
114 | previously defined LOCKBITS, then LOCKBITS is not redefined. LOCKBITS is |
---|
115 | currently known to be defined in the I/O header files for the XMEGA devices. |
---|
116 | |
---|
117 | \par API Usage Example |
---|
118 | |
---|
119 | Putting all of this together is easy: |
---|
120 | |
---|
121 | \code |
---|
122 | #include <avr/io.h> |
---|
123 | |
---|
124 | LOCKBITS = (LB_MODE_1 & BLB0_MODE_3 & BLB1_MODE_4); |
---|
125 | |
---|
126 | int main(void) |
---|
127 | { |
---|
128 | return 0; |
---|
129 | } |
---|
130 | \endcode |
---|
131 | |
---|
132 | Or: |
---|
133 | |
---|
134 | \code |
---|
135 | #include <avr/io.h> |
---|
136 | |
---|
137 | unsigned char __lock __attribute__((section (".lock"))) = |
---|
138 | (LB_MODE_1 & BLB0_MODE_3 & BLB1_MODE_4); |
---|
139 | |
---|
140 | int main(void) |
---|
141 | { |
---|
142 | return 0; |
---|
143 | } |
---|
144 | \endcode |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | However there are a number of caveats that you need to be aware of to |
---|
149 | use this API properly. |
---|
150 | |
---|
151 | Be sure to include <avr/io.h> to get all of the definitions for the API. |
---|
152 | The LOCKBITS macro defines a global variable to store the lockbit data. This |
---|
153 | variable is assigned to its own linker section. Assign the desired lockbit |
---|
154 | values immediately in the variable initialization. |
---|
155 | |
---|
156 | The .lock section in the ELF file will get its values from the initial |
---|
157 | variable assignment ONLY. This means that you can NOT assign values to |
---|
158 | this variable in functions and the new values will not be put into the |
---|
159 | ELF .lock section. |
---|
160 | |
---|
161 | The global variable is declared in the LOCKBITS macro has two leading |
---|
162 | underscores, which means that it is reserved for the "implementation", |
---|
163 | meaning the library, so it will not conflict with a user-named variable. |
---|
164 | |
---|
165 | You must initialize the lockbit variable to some meaningful value, even |
---|
166 | if it is the default value. This is because the lockbits default to a |
---|
167 | logical 1, meaning unprogrammed. Normal uninitialized data defaults to all |
---|
168 | locgial zeros. So it is vital that all lockbits are initialized, even with |
---|
169 | default data. If they are not, then the lockbits may not programmed to the |
---|
170 | desired settings and can possibly put your device into an unrecoverable |
---|
171 | state. |
---|
172 | |
---|
173 | Be sure to have the -mmcu=<em>device</em> flag in your compile command line and |
---|
174 | your linker command line to have the correct device selected and to have |
---|
175 | the correct I/O header file included when you include <avr/io.h>. |
---|
176 | |
---|
177 | You can print out the contents of the .lock section in the ELF file by |
---|
178 | using this command line: |
---|
179 | \code |
---|
180 | avr-objdump -s -j .lock <ELF file> |
---|
181 | \endcode |
---|
182 | |
---|
183 | */ |
---|
184 | |
---|
185 | |
---|
186 | #ifndef __ASSEMBLER__ |
---|
187 | |
---|
188 | #ifndef LOCKMEM |
---|
189 | #define LOCKMEM __attribute__((section (".lock"))) |
---|
190 | #endif |
---|
191 | |
---|
192 | #ifndef LOCKBITS |
---|
193 | #define LOCKBITS unsigned char __lock LOCKMEM |
---|
194 | #endif |
---|
195 | |
---|
196 | #endif /* !__ASSEMBLER */ |
---|
197 | |
---|
198 | |
---|
199 | /* Lock Bit Modes */ |
---|
200 | #if defined(__LOCK_BITS_EXIST) |
---|
201 | #define LB_MODE_1 (0xFF) |
---|
202 | #define LB_MODE_2 (0xFE) |
---|
203 | #define LB_MODE_3 (0xFC) |
---|
204 | #endif |
---|
205 | |
---|
206 | #if defined(__BOOT_LOCK_BITS_0_EXIST) |
---|
207 | #define BLB0_MODE_1 (0xFF) |
---|
208 | #define BLB0_MODE_2 (0xFB) |
---|
209 | #define BLB0_MODE_3 (0xF3) |
---|
210 | #define BLB0_MODE_4 (0xF7) |
---|
211 | #endif |
---|
212 | |
---|
213 | #if defined(__BOOT_LOCK_BITS_1_EXIST) |
---|
214 | #define BLB1_MODE_1 (0xFF) |
---|
215 | #define BLB1_MODE_2 (0xEF) |
---|
216 | #define BLB1_MODE_3 (0xCF) |
---|
217 | #define BLB1_MODE_4 (0xDF) |
---|
218 | #endif |
---|
219 | |
---|
220 | #if defined(__BOOT_LOCK_APPLICATION_TABLE_BITS_EXIST) |
---|
221 | #define BLBAT0 ~_BV(2) |
---|
222 | #define BLBAT1 ~_BV(3) |
---|
223 | #endif |
---|
224 | |
---|
225 | #if defined(__BOOT_LOCK_APPLICATION_BITS_EXIST) |
---|
226 | #define BLBA0 ~_BV(4) |
---|
227 | #define BLBA1 ~_BV(5) |
---|
228 | #endif |
---|
229 | |
---|
230 | #if defined(__BOOT_LOCK_BOOT_BITS_EXIST) |
---|
231 | #define BLBB0 ~_BV(6) |
---|
232 | #define BLBB1 ~_BV(7) |
---|
233 | #endif |
---|
234 | |
---|
235 | |
---|
236 | #define LOCKBITS_DEFAULT (0xFF) |
---|
237 | |
---|
238 | #endif /* _AVR_LOCK_H_ */ |
---|