Skip to content

Commit

Permalink
pwm fixed buzzer works (#279)
Browse files Browse the repository at this point in the history
* pwm fixed buzzer works

* added pwm response to fault bits
  • Loading branch information
Akashem06 authored May 16, 2024
1 parent 081c401 commit 4de6fef
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 37 deletions.
2 changes: 1 addition & 1 deletion libraries/codegen/boards/power_distribution.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@
fault_bitset:
length: 8
bps_persist:
length: 8
length: 16
2 changes: 2 additions & 0 deletions libraries/ms-common/src/arm/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions projects/centre_console/inc/cc_buttons.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
53 changes: 38 additions & 15 deletions projects/centre_console/src/cc_buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)&REGEN_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;
}
4 changes: 1 addition & 3 deletions projects/centre_console/src/pedal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions projects/centre_console/src/update_dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions projects/power_distribution/src/power_seq_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 0 additions & 14 deletions smoke/fan/src/main.c

This file was deleted.

File renamed without changes.
File renamed without changes.
71 changes: 71 additions & 0 deletions smoke/pwm/src/main.c
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 4de6fef

Please sign in to comment.