Skip to content

Commit

Permalink
Merge pull request #82 from Churrosoft/feat/rticV2_migration
Browse files Browse the repository at this point in the history
Update GPIO for pcb v4.x
  • Loading branch information
FDSoftware authored Nov 24, 2023
2 parents b8aae72 + 6e2aae6 commit 56151b8
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 111 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ version = "2.0.1"
features = ["thumbv7-backend"]

[dependencies.rtic-monotonics]
version = "1.0.0"
features = ["cortex-m-systick"]
version = "1.1.0"
features = ["cortex-m-systick", "systick-10khz"]
3 changes: 2 additions & 1 deletion src/app/engine/cpwm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::app::engine::efi_cfg::VRSensor;

#[derive(Copy, Clone)]
pub struct VRStatus {
pub current_time: u32,
pub current_gap: u32,
Expand Down Expand Up @@ -96,7 +97,7 @@ pub fn time_to_angle(trigger: &VRStatus, time: &u32) -> i32 {
return (*time as f32 * trigger.degreesPeruSx32768) as i32 / 32768;
}

pub fn get_crank_angle(trigger: &mut VRStatus, trigger_config: &VRSensor, cpu_tick: u32) -> i32 {
pub fn get_crank_angle(trigger: &VRStatus, trigger_config: &VRSensor, cpu_tick: u32) -> i32 {
const CRANK_ANGLE_MAX: i32 = 360;

//Number of teeth that have passed since tooth 1, multiplied by the angle each tooth represents, plus the angle that tooth 1 is ATDC. This gives accuracy only to the nearest tooth.
Expand Down
2 changes: 1 addition & 1 deletion src/app/engine/pmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use stm32f4xx_hal::gpio::{Output, PushPull};
use crate::app::engine::error::Error;
use crate::app::gpio::ISPI;
use serde::{ Serialize};
pub type PmicT = PMIC<SharedBus<ISPI>, gpio::PA15<Output<PushPull>>>;
pub type PmicT = PMIC<SharedBus<ISPI>, gpio::PB11<Output<PushPull>>>;

#[derive(Serialize)]
#[repr(u8)]
Expand Down
13 changes: 11 additions & 2 deletions src/app/engine/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use micromath::F32Ext;

use stm32f4xx_hal::{
adc::{config::SampleTime, Adc},
pac::ADC1,
pac::ADC2,
};

const EMA_LP_ALPHA: f32 = 0.45f32;
Expand All @@ -14,6 +14,7 @@ pub enum SensorTypes {
CooltanTemp,
AirTemp,
BatteryVoltage,
ExternalLambda,
}

#[derive(Debug,Clone,Copy)]
Expand All @@ -23,13 +24,15 @@ pub struct SensorValues {
pub cooltan_temp: f32,
pub air_temp: f32,
pub batt: f32,
pub ext_o2: f32,

// private:
raw_map: f32,
raw_tps: f32,
raw_temp: f32,
raw_air_temp: f32,
raw_batt: f32,
raw_ext_o2: f32,
}

impl SensorValues {
Expand All @@ -40,12 +43,14 @@ impl SensorValues {
cooltan_temp: 45.69f32,
air_temp: 0.0f32,
batt: 13.42f32,
ext_o2: 0.0f32,
// valores en raw son en bits del ADC; luego se pasan a mV
raw_map: 0.0f32,
raw_tps: 0.0f32,
raw_temp: 0.0f32,
raw_air_temp: 0.0f32,
raw_batt: 0.0f32,
raw_ext_o2: 0.0f32,
}
}

Expand Down Expand Up @@ -86,14 +91,18 @@ impl SensorValues {

self.batt = 14.2;
}

SensorTypes::ExternalLambda => {
self.ext_o2 = raw_value as f32;
}
}
}
}

pub fn get_sensor_raw(
sensor_type: SensorTypes,
adc_pins: &mut ADCMapping,
adc: &mut Adc<ADC1>,
adc: &mut Adc<ADC2>,
) -> u16 {
let a = sensor_type as u8;
let b = a;
Expand Down
165 changes: 111 additions & 54 deletions src/app/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,50 @@ use stm32f4xx_hal::{
pub struct InjectionGpioMapping {
pub iny_1: gpio::PD8<Output<PushPull>>,
pub iny_2: gpio::PD9<Output<PushPull>>,
pub iny_3: gpio::PD10<Output<PushPull>>,
pub iny_4: gpio::PD11<Output<PushPull>>,
}

pub struct IgnitionGpioMapping {
pub ecn_1: gpio::PD12<Output<PushPull>>,
pub ecn_2: gpio::PD13<Output<PushPull>>,
pub ecn_3: gpio::PD14<Output<PushPull>>,
pub ecn_4: gpio::PD15<Output<PushPull>>,
pub ecn_1: gpio::PD10<Output<PushPull>>,
pub ecn_2: gpio::PD11<Output<PushPull>>,
}

pub struct PMICGpioMapping {
pub pmic_enable: gpio::PB12<Output<PushPull>>,
pub pmic_spark: gpio::PB13<Input>,
pub pmic_cs: gpio::PA15<Output<PushPull>>,
pub pmic_nomi: gpio::PC10<Input>,
pub pmic_maxi: gpio::PC11<Input>,
pub pmic1_enable: gpio::PB12<Output<PushPull>>,
pub pmic1_cs: gpio::PB11<Output<PushPull>>,
pub pmic2_enable: gpio::PB13<Output<PushPull>>,
pub pmic2_cs: gpio::PB9<Output<PushPull>>,
}

pub struct LedGpioMapping {
pub led_0: gpio::PC13<Output<PushPull>>,
pub led_1: gpio::PC14<Output<PushPull>>,
pub led_2: gpio::PC15<Output<PushPull>>,
pub led_0: gpio::PC1<Output<PushPull>>,
pub led_1: gpio::PC2<Output<PushPull>>,
pub led_2: gpio::PC3<Output<PushPull>>,
pub led_can_tx: gpio::PD6<Output<PushPull>>,
pub led_can_rx: gpio::PD7<Output<PushPull>>,
pub led_check: gpio::PC9<Output<PushPull>>,
pub led_mil: gpio::PC8<Output<PushPull>>,
pub led_check: gpio::PB1<Output<PushPull>>,
pub led_mil: gpio::PA8<Output<PushPull>>,
}

pub struct AuxIoMapping {
pub in_1: gpio::PC4<Input>,
pub in_2: gpio::PC5<Input>,

pub out_1: gpio::PB5<Output<PushPull>>,
pub out_2: gpio::PB6<Output<PushPull>>,
pub out_3: gpio::PB7<Output<PushPull>>,
pub out_4: gpio::PB8<Output<PushPull>>,
// on external connector
pub in_1: gpio::PD12<Input>,
pub in_2: gpio::PD13<Input>,
pub in_3: gpio::PD14<Input>,
pub in_4: gpio::PD15<Input>,

// on expansion header

pub in_5: gpio::PB5<Input>,
pub in_6: gpio::PB7<Input>,
pub in_7: gpio::PB8<Input>,
pub in_8: gpio::PE1<Input>,


pub out_1: gpio::PC4<Output<PushPull>>,
pub out_2: gpio::PC5<Output<PushPull>>,
pub out_3: gpio::PB0<Output<PushPull>>,
pub out_4: gpio::PA10<Output<PushPull>>,

pub cs_1: gpio::PE14<Output<PushPull>>,
pub cs_2: gpio::PE15<Output<PushPull>>,
Expand All @@ -61,19 +68,42 @@ pub struct RelayMapping {
}

pub struct ADCMapping {
pub mux_a: gpio::PD2<Output<PushPull>>,
pub mux_b: gpio::PD3<Output<PushPull>>,
pub mux_c: gpio::PD4<Output<PushPull>>,
pub analog_in: gpio::PA0<Analog>,
pub baro_cs: gpio::PA6<Output<PushPull>>,

pub mux_a: gpio::PD3<Output<PushPull>>,
pub mux_b: gpio::PD4<Output<PushPull>>,
pub mux_c: gpio::PD5<Output<PushPull>>,
pub analog_in: gpio::PA7<Analog>,
}

pub struct ADC_DMA_Mapping {
pub tps: gpio::PA0<Analog>,
pub iat: gpio::PA1<Analog>,
pub map: gpio::PA2<Analog>,
pub clt: gpio::PA3<Analog>,
pub o2: gpio::PA4<Analog>,
pub vbatt: gpio::PA5<Analog>,
}


pub struct StepperMapping{
pub step: gpio::PE7<Output<PushPull>>,
pub dir: gpio::PE8<Output<PushPull>>,
pub fault: gpio::PE9<Output<PushPull>>,
pub enable: gpio::PE10<Output<PushPull>>,
}

pub struct SDCard {
pub dat_0: Pin<'C', 8>,
pub dat_1: Pin<'C', 9>,
pub dat_2: Pin<'C', 10>,
pub dat_3: Pin<'C',11>,
pub dat_clk: gpio::PC12,
pub cmd: Pin<'D', 2>,

pub cd: gpio::PB6<Input>,
}

pub struct GpioMapping {
// LED's / User feedback
pub led: LedGpioMapping,
Expand Down Expand Up @@ -110,7 +140,10 @@ pub struct GpioMapping {
pub spi_miso: gpio::PB14<Alternate<5>>,
pub spi_mosi: gpio::PB15<Alternate<5, PushPull>>,

pub sd_card: SDCard,

pub adc: ADCMapping,
pub adc_dma: ADC_DMA_Mapping,
}

pub fn init_gpio(
Expand All @@ -123,38 +156,33 @@ pub fn init_gpio(
let mut gpio = GpioMapping {
// LED's / User feedback
led: LedGpioMapping {
led_0: gpio_c.pc13.into_push_pull_output(),
led_1: gpio_c.pc14.into_push_pull_output(),
led_2: gpio_c.pc15.into_push_pull_output(),
led_0: gpio_c.pc1.into_push_pull_output(),
led_1: gpio_c.pc2.into_push_pull_output(),
led_2: gpio_c.pc3.into_push_pull_output(),
led_can_tx: gpio_d.pd6.into_push_pull_output(),
led_can_rx: gpio_d.pd7.into_push_pull_output(),
led_check: gpio_c.pc9.into_push_pull_output(),
led_mil: gpio_c.pc8.into_push_pull_output(),
led_check: gpio_b.pb1.into_push_pull_output(),
led_mil: gpio_a.pa8.into_push_pull_output(),
},

// Injection
injection: InjectionGpioMapping {
iny_1: gpio_d.pd8.into_push_pull_output(),
iny_2: gpio_d.pd9.into_push_pull_output(),
iny_3: gpio_d.pd10.into_push_pull_output(),
iny_4: gpio_d.pd11.into_push_pull_output(),
},

// Ignition
ignition: IgnitionGpioMapping {
ecn_1: gpio_d.pd12.into_push_pull_output(),
ecn_2: gpio_d.pd13.into_push_pull_output(),
ecn_3: gpio_d.pd14.into_push_pull_output(),
ecn_4: gpio_d.pd15.into_push_pull_output(),
ecn_1: gpio_d.pd10.into_push_pull_output(),
ecn_2: gpio_d.pd11.into_push_pull_output(),
},

// PMIC
pmic: PMICGpioMapping {
pmic_enable: gpio_b.pb12.into_push_pull_output(),
pmic_spark: gpio_b.pb13.into_input(),
pmic_cs: gpio_a.pa15.into_push_pull_output(),
pmic_nomi: gpio_c.pc10.into_input(),
pmic_maxi: gpio_c.pc11.into_input(),
pmic1_enable: gpio_b.pb12.into_push_pull_output(),
pmic1_cs: gpio_b.pb11.into_push_pull_output(),
pmic2_enable: gpio_b.pb13.into_push_pull_output(),
pmic2_cs: gpio_b.pb9.into_push_pull_output(),
},

// CKP/CMP
Expand Down Expand Up @@ -182,13 +210,19 @@ pub fn init_gpio(

// AUX I/O
aux: AuxIoMapping {
in_1: gpio_c.pc4.into_input(),
in_2: gpio_c.pc5.into_input(),

out_1: gpio_b.pb5.into_push_pull_output(),
out_2: gpio_b.pb6.into_push_pull_output(),
out_3: gpio_b.pb7.into_push_pull_output(),
out_4: gpio_b.pb8.into_push_pull_output(),
in_1: gpio_d.pd12.into_input(),
in_2: gpio_d.pd13.into_input(),
in_3: gpio_d.pd14.into_input(),
in_4: gpio_d.pd15.into_input(),
in_5: gpio_b.pb5.into_input(),
in_6: gpio_b.pb7.into_input(),
in_7: gpio_b.pb8.into_input(),
in_8: gpio_e.pe1.into_input(),

out_1: gpio_c.pc4.into_push_pull_output(),
out_2: gpio_c.pc5.into_push_pull_output(),
out_3: gpio_b.pb0.into_push_pull_output(),
out_4: gpio_a.pa10.into_push_pull_output(),

cs_1: gpio_e.pe14.into_push_pull_output(),
cs_2: gpio_e.pe15.into_push_pull_output(),
Expand All @@ -203,11 +237,33 @@ pub fn init_gpio(
spi_miso: gpio_b.pb14.into_alternate(),
spi_mosi: gpio_b.pb15.into_alternate().internal_pull_up(true),

adc_dma: ADC_DMA_Mapping {
tps: gpio_a.pa0.into_analog(),
iat: gpio_a.pa1.into_analog(),
map:gpio_a.pa2.into_analog(),
clt: gpio_a.pa3.into_analog(),
o2: gpio_a.pa4.into_analog(),
vbatt: gpio_a.pa5.into_analog(),
},

adc: ADCMapping {
mux_a: gpio_d.pd2.into_push_pull_output(),
mux_b: gpio_d.pd3.into_push_pull_output(),
mux_c: gpio_d.pd4.into_push_pull_output(),
analog_in: gpio_a.pa0.into_analog(),

baro_cs: gpio_a.pa6.into_push_pull_output(),

mux_a: gpio_d.pd3.into_push_pull_output(),
mux_b: gpio_d.pd4.into_push_pull_output(),
mux_c: gpio_d.pd5.into_push_pull_output(),

analog_in: gpio_a.pa7.into_analog(),
},

sd_card: SDCard {dat_0 : gpio_c.pc8.internal_pull_up(true),
dat_1 : gpio_c.pc9.internal_pull_up(true),
dat_2 : gpio_c.pc10.internal_pull_up(true),
dat_3 : gpio_c.pc11.internal_pull_up(true),
dat_clk : gpio_c.pc12,
cmd : gpio_d.pd2.internal_pull_up(true),
cd: gpio_b.pb6.into_input(),
},
};

Expand All @@ -226,7 +282,8 @@ pub fn init_gpio(
// 4231 reversa
// 1234 derecho

gpio.pmic.pmic_cs.set_high();
gpio.pmic.pmic1_cs.set_high();
gpio.pmic.pmic2_cs.set_high();
gpio.memory_cs.set_high();

return gpio;
Expand Down
Loading

0 comments on commit 56151b8

Please sign in to comment.