5
5
#![ no_main]
6
6
#![ no_std]
7
7
8
+ mod utils;
9
+
8
10
#[ rtic:: app( device = stm32g4xx_hal:: stm32g4:: stm32g474, peripherals = true ) ]
9
11
mod app {
12
+ use crate :: utils:: logger;
10
13
use stm32g4xx_hal:: flash:: { FlashExt , FlashSize , FlashWriter , Parts } ;
11
14
use stm32g4xx_hal:: prelude:: * ;
12
15
use stm32g4xx_hal:: rcc:: { PllConfig , RccExt } ;
13
16
17
+ const LOG_LEVEL : log:: LevelFilter = log:: LevelFilter :: Info ;
18
+
14
19
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
15
20
16
21
// Resources shared between tasks
@@ -35,9 +40,6 @@ mod app {
35
40
36
41
#[ init]
37
42
fn init ( cx : init:: Context ) -> ( Shared , Local , init:: Monotonics ) {
38
- // let dp = Peripherals::take().unwrap();
39
- // let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
40
-
41
43
let dp = cx. device ;
42
44
let cp = cx. core ;
43
45
@@ -75,7 +77,6 @@ mod app {
75
77
}
76
78
77
79
// *** FLASH Memory ***
78
- //let mut data = [0xBE, 0xEF, 0xCA, 0xFE];
79
80
let one_byte = [ 0x12 as u8 ] ;
80
81
let two_bytes = [ 0xAB , 0xCD as u8 ] ;
81
82
let three_bytes = [ 0x12 , 0x34 , 0x56 as u8 ] ;
@@ -87,117 +88,216 @@ mod app {
87
88
] ;
88
89
let mut flash = dp. FLASH . constrain ( ) ;
89
90
let mut flash_writer = flash. writer :: < 2048 > ( FlashSize :: Sz256K ) ;
91
+
90
92
const FLASH_SPACING : u32 = 16 ; // Separate flash writes by 16 bytes
93
+ const FLASH_EXAMPLE_START_ADDRESS : u32 = 0x1FC00 ;
91
94
92
- flash_writer. erase ( 0x1FC00 , 128 ) . unwrap ( ) ; // Erase entire page
95
+ logger:: info!(
96
+ "Erasing 128 bytes at address {}" ,
97
+ FLASH_EXAMPLE_START_ADDRESS
98
+ ) ;
99
+ flash_writer
100
+ . erase ( FLASH_EXAMPLE_START_ADDRESS , 128 )
101
+ . unwrap ( ) ; // Erase entire page
93
102
94
103
for i in 0 ..6 {
95
104
match i {
96
105
0 => {
97
106
// This test should fail, as the data needs to be divisible by 8 and force padding is false
98
- let result = flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & one_byte, false ) ;
107
+ let result = flash_writer. write (
108
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
109
+ & one_byte,
110
+ false ,
111
+ ) ;
99
112
assert ! ( result. is_err( ) ) ;
100
113
assert_eq ! (
101
114
result. err( ) . unwrap( ) ,
102
115
stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
103
116
) ;
104
117
105
118
// This test should pass, as the data needs to be divisible by 8 and force padding is true, so the one_byte array will be padded with 7 bytes of 0xFF
106
- let result = flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & one_byte, true ) ;
119
+ let result = flash_writer. write (
120
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
121
+ & one_byte,
122
+ true ,
123
+ ) ;
107
124
assert ! ( result. is_ok( ) ) ;
125
+ logger:: info!(
126
+ "Wrote 1 byte to address {}" ,
127
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
128
+ ) ;
108
129
}
109
130
1 => {
110
131
// This test should fail, as the data needs to be divisible by 8 and force padding is false
111
- let result = flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & two_bytes, false ) ;
132
+ let result = flash_writer. write (
133
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
134
+ & two_bytes,
135
+ false ,
136
+ ) ;
112
137
assert ! ( result. is_err( ) ) ;
113
138
assert_eq ! (
114
139
result. err( ) . unwrap( ) ,
115
140
stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
116
141
) ;
117
142
118
143
// This test should pass, as the data needs to be divisible by 8 and force padding is true, so the one_byte array will be padded with 7 bytes of 0xFF
119
- let result = flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & two_bytes, true ) ;
144
+ let result = flash_writer. write (
145
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
146
+ & two_bytes,
147
+ true ,
148
+ ) ;
120
149
assert ! ( result. is_ok( ) ) ;
150
+ logger:: info!(
151
+ "Wrote 2 bytes to address {}" ,
152
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
153
+ ) ;
121
154
}
122
155
2 => {
123
156
// This test should fail, as the data needs to be divisible by 8 and force padding is false
124
- let result =
125
- flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & three_bytes, false ) ;
157
+ let result = flash_writer. write (
158
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
159
+ & three_bytes,
160
+ false ,
161
+ ) ;
126
162
assert ! ( result. is_err( ) ) ;
127
163
assert_eq ! (
128
164
result. err( ) . unwrap( ) ,
129
165
stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
130
166
) ;
131
167
132
168
// This test should pass, as the data needs to be divisible by 8 and force padding is true, so the one_byte array will be padded with 7 bytes of 0xFF
133
- let result =
134
- flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & three_bytes, true ) ;
169
+ let result = flash_writer. write (
170
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
171
+ & three_bytes,
172
+ true ,
173
+ ) ;
135
174
assert ! ( result. is_ok( ) ) ;
175
+ logger:: info!(
176
+ "Wrote 3 bytes to address {}" ,
177
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
178
+ ) ;
136
179
}
137
180
3 => {
138
181
// This test should fail, as the data needs to be divisible by 8 and force padding is false
139
- let result =
140
- flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & four_bytes, false ) ;
182
+ let result = flash_writer. write (
183
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
184
+ & four_bytes,
185
+ false ,
186
+ ) ;
141
187
assert ! ( result. is_err( ) ) ;
142
188
assert_eq ! (
143
189
result. err( ) . unwrap( ) ,
144
190
stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
145
191
) ;
146
192
147
193
// This test should pass, as the data needs to be divisible by 8 and force padding is true, so the one_byte array will be padded with 7 bytes of 0xFF
148
- let result = flash_writer. write ( 0x1FC00 + i * FLASH_SPACING , & four_bytes, true ) ;
194
+ let result = flash_writer. write (
195
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
196
+ & four_bytes,
197
+ true ,
198
+ ) ;
149
199
assert ! ( result. is_ok( ) ) ;
200
+ logger:: info!(
201
+ "Wrote 4 bytes to address {}" ,
202
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
203
+ ) ;
204
+ }
205
+ 4 => {
206
+ flash_writer
207
+ . write (
208
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
209
+ & eight_bytes,
210
+ false ,
211
+ )
212
+ . unwrap ( ) ;
213
+ logger:: info!(
214
+ "Wrote 8 bytes to address {}" ,
215
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
216
+ ) ;
217
+ }
218
+ 5 => {
219
+ flash_writer
220
+ . write ( FLASH_EXAMPLE_START_ADDRESS + i * 16 , & sixteen_bytes, false )
221
+ . unwrap ( ) ;
222
+ logger:: info!(
223
+ "Wrote 16 bytes to address {}" ,
224
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
225
+ ) ;
150
226
}
151
- 4 => flash_writer
152
- . write ( 0x1FC00 + i * FLASH_SPACING , & eight_bytes, false )
153
- . unwrap ( ) ,
154
- 5 => flash_writer
155
- . write ( 0x1FC00 + i * 16 , & sixteen_bytes, false )
156
- . unwrap ( ) ,
157
227
_ => ( ) ,
158
228
}
159
229
}
160
230
231
+ logger:: info!( "Validating data written data by performing read and compare" ) ;
232
+
161
233
for i in 0 ..6 {
162
234
match i {
163
235
0 => {
164
- let bytes = flash_writer. read ( 0x1FC00 as u32 , one_byte. len ( ) ) . unwrap ( ) ;
236
+ let bytes = flash_writer
237
+ . read ( FLASH_EXAMPLE_START_ADDRESS as u32 , one_byte. len ( ) )
238
+ . unwrap ( ) ;
165
239
assert ! ( compare_arrays( & bytes, & one_byte) ) ;
240
+ logger:: info!( "Validated 1 byte data" ) ;
166
241
}
167
242
1 => {
168
243
let bytes = flash_writer
169
- . read ( 0x1FC00 + i * FLASH_SPACING , two_bytes. len ( ) )
244
+ . read (
245
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
246
+ two_bytes. len ( ) ,
247
+ )
170
248
. unwrap ( ) ;
171
249
assert ! ( compare_arrays( & bytes, & two_bytes) ) ;
250
+ logger:: info!( "Validated 2 byte data" ) ;
172
251
}
173
252
2 => {
174
253
let bytes = flash_writer
175
- . read ( 0x1FC00 + i * FLASH_SPACING , three_bytes. len ( ) )
254
+ . read (
255
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
256
+ three_bytes. len ( ) ,
257
+ )
176
258
. unwrap ( ) ;
177
259
assert ! ( compare_arrays( & bytes, & three_bytes) ) ;
260
+ logger:: info!( "Validated 3 byte data" ) ;
178
261
}
179
262
3 => {
180
263
let bytes = flash_writer
181
- . read ( 0x1FC00 + i * FLASH_SPACING , four_bytes. len ( ) )
264
+ . read (
265
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
266
+ four_bytes. len ( ) ,
267
+ )
182
268
. unwrap ( ) ;
183
269
assert ! ( compare_arrays( & bytes, & four_bytes) ) ;
270
+ logger:: info!( "Validated 4 byte data" ) ;
184
271
}
185
272
4 => {
186
273
let bytes = flash_writer
187
- . read ( 0x1FC00 + i * FLASH_SPACING , eight_bytes. len ( ) )
274
+ . read (
275
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
276
+ eight_bytes. len ( ) ,
277
+ )
188
278
. unwrap ( ) ;
189
279
assert ! ( compare_arrays( & bytes, & eight_bytes) ) ;
280
+ logger:: info!( "Validated 8 byte data" ) ;
190
281
}
191
282
5 => {
192
283
let bytes = flash_writer
193
- . read ( 0x1FC00 + i * FLASH_SPACING , sixteen_bytes. len ( ) )
284
+ . read (
285
+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
286
+ sixteen_bytes. len ( ) ,
287
+ )
194
288
. unwrap ( ) ;
195
289
assert ! ( compare_arrays( & bytes, & sixteen_bytes) ) ;
290
+ logger:: info!( "Validated 5 byte data" ) ;
196
291
}
197
292
_ => ( ) ,
198
293
}
199
294
}
200
295
296
+ logger:: info!(
297
+ "Finished flash example at address {}" ,
298
+ FLASH_EXAMPLE_START_ADDRESS
299
+ ) ;
300
+
201
301
(
202
302
// Initialization of shared resources
203
303
Shared { } ,
0 commit comments