mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-04 18:29:48 +02:00
Add timeout to CAN write to prevent crashing
This commit is contained in:
parent
56f7f205e4
commit
7025811c6e
4 changed files with 31 additions and 10 deletions
|
@ -271,10 +271,7 @@ int CAN_write_frame(const CAN_frame_t *p_frame) {
|
||||||
// Write the frame to the controller
|
// Write the frame to the controller
|
||||||
CAN_write_frame_phy(p_frame);
|
CAN_write_frame_phy(p_frame);
|
||||||
|
|
||||||
// wait for the frame tx to complete
|
return xSemaphoreTake(sem_tx_complete, 20) == pdTRUE ? 0 : -1;
|
||||||
xSemaphoreTake(sem_tx_complete, portMAX_DELAY);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CAN_stop() {
|
int CAN_stop() {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "ESP32CAN.h"
|
#include "ESP32CAN.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
int ESP32CAN::CANInit()
|
int ESP32CAN::CANInit()
|
||||||
{
|
{
|
||||||
|
@ -6,7 +7,24 @@ int ESP32CAN::CANInit()
|
||||||
}
|
}
|
||||||
int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame)
|
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()
|
int ESP32CAN::CANStop()
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,14 +3,17 @@
|
||||||
|
|
||||||
#include "CAN_config.h"
|
#include "CAN_config.h"
|
||||||
#include "CAN.h"
|
#include "CAN.h"
|
||||||
|
extern uint8_t LEDcolor;
|
||||||
|
|
||||||
class ESP32CAN
|
class ESP32CAN
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
bool tx_ok = true;
|
||||||
int CANInit();
|
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 CANWriteFrame(const CAN_frame_t* p_frame);
|
||||||
int CANStop();
|
int CANStop();
|
||||||
|
void CANSetCfg(CAN_device_t *can_cfg);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ESP32CAN ESP32Can;
|
extern ESP32CAN ESP32Can;
|
||||||
|
|
|
@ -81,10 +81,12 @@ uint16_t stat_batt_power = 0; //power going in/out of battery
|
||||||
#define GREEN 0
|
#define GREEN 0
|
||||||
#define YELLOW 1
|
#define YELLOW 1
|
||||||
#define RED 2
|
#define RED 2
|
||||||
|
#define BLUE 3
|
||||||
|
|
||||||
Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
|
Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
|
||||||
static uint8_t brightness = 0;
|
static uint8_t brightness = 0;
|
||||||
static bool rampUp = true;
|
static bool rampUp = true;
|
||||||
const uint8_t maxBrightness = 255;
|
const uint8_t maxBrightness = 100;
|
||||||
uint8_t LEDcolor = GREEN;
|
uint8_t LEDcolor = GREEN;
|
||||||
|
|
||||||
//Contactor parameters
|
//Contactor parameters
|
||||||
|
@ -161,8 +163,6 @@ void setup()
|
||||||
|
|
||||||
// Init LED control
|
// Init LED control
|
||||||
pixels.begin();
|
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
|
//Inverter Setup
|
||||||
#ifdef SOLAX_CAN
|
#ifdef SOLAX_CAN
|
||||||
|
@ -508,8 +508,11 @@ void handle_LED_state()
|
||||||
case YELLOW:
|
case YELLOW:
|
||||||
pixels.setPixelColor(0, pixels.Color(brightness, brightness, 0)); // Yellow pulsing LED
|
pixels.setPixelColor(0, pixels.Color(brightness, brightness, 0)); // Yellow pulsing LED
|
||||||
break;
|
break;
|
||||||
|
case BLUE:
|
||||||
|
pixels.setPixelColor(0, pixels.Color(0, 0, brightness)); //Blue pulsing LED
|
||||||
|
break;
|
||||||
case RED:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue