diff --git a/libraries/codegen/boards/power_distribution.yaml b/libraries/codegen/boards/power_distribution.yaml index 2c4a2a599..e1ca93326 100644 --- a/libraries/codegen/boards/power_distribution.yaml +++ b/libraries/codegen/boards/power_distribution.yaml @@ -60,4 +60,4 @@ fault_bitset: length: 8 bps_persist: - length: 8 + length: 16 diff --git a/libraries/ms-common/src/arm/pwm.c b/libraries/ms-common/src/arm/pwm.c index cb126bf91..6d7ed1e03 100644 --- a/libraries/ms-common/src/arm/pwm.c +++ b/libraries/ms-common/src/arm/pwm.c @@ -116,6 +116,8 @@ StatusCode pwm_set_pulse(PwmTimer timer, uint16_t pulse_width_us) { .TIM_Pulse = pulse_width_us, .TIM_OutputState = TIM_OutputState_Enable, .TIM_OCPolarity = TIM_OCPolarity_High, + .TIM_OutputNState = TIM_OutputNState_Enable, + .TIM_OCNPolarity = TIM_OCNPolarity_High, }; // Enable PWM on all channels. diff --git a/projects/centre_console/inc/cc_buttons.h b/projects/centre_console/inc/cc_buttons.h index 8c17eee98..accb03c98 100644 --- a/projects/centre_console/inc/cc_buttons.h +++ b/projects/centre_console/inc/cc_buttons.h @@ -1,6 +1,7 @@ #pragma once #include "cc_hw_defs.h" +#include "centre_console_getters.h" #include "drive_fsm.h" #include "i2c.h" #include "pca9555_gpio_expander.h" diff --git a/projects/centre_console/src/cc_buttons.c b/projects/centre_console/src/cc_buttons.c index 287d7af8c..cf4ecab3c 100644 --- a/projects/centre_console/src/cc_buttons.c +++ b/projects/centre_console/src/cc_buttons.c @@ -5,16 +5,15 @@ static GpioAddress buzzer = BUZZER; static SoftTimer s_timer; static bool beep_flag = true; -// static void prv_buzzer_beep(SoftTimerId id) { -// if (beep_flag) { -// pwm_set_dc(PWM_TIMER_4, 100); -// soft_timer_start(&s_timer); -// beep_flag = false; -// } else { -// pwm_set_dc(PWM_TIMER_4, 0); -// beep_flag = true; -// } -// } +static void prv_buzzer_beep(SoftTimerId id) { + if (beep_flag) { + soft_timer_start(&s_timer); + beep_flag = false; + } else { + pwm_set_dc(PWM_TIMER_1, 0); + beep_flag = true; + } +} // Notifies drive/power task of button press event StatusCode get_button_press(void) { @@ -25,40 +24,64 @@ StatusCode get_button_press(void) { if (pca9555_reg_val == PCA9555_REG_DEFAULT) { // No button pressed return STATUS_CODE_OK; } + if ((~(pca9555_reg_val)®EN_BTN_MASK) != 0) { LOG_DEBUG("REGEN PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(cc_notify_task, REGEN_BUTTON_EVENT); } if ((~(pca9555_reg_val)&HAZARD_BTN_MASK) != 0) { LOG_DEBUG("HAZARD PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(cc_notify_task, HAZARD_BUTTON_EVENT); } if ((~(pca9555_reg_val)&POWER_BTN_MASK) != 0) { LOG_DEBUG("POWER PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(cc_notify_task, POWER_BUTTON_EVENT); } if ((~(pca9555_reg_val)&DRIVE_BTN_MASK) != 0) { LOG_DEBUG("DRIVE PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(drive, DRIVE_BUTTON_EVENT); } if ((~(pca9555_reg_val)&NEUTRAL_BTN_MASK) != 0) { LOG_DEBUG("NEUT PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(drive, NEUTRAL_BUTTON_EVENT); } if ((~(pca9555_reg_val)&REVERSE_BTN_MASK) != 0) { LOG_DEBUG("REV PRESSED\n"); + if (beep_flag && !get_battery_status_fault() && !get_pd_status_bps_persist()) { + pwm_set_dc(PWM_TIMER_1, 5); + soft_timer_start(&s_timer); + } notify(drive, REVERSE_BUTTON_EVENT); } - // soft_timer_start(&s_timer); return STATUS_CODE_OK; } StatusCode init_cc_buttons(Task *task) { cc_notify_task = task; - // gpio_init_pin(&buzzer, GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); - // pwm_init(PWM_TIMER_1, BUZZER_MICROSECONDS); - // pwm_set_dc(PWM_TIMER_1, 5); - // soft_timer_init(BEEP_MS, prv_buzzer_beep, &s_timer); + gpio_init_pin(&buzzer, GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); + pwm_init(PWM_TIMER_1, BUZZER_MICROSECONDS); + pwm_set_dc(PWM_TIMER_1, 0); + soft_timer_init(BEEP_MS, prv_buzzer_beep, &s_timer); return STATUS_CODE_OK; } diff --git a/projects/centre_console/src/pedal.c b/projects/centre_console/src/pedal.c index 14a10890a..5b4796a3a 100644 --- a/projects/centre_console/src/pedal.c +++ b/projects/centre_console/src/pedal.c @@ -25,7 +25,7 @@ static void prv_read_throttle_data(uint32_t *reading) { } else if (calculated_reading > 1) { calculated_reading = 1; } - LOG_DEBUG("READING %d\n", (int)(calculated_reading * 100)); + // LOG_DEBUG("READING %d\n", (int)(calculated_reading * 100)); memcpy(reading, &calculated_reading, sizeof(calculated_reading)); } @@ -38,12 +38,10 @@ void pedal_run() { // Sending messages if (brake_state == GPIO_STATE_LOW) { // Brake is pressed - Send brake data with throttle as 1 - LOG_DEBUG("BRAKES PRESSED\n"); set_cc_pedal_brake_output(1); set_cc_pedal_throttle_output(0); } else { // Brake is not pressed - LOG_DEBUG("BRAKES NOT PRESSED\n"); set_cc_pedal_brake_output(0); set_cc_pedal_throttle_output(throttle_position); } diff --git a/projects/centre_console/src/update_dashboard.c b/projects/centre_console/src/update_dashboard.c index 6dcc821c8..42ffab0e4 100644 --- a/projects/centre_console/src/update_dashboard.c +++ b/projects/centre_console/src/update_dashboard.c @@ -5,6 +5,7 @@ #include "centre_console_setters.h" #include "drive_fsm.h" #include "pca9555_gpio_expander.h" +#include "pwm.h" #include "seg_display.h" // Multiplication factor to convert CAN drive output velocity (cm/s) to kph @@ -97,15 +98,22 @@ void update_indicators(uint32_t notif) { // Update Aux warning LED if (get_pd_status_fault_bitset()) { + pwm_set_dc(PWM_TIMER_1, 50); pca9555_gpio_set_state(&s_output_leds[AUX_WARNING_LED], PCA9555_GPIO_STATE_HIGH); } else { + // PWM will not stop, driver will pull over and diagnose issue pca9555_gpio_set_state(&s_output_leds[AUX_WARNING_LED], PCA9555_GPIO_STATE_LOW); } - if (get_battery_status_fault() || get_pd_status_power_state() == EE_POWER_FAULT_STATE || - get_pd_status_bps_persist()) { + if (get_battery_status_fault() & get_pd_status_bps_persist() & (1 << 15)) { + pwm_set_dc(PWM_TIMER_1, 100); + gpio_set_state(&bps_led, GPIO_STATE_LOW); + } else if ((get_battery_status_fault() & get_pd_status_bps_persist() & (1 << 14)) || + get_pd_status_power_state() == EE_POWER_FAULT_STATE) { + pwm_set_dc(PWM_TIMER_1, 10); gpio_set_state(&bps_led, GPIO_STATE_LOW); } else { + // PWM will not stop, driver will pull over and diagnose issue gpio_set_state(&bps_led, GPIO_STATE_HIGH); } } diff --git a/projects/power_distribution/src/power_seq_fsm.c b/projects/power_distribution/src/power_seq_fsm.c index 9e30d03a2..abf2a940b 100644 --- a/projects/power_distribution/src/power_seq_fsm.c +++ b/projects/power_distribution/src/power_seq_fsm.c @@ -95,7 +95,7 @@ static void prv_fault_state_output(void *context) { (get_motor_velocity_velocity_l() + get_motor_velocity_velocity_r()) / 2; persist_commit(&s_persist); pd_set_active_output_group(OUTPUT_GROUP_POWER_FAULT); - set_pd_status_bps_persist(true); + set_pd_status_bps_persist(s_bps_storage.fault_bitset); set_pd_status_power_state(EE_POWER_FAULT_STATE); // TODO(devAdhiraj): start bps strobe } @@ -124,7 +124,7 @@ StatusCode init_power_seq(void) { memset(&s_bps_storage, 0, sizeof(s_bps_storage)); persist_init(&s_persist, BPS_FAULT_FLASH_PAGE, &s_bps_storage, sizeof(s_bps_storage), true); persist_ctrl_periodic(&s_persist, false); - if (s_bps_storage.fault_bitset) set_pd_status_bps_persist(true); + if (s_bps_storage.fault_bitset) set_pd_status_bps_persist(s_bps_storage.fault_bitset); fsm_init(power_seq, s_power_seq_state_list, s_power_seq_transitions, POWER_STATE_OFF, NULL); return STATUS_CODE_OK; diff --git a/smoke/fan/src/main.c b/smoke/fan/src/main.c deleted file mode 100644 index 20fc045a4..000000000 --- a/smoke/fan/src/main.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "gpio.h" -#include "log.h" -#include "pwm.h" - -GpioAddress pin = { .port = GPIO_PORT_B, .pin = 15 }; -int main(void) { - gpio_init(); - log_init(); - gpio_init_pin(&pin, GPIO_ALTFN_PUSH_PULL, GPIO_STATE_HIGH); - pwm_init(PWM_TIMER_1, 250); - pwm_set_dc(PWM_TIMER_1, 50); - while (1) { - } -} diff --git a/smoke/fan/README.md b/smoke/pwm/README.md similarity index 100% rename from smoke/fan/README.md rename to smoke/pwm/README.md diff --git a/smoke/fan/config.json b/smoke/pwm/config.json similarity index 100% rename from smoke/fan/config.json rename to smoke/pwm/config.json diff --git a/smoke/pwm/src/main.c b/smoke/pwm/src/main.c new file mode 100644 index 000000000..6f2950a40 --- /dev/null +++ b/smoke/pwm/src/main.c @@ -0,0 +1,71 @@ +#include "gpio.h" +#include "log.h" +#include "pwm.h" +#include "stm32f10x.h" +#include "stm32f10x_rcc.h" +#include "stm32f10x_tim.h" + +// TIM 1 +static const GpioAddress pwm_tim_1[] = { + { .port = GPIO_PORT_A, .pin = 8 }, + { .port = GPIO_PORT_A, .pin = 9 }, + { .port = GPIO_PORT_A, .pin = 10 }, + { .port = GPIO_PORT_A, .pin = 11 }, +}; + +// TIM 1N +static const GpioAddress pwm_tim_1n[] = { + { .port = GPIO_PORT_B, .pin = 13 }, + { .port = GPIO_PORT_B, .pin = 14 }, + { .port = GPIO_PORT_B, .pin = 15 }, +}; + +// TIM 3 +static const GpioAddress pwm_tim_3[] = { + { .port = GPIO_PORT_A, .pin = 6 }, + { .port = GPIO_PORT_A, .pin = 7 }, + { .port = GPIO_PORT_B, .pin = 0 }, + { .port = GPIO_PORT_B, .pin = 1 }, +}; + +// TIM 4 +static const GpioAddress pwm_tim_4[] = { + { .port = GPIO_PORT_B, .pin = 6 }, + { .port = GPIO_PORT_B, .pin = 7 }, + { .port = GPIO_PORT_B, .pin = 8 }, + { .port = GPIO_PORT_A, .pin = 9 }, +}; + +int main(void) { + gpio_init(); + log_init(); + + // Initialize PWM pins for TIM1 + for (uint8_t i = 0; i < SIZEOF_ARRAY(pwm_tim_1); i++) { + gpio_init_pin(&pwm_tim_1[i], GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); + } + // Initialize PWM pins for TIM1_N + for (uint8_t i = 0; i < SIZEOF_ARRAY(pwm_tim_1n); i++) { + gpio_init_pin(&pwm_tim_1n[i], GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); + } + pwm_init(PWM_TIMER_1, 250); + pwm_set_dc(PWM_TIMER_1, 50); + + // Initialize PWM pins for TIM3 + for (uint8_t i = 0; i < SIZEOF_ARRAY(pwm_tim_3); i++) { + gpio_init_pin(&pwm_tim_3[i], GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); + } + pwm_init(PWM_TIMER_3, 250); + pwm_set_dc(PWM_TIMER_3, 50); + + // Initialize PWM pins for TIM4 + for (uint8_t i = 0; i < SIZEOF_ARRAY(pwm_tim_4); i++) { + gpio_init_pin(&pwm_tim_4[i], GPIO_ALTFN_PUSH_PULL, GPIO_STATE_LOW); + } + pwm_init(PWM_TIMER_4, 250); + pwm_set_dc(PWM_TIMER_4, 50); + + while (1) { + // Main loop + } +}