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
294
295
296
297
298
299
300
301
//! Couple of macros for linker section memory initialization. This crate is
//! designed for use on platforms with 32-bit aligned memory and 32-bit memory
//! access, but was tested with cortex-m cores only.
//!
//! This crate provides section memory initialization macro in a couple of
//! following variants.
//! - [`init_sections`]
//!
//! Use this macro if your section is defined using symbols
//! - `__s<section>` for section VMA start,
//! - `__e<section>` for section VMA end,
//! - `__si<section>` for section LMA start.
//!
//! ```
//! init_sections!(buffers, sram2, sram3);
//! ```
//!
//! - [`init_sections_with_prefixes`]
//!
//! Use if you want to specify your section boundary symbols manually.
//!
//! ```
//! init_sections_with_prefixes!(
//! buffers(__s, __e, __si),
//! sram2(__s, __e, __si),
//! sram3(__s, __e, __si)
//! );
//! ```
//!
//! # Example
//!
//! Simple example defines a section `.custom_data` with start at 4-byte aligned `__scustom_data`
//! and 4-byte aligned end at `__ecustom_data`. The initialization data goes at `__sicustom_data`.
//!
//! ```text
//! MEMORY
//! {
//! FLASH : ORIGIN = 0x08000000, LENGTH = 32K
//! RAM : ORIGIN = 0x20000000, LENGTH = 16K
//! DATA : ORIGIN = 0x20004000, LENGTH = 16K
//! }
//!
//! SECTIONS
//! {
//! .custom_data : ALIGN(4)
//! {
//! . = ALIGN(4);
//! __scustom_data = .;
//! *(.custom_data .custom_data.*);
//!
//! . = ALIGN(4);
//! __ecustom_data = .;
//! } > DATA AT>FLASH
//!
//! __sicustom_data = LOADADDR(.custom_data);
//! } INSERT BEFORE .uninit;
//! ```
//!
//! In rust code it is needed to call macro [`init_sections`] with proper argument. the
//! [`init_sections`] macro shall be usually called before or at the beginning of `main` function.
//!
//! ```
//! #![no_std]
//! #![no_main]
//!
//! use linker_sections::init_sections;
//! use {defmt_rtt as _, panic_probe as _};
//!
//! const INITIAL_VALUE: u32 = 0xDEAD_BEEF;
//!
//! #[unsafe(link_section = ".custom_data")]
//! static mut STATIC_VARIABLE: u32 = INITIAL_VALUE;
//! #[cortex_m_rt::pre_init]
//! unsafe fn pre_init() {
//! init_sections!(custom_data);
//! }
//!
//! #[cortex_m_rt::entry]
//! fn main() -> ! {
//! // print initialized variable value
//! unsafe {
//! defmt::info!("STATIC_VARIABLE = 0x{:08X}", STATIC_VARIABLE);
//! }
//!
//! loop {}
//! }
//! ```
//!
//! # Safety
//!
//! - The symbols must be 4-byte aligned.
//! - The symbols must point to memory with required access (read, write).
//! - The symbols must represent continuos memory.
//!
//! # Limitations
//!
//! - Each section's name shall be a valid rust function name, but it does not have to be snake_case.
//! - Only one macro can be called and it can be called at most once.
pub extern crate with_builtin_macros;
pub use ;
/// Defines pre-init function initializing linker section memory.
///
/// This macro accepts linker section names as arguments and assumes the linker symbols are named
/// after given section names prefixed with
/// - `__s` for section VMA's start (usually points to RAM)
/// - `__e` for section VMA's end (usually points to RAM)
/// - `__si` for section LMA's start (usually points to FLASH)
///
/// If the symbols in the linker script are named `__scustom_data`, `__ecustom_data` and `__sicustom_data`,
/// as depicted in an example below, the macro call should be
///
/// ```
/// init_sections!(custom_data)
/// ```
///
/// ```text
/// MEMORY
/// {
/// FLASH : ORIGIN = 0x08000000, LENGTH = 32K
/// RAM : ORIGIN = 0x20000000, LENGTH = 16K
/// DATA : ORIGIN = 0x20004000, LENGTH = 16K
/// }
///
/// SECTIONS
/// {
/// .custom_data : ALIGN(4)
/// {
/// . = ALIGN(4);
/// __scustom_data = .;
/// *(.custom_data .custom_data.*);
///
/// . = ALIGN(4);
/// __ecustom_data = .;
/// } > DATA AT>FLASH
///
/// __sicustom_data = LOADADDR(.custom_data);
/// } INSERT BEFORE .uninit;
/// ```
///
/// Multiple section names could be passed as
///
/// ```
/// init_sections!(section_a, section_b, section_c);
/// ```
/// ```
/// init_sections!(section_a, section_b, section_c,);
/// ```
/// ```
/// init_sections!(section_a section_b section_c);
/// ```
;
}
/// Defines pre-init function initializing linker section memory.
///
/// This macro accepts linker section names and symbol prefixes as arguments. If your section
/// symbols are prefixed with `__s`, `__e` and `__si`, look at [`init_sections`].
///
/// If the symbols in the linker script are named `__scustom_data`, `__ecustom_data` and `__sicustom_data`,
/// as depicted in an example below, the macro call should be
///
/// ```
/// init_sections_with_prefixes!(custom_data(__s, __e, __si))
/// ```
///
/// ```text
/// MEMORY
/// {
/// FLASH : ORIGIN = 0x08000000, LENGTH = 32K
/// RAM : ORIGIN = 0x20000000, LENGTH = 16K
/// DATA : ORIGIN = 0x20004000, LENGTH = 16K
/// }
///
/// SECTIONS
/// {
/// .custom_data : ALIGN(4)
/// {
/// . = ALIGN(4);
/// __scustom_data = .;
/// *(.custom_data .custom_data.*);
///
/// . = ALIGN(4);
/// __ecustom_data = .;
/// } > DATA AT>FLASH
///
/// __sicustom_data = LOADADDR(.custom_data);
/// } INSERT BEFORE .uninit;
/// ```
///
/// Multiple section names could be passed as
///
/// ```
/// init_sections_with_prefixes!(section_a(__s, __e __si), section_b(__s, __e, __si));
/// ```
/// ```
/// init_sections_with_prefixes!(section_a(__s,__e,__si),);
/// ```
/// ```
/// init_sections_with_prefixes!(
/// section_a(__s, __e, __si)
/// section_b(__s, __e, __si,)
/// section_c(__s __e __si)
/// );
/// ```
;
}
;
}
pub unsafe