Skip to content

Instantly share code, notes, and snippets.

@metajack
Created July 13, 2021 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metajack/6243ae52a6a46e9732643713790792f0 to your computer and use it in GitHub Desktop.
Save metajack/6243ae52a6a46e9732643713790792f0 to your computer and use it in GitHub Desktop.
#![no_main]
#![no_std]
use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use hal::{
pwr::PwrExt,
rcc::RccExt,
};
use panic_rtt_target as _;
use rtt_target::rprintln;
use stm32h7xx_hal::{self as hal, pac, prelude::*};
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
struct LedState {
on: u16,
off: u16,
}
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
struct LedArray {
reg_addr: u8,
leds: [LedState; 16],
}
#[link_section = ".sram1"]
static mut LED_ARRAY: MaybeUninit<LedArray> = MaybeUninit::uninit();
unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
core::slice::from_raw_parts((p as *const T) as *const u8, core::mem::size_of::<T>())
}
#[entry]
fn main() -> ! {
rtt_target::rtt_init_print!();
let _cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let pwr = dp.PWR.constrain();
// set to Vcore = maximum freq, vos0
let pwrcfg = pwr.vos0(&dp.SYSCFG).freeze();
let rcc = dp.RCC.constrain();
// 480MHz system clock
let _ccdr = rcc
.use_hse(16.mhz())
.sys_ck(480.mhz())
.pll1_strategy(hal::rcc::PllConfigStrategy::Iterative)
.pll1_p_ck(480.mhz()) // 480 MHz
.pll1_q_ck(160.mhz()) // 160 MHz
.pll1_r_ck(320.mhz()) // 320 MHz
.pll3_strategy(hal::rcc::PllConfigStrategy::Iterative)
.pll3_p_ck(49_166_667.hz())
.pll3_q_ck(157_333_334.hz())
.pll3_r_ck(24_583_334.hz())
.freeze(pwrcfg, &dp.SYSCFG);
rprintln!("sizeof LedArray is {}", core::mem::size_of::<LedArray>());
// initialize LED_ARRAY
unsafe {
LED_ARRAY.as_mut_ptr().write(LedArray {
reg_addr: 0x06,
leds: [
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0xfff },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0x100 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0 },
LedState { on: 0, off: 0xfff },
LedState { on: 0, off: 0 },
],
});
}
let led_array = unsafe { &*LED_ARRAY.as_ptr() };
// rprintln!("reg_addr = {}", led_array.reg_addr);
// rprintln!("leds[0].on = {}", {led_array.leds[0].on});
// rprintln!("leds[15].off = {}", {led_array.leds[15].off});
let led_bytes: &[u8] = unsafe { any_as_u8_slice(led_array) };
for (idx, b) in led_bytes.iter().enumerate() {
rprintln!("{}: {:02x}", idx, b);
}
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment