|
| 1 | +//! Example using a OCTOSPI HyperRAM in memory-mapped mode |
| 2 | +//! |
| 3 | +//! Tested on a STM32H735G-DK with a Cypress S70KL1281DABHI023 |
| 4 | +
|
| 5 | +#![deny(warnings)] |
| 6 | +#![no_main] |
| 7 | +#![no_std] |
| 8 | + |
| 9 | +use core::mem; |
| 10 | +use core::slice; |
| 11 | + |
| 12 | +#[macro_use] |
| 13 | +mod utilities; |
| 14 | + |
| 15 | +use cortex_m_rt::entry; |
| 16 | +use stm32h7xx_hal::rcc::rec::{OctospiClkSel, OctospiClkSelGetter}; |
| 17 | +use stm32h7xx_hal::{gpio::Speed::High, pac, prelude::*, xspi::HyperbusConfig}; |
| 18 | + |
| 19 | +use log::info; |
| 20 | + |
| 21 | +#[entry] |
| 22 | +fn main() -> ! { |
| 23 | + utilities::logger::init(); |
| 24 | + let dp = pac::Peripherals::take().unwrap(); |
| 25 | + |
| 26 | + // Constrain and Freeze power |
| 27 | + let pwr = dp.PWR.constrain(); |
| 28 | + let pwrcfg = example_power!(pwr).freeze(); |
| 29 | + |
| 30 | + // Constrain and Freeze clock |
| 31 | + let rcc = dp.RCC.constrain(); |
| 32 | + let ccdr = rcc.sys_ck(320.mhz()).freeze(pwrcfg, &dp.SYSCFG); |
| 33 | + |
| 34 | + // Octospi from HCLK at 160MHz |
| 35 | + assert_eq!(ccdr.clocks.hclk().0, 160_000_000); |
| 36 | + assert_eq!( |
| 37 | + ccdr.peripheral.OCTOSPI1.get_kernel_clk_mux(), |
| 38 | + OctospiClkSel::RCC_HCLK3 |
| 39 | + ); |
| 40 | + |
| 41 | + // Acquire the GPIO peripherals. This also enables the clock for |
| 42 | + // the GPIOs in the RCC register. |
| 43 | + let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG); |
| 44 | + let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB); |
| 45 | + let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF); |
| 46 | + |
| 47 | + let _tracweswo = gpiob.pb3.into_alternate_af0(); |
| 48 | + |
| 49 | + let _ncs = gpiog |
| 50 | + .pg12 |
| 51 | + .into_alternate_af3() |
| 52 | + .set_speed(High) |
| 53 | + .internal_pull_up(true); |
| 54 | + let _dqs = gpiof |
| 55 | + .pf12 |
| 56 | + .into_alternate_af9() |
| 57 | + .set_speed(High) |
| 58 | + .internal_pull_up(true); |
| 59 | + let _clk = gpiof |
| 60 | + .pf4 |
| 61 | + .into_alternate_af9() |
| 62 | + .set_speed(High) |
| 63 | + .internal_pull_up(true); |
| 64 | + let _io0 = gpiof |
| 65 | + .pf0 |
| 66 | + .into_alternate_af9() |
| 67 | + .set_speed(High) |
| 68 | + .internal_pull_up(true); |
| 69 | + let _io1 = gpiof |
| 70 | + .pf1 |
| 71 | + .into_alternate_af9() |
| 72 | + .set_speed(High) |
| 73 | + .internal_pull_up(true); |
| 74 | + let _io2 = gpiof |
| 75 | + .pf2 |
| 76 | + .into_alternate_af9() |
| 77 | + .set_speed(High) |
| 78 | + .internal_pull_up(true); |
| 79 | + let _io3 = gpiof |
| 80 | + .pf3 |
| 81 | + .into_alternate_af9() |
| 82 | + .set_speed(High) |
| 83 | + .internal_pull_up(true); |
| 84 | + let _io4 = gpiog |
| 85 | + .pg0 |
| 86 | + .into_alternate_af9() |
| 87 | + .set_speed(High) |
| 88 | + .internal_pull_up(true); |
| 89 | + let _io5 = gpiog |
| 90 | + .pg1 |
| 91 | + .into_alternate_af9() |
| 92 | + .set_speed(High) |
| 93 | + .internal_pull_up(true); |
| 94 | + let _io6 = gpiog |
| 95 | + .pg10 |
| 96 | + .into_alternate_af3() |
| 97 | + .set_speed(High) |
| 98 | + .internal_pull_up(true); |
| 99 | + let _io7 = gpiog |
| 100 | + .pg11 |
| 101 | + .into_alternate_af9() |
| 102 | + .set_speed(High) |
| 103 | + .internal_pull_up(true); |
| 104 | + |
| 105 | + info!(""); |
| 106 | + info!("stm32h7xx-hal example - OCTOSPI HyperRAM"); |
| 107 | + info!(""); |
| 108 | + |
| 109 | + // Initialise a HyperRAM on the OCTOSPI2 peripheral |
| 110 | + let ram_slice = unsafe { |
| 111 | + let hyperram_size = 16 * 1024 * 1024; // 16 MByte |
| 112 | + let config = HyperbusConfig::new(80.mhz()) |
| 113 | + .device_size_bytes(24) // 16 Mbyte |
| 114 | + .refresh_interval(4.us()) |
| 115 | + .read_write_recovery(4) // 50ns |
| 116 | + .access_initial_latency(6); |
| 117 | + |
| 118 | + let hyperram = dp.OCTOSPI2.octospi_hyperbus_unchecked( |
| 119 | + config, |
| 120 | + &ccdr.clocks, |
| 121 | + ccdr.peripheral.OCTOSPI2, |
| 122 | + ); |
| 123 | + |
| 124 | + info!("Created HyperRAM.."); |
| 125 | + info!("{}", hyperram); |
| 126 | + info!(""); |
| 127 | + |
| 128 | + // Initialise and convert raw pointer to slice |
| 129 | + let ram_ptr: *mut u32 = hyperram.init(); |
| 130 | + slice::from_raw_parts_mut( |
| 131 | + ram_ptr, |
| 132 | + hyperram_size / mem::size_of::<u32>(), |
| 133 | + ) |
| 134 | + }; |
| 135 | + |
| 136 | + info!("Writing checkerboard pattern..."); |
| 137 | + for x in ram_slice.iter_mut() { |
| 138 | + *x = 0xAA55AA55; |
| 139 | + } |
| 140 | + info!("Reading checkerboard pattern..."); |
| 141 | + for (i, x) in ram_slice.iter().enumerate() { |
| 142 | + assert_eq!( |
| 143 | + *x, |
| 144 | + 0xAA55AA55, |
| 145 | + "Mismatch at address 0x{:x} (0x{:x} != 0xaa55aa55)", |
| 146 | + i * 4, |
| 147 | + *x |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + info!("Writing reverse checkerboard pattern..."); |
| 152 | + for x in ram_slice.iter_mut() { |
| 153 | + *x = 0x55AA55AA; |
| 154 | + } |
| 155 | + info!("Reading reverse checkerboard pattern..."); |
| 156 | + for (i, x) in ram_slice.iter().enumerate() { |
| 157 | + assert_eq!( |
| 158 | + *x, |
| 159 | + 0x55AA55AA, |
| 160 | + "Mismatch at address 0x{:x} (0x{:x} != 0x55aa55aa)", |
| 161 | + i * 4, |
| 162 | + *x |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + info!("Success!"); |
| 167 | + info!(""); |
| 168 | + |
| 169 | + loop { |
| 170 | + cortex_m::asm::nop(); |
| 171 | + } |
| 172 | +} |
0 commit comments