|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +use panic_rtt_target as _; |
| 5 | + |
| 6 | +#[rtic::app(device = stm32f3xx_hal::pac, dispatchers = [TIM20_BRK, TIM20_UP, TIM20_TRG_COM])] |
| 7 | +mod app { |
| 8 | + use dwt_systick_monotonic::DwtSystick; |
| 9 | + use rtt_target::{rprintln, rtt_init_print}; |
| 10 | + use stm32f3xx_hal::{prelude::*, serial::{Event, Serial}}; |
| 11 | + use stm32f3xx_hal::gpio::{self, Output, PushPull, Alternate, U}; |
| 12 | + |
| 13 | + #[monotonic(binds = SysTick, default = true)] |
| 14 | + type DwtMono = DwtSystick<48_000_000>; |
| 15 | + |
| 16 | + type SerialType = Serial<stm32f3xx_hal::pac::USART1, (gpio::Pin<gpio::Gpioa, U<9_u8>, Alternate<PushPull, 7_u8>>, gpio::Pin<gpio::Gpioa, U<10_u8>, Alternate<PushPull, 7_u8>>)>; |
| 17 | + type DirType = stm32f3xx_hal::gpio::Pin<gpio::Gpioe, U<13_u8>, Output<PushPull>>; |
| 18 | + #[resources] |
| 19 | + struct Resources { |
| 20 | + serial: SerialType, |
| 21 | + dir: DirType, |
| 22 | + } |
| 23 | + |
| 24 | + #[init] |
| 25 | + fn init(cx: init::Context) -> (init::LateResources, init::Monotonics) { |
| 26 | + let mut flash = cx.device.FLASH.constrain(); |
| 27 | + let mut rcc = cx.device.RCC.constrain(); |
| 28 | + let mut dcb = cx.core.DCB; |
| 29 | + let dwt = cx.core.DWT; |
| 30 | + let systick = cx.core.SYST; |
| 31 | + |
| 32 | + rtt_init_print!(NoBlockSkip, 4096); |
| 33 | + rprintln!("pre init"); |
| 34 | + |
| 35 | + // Initialize the clocks |
| 36 | + let clocks = rcc.cfgr.sysclk(48.MHz()).freeze(&mut flash.acr); |
| 37 | + let mono = DwtSystick::new(&mut dcb, dwt, systick, clocks.sysclk().0); |
| 38 | + |
| 39 | + // Initialize the peripherals |
| 40 | + // DIR |
| 41 | + let mut gpioe = cx.device.GPIOE.split(&mut rcc.ahb); |
| 42 | + let mut dir : DirType = gpioe.pe13.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper); |
| 43 | + dir.set_low().unwrap(); |
| 44 | + |
| 45 | + // SERIAL |
| 46 | + let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb); |
| 47 | + let mut pins = ( |
| 48 | + gpioa |
| 49 | + .pa9 |
| 50 | + .into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh), |
| 51 | + gpioa |
| 52 | + .pa10 |
| 53 | + .into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh), |
| 54 | + ); |
| 55 | + pins.1.internal_pull_up(&mut gpioa.pupdr, true); |
| 56 | + let mut serial: SerialType = Serial::usart1(cx.device.USART1, pins, 19200_u32.Bd(), clocks, &mut rcc.apb2); |
| 57 | + serial.listen(Event::Rxne); |
| 58 | + |
| 59 | + rprintln!("post init"); |
| 60 | + |
| 61 | + task1::spawn().unwrap(); |
| 62 | + |
| 63 | + (init::LateResources {serial, dir}, init::Monotonics(mono)) |
| 64 | + } |
| 65 | + |
| 66 | + #[task(binds = USART1_EXTI25, resources = [serial, dir])] |
| 67 | + fn protocol_serial_task(cx: protocol_serial_task::Context) { |
| 68 | + let mut serial = cx.resources.serial; |
| 69 | + let mut dir = cx.resources.dir; |
| 70 | + |
| 71 | + serial.lock(|serial| { |
| 72 | + dir.lock(|dir| { |
| 73 | + if serial.is_rxne() { |
| 74 | + dir.set_high().unwrap(); |
| 75 | + serial.unlisten(Event::Rxne); |
| 76 | + match serial.read() { |
| 77 | + Ok(byte) => { |
| 78 | + serial.write(byte).unwrap(); |
| 79 | + serial.listen(Event::Tc); |
| 80 | + }, |
| 81 | + Err(_error) => rprintln!("irq error"), |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + if serial.is_tc() { |
| 86 | + dir.set_low().unwrap(); |
| 87 | + serial.unlisten(Event::Tc); |
| 88 | + serial.listen(Event::Rxne); |
| 89 | + } |
| 90 | + }) |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + #[task] |
| 95 | + fn task1(_cx: task1::Context) { |
| 96 | + rprintln!("task1"); |
| 97 | + } |
| 98 | + |
| 99 | + #[idle] |
| 100 | + fn idle(_: idle::Context) -> ! { |
| 101 | + rprintln!("idle"); |
| 102 | + loop { |
| 103 | + cortex_m::asm::nop(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments