Init Allocator and USART receiving

This commit is contained in:
Игорь Жуков 2024-12-02 15:15:39 +03:00
parent 7d7b443978
commit 007e793ded
3 changed files with 79 additions and 28 deletions

View File

@ -38,5 +38,5 @@ rustflags = [
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)
[env]
DEFMT_LOG = "info" # default log level for defmt
DEFMT_LOG = "trace" # default log level for defmt
DEFMT_RTT_BUFFER_SIZE = "1024" # default RTT buffer size

View File

@ -1,38 +1,37 @@
[package]
authors = ["Igor Zhukov"]
edition = "2018"
edition = "2021"
readme = "README.md"
name = "MRTIC"
version = "0.1.0"
[dependencies]
# cortex-m crates
cortex-m-rt = "0.7.3"
panic-halt = "0.2.0"
cortex-m-semihosting = "0.3.3"
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
rtt-target = "0.5.0"
rtic = { version = "2.1.1", features = ["thumbv7-backend"] }
#stm32f746 HAL crate
stm32f7xx-hal = { version ="0.8.0", features = ["stm32f746", "rt"] }
# crates for advanced defmt logging
defmt = "0.3.8"
defmt-rtt = "0.4.1"
# panic handler to print backtrace via defmt
panic-probe = { version = "0.3.2", features = ["print-defmt"] }
# RTIC crates
rtic = { version = "2.1.1", features = ["thumbv7-backend"] }
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"
# Uncomment for the allocator example.
# alloc-cortex-m = "0.4.0"
# custom heap allocator for rust collections
embedded-alloc = "0.6.0"
# Uncomment for the device example.
# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`,
# and then use `cargo build --example device` to build it.
# [dependencies.stm32f3]
# features = ["stm32f303", "rt"]
# version = "0.7.1"
# data structures
ringbuffer = { version = "0.15.0", features = [] }
# this lets you use `cargo fix`!
[[bin]]
name = "MRTIC"
test = false

View File

@ -2,22 +2,45 @@
#![no_std]
use defmt_rtt as _;
use panic_halt as _;
use panic_probe as _;
extern crate alloc;
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
#[rtic::app(device = stm32f7xx_hal::pac, peripherals = true, dispatchers = [EXTI0])]
mod app {
//Declare Parameters
use core::fmt::Write;
systick_monotonic!(Mono, 1000);
use stm32f7xx_hal::{
gpio::{GpioExt, Output, PushPull, PI1}, pac::{Interrupt, USART1}, prelude::*, rcc::RccExt, serial::{Config, Rx, Serial, Tx}
gpio::{GpioExt, Output, PushPull, PI1},
pac::{Interrupt, USART1},
prelude::*,
rcc::RccExt,
serial::{self, Config, Event, Rx, Serial, Tx}
};
use rtic_monotonics::systick::prelude::*;
use alloc::string::String;
use embedded_alloc::LlffHeap as Heap;
use ringbuffer::{AllocRingBuffer, RingBuffer};
#[global_allocator]
static HEAP: Heap = Heap::empty();
#[shared]
struct Shared {}
struct Shared {
message_buffer: AllocRingBuffer<u8>
}
#[local]
struct Local {
@ -27,9 +50,19 @@ mod app {
rx_vcp: Rx<USART1>,
}
//Init Function
#[init]
fn init(ctx: init::Context) -> (Shared, Local) {
//Allocator
{
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 2048;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}
rtic::pend(Interrupt::UART4);
// Cortex-M peripherals
@ -55,13 +88,15 @@ mod app {
..Default::default()
};
let usart1 =
let mut usart1 =
Serial::new(
ctx.device.USART1,
(usart1_tx, usart1_rx),
&clocks,
usart1_config);
usart1.listen(Event::Rxne);
let (tx_uart1, rx_uart1) = usart1.split();
//LED
@ -69,15 +104,19 @@ mod app {
.pi1
.into_push_pull_output();
ld1.set_high();
let read_buf_serial: AllocRingBuffer<u8> = AllocRingBuffer::new(128);
//TASKS
blinker::spawn().ok();
serial_task::spawn().ok();
//serial_task::spawn().ok();
defmt::info!("INITED");
(
Shared {},
Shared {
message_buffer: read_buf_serial
},
Local {
led: ld1,
state : true,
@ -90,7 +129,6 @@ mod app {
#[idle]
fn idle(_ctx: idle::Context) -> ! {
loop {
defmt::info!("IDLE...");
for _ in 0..100_000 {
cortex_m::asm::nop();
}
@ -105,7 +143,6 @@ mod app {
#[task(local = [led, state], priority = 1)]
async fn blinker(ctx: blinker::Context){
loop{
defmt::info!("BLINKING...");
if *ctx.local.state{
ctx.local.led.set_low();
@ -121,7 +158,7 @@ mod app {
}
}
#[task(local = [tx_vcp, rx_vcp], priority = 1)]
#[task(local = [tx_vcp], priority = 1)]
async fn serial_task(ctx: serial_task::Context){
defmt::info!("Start sending data");
loop{
@ -130,8 +167,23 @@ mod app {
ctx.local.tx_vcp.write_str("Hello\n").unwrap();
Mono::delay(500.millis()).await;
Mono::delay(1000.millis()).await;
}
}
#[task(binds = USART1, local = [rx_vcp], shared = [message_buffer])]
fn usart1_handler(ctx: usart1_handler::Context){
let serial = ctx.local.rx_vcp;
defmt::trace!("RX not empty");
if let Ok(byte) = serial.read() {
defmt::debug!("RX Byte value: {=u8:x}", byte);
}
}
//APP ENDS
}