From 7db75f46447c50241246793f9941a3b2628b3e60 Mon Sep 17 00:00:00 2001 From: DragonflyPS Date: Thu, 1 Feb 2024 15:32:12 -0800 Subject: [PATCH] Bug Fix Moved adding irq handler for pulse trip to init function, core no longer locks up --- Powercore-V2.0 Firmware/main.c | 6 ++--- Powercore-V2.0 Firmware/pulse_generator.c | 31 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Powercore-V2.0 Firmware/main.c b/Powercore-V2.0 Firmware/main.c index ab7e194..e176196 100644 --- a/Powercore-V2.0 Firmware/main.c +++ b/Powercore-V2.0 Firmware/main.c @@ -39,9 +39,9 @@ bool cut_on_off_irq(repeating_timer_t *rt) { cutting_enabled = true; begin_output_pulses(OUTPUT_ON_TIME, OUTPUT_OFF_TIME, ISO_PULSE); - } else if(gpio_get(CUT_nEN_PIN)) { + } else if(gpio_get(CUT_nEN_PIN)) { - cutting_enabled == false; + cutting_enabled = false; disable_gate_driver(); } @@ -66,7 +66,7 @@ int main() { CC_Charger_init(CHARGER_CURRENT, CAP_VOLTAGE_PWM_LEVEL); //Setup the CC Charger Outputs - pulse_generator_init(120); //Setup Pulse generator + pulse_generator_init(70); //Setup Pulse generator gpio_init(CUT_nEN_PIN); gpio_set_dir(CUT_nEN_PIN, GPIO_IN); diff --git a/Powercore-V2.0 Firmware/pulse_generator.c b/Powercore-V2.0 Firmware/pulse_generator.c index d15409c..340fc0e 100644 --- a/Powercore-V2.0 Firmware/pulse_generator.c +++ b/Powercore-V2.0 Firmware/pulse_generator.c @@ -34,6 +34,8 @@ alarm_id_t timeout_alarm_id; uint32_t pulse_history[8] = {0, 0, 0, 0, 0, 0, 0, 0}; uint32_t pulse_counter = 0; +bool tracker = false; + //Prototype functions int64_t begin_off_time(alarm_id_t id, void *user_data); @@ -82,7 +84,6 @@ int64_t begin_on_time(alarm_id_t id, void *user_data){ output_state = WAITING_FOR_IGNITION; //Update pulse generator state gpio_set_irq_enabled(OUTPUT_CURRENT_TRIP_PIN, GPIO_IRQ_EDGE_RISE, true); //Configure detection of the spark - gpio_add_raw_irq_handler(OUTPUT_CURRENT_TRIP_PIN, &output_current_trip_irq); if(iso_pulse_mode) { //Check pulse mode is iso-pulse @@ -120,11 +121,11 @@ int64_t begin_off_time(alarm_id_t id, void *user_data){ add_alarm_in_us(15, change_CC_timing, NULL, true); //Setup the alarm to correct CC charger timing add_alarm_in_us(pulse_off_time-11, begin_on_time, NULL, true); //Setup the alarm to turn on the output MOSFET after the off time - pulse_counter -= pulse_history[0] & 1; //Subtract the 512th pulse state from the counter + pulse_counter -= pulse_history[0] & 0x1; //Subtract the 512th pulse state from the counter for(int i = 0; i < 7; i++) { - pulse_history[i] = ((pulse_history[i+1] & 1) << 31) + pulse_history[i] >> 1 ; //Binary shift the whole array (left shift each int and load in the first bit of the one above it) + pulse_history[i] = ((pulse_history[i+1] & 0x1) << 31) + pulse_history[i] >> 1 ; //Binary shift the whole array (left shift each int and load in the first bit of the one above it) } @@ -135,6 +136,7 @@ int64_t begin_off_time(alarm_id_t id, void *user_data){ pulse_history[7] = pulse_history[7] + (1 << 31); pulse_counter += 1; + } output_state = SPARK_OFF; //Set state machine state to SPARK_OFF @@ -153,6 +155,21 @@ int64_t begin_off_time(alarm_id_t id, void *user_data){ } +int64_t first_off_time(){ + + gpio_put(OUTPUT_EN_PIN, false); //Turn off output MOSFET + + gpio_set_irq_enabled(OUTPUT_CURRENT_TRIP_PIN, GPIO_IRQ_EDGE_RISE, false); //Disable the ignition sense irq + + LIMIT_set_timing(97, 7, false); //Limit CC Charger PWM duty cycle to avoid inrush (was 97) + + enable_CC_timing(); //Start CC Charger + + add_alarm_in_us(15, change_CC_timing, NULL, true); //Setup the alarm to correct CC charger timing + add_alarm_in_us(pulse_off_time-11, begin_on_time, NULL, true); //Setup the alarm to turn on the output MOSFET after the off time + +} + void begin_output_pulses(uint32_t on_time, uint32_t off_time, bool iso_pulse) { //Load in the pulse parameters @@ -165,7 +182,7 @@ void begin_output_pulses(uint32_t on_time, uint32_t off_time, bool iso_pulse) { pulse_history[i] = 0; pulse_counter = 0; - begin_off_time(-1, NULL); + first_off_time(); } @@ -186,8 +203,14 @@ void pulse_generator_init(uint32_t trip_current) { pwm_set_gpio_level(SPARK_THRESHOLD_PWM_PIN, trip_current); + pwm_set_enabled(pwm_gpio_to_slice_num(SPARK_THRESHOLD_PWM_PIN), true); + gpio_set_function(PULSE_COUNTER_PWM_PIN, GPIO_FUNC_PWM); pwm_set_wrap(pwm_gpio_to_slice_num(PULSE_COUNTER_PWM_PIN), 2500); + pwm_set_enabled(pwm_gpio_to_slice_num(PULSE_COUNTER_PWM_PIN), true); + + gpio_add_raw_irq_handler(OUTPUT_CURRENT_TRIP_PIN, &output_current_trip_irq); + } \ No newline at end of file