From 27001441b21af8b00cfa5a9dce50d35d63293401 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Fri, 2 Jun 2023 17:39:46 -0300 Subject: [PATCH] temp --- src/main.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 74f9a67..e6a2cc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,14 +11,13 @@ mod packet; #[rtic::app(device = stm32f4xx_hal::pac, peripherals = true)] mod app { - use cortex_m::peripheral::NVIC; use hal::block; use hal::gpio; use hal::pac::{SPI1, TIM5, USART1}; + use hal::prelude::*; use hal::serial::{Config, Event, Serial}; use hal::spi::{Mode, Phase, Polarity, Spi}; use hal::timer::Delay; - use hal::{interrupt, prelude::*}; use rf4463::config::RADIO_CONFIG_500_2; use rf4463::Rf4463; use ringbuffer::{ConstGenericRingBuffer, RingBuffer, RingBufferRead, RingBufferWrite}; @@ -44,13 +43,15 @@ mod app { enum SlaveCmd { BufferStatus, SendPacket, + GetTemp, } impl SlaveCmd { pub fn from_u8(i: u8) -> Option<Self> { match i { - 0 => Some(Self::BufferStatus), - 1 => Some(Self::SendPacket), + 0x00 => Some(Self::BufferStatus), + 0x01 => Some(Self::SendPacket), + 0x02 => Some(Self::GetTemp), _ => None, } } @@ -64,6 +65,7 @@ mod app { #[shared] struct Shared { + radio_temp: f32, tx_buf: ConstGenericRingBuffer<Packet, BUFFER_LEN>, } @@ -85,7 +87,7 @@ mod app { let clocks = rcc .cfgr .use_hse(25.MHz()) - .sysclk(84.MHz()) + .sysclk(100.MHz()) .pclk1(48.MHz()) .pclk2(48.MHz()) .freeze(); @@ -118,13 +120,9 @@ mod app { usart.listen(Event::Rxne); - // enable usart interrupt - unsafe { - NVIC::unmask(interrupt::USART1); - } - ( Shared { + radio_temp: 0.0, tx_buf: ConstGenericRingBuffer::new(), }, Local { @@ -136,17 +134,29 @@ mod app { ) } - #[idle(local = [radio], shared=[tx_buf])] + #[idle(local=[radio], shared=[tx_buf, radio_temp])] fn idle(mut ctx: idle::Context) -> ! { + let mut i = 0; loop { if let Some(mut pkt) = ctx.shared.tx_buf.lock(|buf| buf.dequeue()) { ctx.local.radio.tx(&mut pkt.data).unwrap(); } + + i += 1; + + // get temp every 500 packets, or whenever when idle TODO + if i >= 500 { + i = 0; + + if let Ok(new_temp) = ctx.local.radio.get_temp() { + ctx.shared.radio_temp.lock(|cur_temp| *cur_temp = new_temp); + } + } } } - #[task(binds = USART1, local = [state, usart], shared = [tx_buf])] - fn usart1(ctx: usart1::Context) { + #[task(binds = USART1, priority=1, local = [state, usart], shared = [tx_buf, radio_temp])] + fn usart1(mut ctx: usart1::Context) { let state = ctx.local.state; let usart = ctx.local.usart; let mut buf = ctx.shared.tx_buf; @@ -174,6 +184,11 @@ mod app { *state = SlaveState::RecvPacket(Packet::default()); } + + SlaveCmd::GetTemp => { + let temp = ctx.shared.radio_temp.lock(|x| *x); + usart.bwrite_all(&temp.to_le_bytes()).unwrap(); + } } } } -- GitLab