@@ -88,6 +88,14 @@ mod app {
8888 0x12 , 0x34 , 0x56 , 0x78 , 0x9A , 0xBC , 0xDE , 0xF0 , 0x12 , 0x34 , 0x56 , 0x78 , 0x9A , 0xBC ,
8989 0xDE , 0xF0 as u8 ,
9090 ] ;
91+ let data = [
92+ & one_byte[ ..] ,
93+ & two_bytes[ ..] ,
94+ & three_bytes[ ..] ,
95+ & four_bytes[ ..] ,
96+ & eight_bytes[ ..] ,
97+ & sixteen_bytes[ ..] ,
98+ ] ;
9199 let mut flash = dp. FLASH . constrain ( ) ;
92100 let mut flash_writer = flash. writer :: < 2048 > ( FlashSize :: Sz256K ) ;
93101
@@ -102,197 +110,40 @@ mod app {
102110 . erase ( FLASH_EXAMPLE_START_ADDRESS , 128 )
103111 . unwrap ( ) ; // Erase entire page
104112
105- for i in 0 ..6 {
106- match i {
107- 0 => {
108- // This test should fail, as the data needs to be divisible by 8 and force padding is false
109- let result = flash_writer. write (
110- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
111- & one_byte,
112- false ,
113- ) ;
114- assert ! ( result. is_err( ) ) ;
115- assert_eq ! (
116- result. err( ) . unwrap( ) ,
117- stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
118- ) ;
119-
120- // 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
121- let result = flash_writer. write (
122- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
123- & one_byte,
124- true ,
125- ) ;
126- assert ! ( result. is_ok( ) ) ;
127- logger:: info!(
128- "Wrote 1 byte to address {}" ,
129- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
130- ) ;
131- }
132- 1 => {
133- // This test should fail, as the data needs to be divisible by 8 and force padding is false
134- let result = flash_writer. write (
135- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
136- & two_bytes,
137- false ,
138- ) ;
139- assert ! ( result. is_err( ) ) ;
140- assert_eq ! (
141- result. err( ) . unwrap( ) ,
142- stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
143- ) ;
144-
145- // 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
146- let result = flash_writer. write (
147- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
148- & two_bytes,
149- true ,
150- ) ;
151- assert ! ( result. is_ok( ) ) ;
152- logger:: info!(
153- "Wrote 2 bytes to address {}" ,
154- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
155- ) ;
156- }
157- 2 => {
158- // This test should fail, as the data needs to be divisible by 8 and force padding is false
159- let result = flash_writer. write (
160- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
161- & three_bytes,
162- false ,
163- ) ;
164- assert ! ( result. is_err( ) ) ;
165- assert_eq ! (
166- result. err( ) . unwrap( ) ,
167- stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
168- ) ;
169-
170- // 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
171- let result = flash_writer. write (
172- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
173- & three_bytes,
174- true ,
175- ) ;
176- assert ! ( result. is_ok( ) ) ;
177- logger:: info!(
178- "Wrote 3 bytes to address {}" ,
179- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
180- ) ;
181- }
182- 3 => {
183- // This test should fail, as the data needs to be divisible by 8 and force padding is false
184- let result = flash_writer. write (
185- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
186- & four_bytes,
187- false ,
188- ) ;
189- assert ! ( result. is_err( ) ) ;
190- assert_eq ! (
191- result. err( ) . unwrap( ) ,
192- stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
193- ) ;
194-
195- // 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
196- let result = flash_writer. write (
197- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
198- & four_bytes,
199- true ,
200- ) ;
201- assert ! ( result. is_ok( ) ) ;
202- logger:: info!(
203- "Wrote 4 bytes to address {}" ,
204- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
205- ) ;
206- }
207- 4 => {
208- flash_writer
209- . write (
210- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
211- & eight_bytes,
212- false ,
213- )
214- . unwrap ( ) ;
215- logger:: info!(
216- "Wrote 8 bytes to address {}" ,
217- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
218- ) ;
219- }
220- 5 => {
221- flash_writer
222- . write ( FLASH_EXAMPLE_START_ADDRESS + i * 16 , & sixteen_bytes, false )
223- . unwrap ( ) ;
224- logger:: info!(
225- "Wrote 16 bytes to address {}" ,
226- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
227- ) ;
228- }
229- _ => ( ) ,
230- }
113+ for ( i, data) in data. iter ( ) . enumerate ( ) {
114+ let i = i as u32 ;
115+ // This test should fail, as the data needs to be divisible by 8 and force padding is false
116+ let result =
117+ flash_writer. write ( FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING , data, false ) ;
118+ assert ! ( data. len( ) % 8 == 0 || result. is_err( ) ) ;
119+ assert_eq ! (
120+ result. err( ) . unwrap( ) ,
121+ stm32g4xx_hal:: flash:: Error :: ArrayMustBeDivisibleBy8
122+ ) ;
123+
124+ // This test should pass, as the data needs to be divisible by 8 and force padding is true.
125+ // For example, the one_byte array will be padded with 7 bytes of 0xFF
126+ let result =
127+ flash_writer. write ( FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING , data, true ) ;
128+ assert ! ( result. is_ok( ) ) ;
129+ logger:: info!(
130+ "Wrote {} byte to address {}" ,
131+ data. len( ) ,
132+ FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING
133+ ) ;
231134 }
232135
233- logger:: info!( "Validating data written data by performing read and compare" ) ;
234-
235- for i in 0 ..6 {
236- match i {
237- 0 => {
238- let bytes = flash_writer
239- . read ( FLASH_EXAMPLE_START_ADDRESS as u32 , one_byte. len ( ) )
240- . unwrap ( ) ;
241- assert ! ( compare_arrays( & bytes, & one_byte) ) ;
242- logger:: info!( "Validated 1 byte data" ) ;
243- }
244- 1 => {
245- let bytes = flash_writer
246- . read (
247- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
248- two_bytes. len ( ) ,
249- )
250- . unwrap ( ) ;
251- assert ! ( compare_arrays( & bytes, & two_bytes) ) ;
252- logger:: info!( "Validated 2 byte data" ) ;
253- }
254- 2 => {
255- let bytes = flash_writer
256- . read (
257- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
258- three_bytes. len ( ) ,
259- )
260- . unwrap ( ) ;
261- assert ! ( compare_arrays( & bytes, & three_bytes) ) ;
262- logger:: info!( "Validated 3 byte data" ) ;
263- }
264- 3 => {
265- let bytes = flash_writer
266- . read (
267- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
268- four_bytes. len ( ) ,
269- )
270- . unwrap ( ) ;
271- assert ! ( compare_arrays( & bytes, & four_bytes) ) ;
272- logger:: info!( "Validated 4 byte data" ) ;
273- }
274- 4 => {
275- let bytes = flash_writer
276- . read (
277- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
278- eight_bytes. len ( ) ,
279- )
280- . unwrap ( ) ;
281- assert ! ( compare_arrays( & bytes, & eight_bytes) ) ;
282- logger:: info!( "Validated 8 byte data" ) ;
283- }
284- 5 => {
285- let bytes = flash_writer
286- . read (
287- FLASH_EXAMPLE_START_ADDRESS + i * FLASH_SPACING ,
288- sixteen_bytes. len ( ) ,
289- )
290- . unwrap ( ) ;
291- assert ! ( compare_arrays( & bytes, & sixteen_bytes) ) ;
292- logger:: info!( "Validated 5 byte data" ) ;
293- }
294- _ => ( ) ,
295- }
136+ logger:: info!( "Validating data written by performing read and compare" ) ;
137+
138+ for ( i, data) in data. iter ( ) . enumerate ( ) {
139+ let bytes = flash_writer
140+ . read (
141+ FLASH_EXAMPLE_START_ADDRESS + i as u32 * FLASH_SPACING ,
142+ data. len ( ) ,
143+ )
144+ . unwrap ( ) ;
145+ assert_eq ! ( & bytes, data) ;
146+ logger:: info!( "Validated {} byte data" , data. len( ) ) ;
296147 }
297148
298149 logger:: info!(
0 commit comments