Merge pull request #49 from dalathegreat/main

Add timeout to CAN write to prevent crashing
This commit is contained in:
rjsc 2023-08-16 22:31:51 +01:00 committed by GitHub
commit beaeddcbe9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 9 deletions

View file

@ -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() {

View file

@ -1,4 +1,5 @@
#include "ESP32CAN.h"
#include <Arduino.h>
int ESP32CAN::CANInit()
{
@ -6,7 +7,24 @@ int ESP32CAN::CANInit()
}
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()
{

View file

@ -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;

View file

@ -76,6 +76,8 @@ uint16_t cell_min_voltage = 3700; //Stores the minimum cell voltage value in the
#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;
@ -178,8 +180,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
//Inform user what setup is used
#ifdef DUAL_CAN
@ -554,8 +554,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;