AVR-LibC
2.3.0
Standard C library for AVR-GCC
AVR-LibC Manual
AVR-LibC Sources
Main Page
User Manual
Library Reference
FAQ
Example Projects
Index
Loading...
Searching...
No Matches
source
avr-libc-onlinedocs
include
avr
lock.h
Go to the documentation of this file.
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
/* avr/lock.h - Lock Bits API */
32
33
#ifndef _AVR_LOCK_H_
34
#define _AVR_LOCK_H_ 1
35
36
37
/** \file */
38
/** \defgroup avr_lock <avr/lock.h>: Lockbit Support
39
40
\par Introduction
41
42
The Lockbit API allows a user to specify the lockbit settings for the
43
specific AVR device they are compiling for. These lockbit settings will be
44
placed in a special section in the ELF output file, after linking.
45
46
Programming tools can take advantage of the lockbit information embedded in
47
the ELF file, by extracting this information and determining if the lockbits
48
need to be programmed after programming the Flash and EEPROM memories.
49
This also allows a single ELF file to contain all the
50
information needed to program an AVR.
51
52
To use the Lockbit API, include the <avr/io.h> header file, which in turn
53
automatically includes the individual I/O header file and the <avr/lock.h>
54
file. These other two files provides everything necessary to set the AVR
55
lockbits.
56
57
\par Lockbit API
58
59
Each I/O header file may define up to 3 macros that controls what kinds
60
of lockbits are available to the user.
61
62
If __LOCK_BITS_EXIST is defined, then two lock bits are available to the
63
user and 3 mode settings are defined for these two bits.
64
65
If __BOOT_LOCK_BITS_0_EXIST is defined, then the two BLB0 lock bits are
66
available to the user and 4 mode settings are defined for these two bits.
67
68
If __BOOT_LOCK_BITS_1_EXIST is defined, then the two BLB1 lock bits are
69
available to the user and 4 mode settings are defined for these two bits.
70
71
If __BOOT_LOCK_APPLICATION_TABLE_BITS_EXIST is defined then two lock bits
72
are available to set the locking mode for the Application Table Section
73
(which is used in the XMEGA family).
74
75
If __BOOT_LOCK_APPLICATION_BITS_EXIST is defined then two lock bits are
76
available to set the locking mode for the Application Section (which is used
77
in the XMEGA family).
78
79
If __BOOT_LOCK_BOOT_BITS_EXIST is defined then two lock bits are available
80
to set the locking mode for the Boot Loader Section (which is used in the
81
XMEGA family).
82
83
The AVR lockbit modes have inverted values, logical 1 for an unprogrammed
84
(disabled) bit and logical 0 for a programmed (enabled) bit. The defined
85
macros for each individual lock bit represent this in their definition by a
86
bit-wise inversion of a mask. For example, the LB_MODE_3 macro is defined
87
as:
88
\code
89
#define LB_MODE_3 (0xFC)
90
` \endcode
91
92
To combine the lockbit mode macros together to represent a whole byte,
93
use the bitwise AND operator, like so:
94
\code
95
(LB_MODE_3 & BLB0_MODE_2)
96
\endcode
97
98
<avr/lock.h> also defines a macro that provides a default lockbit value:
99
LOCKBITS_DEFAULT which is defined to be 0xFF.
100
101
See the AVR device specific datasheet for more details about these
102
lock bits and the available mode settings.
103
104
A convenience macro, LOCKMEM, is defined as a GCC attribute for a
105
custom-named section of ".lock".
106
107
A convenience macro, LOCKBITS, is defined that declares a variable, __lock,
108
of type unsigned char with the attribute defined by LOCKMEM. This variable
109
allows the end user to easily set the lockbit data.
110
111
\note If a device-specific I/O header file has previously defined LOCKMEM,
112
then LOCKMEM is not redefined. If a device-specific I/O header file has
113
previously defined LOCKBITS, then LOCKBITS is not redefined. LOCKBITS is
114
currently known to be defined in the I/O header files for the XMEGA devices.
115
116
\par API Usage Example
117
118
Putting all of this together is easy:
119
120
\code
121
#include <avr/io.h>
122
123
LOCKBITS = (LB_MODE_1 & BLB0_MODE_3 & BLB1_MODE_4);
124
125
int main(void)
126
{
127
return 0;
128
}
129
\endcode
130
131
Or:
132
133
\code
134
#include <avr/io.h>
135
136
unsigned char __lock __attribute__((section (".lock"))) =
137
(LB_MODE_1 & BLB0_MODE_3 & BLB1_MODE_4);
138
139
int main(void)
140
{
141
return 0;
142
}
143
\endcode
144
145
146
147
However there are a number of caveats that you need to be aware of to
148
use this API properly.
149
150
Be sure to include <avr/io.h> to get all of the definitions for the API.
151
The LOCKBITS macro defines a global variable to store the lockbit data. This
152
variable is assigned to its own linker section. Assign the desired lockbit
153
values immediately in the variable initialization.
154
155
The .lock section in the ELF file will get its values from the initial
156
variable assignment ONLY. This means that you can NOT assign values to
157
this variable in functions and the new values will not be put into the
158
ELF .lock section.
159
160
The global variable is declared in the LOCKBITS macro has two leading
161
underscores, which means that it is reserved for the "implementation",
162
meaning the library, so it will not conflict with a user-named variable.
163
164
You must initialize the lockbit variable to some meaningful value, even
165
if it is the default value. This is because the lockbits default to a
166
logical 1, meaning unprogrammed. Normal uninitialized data defaults to all
167
locgial zeros. So it is vital that all lockbits are initialized, even with
168
default data. If they are not, then the lockbits may not programmed to the
169
desired settings and can possibly put your device into an unrecoverable
170
state.
171
172
Be sure to have the -mmcu=<em>device</em> flag in your compile command line and
173
your linker command line to have the correct device selected and to have
174
the correct I/O header file included when you include <avr/io.h>.
175
176
You can print out the contents of the .lock section in the ELF file by
177
using this command line:
178
\code
179
avr-objdump -s -j .lock <ELF file>
180
\endcode
181
182
*/
183
184
185
#if !(defined(__ASSEMBLER__) || defined(__DOXYGEN__))
186
187
#ifndef LOCKMEM
188
#define LOCKMEM __attribute__((__used__, __section__ (".lock"
)))
189
#endif
190
191
#ifndef LOCKBITS
192
#define LOCKBITS unsigned char __lock LOCKMEM
193
#endif
194
195
/* Lock Bit Modes */
196
#if defined(__LOCK_BITS_EXIST)
197
#define LB_MODE_1 (0xFF)
198
#define LB_MODE_2 (0xFE)
199
#define LB_MODE_3 (0xFC)
200
#endif
201
202
#if defined(__BOOT_LOCK_BITS_0_EXIST)
203
#define BLB0_MODE_1 (0xFF)
204
#define BLB0_MODE_2 (0xFB)
205
#define BLB0_MODE_3 (0xF3)
206
#define BLB0_MODE_4 (0xF7)
207
#endif
208
209
#if defined(__BOOT_LOCK_BITS_1_EXIST)
210
#define BLB1_MODE_1 (0xFF)
211
#define BLB1_MODE_2 (0xEF)
212
#define BLB1_MODE_3 (0xCF)
213
#define BLB1_MODE_4 (0xDF)
214
#endif
215
216
#if defined(__BOOT_LOCK_APPLICATION_TABLE_BITS_EXIST)
217
#define BLBAT0 ~_BV(2)
218
#define BLBAT1 ~_BV(3)
219
#endif
220
221
#if defined(__BOOT_LOCK_APPLICATION_BITS_EXIST)
222
#define BLBA0 ~_BV(4)
223
#define BLBA1 ~_BV(5)
224
#endif
225
226
#if defined(__BOOT_LOCK_BOOT_BITS_EXIST)
227
#define BLBB0 ~_BV(6)
228
#define BLBB1 ~_BV(7)
229
#endif
230
231
#ifndef LOCKBITS_DEFAULT
232
#define LOCKBITS_DEFAULT (0xFF)
233
#endif
234
235
#endif
/* !(__ASSEMBLER || __DOXYGEN__) */
236
237
238
#endif
/* _AVR_LOCK_H_ */
Generated on Sun Dec 28 2025 13:38:37 for AVR-LibC by
1.9.6