Skip to content

Commit 2da85df

Browse files
committed
Add example for OCTOSPI
Tested on a STM32H735G-DK with a Macronix MX25LM51245GXDI00
1 parent b0b6a4f commit 2da85df

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/octospi.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//! OCTOSPI peripheral example in indirect mode
2+
//!
3+
//! Tested on a STM32H735G-DK with a Macronix MX25LM51245GXDI00
4+
5+
#![deny(warnings)]
6+
#![no_main]
7+
#![no_std]
8+
9+
#[macro_use]
10+
mod utilities;
11+
12+
use cortex_m_rt::entry;
13+
use stm32h7xx_hal::rcc::rec::{OctospiClkSel, OctospiClkSelGetter};
14+
use stm32h7xx_hal::{
15+
pac, prelude::*, xspi::OctospiMode, xspi::OctospiWord as XW,
16+
};
17+
18+
use log::info;
19+
20+
#[entry]
21+
fn main() -> ! {
22+
utilities::logger::init();
23+
let dp = pac::Peripherals::take().unwrap();
24+
25+
// Constrain and Freeze power
26+
let pwr = dp.PWR.constrain();
27+
let pwrcfg = example_power!(pwr).freeze();
28+
29+
// Constrain and Freeze clock
30+
let rcc = dp.RCC.constrain();
31+
let ccdr = rcc.sys_ck(96.mhz()).freeze(pwrcfg, &dp.SYSCFG);
32+
33+
// Octospi from HCLK at 48MHz
34+
assert_eq!(ccdr.clocks.hclk().0, 48_000_000);
35+
assert_eq!(
36+
ccdr.peripheral.OCTOSPI1.get_kernel_clk_mux(),
37+
OctospiClkSel::RCC_HCLK3
38+
);
39+
40+
// Acquire the GPIO peripherals. This also enables the clock for
41+
// the GPIOs in the RCC register.
42+
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
43+
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
44+
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
45+
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
46+
47+
let _ncs = gpiog.pg6.into_alternate_af10();
48+
let _clk = gpiof.pf10.into_alternate_af9();
49+
let _io0 = gpiod.pd11.into_alternate_af9();
50+
let _io1 = gpiod.pd12.into_alternate_af9();
51+
let _io2 = gpioe.pe2.into_alternate_af9();
52+
let _io3 = gpiod.pd13.into_alternate_af9();
53+
let _io4 = gpiod.pd4.into_alternate_af10();
54+
let _io5 = gpiod.pd5.into_alternate_af10();
55+
let _io6 = gpiog.pg9.into_alternate_af9();
56+
let _io7 = gpiod.pd7.into_alternate_af10();
57+
58+
info!("");
59+
info!("stm32h7xx-hal example - OCTOSPI");
60+
info!("");
61+
62+
// Initialise the OCTOSPI peripheral.
63+
let mut octospi = dp.OCTOSPI1.octospi_unchecked(
64+
12.mhz(),
65+
&ccdr.clocks,
66+
ccdr.peripheral.OCTOSPI1,
67+
);
68+
69+
octospi.configure_mode(OctospiMode::OneBit).unwrap();
70+
71+
// RDID Read Identification. Abuses address as instruction phase, but that
72+
// works in SPI mode.
73+
let mut read: [u8; 3] = [0; 3];
74+
octospi.read(0x9F, &mut read).unwrap();
75+
info!("Read with instruction 0x9F : {:x?}", read);
76+
77+
// Switch Macronix MX25LM51245GXDI00 to SDR OPI
78+
// Set WREN bit
79+
octospi
80+
.write_extended(XW::U8(0x06), XW::None, XW::None, &[])
81+
.unwrap();
82+
// Write Configuration Register 2
83+
octospi
84+
.write_extended(XW::U8(0x72), XW::U32(0), XW::None, &[1])
85+
.unwrap();
86+
// Change bus mode
87+
octospi.configure_mode(OctospiMode::EightBit).unwrap();
88+
89+
// RDID Read Identification
90+
let mut read: [u8; 3] = [0; 3];
91+
octospi
92+
.read_extended(XW::U16(0x9F60), XW::U32(0), XW::None, 4, &mut read)
93+
.unwrap();
94+
95+
info!("Read with instruction 0x9F60 : {:x?}", read);
96+
97+
loop {}
98+
}

0 commit comments

Comments
 (0)