Skip to content

Commit

Permalink
start doing more can abstraction haha
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeechey committed Mar 3, 2025
1 parent c3431d3 commit 85cffe5
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 77 deletions.
8 changes: 1 addition & 7 deletions boards/stm32f767zi/src/bin/state_machine_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use embassy_stm32::{
filter::Mask32, Can, Fifo, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler,
TxInterruptHandler,
},
gpio::Pin,
peripherals::CAN1,
};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Watch};
Expand All @@ -20,13 +21,6 @@ use hyped_core::{comms::boards::Board, states::State};
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
CAN1_RX0 => Rx0InterruptHandler<CAN1>;
CAN1_RX1 => Rx1InterruptHandler<CAN1>;
CAN1_SCE => SceInterruptHandler<CAN1>;
CAN1_TX => TxInterruptHandler<CAN1>;
});

/// The current state of the state machine.
pub static CURRENT_STATE: Watch<CriticalSectionRawMutex, State, 1> = Watch::new();
static _BOARD: Board = Board::StateMachineTester;
Expand Down
32 changes: 2 additions & 30 deletions boards/stm32f767zi/src/bin/state_transition_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,21 @@
#![no_main]

use embassy_executor::Spawner;
use embassy_stm32::{
bind_interrupts,
can::{
filter::Mask32, Can, Fifo, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler,
TxInterruptHandler,
},
peripherals::CAN1,
};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Watch};
use embassy_time::{Duration, Timer};
use hyped_boards_stm32f767zi::{
emergency, request_transition,
tasks::{
can_receiver::can_receiver,
can_sender::{can_sender, CAN_SEND},
can::{can, CAN_SEND},
state_updater::state_updater,
},
};
use hyped_core::{
comms::{boards::Board, messages::CanMessage, state_transition::StateTransition},
states::State,
};
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
CAN1_RX0 => Rx0InterruptHandler<CAN1>;
CAN1_RX1 => Rx1InterruptHandler<CAN1>;
CAN1_SCE => SceInterruptHandler<CAN1>;
CAN1_TX => TxInterruptHandler<CAN1>;
});

/// The current state of the state machine.
pub static CURRENT_STATE: Watch<CriticalSectionRawMutex, State, 1> = Watch::new();
static BOARD: Board = Board::StateMachineTester;
Expand All @@ -42,19 +25,8 @@ static BOARD: Board = Board::StateMachineTester;
async fn main(spawner: Spawner) -> ! {
let p = embassy_stm32::init(Default::default());

// Initialise CAN
static CAN: StaticCell<Can<'static>> = StaticCell::new();
let can = CAN.init(Can::new(p.CAN1, p.PD0, p.PD1, Irqs));
can.modify_filters()
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
can.modify_config().set_bitrate(500_000);
can.enable().await;
defmt::info!("CAN enabled");

let (tx, rx) = can.split();
spawner.must_spawn(can(p.CAN1, p.PD0, p.PD1));

spawner.must_spawn(can_receiver(rx));
spawner.must_spawn(can_sender(tx));
spawner.must_spawn(state_updater(CURRENT_STATE.sender()));

let can_sender = CAN_SEND.sender();
Expand Down
3 changes: 1 addition & 2 deletions boards/stm32f767zi/src/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// CAN
pub mod can_receiver;
pub mod can_sender;
pub mod can;
pub mod heartbeat_coordinator;
pub mod heartbeats_responder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
use super::heartbeats_responder::INCOMING_HEARTBEATS;
use embassy_stm32::can::{CanRx, Id};
use embassy_futures::join::join;
use embassy_stm32::{
bind_interrupts,
can::{
filter::Mask32, Can, CanRx, CanTx, ExtendedId, Fifo, Frame, Id, Rx0InterruptHandler,
Rx1InterruptHandler, RxPin, SceInterruptHandler, TxInterruptHandler, TxPin,
},
peripherals::CAN1,
};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{Duration, Timer};
use hyped_can::HypedCanFrame;
use hyped_core::comms::{messages::CanMessage, state_transition::StateTransition};
use static_cell::StaticCell;

use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
CAN1_RX0 => Rx0InterruptHandler<CAN1>;
CAN1_RX1 => Rx1InterruptHandler<CAN1>;
CAN1_SCE => SceInterruptHandler<CAN1>;
CAN1_TX => TxInterruptHandler<CAN1>;
});

#[embassy_executor::task]
pub async fn can(can_1: CAN1, rx_pin: impl RxPin<CAN1>, tx_pin: impl TxPin<CAN1>) {
// Initialise CAN
static CAN: StaticCell<Can<'static>> = StaticCell::new();
let can = CAN.init(Can::new(can_1, rx_pin, tx_pin, Irqs));
can.modify_filters()
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
can.modify_config().set_bitrate(500_000);
can.enable().await;
defmt::info!("CAN enabled");

join(can_receiver(can.split().1), can_sender(can.split().0)).await;
}

/// Stores incoming state transitions received from CAN.
/// All boards should listen to this channel and update their states accordingly.
pub static INCOMING_STATE_TRANSITIONS: Channel<CriticalSectionRawMutex, StateTransition, 10> =
Expand All @@ -22,8 +52,7 @@ pub static INCOMING_STATE_TRANSITION_REQUESTS: Channel<

/// Task that receives CAN messages and puts them into a channel.
/// Currently only supports StateTransition and StateTransitionRequest messages.
#[embassy_executor::task]
pub async fn can_receiver(mut rx: CanRx<'static>) {
async fn can_receiver(mut rx: CanRx<'static>) {
let state_transition_sender = INCOMING_STATE_TRANSITIONS.sender();
let state_transition_request_sender = INCOMING_STATE_TRANSITION_REQUESTS.sender();

Expand Down Expand Up @@ -66,3 +95,25 @@ pub async fn can_receiver(mut rx: CanRx<'static>) {
Timer::after(Duration::from_millis(10)).await;
}
}

/// Channel for sending CAN messages.
pub static CAN_SEND: Channel<CriticalSectionRawMutex, CanMessage, 10> = Channel::new();

/// Task that sends CAN messages from a channel.
async fn can_sender(mut tx: CanTx<'static>) {
let can_sender = CAN_SEND.receiver();

loop {
let message = can_sender.receive().await;
let can_frame: HypedCanFrame = message.into();

let id = Id::Extended(ExtendedId::new(can_frame.can_id).unwrap());
let data = can_frame.data;

let frame = Frame::new_data(id, &data).unwrap();

tx.write(&frame).await;

Timer::after(Duration::from_millis(10)).await;
}
}
29 changes: 0 additions & 29 deletions boards/stm32f767zi/src/tasks/can_sender.rs

This file was deleted.

2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/heartbeat_coordinator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::emergency;

use super::can_sender::CAN_SEND;
use super::can::CAN_SEND;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{Duration, Instant, Timer};
use hyped_core::{
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/heartbeats_responder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::can_sender::CAN_SEND;
use super::can::CAN_SEND;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{Duration, Timer};
use hyped_core::comms::{boards::Board, heartbeat::Heartbeat, messages::CanMessage};
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/read_keyence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hyped_core::comms::{
};
use hyped_sensors::keyence::Keyence;

use super::can_sender::CAN_SEND;
use super::can::CAN_SEND;

/// Used to keep the latest temperature sensor value.
pub static CURRENT_KEYENCE_STRIPE_COUNT: Watch<CriticalSectionRawMutex, u32, 1> = Watch::new();
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/read_temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hyped_core::{
use hyped_sensors::temperature::{Status, Temperature, TemperatureAddresses};
use hyped_sensors::SensorValueRange;

use super::can_sender::CAN_SEND;
use super::can::CAN_SEND;

type I2c1Bus = Mutex<NoopRawMutex, RefCell<I2c<'static, Blocking>>>;

Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/state_machine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{can_receiver::INCOMING_STATE_TRANSITION_REQUESTS, can_sender::CAN_SEND};
use super::{can::CAN_SEND, can::INCOMING_STATE_TRANSITION_REQUESTS};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Sender};
use embassy_time::{Duration, Timer};
use hyped_core::{
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/tasks/state_updater.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::can_receiver::INCOMING_STATE_TRANSITIONS;
use super::can::INCOMING_STATE_TRANSITIONS;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Sender};
use hyped_core::states::State;

Expand Down

0 comments on commit 85cffe5

Please sign in to comment.