Created
July 23, 2022 13:46
-
-
Save thelinuxpoint/b58e1d4a7ee903f4aab2d4eabd0fd384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use panic_halt; | |
use cortex_m::asm::delay; | |
use cortex_m_rt::entry; // The runtime | |
use embedded_hal::digital::v2::OutputPin; // the `set_high/low`function | |
// Hardware abstract Layer | |
use stm32f1xx_hal::{delay::Delay, pac, prelude::*,serial::{Config, Serial}}; // STM32F1 specific functions | |
use stm32f1xx_hal::usb::{Peripheral, UsbBus}; | |
use stm32f1xx_hal::pac::{Interrupt}; | |
use stm32f1xx_hal::{stm32}; | |
use stm32f1xx_hal::pac::interrupt; | |
use stm32f1xx_hal::gpio::*; | |
use stm32f1xx_hal::pac::USART1; | |
use stm32f1xx_hal::serial::Pins; | |
use systick_monotonic::{fugit::Duration, Systick}; | |
use core::panic::PanicInfo; | |
use core::mem::MaybeUninit; | |
use stm32f1xx_hal::gpio::gpioa::{PA9,PA10}; | |
// RTIC | |
use rtic::app; | |
use rtic; | |
// use rtt_target::{rprintln, rtt_init_print}; | |
use core::fmt::Write; | |
#[rtic::app(device = stm32f1xx_hal::pac, peripherals = true, dispatchers = [SPI1])] | |
mod app { | |
use super::*; | |
use stm32f1xx_hal::serial::{Config,Tx,Rx, Serial}; | |
#[shared] | |
struct Shared { | |
// serial: Serial<USART1,(PA9<Alternate<PushPull>>, PA10<Input<Floating>>)> | |
} | |
#[local] | |
struct Local { | |
led: gpioc::PC13<Output<PushPull>>, | |
int_pin: gpioa::PA7<Input<Floating>>, | |
led_2: gpiob::PB5<Output<PushPull>>, | |
state: bool, | |
cons: u32, | |
tx: Tx<USART1>, | |
rx: Rx<USART1> | |
} | |
#[monotonic(binds = SysTick, default = true)] | |
type MonoTimer = Systick<1000>; | |
#[init] | |
fn init(mut cx: init::Context) -> (Shared, Local, init::Monotonics) { | |
// let dp = pac::Peripherals::take().unwrap(); | |
// let cp = cortex_m::Peripherals::take().unwrap(); | |
// let cp = cortex_m::Peripherals::take().unwrap(); | |
let mut rcc = cx.device.RCC.constrain(); | |
let mono = Systick::new(cx.core.SYST, 48_000_000); | |
// let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); | |
let mut gpioc = cx.device.GPIOC.split(&mut rcc.apb2); | |
let mut gpioa = cx.device.GPIOA.split(&mut rcc.apb2); | |
let mut gpiob = cx.device.GPIOB.split(&mut rcc.apb2); | |
let mut afio = cx.device.AFIO.constrain(&mut rcc.apb2); | |
let mut flash = cx.device.FLASH.constrain(); | |
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); | |
let mut led_2 = gpiob.pb5.into_push_pull_output(&mut gpiob.crl); | |
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh); | |
let rx = gpioa.pa10; | |
// let mut dp = cx.device; | |
let clocks = rcc | |
.cfgr | |
.use_hse(8.mhz()) | |
.sysclk(48.mhz()) | |
.pclk1(24.mhz()) | |
.freeze(&mut flash.acr); | |
let serial = Serial::usart1( | |
cx.device.USART1, | |
(tx, rx), | |
&mut afio.mapr, | |
Config::default().baudrate(9600.bps()), | |
clocks, | |
&mut rcc.apb2 | |
); | |
let (mut tex, rex) = serial.split(); | |
let mut int_pin = gpioa.pa7.into_floating_input(&mut gpioa.crl); | |
int_pin.make_interrupt_source(&mut afio); | |
int_pin.trigger_on_edge(&mut cx.device.EXTI, Edge::RISING_FALLING); | |
int_pin.enable_interrupt(&mut cx.device.EXTI); | |
rtic::pend(Interrupt::EXTI9_5); | |
( | |
Shared {}, | |
Local { led, int_pin,led_2, state: false,cons:1,tx:tex,rx:rex }, | |
init::Monotonics(mono), | |
) | |
} | |
#[idle(local = [led_2,cons,tx,rx])] | |
fn idle(cx: idle::Context) -> !{ | |
let mut tx = cx.local.tx; | |
let mut rx = cx.local.rx; | |
loop { | |
// hprintln!("idle {}",*cx.local.cons).unwrap(); | |
writeln!(tx, "\rHello formatted string {}", *cx.local.cons).unwrap(); | |
*cx.local.cons+=1; | |
if *cx.local.cons == 100 { | |
*cx.local.cons = 1; | |
} | |
cx.local.led_2.set_high().ok(); | |
delay(48000000/(*cx.local.cons)); | |
cx.local.led_2.set_low().ok(); | |
delay(48000000/(*cx.local.cons)); | |
} | |
} | |
#[task(binds = EXTI9_5 ,local = [led, int_pin, state])] | |
fn blink(cx: blink::Context) { | |
if cx.local.int_pin.check_interrupt() { | |
cx.local.led.toggle(); | |
// if we don't clear this bit, the ISR would trigger indefinitely | |
cx.local.int_pin.clear_interrupt_pending_bit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment