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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
#![no_main]
#![no_std]
// Tested on weact stm32h7b0 board + w25q64 spi flash
use defmt::info;
use embassy_executor::Spawner;
use embassy_stm32::Config;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::mode::Blocking;
use embassy_stm32::ospi::{
AddressSize, ChipSelectHighTime, DummyCycles, FIFOThresholdLevel, Instance, MemorySize, MemoryType, Ospi,
OspiWidth, TransferConfig, WrapSize,
};
use embassy_stm32::time::Hertz;
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
// RCC config
let mut config = Config::default();
info!("START");
{
use embassy_stm32::rcc::*;
config.rcc.hsi = Some(HSIPrescaler::DIV1);
config.rcc.csi = true;
// Needed for USB
config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true });
// External oscillator 25MHZ
config.rcc.hse = Some(Hse {
freq: Hertz(25_000_000),
mode: HseMode::Oscillator,
});
config.rcc.pll1 = Some(Pll {
source: PllSource::HSE,
prediv: PllPreDiv::DIV5,
mul: PllMul::MUL112,
divp: Some(PllDiv::DIV2),
divq: Some(PllDiv::DIV2),
divr: Some(PllDiv::DIV2),
});
config.rcc.sys = Sysclk::PLL1_P;
config.rcc.ahb_pre = AHBPrescaler::DIV2;
config.rcc.apb1_pre = APBPrescaler::DIV2;
config.rcc.apb2_pre = APBPrescaler::DIV2;
config.rcc.apb3_pre = APBPrescaler::DIV2;
config.rcc.apb4_pre = APBPrescaler::DIV2;
config.rcc.voltage_scale = VoltageScale::Scale0;
}
// Initialize peripherals
let p = embassy_stm32::init(config);
let qspi_config = embassy_stm32::ospi::Config {
fifo_threshold: FIFOThresholdLevel::_16Bytes,
memory_type: MemoryType::Micron,
device_size: MemorySize::_8MiB,
chip_select_high_time: ChipSelectHighTime::_1Cycle,
free_running_clock: false,
clock_mode: false,
wrap_size: WrapSize::None,
clock_prescaler: 4,
sample_shifting: true,
delay_hold_quarter_cycle: false,
chip_select_boundary: 0,
delay_block_bypass: true,
max_transfer: 0,
refresh: 0,
};
let ospi = embassy_stm32::ospi::Ospi::new_blocking_quadspi(
p.OCTOSPI1,
p.PB2,
p.PD11,
p.PD12,
p.PE2,
p.PD13,
p.PB6,
qspi_config,
);
let mut flash = FlashMemory::new(ospi).await;
let flash_id = flash.read_id();
info!("FLASH ID: {=[u8]:x}", flash_id);
let mut wr_buf = [0u8; 8];
for i in 0..8 {
wr_buf[i] = i as u8;
}
let mut rd_buf = [0u8; 8];
flash.erase_sector(0).await;
flash.write_memory(0, &wr_buf, true).await;
flash.read_memory(0, &mut rd_buf, true);
info!("WRITE BUF: {=[u8]:#X}", wr_buf);
info!("READ BUF: {=[u8]:#X}", rd_buf);
flash.enable_mm().await;
info!("Enabled memory mapped mode");
let first_u32 = unsafe { *(0x90000000 as *const u32) };
assert_eq!(first_u32, 0x03020100);
let second_u32 = unsafe { *(0x90000004 as *const u32) };
assert_eq!(second_u32, 0x07060504);
flash.disable_mm().await;
info!("DONE");
// Output pin PE3
let mut led = Output::new(p.PE3, Level::Low, Speed::Low);
loop {
led.toggle();
Timer::after_millis(1000).await;
}
}
const MEMORY_PAGE_SIZE: usize = 8;
const CMD_QUAD_READ: u8 = 0x6B;
const CMD_QUAD_WRITE_PG: u8 = 0x32;
const CMD_READ_ID: u8 = 0x9F;
const CMD_ENABLE_RESET: u8 = 0x66;
const CMD_RESET: u8 = 0x99;
const CMD_WRITE_ENABLE: u8 = 0x06;
const CMD_CHIP_ERASE: u8 = 0xC7;
const CMD_SECTOR_ERASE: u8 = 0x20;
const CMD_BLOCK_ERASE_32K: u8 = 0x52;
const CMD_BLOCK_ERASE_64K: u8 = 0xD8;
const CMD_READ_SR: u8 = 0x05;
const CMD_READ_CR: u8 = 0x35;
const CMD_WRITE_SR: u8 = 0x01;
const CMD_WRITE_CR: u8 = 0x31;
/// Implementation of access to flash chip.
/// Chip commands are hardcoded as it depends on used chip.
/// This implementation is using chip GD25Q64C from Giga Device
pub struct FlashMemory<I: Instance> {
ospi: Ospi<'static, I, Blocking>,
}
impl<I: Instance> FlashMemory<I> {
pub async fn new(ospi: Ospi<'static, I, Blocking>) -> Self {
let mut memory = Self { ospi };
memory.reset_memory().await;
memory.enable_quad();
memory
}
async fn qpi_mode(&mut self) {
// Enter qpi mode
self.exec_command(0x38).await;
// Set read param
let transaction = TransferConfig {
iwidth: OspiWidth::QUAD,
dwidth: OspiWidth::QUAD,
instruction: Some(0xC0),
..Default::default()
};
self.enable_write().await;
self.ospi.blocking_write(&[0x30_u8], transaction).unwrap();
self.wait_write_finish();
}
pub async fn disable_mm(&mut self) {
self.ospi.disable_memory_mapped_mode();
}
pub async fn enable_mm(&mut self) {
self.qpi_mode().await;
let read_config = TransferConfig {
iwidth: OspiWidth::QUAD,
isize: AddressSize::_8Bit,
adwidth: OspiWidth::QUAD,
adsize: AddressSize::_24bit,
dwidth: OspiWidth::QUAD,
instruction: Some(0x0B), // Fast read in QPI mode
dummy: DummyCycles::_8,
..Default::default()
};
let write_config = TransferConfig {
iwidth: OspiWidth::SING,
isize: AddressSize::_8Bit,
adwidth: OspiWidth::SING,
adsize: AddressSize::_24bit,
dwidth: OspiWidth::QUAD,
instruction: Some(0x32), // Write config
dummy: DummyCycles::_0,
..Default::default()
};
self.ospi.enable_memory_mapped_mode(read_config, write_config).unwrap();
}
fn enable_quad(&mut self) {
let cr = self.read_cr();
// info!("Read cr: {:x}", cr);
self.write_cr(cr | 0x02);
// info!("Read cr after writing: {:x}", cr);
}
pub fn disable_quad(&mut self) {
let cr = self.read_cr();
self.write_cr(cr & (!(0x02)));
}
async fn exec_command_4(&mut self, cmd: u8) {
let transaction = TransferConfig {
iwidth: OspiWidth::QUAD,
adwidth: OspiWidth::NONE,
// adsize: AddressSize::_24bit,
dwidth: OspiWidth::NONE,
instruction: Some(cmd as u32),
address: None,
dummy: DummyCycles::_0,
..Default::default()
};
self.ospi.blocking_command(&transaction).unwrap();
}
async fn exec_command(&mut self, cmd: u8) {
let transaction = TransferConfig {
iwidth: OspiWidth::SING,
adwidth: OspiWidth::NONE,
// adsize: AddressSize::_24bit,
dwidth: OspiWidth::NONE,
instruction: Some(cmd as u32),
address: None,
dummy: DummyCycles::_0,
..Default::default()
};
// info!("Excuting command: {:x}", transaction.instruction);
self.ospi.blocking_command(&transaction).unwrap();
}
pub async fn reset_memory(&mut self) {
self.exec_command_4(CMD_ENABLE_RESET).await;
self.exec_command_4(CMD_RESET).await;
self.exec_command(CMD_ENABLE_RESET).await;
self.exec_command(CMD_RESET).await;
self.wait_write_finish();
}
pub async fn enable_write(&mut self) {
self.exec_command(CMD_WRITE_ENABLE).await;
}
pub fn read_id(&mut self) -> [u8; 3] {
let mut buffer = [0; 3];
let transaction: TransferConfig = TransferConfig {
iwidth: OspiWidth::SING,
isize: AddressSize::_8Bit,
adwidth: OspiWidth::NONE,
// adsize: AddressSize::_24bit,
dwidth: OspiWidth::SING,
instruction: Some(CMD_READ_ID as u32),
..Default::default()
};
// info!("Reading id: 0x{:X}", transaction.instruction);
self.ospi.blocking_read(&mut buffer, transaction).unwrap();
buffer
}
pub fn read_id_4(&mut self) -> [u8; 3] {
let mut buffer = [0; 3];
let transaction: TransferConfig = TransferConfig {
iwidth: OspiWidth::SING,
isize: AddressSize::_8Bit,
adwidth: OspiWidth::NONE,
dwidth: OspiWidth::QUAD,
instruction: Some(CMD_READ_ID as u32),
..Default::default()
};
info!("Reading id: 0x{:X}", transaction.instruction);
self.ospi.blocking_read(&mut buffer, transaction).unwrap();
buffer
}
pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) {
let transaction = TransferConfig {
iwidth: OspiWidth::SING,
adwidth: OspiWidth::SING,
adsize: AddressSize::_24bit,
dwidth: OspiWidth::QUAD,
instruction: Some(CMD_QUAD_READ as u32),
address: Some(addr),
dummy: DummyCycles::_8,
..Default::default()
};
if use_dma {
self.ospi.blocking_read(buffer, transaction).unwrap();
} else {
self.ospi.blocking_read(buffer, transaction).unwrap();
}
}
fn wait_write_finish(&mut self) {
while (self.read_sr() & 0x01) != 0 {}
}
async fn perform_erase(&mut self, addr: u32, cmd: u8) {
let transaction = TransferConfig {
iwidth: OspiWidth::SING,
adwidth: OspiWidth::SING,
adsize: AddressSize::_24bit,
dwidth: OspiWidth::NONE,
instruction: Some(cmd as u32),
address: Some(addr),
dummy: DummyCycles::_0,
..Default::default()
};
self.enable_write().await;
self.ospi.blocking_command(&transaction).unwrap();
self.wait_write_finish();
}
pub async fn erase_sector(&mut self, addr: u32) {
self.perform_erase(addr, CMD_SECTOR_ERASE).await;
}
pub async fn erase_block_32k(&mut self, addr: u32) {
self.perform_erase(addr, CMD_BLOCK_ERASE_32K).await;
}
pub async fn erase_block_64k(&mut self, addr: u32) {
self.perform_erase(addr, CMD_BLOCK_ERASE_64K).await;
}
pub async fn erase_chip(&mut self) {
self.exec_command(CMD_CHIP_ERASE).await;
}
async fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) {
assert!(
(len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32,
"write_page(): page write length exceeds page boundary (len = {}, addr = {:X}",
len,
addr
);
let transaction = TransferConfig {
iwidth: OspiWidth::SING,
adsize: AddressSize::_24bit,
adwidth: OspiWidth::SING,
dwidth: OspiWidth::QUAD,
instruction: Some(CMD_QUAD_WRITE_PG as u32),
address: Some(addr),
dummy: DummyCycles::_0,
..Default::default()
};
self.enable_write().await;
if use_dma {
self.ospi.blocking_write(buffer, transaction).unwrap();
} else {
self.ospi.blocking_write(buffer, transaction).unwrap();
}
self.wait_write_finish();
}
pub async fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) {
let mut left = buffer.len();
let mut place = addr;
let mut chunk_start = 0;
while left > 0 {
let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize;
let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left };
let chunk = &buffer[chunk_start..(chunk_start + chunk_size)];
self.write_page(place, chunk, chunk_size, use_dma).await;
place += chunk_size as u32;
left -= chunk_size;
chunk_start += chunk_size;
}
}
fn read_register(&mut self, cmd: u8) -> u8 {
let mut buffer = [0; 1];
let transaction: TransferConfig = TransferConfig {
iwidth: OspiWidth::SING,
isize: AddressSize::_8Bit,
adwidth: OspiWidth::NONE,
adsize: AddressSize::_24bit,
dwidth: OspiWidth::SING,
instruction: Some(cmd as u32),
address: None,
dummy: DummyCycles::_0,
..Default::default()
};
self.ospi.blocking_read(&mut buffer, transaction).unwrap();
// info!("Read w25q64 register: 0x{:x}", buffer[0]);
buffer[0]
}
fn write_register(&mut self, cmd: u8, value: u8) {
let buffer = [value; 1];
let transaction: TransferConfig = TransferConfig {
iwidth: OspiWidth::SING,
isize: AddressSize::_8Bit,
instruction: Some(cmd as u32),
adsize: AddressSize::_24bit,
adwidth: OspiWidth::NONE,
dwidth: OspiWidth::SING,
address: None,
dummy: DummyCycles::_0,
..Default::default()
};
self.ospi.blocking_write(&buffer, transaction).unwrap();
}
pub fn read_sr(&mut self) -> u8 {
self.read_register(CMD_READ_SR)
}
pub fn read_cr(&mut self) -> u8 {
self.read_register(CMD_READ_CR)
}
pub fn write_sr(&mut self, value: u8) {
self.write_register(CMD_WRITE_SR, value);
}
pub fn write_cr(&mut self, value: u8) {
self.write_register(CMD_WRITE_CR, value);
}
}
|