Skip to content

Commit

Permalink
Merge pull request #48 from dalathegreat/can-crash
Browse files Browse the repository at this point in the history
Add timeout to CAN write to prevent crashing
  • Loading branch information
dalathegreat authored Aug 16, 2023
2 parents 56f7f20 + 7025811 commit 0ee021b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
5 changes: 1 addition & 4 deletions Software/CAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ int CAN_write_frame(const CAN_frame_t *p_frame) {
// Write the frame to the controller
CAN_write_frame_phy(p_frame);

// wait for the frame tx to complete
xSemaphoreTake(sem_tx_complete, portMAX_DELAY);

return 0;
return xSemaphoreTake(sem_tx_complete, 20) == pdTRUE ? 0 : -1;
}

int CAN_stop() {
Expand Down
20 changes: 19 additions & 1 deletion Software/ESP32CAN.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
#include "ESP32CAN.h"
#include <Arduino.h>

int ESP32CAN::CANInit()
{
return CAN_init();
}
int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame)
{
return CAN_write_frame(p_frame);
static unsigned long start_time;
int result = -1;
if(tx_ok){
result = CAN_write_frame(p_frame);
tx_ok = (result == 0) ? true : false;
if(tx_ok == false){
Serial.println("CAN failure! Check wires");
LEDcolor = 3;
start_time = millis();
}
}
else{
if((millis() - start_time) >= 2000){
tx_ok = true;
LEDcolor = 0;
}
}
return result;
}
int ESP32CAN::CANStop()
{
Expand Down
5 changes: 4 additions & 1 deletion Software/ESP32CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

#include "CAN_config.h"
#include "CAN.h"
extern uint8_t LEDcolor;

class ESP32CAN
{
public:
bool tx_ok = true;
int CANInit();
int CANConfigFilter(const CAN_filter_t* p_filter);
int CANConfigFilter(const CAN_filter_t* p_filter);
int CANWriteFrame(const CAN_frame_t* p_frame);
int CANStop();
void CANSetCfg(CAN_device_t *can_cfg);
};

extern ESP32CAN ESP32Can;
Expand Down
11 changes: 7 additions & 4 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ uint16_t stat_batt_power = 0; //power going in/out of battery
#define GREEN 0
#define YELLOW 1
#define RED 2
#define BLUE 3

Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
static uint8_t brightness = 0;
static bool rampUp = true;
const uint8_t maxBrightness = 255;
const uint8_t maxBrightness = 100;
uint8_t LEDcolor = GREEN;

//Contactor parameters
Expand Down Expand Up @@ -161,8 +163,6 @@ void setup()

// Init LED control
pixels.begin();
pixels.setPixelColor(0, pixels.Color(0, 0, 255)); // Blue LED full brightness while battery and CAN is starting.
pixels.show(); // Incase of crash due to CAN polarity / termination, LED will remain BLUE

//Inverter Setup
#ifdef SOLAX_CAN
Expand Down Expand Up @@ -508,8 +508,11 @@ void handle_LED_state()
case YELLOW:
pixels.setPixelColor(0, pixels.Color(brightness, brightness, 0)); // Yellow pulsing LED
break;
case BLUE:
pixels.setPixelColor(0, pixels.Color(0, 0, brightness)); //Blue pulsing LED
break;
case RED:
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red LED full brightness
pixels.setPixelColor(0, pixels.Color(150, 0, 0)); // Red LED full brightness
break;
default:
break;
Expand Down

0 comments on commit 0ee021b

Please sign in to comment.