#![deny(unsafe_code)] #![no_std] #![no_main] mod i2c_reg_slave; mod i2c_slave; use panic_halt as _; #[rtic::app(device = stm32f1::stm32f103, peripherals = true)] mod app { use stm32f1xx_hal::{dma, gpio, pac, prelude::*, serial, timer}; use systick_monotonic::Systick; // A monotonic timer to enable scheduling in RTIC #[monotonic(binds = SysTick, default = true)] type MyMono = Systick<100>; // 100 Hz / 10 ms granularity #[shared] struct Shared { #[lock_free] buffer: [u8; 256], } #[local] struct Local { tx: Option, dma::dma1::C4>>, delay_us: timer::DelayUs, led: gpio::gpioc::PC13, } #[init] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { // Take ownership over the raw flash and rcc devices and convert them into the corresponding // HAL structs let mut flash = cx.device.FLASH.constrain(); let rcc = cx.device.RCC.constrain(); // Freeze the configuration of all the clocks in the system and store the frozen frequencies in // `clocks` let clocks = rcc .cfgr // hf external quartz frequency .use_hse(8.MHz()) // system clock frequency .sysclk(72.MHz()) .freeze(&mut flash.acr); // Initialize the monotonic let mono = Systick::new(cx.core.SYST, clocks.sysclk().to_Hz()); // Acquire the peripherals let mut gpioa = cx.device.GPIOA.split(); let mut gpioc = cx.device.GPIOC.split(); let mut afio = cx.device.AFIO.constrain(); let dma1 = cx.device.DMA1.split(); // Serial config let serial = serial::Serial::new( cx.device.USART1, ( gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh), gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh), ), &mut afio.mapr, serial::Config { baudrate: 250_000.bps(), ..Default::default() }, &clocks, ); ( Shared { buffer: [0b01010101; 256], }, Local { tx: Some(serial.tx.with_dma(dma1.4)), // Configure timer delay_us: cx.device.TIM2.delay_us(&clocks), // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function // in order to configure the port. For pins 0-7, crl should be passed instead. led: gpioc.pc13.into_push_pull_output(&mut gpioc.crh), }, init::Monotonics(mono), ) } #[idle(local = [tx, delay_us, led], shared = [&buffer])] fn idle(cx: idle::Context) -> ! { let mut tx = cx.local.tx.take().unwrap(); loop { let xfer = tx.write(cx.shared.buffer); cx.local.delay_us.delay(1.secs()); cx.local.led.set_high(); cx.local.delay_us.delay(1.secs()); cx.local.led.set_low(); (_, tx) = xfer.wait(); } } }