Skip to content

Commit f5fdae3

Browse files
committed
Post review changes
- Added logging and const test address location to flash_with_rtic.rs - Changed type formatting on a buffer array in flash.rs - Removed //TODO and #[allow(dead_code)] from flash register structure Parts.
1 parent fcc39d3 commit f5fdae3

File tree

2 files changed

+129
-31
lines changed

2 files changed

+129
-31
lines changed

examples/flash_with_rtic.rs

Lines changed: 128 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
#![no_main]
66
#![no_std]
77

8+
mod utils;
9+
810
#[rtic::app(device = stm32g4xx_hal::stm32g4::stm32g474, peripherals = true)]
911
mod app {
12+
use crate::utils::logger;
1013
use stm32g4xx_hal::flash::{FlashExt, FlashSize, FlashWriter, Parts};
1114
use stm32g4xx_hal::prelude::*;
1215
use stm32g4xx_hal::rcc::{PllConfig, RccExt};
1316

17+
const LOG_LEVEL: log::LevelFilter = log::LevelFilter::Info;
18+
1419
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
1520

1621
// Resources shared between tasks
@@ -35,9 +40,6 @@ mod app {
3540

3641
#[init]
3742
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-
4143
let dp = cx.device;
4244
let cp = cx.core;
4345

@@ -75,7 +77,6 @@ mod app {
7577
}
7678

7779
// *** FLASH Memory ***
78-
//let mut data = [0xBE, 0xEF, 0xCA, 0xFE];
7980
let one_byte = [0x12 as u8];
8081
let two_bytes = [0xAB, 0xCD as u8];
8182
let three_bytes = [0x12, 0x34, 0x56 as u8];
@@ -87,117 +88,216 @@ mod app {
8788
];
8889
let mut flash = dp.FLASH.constrain();
8990
let mut flash_writer = flash.writer::<2048>(FlashSize::Sz256K);
91+
9092
const FLASH_SPACING: u32 = 16; // Separate flash writes by 16 bytes
93+
const FLASH_EXAMPLE_START_ADDRESS: u32 = 0x1FC00;
9194

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
93102

94103
for i in 0..6 {
95104
match i {
96105
0 => {
97106
// 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+
);
99112
assert!(result.is_err());
100113
assert_eq!(
101114
result.err().unwrap(),
102115
stm32g4xx_hal::flash::Error::ArrayMustBeDivisibleBy8
103116
);
104117

105118
// 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+
);
107124
assert!(result.is_ok());
125+
logger::info!(
126+
"Wrote 1 byte to address {}",
127+
FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
128+
);
108129
}
109130
1 => {
110131
// 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+
);
112137
assert!(result.is_err());
113138
assert_eq!(
114139
result.err().unwrap(),
115140
stm32g4xx_hal::flash::Error::ArrayMustBeDivisibleBy8
116141
);
117142

118143
// 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+
);
120149
assert!(result.is_ok());
150+
logger::info!(
151+
"Wrote 2 bytes to address {}",
152+
FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
153+
);
121154
}
122155
2 => {
123156
// 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+
);
126162
assert!(result.is_err());
127163
assert_eq!(
128164
result.err().unwrap(),
129165
stm32g4xx_hal::flash::Error::ArrayMustBeDivisibleBy8
130166
);
131167

132168
// 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+
);
135174
assert!(result.is_ok());
175+
logger::info!(
176+
"Wrote 3 bytes to address {}",
177+
FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
178+
);
136179
}
137180
3 => {
138181
// 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+
);
141187
assert!(result.is_err());
142188
assert_eq!(
143189
result.err().unwrap(),
144190
stm32g4xx_hal::flash::Error::ArrayMustBeDivisibleBy8
145191
);
146192

147193
// 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+
);
149199
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+
);
150226
}
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(),
157227
_ => (),
158228
}
159229
}
160230

231+
logger::info!("Validating data written data by performing read and compare");
232+
161233
for i in 0..6 {
162234
match i {
163235
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();
165239
assert!(compare_arrays(&bytes, &one_byte));
240+
logger::info!("Validated 1 byte data");
166241
}
167242
1 => {
168243
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+
)
170248
.unwrap();
171249
assert!(compare_arrays(&bytes, &two_bytes));
250+
logger::info!("Validated 2 byte data");
172251
}
173252
2 => {
174253
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+
)
176258
.unwrap();
177259
assert!(compare_arrays(&bytes, &three_bytes));
260+
logger::info!("Validated 3 byte data");
178261
}
179262
3 => {
180263
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+
)
182268
.unwrap();
183269
assert!(compare_arrays(&bytes, &four_bytes));
270+
logger::info!("Validated 4 byte data");
184271
}
185272
4 => {
186273
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+
)
188278
.unwrap();
189279
assert!(compare_arrays(&bytes, &eight_bytes));
280+
logger::info!("Validated 8 byte data");
190281
}
191282
5 => {
192283
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+
)
194288
.unwrap();
195289
assert!(compare_arrays(&bytes, &sixteen_bytes));
290+
logger::info!("Validated 5 byte data");
196291
}
197292
_ => (),
198293
}
199294
}
200295

296+
logger::info!(
297+
"Finished flash example at address {}",
298+
FLASH_EXAMPLE_START_ADDRESS
299+
);
300+
201301
(
202302
// Initialization of shared resources
203303
Shared {},

src/flash.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
280280

281281
// Check if there is enough data to make 2 words, if there isn't, pad the data with 0xFF
282282
if idx + 8 > data.len() {
283-
let mut tmp_buffer = [255 as u8; 8];
283+
let mut tmp_buffer = [255u8; 8];
284284
for jdx in idx..data.len() {
285285
tmp_buffer[jdx] = data[idx + jdx];
286286
}
@@ -383,8 +383,6 @@ impl FlashExt for FLASH {
383383
}
384384
}
385385

386-
// TODO: Remove after ready to use flash write code
387-
#[allow(dead_code)]
388386
/// Constrained FLASH peripheral
389387
pub struct Parts {
390388
/// Opaque ACR register

0 commit comments

Comments
 (0)