Skip to content

Commit

Permalink
Merge pull request #15 from ZackPierce/more_type_staging_and_encapsul…
Browse files Browse the repository at this point in the history
…ation

More type staging and encapsulation
  • Loading branch information
ZackPierce authored Aug 21, 2018
2 parents 50c41b8 + bf3a453 commit 2d75505
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 114 deletions.
56 changes: 8 additions & 48 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use cortex_m;
use dac_mcp4922::Mcp4922;
use dac_mcp4922::MODE as DAC_MODE;
use dual_signal::HighLowReader;
use fault_can_protocol::*;
use ms_timer::MsTimer;
use nucleo_f767zi::debug_console::DebugConsole;
use nucleo_f767zi::hal::adc::{Adc, AdcChannel, AdcPrescaler, AdcSampleTime};
use nucleo_f767zi::hal::can::{Can, CanError, DataFrame};
use nucleo_f767zi::hal::delay::Delay;
use nucleo_f767zi::hal::can::Can;
use nucleo_f767zi::hal::iwdg::{Iwdg, IwdgConfig, WatchdogTimeout};
use nucleo_f767zi::hal::prelude::*;
use nucleo_f767zi::hal::rcc::ResetConditions;
Expand All @@ -18,7 +16,6 @@ use nucleo_f767zi::hal::stm32f7x7;
use nucleo_f767zi::hal::stm32f7x7::{ADC1, ADC2, ADC3, IWDG};
use nucleo_f767zi::led::{Color, Leds};
use nucleo_f767zi::UserButtonPin;
use oscc_magic_byte::*;

pub use types::*;

Expand Down Expand Up @@ -53,7 +50,6 @@ pub struct FullBoard {
pub debug_console: DebugConsole,
pub leds: Leds,
pub user_button: UserButtonPin,
pub delay: Delay,
pub timer_ms: MsTimer,
pub can_publish_timer: CanPublishTimer,
pub wdg: Iwdg<IWDG>,
Expand All @@ -74,13 +70,8 @@ pub struct FullBoard {
pub struct Board {
pub leds: Leds,
pub user_button: UserButtonPin,
pub delay: Delay,
pub can_publish_timer: CanPublishTimer,
pub wdg: Iwdg<IWDG>,
pub reset_conditions: ResetConditions,
control_can: ControlCan,
obd_can: ObdCan,
fault_report_can_frame: DataFrame,
}

impl FullBoard {
Expand Down Expand Up @@ -233,7 +224,7 @@ impl FullBoard {
(can1_tx, can1_rx),
&mut rcc.apb1,
&config::CONTROL_CAN_CONFIG,
).expect("Failed to configure ontrol CAN (CAN1)");
).expect("Failed to configure control CAN (CAN1)");

let obd_can = Can::can2(
peripherals.CAN2,
Expand Down Expand Up @@ -289,7 +280,6 @@ impl FullBoard {
user_button: gpioc
.pc13
.into_pull_down_input(&mut gpioc.moder, &mut gpioc.pupdr),
delay: Delay::new(core_peripherals.SYST, clocks),
timer_ms: MsTimer::new(core_peripherals.DWT, clocks),
can_publish_timer: CanPublishTimer::tim2(
peripherals.TIM2,
Expand Down Expand Up @@ -337,12 +327,14 @@ impl FullBoard {
SteeringPins,
MsTimer,
DebugConsole,
CanPublishTimer,
ControlCan,
ObdCan,
) {
let FullBoard {
debug_console,
leds,
user_button,
delay,
timer_ms,
can_publish_timer,
wdg,
Expand All @@ -363,13 +355,8 @@ impl FullBoard {
Board {
leds,
user_button,
delay,
can_publish_timer,
wdg,
reset_conditions,
control_can,
obd_can,
fault_report_can_frame: default_fault_report_data_frame(),
},
brake_dac,
brake_pins,
Expand All @@ -382,6 +369,9 @@ impl FullBoard {
steering_pins,
timer_ms,
debug_console,
can_publish_timer,
control_can,
obd_can,
)
}
}
Expand All @@ -390,14 +380,6 @@ impl Board {
pub fn user_button(&mut self) -> bool {
self.user_button.is_high()
}

pub fn control_can(&mut self) -> &mut ControlCan {
&mut self.control_can
}

pub fn obd_can(&mut self) -> &mut ObdCan {
&mut self.obd_can
}
}

// brake module owns ADC1
Expand Down Expand Up @@ -442,28 +424,6 @@ impl HighLowReader for TorqueSensor {
}
}

impl FaultReportPublisher for Board {
fn publish_fault_report(&mut self, fault_report: &OsccFaultReport) -> Result<(), CanError> {
{
self.fault_report_can_frame
.set_data_length(OSCC_FAULT_REPORT_CAN_DLC as _);

let data = self.fault_report_can_frame.data_as_mut();

data[0] = OSCC_MAGIC_BYTE_0;
data[1] = OSCC_MAGIC_BYTE_1;
data[2] = (fault_report.fault_origin_id & 0xFF) as _;
data[3] = ((fault_report.fault_origin_id >> 8) & 0xFF) as _;
data[4] = ((fault_report.fault_origin_id >> 16) & 0xFF) as _;
data[5] = ((fault_report.fault_origin_id >> 24) & 0xFF) as _;
data[6] = fault_report.dtcs;
}

self.control_can
.transmit(&self.fault_report_can_frame.into())
}
}

pub fn hard_fault_indicator() {
cortex_m::interrupt::free(|_cs| unsafe {
let peripherals = stm32f7x7::Peripherals::steal();
Expand Down
26 changes: 14 additions & 12 deletions src/brake/kia_soul_ev_niro/brake_module.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// https://github.com/jonlamb-gh/oscc/tree/devel/firmware/brake/kia_soul_ev_niro

use super::types::*;
use board::{Board, BrakePedalPositionSensor};
use board::BrakePedalPositionSensor;
use brake_can_protocol::*;
use can_gateway_module::CanGatewayModule;
use core::fmt::Write;
use dtc::DtcBitfield;
use dual_signal::DualSignal;
Expand Down Expand Up @@ -76,11 +77,11 @@ impl BrakeModule {
&mut self.brake_pins.brake_light_enable
}

pub fn brake_dac(&mut self) -> &mut BrakeDac {
fn brake_dac(&mut self) -> &mut BrakeDac {
&mut self.brake_dac
}

pub fn disable_control(&mut self, debug_console: &mut DebugConsole) {
fn disable_control(&mut self, debug_console: &mut DebugConsole) {
if self.control_state.enabled {
self.brake_pedal_position.prevent_signal_discontinuity();

Expand All @@ -95,7 +96,7 @@ impl BrakeModule {
}
}

pub fn enable_control(&mut self, debug_console: &mut DebugConsole) {
fn enable_control(&mut self, debug_console: &mut DebugConsole) {
if !self.control_state.enabled && !self.control_state.operator_override {
self.brake_pedal_position.prevent_signal_discontinuity();

Expand All @@ -109,7 +110,7 @@ impl BrakeModule {
}
}

pub fn update_brake(&mut self, spoof_command_high: u16, spoof_command_low: u16) {
fn update_brake(&mut self, spoof_command_high: u16, spoof_command_low: u16) {
if self.control_state.enabled {
let spoof_high = num::clamp(
spoof_command_high,
Expand Down Expand Up @@ -140,7 +141,7 @@ impl BrakeModule {
&mut self,
timer_ms: &MsTimer,
debug_console: &mut DebugConsole,
board: &mut Board,
can_gateway: &mut CanGatewayModule,
) {
if self.control_state.enabled || self.control_state.dtcs > 0 {
self.brake_pedal_position.update();
Expand Down Expand Up @@ -168,7 +169,7 @@ impl BrakeModule {
.dtcs
.set(OSCC_BRAKE_DTC_INVALID_SENSOR_VAL);

self.publish_fault_report(board);
self.publish_fault_report(can_gateway);

writeln!(
debug_console,
Expand All @@ -187,7 +188,7 @@ impl BrakeModule {
.dtcs
.set(OSCC_BRAKE_DTC_OPERATOR_OVERRIDE);

self.publish_fault_report(board);
self.publish_fault_report(can_gateway);

self.control_state.operator_override = true;

Expand All @@ -199,19 +200,20 @@ impl BrakeModule {
}
}

pub fn publish_brake_report(&mut self, board: &mut Board) {
pub fn publish_brake_report(&mut self, can_gateway: &mut CanGatewayModule) {
self.brake_report.enabled = self.control_state.enabled;
self.brake_report.operator_override = self.control_state.operator_override;
self.brake_report.dtcs = self.control_state.dtcs;

self.brake_report.transmit(&mut board.control_can());
self.brake_report.transmit(&mut can_gateway.control_can());
}

pub fn publish_fault_report(&mut self, board: &mut Board) {
pub fn publish_fault_report(&mut self, can_gateway: &mut CanGatewayModule) {
self.fault_report_frame.fault_report.fault_origin_id = FAULT_ORIGIN_BRAKE;
self.fault_report_frame.fault_report.dtcs = self.control_state.dtcs;

self.fault_report_frame.transmit(&mut board.control_can());
self.fault_report_frame
.transmit(&mut can_gateway.control_can());
}

// TODO - error handling
Expand Down
76 changes: 67 additions & 9 deletions src/can_gateway_module.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
// https://github.com/jonlamb-gh/oscc/tree/devel/firmware/can_gateway

use board::Board;
use nucleo_f767zi::hal::can::CanFrame;
use fault_can_protocol::*;
use nucleo_f767zi::hal::can::{CanError, CanFrame, DataFrame, RxFifo};
use nucleo_f767zi::hal::prelude::*;
use oscc_magic_byte::*;
use types::*;
use vehicle::*;

// TODO - use some form of println! logging that prefixes with a module name?

pub struct CanGatewayModule {}
pub struct CanGatewayModule {
can_publish_timer: CanPublishTimer,
control_can: ControlCan,
obd_can: ObdCan,
fault_report_can_frame: DataFrame,
}

impl CanGatewayModule {
pub fn new() -> Self {
CanGatewayModule {}
pub fn new(
can_publish_timer: CanPublishTimer,
control_can: ControlCan,
obd_can: ObdCan,
) -> Self {
CanGatewayModule {
can_publish_timer,
control_can,
obd_can,
fault_report_can_frame: default_fault_report_data_frame(),
}
}
pub fn republish_obd_frames_to_control_can_bus(&mut self) {
// poll both OBD CAN FIFOs
for fifo in &[RxFifo::Fifo0, RxFifo::Fifo1] {
if let Ok(rx_frame) = self.obd_can().receive(fifo) {
self.republish_obd_frame_to_control_can_bus(&rx_frame);
}
}
}

pub fn init_devices(&self, _board: &mut Board) {}

// TODO - error handling
pub fn republish_obd_frame_to_control_can_bus(&mut self, frame: &CanFrame, board: &mut Board) {
fn republish_obd_frame_to_control_can_bus(&mut self, frame: &CanFrame) {
let id: u32 = frame.id().into();
let mut is_a_match = false;

Expand All @@ -34,8 +57,43 @@ impl CanGatewayModule {
}
}

if is_a_match && board.control_can().transmit(&frame).is_err() {
if is_a_match && self.control_can().transmit(&frame).is_err() {
// TODO - error handling
}
}

// TODO - hide these details, switch to a publisher approach
pub fn control_can(&mut self) -> &mut ControlCan {
&mut self.control_can
}

pub fn obd_can(&mut self) -> &mut ObdCan {
&mut self.obd_can
}

pub fn wait_for_publish(&mut self) -> bool {
self.can_publish_timer.wait().is_ok()
}
}

impl FaultReportPublisher for CanGatewayModule {
fn publish_fault_report(&mut self, fault_report: &OsccFaultReport) -> Result<(), CanError> {
{
self.fault_report_can_frame
.set_data_length(OSCC_FAULT_REPORT_CAN_DLC as _);

let data = self.fault_report_can_frame.data_as_mut();

data[0] = OSCC_MAGIC_BYTE_0;
data[1] = OSCC_MAGIC_BYTE_1;
data[2] = (fault_report.fault_origin_id & 0xFF) as _;
data[3] = ((fault_report.fault_origin_id >> 8) & 0xFF) as _;
data[4] = ((fault_report.fault_origin_id >> 16) & 0xFF) as _;
data[5] = ((fault_report.fault_origin_id >> 24) & 0xFF) as _;
data[6] = fault_report.dtcs;
}

self.control_can
.transmit(&self.fault_report_can_frame.into())
}
}
Loading

0 comments on commit 2d75505

Please sign in to comment.