USART sending via VCP
This commit is contained in:
parent
79fb79497e
commit
7d7b443978
@ -17,7 +17,7 @@ stm32f7xx-hal = { version ="0.8.0", features = ["stm32f746", "rt"] }
|
||||
defmt = "0.3.8"
|
||||
defmt-rtt = "0.4.1"
|
||||
panic-probe = { version = "0.3.2", features = ["print-defmt"] }
|
||||
|
||||
rtic-monotonics = {version = "2.0.0", features = ["cortex-m-systick"]}
|
||||
# Uncomment for the panic example.
|
||||
#panic-itm = "0.4.1"
|
||||
#panic-semihosting = "0.6.0"
|
||||
|
90
src/main.rs
90
src/main.rs
@ -7,40 +7,84 @@ use panic_halt as _;
|
||||
#[rtic::app(device = stm32f7xx_hal::pac, peripherals = true, dispatchers = [EXTI0])]
|
||||
mod app {
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
systick_monotonic!(Mono, 1000);
|
||||
|
||||
use stm32f7xx_hal::{
|
||||
gpio::{GpioExt, Output, PushPull, PI1},
|
||||
pac::Interrupt,
|
||||
rcc::RccExt,
|
||||
gpio::{GpioExt, Output, PushPull, PI1}, pac::{Interrupt, USART1}, prelude::*, rcc::RccExt, serial::{Config, Rx, Serial, Tx}
|
||||
};
|
||||
use rtic_monotonics::systick::prelude::*;
|
||||
|
||||
#[shared]
|
||||
struct Shared {}
|
||||
|
||||
#[local]
|
||||
struct Local {
|
||||
ld1: PI1<Output<PushPull>>,
|
||||
state: bool
|
||||
led: PI1<Output<PushPull>>,
|
||||
state: bool,
|
||||
tx_vcp: Tx<USART1>,
|
||||
rx_vcp: Rx<USART1>,
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(ctx: init::Context) -> (Shared, Local) {
|
||||
defmt::info!("INITED");
|
||||
|
||||
rtic::pend(Interrupt::UART4);
|
||||
|
||||
// Cortex-M peripherals
|
||||
let _rcc = ctx.device.RCC.constrain();
|
||||
let rcc = ctx.device.RCC.constrain();
|
||||
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
|
||||
|
||||
//Systick
|
||||
|
||||
Mono::start(ctx.core.SYST, 36_000_000);
|
||||
|
||||
//GPIO
|
||||
let GPIOI = ctx.device.GPIOI.split();
|
||||
let GPIOA = ctx.device.GPIOA.split();
|
||||
let GPIOB = ctx.device.GPIOB.split();
|
||||
|
||||
|
||||
//UART
|
||||
let usart1_tx = GPIOA.pa9.into_alternate();
|
||||
let usart1_rx = GPIOB.pb7.into_alternate();
|
||||
|
||||
let usart1_config = Config {
|
||||
baud_rate: 9600.bps(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let usart1 =
|
||||
Serial::new(
|
||||
ctx.device.USART1,
|
||||
(usart1_tx, usart1_rx),
|
||||
&clocks,
|
||||
usart1_config);
|
||||
|
||||
let (tx_uart1, rx_uart1) = usart1.split();
|
||||
|
||||
//LED
|
||||
let mut ld1 = GPIOI
|
||||
.pi1
|
||||
.into_push_pull_output();
|
||||
|
||||
.into_push_pull_output();
|
||||
ld1.set_high();
|
||||
|
||||
//Starting Tasks
|
||||
//TASKS
|
||||
blinker::spawn().ok();
|
||||
|
||||
(Shared {}, Local {ld1, state : true})
|
||||
serial_task::spawn().ok();
|
||||
|
||||
defmt::info!("INITED");
|
||||
(
|
||||
|
||||
Shared {},
|
||||
Local {
|
||||
led: ld1,
|
||||
state : true,
|
||||
tx_vcp: tx_uart1,
|
||||
rx_vcp: rx_uart1
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[idle]
|
||||
@ -58,26 +102,36 @@ mod app {
|
||||
defmt::info!("UART0");
|
||||
}
|
||||
|
||||
#[task(local = [ld1, state], priority = 1)]
|
||||
#[task(local = [led, state], priority = 1)]
|
||||
async fn blinker(ctx: blinker::Context){
|
||||
loop{
|
||||
defmt::info!("BLINKING...");
|
||||
|
||||
if *ctx.local.state{
|
||||
ctx.local.ld1.set_low();
|
||||
ctx.local.led.set_low();
|
||||
*ctx.local.state = false;
|
||||
}
|
||||
else{
|
||||
ctx.local.ld1.set_high();
|
||||
ctx.local.led.set_high();
|
||||
*ctx.local.state = true;
|
||||
}
|
||||
|
||||
|
||||
for _ in 0..100_000 {
|
||||
cortex_m::asm::nop();
|
||||
}
|
||||
Mono::delay(100.millis()).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task(local = [tx_vcp, rx_vcp], priority = 1)]
|
||||
async fn serial_task(ctx: serial_task::Context){
|
||||
defmt::info!("Start sending data");
|
||||
loop{
|
||||
|
||||
|
||||
ctx.local.tx_vcp.write_str("Hello\n").unwrap();
|
||||
|
||||
|
||||
Mono::delay(500.millis()).await;
|
||||
}
|
||||
}
|
||||
//APP ENDS
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user