137 lines
3.4 KiB
Plaintext
137 lines
3.4 KiB
Plaintext
#![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)
|
|
.set_input_config(6, gpio::InputConfig::PullUpDown)
|
|
// 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_slave_select_output_enabled(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 {
|
|
|
|
p.gpio_a.reset_bit(4);
|
|
spi.set_data(0xFFFF);
|
|
|
|
while !spi.rx_buffer_not_empty() {
|
|
}
|
|
|
|
let i = spi.data() >> 3;
|
|
let temp = (i as f32) * 0.25;
|
|
|
|
while spi.busy() {
|
|
}
|
|
p.gpio_a.set_bit(4);
|
|
|
|
|
|
|
|
let x = format!("> {}\r\n", temp).into_bytes();
|
|
|
|
printer.print(x);
|
|
|
|
for y in 0u32..0xFFFFFF {
|
|
unsafe { asm!("nop" :::: "volatile") };
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
entry_point!(main);
|
|
fn main(scs: scs::Instance, p: hcl::platform::Instance) {
|
|
declare_thread_stack!(stack, 4096);
|
|
|
|
unsafe {
|
|
hcl::set_process_stack(&stack);
|
|
hcl::use_process_stack(true);
|
|
}
|
|
|
|
run(scs, p);
|
|
}
|