125 lines
3.1 KiB
Rust
125 lines
3.1 KiB
Rust
#![no_std]
|
|
#![feature(asm, used, const_fn, naked_functions, alloc, box_syntax)]
|
|
#![no_main]
|
|
#![feature(extern_prelude)]
|
|
|
|
#[macro_use]
|
|
extern crate hcl;
|
|
|
|
#[macro_use]
|
|
extern crate alloc;
|
|
|
|
use core::mem;
|
|
use hcl::platform::gpio;
|
|
use hcl::platform::irq;
|
|
use hcl::platform::rcc;
|
|
use hcl::platform::scs;
|
|
use hcl::platform::timer;
|
|
use hcl::platform::usart;
|
|
use hcl::platform::dma;
|
|
use hcl::dma::*;
|
|
|
|
mod printer;
|
|
|
|
use printer::UsartPrinter;
|
|
|
|
fn configure_clocks(rcc: &mut rcc::RCC) {
|
|
rcc.clock_control.set_hse_on(true);
|
|
while !rcc.clock_control.hse_ready() {
|
|
}
|
|
|
|
rcc.clock_config.configure(|c| c
|
|
.set_pll_multiplier(10)
|
|
.set_pll_source(rcc::PllSource::HsiDiv2));
|
|
|
|
rcc.clock_control.set_pll_on(true);
|
|
while !rcc.clock_control.pll_ready() {
|
|
}
|
|
|
|
rcc.clock_config.switch_clock_source(rcc::SystemClockSource::Pll);
|
|
}
|
|
|
|
fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC,
|
|
gpio: &mut gpio::GPIO,
|
|
usart: &mut usart::USART) {
|
|
rcc.apb2_enable.configure(|a| a
|
|
.set_gpio_a(true)
|
|
.set_spi1(true));
|
|
rcc.apb1_enable.configure(|a| a
|
|
.set_usart2(true));
|
|
|
|
gpio.configure(|g| g
|
|
.set_mode(2, gpio::PinMode::Output50MHz)
|
|
.set_output_config(2, gpio::OutputConfig::AfPushPull)
|
|
.set_mode(3, gpio::PinMode::Output50MHz)
|
|
.set_output_config(3, gpio::OutputConfig::AfPushPull)
|
|
// SCK1
|
|
.set_mode(5, gpio::PinMode::Output50MHz)
|
|
.set_output_config(5, gpio::OutputConfig::AfPushPull)
|
|
// MOSI1
|
|
.set_mode(7, gpio::PinMode::Output50MHz)
|
|
.set_output_config(7, gpio::OutputConfig::AfPushPull)
|
|
// MISO1
|
|
.set_mode(6, gpio::PinMode::Input)
|
|
// NSS1
|
|
.set_mode(4, gpio::PinMode::Output50MHz)
|
|
.set_output_config(4, gpio::OutputConfig::PushPull));
|
|
|
|
usart.configure(|u| u
|
|
.set_enabled(true)
|
|
.set_tx_enabled(true)
|
|
.set_baudgen((21, 11))); // 115.2 kbaud
|
|
}
|
|
|
|
|
|
|
|
// allowing inlining into main() breaks the stack, since main() must be naked to set up a process stack.
|
|
#[inline(never)]
|
|
fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) {
|
|
configure_clocks(&mut p.rcc);
|
|
configure_peripherals(&mut p.rcc, &mut p.gpio_a, &mut p.usart2);
|
|
|
|
let mut printer = UsartPrinter::init(p.usart2);
|
|
|
|
let mut spi = p.spi1;
|
|
spi.configure(|s| s
|
|
.set_enabled(true)
|
|
.set_master_mode(true)
|
|
.set_bidi_tx(false)
|
|
.set_clock_divider(128)
|
|
.set_data_16bit(true)
|
|
.set_clock_idles_high(false)
|
|
.set_lsb_first(false)
|
|
.set_clock_skip_first(true));
|
|
|
|
loop {
|
|
for i in 0u16..0xffff {
|
|
let x = format!("> {}\r\n", i).into_bytes();
|
|
|
|
p.gpio_a.set_bit(4);
|
|
spi.set_data(i.into());
|
|
|
|
while spi.busy() {
|
|
|
|
}
|
|
p.gpio_a.reset_bit(4);
|
|
|
|
|
|
printer.print(x);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
entry_point!(main);
|
|
fn main(scs: scs::Instance, p: hcl::platform::Instance) {
|
|
declare_thread_stack!(stack, 512);
|
|
|
|
unsafe {
|
|
hcl::set_process_stack(&stack);
|
|
hcl::use_process_stack(true);
|
|
}
|
|
|
|
run(scs, p);
|
|
}
|