Skip to content

Commit 0ee021b

Browse files
authored
Merge pull request #48 from dalathegreat/can-crash
Add timeout to CAN write to prevent crashing
2 parents 56f7f20 + 7025811 commit 0ee021b

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

Software/CAN.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ int CAN_write_frame(const CAN_frame_t *p_frame) {
271271
// Write the frame to the controller
272272
CAN_write_frame_phy(p_frame);
273273

274-
// wait for the frame tx to complete
275-
xSemaphoreTake(sem_tx_complete, portMAX_DELAY);
276-
277-
return 0;
274+
return xSemaphoreTake(sem_tx_complete, 20) == pdTRUE ? 0 : -1;
278275
}
279276

280277
int CAN_stop() {

Software/ESP32CAN.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
#include "ESP32CAN.h"
2+
#include <Arduino.h>
23

34
int ESP32CAN::CANInit()
45
{
56
return CAN_init();
67
}
78
int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame)
89
{
9-
return CAN_write_frame(p_frame);
10+
static unsigned long start_time;
11+
int result = -1;
12+
if(tx_ok){
13+
result = CAN_write_frame(p_frame);
14+
tx_ok = (result == 0) ? true : false;
15+
if(tx_ok == false){
16+
Serial.println("CAN failure! Check wires");
17+
LEDcolor = 3;
18+
start_time = millis();
19+
}
20+
}
21+
else{
22+
if((millis() - start_time) >= 2000){
23+
tx_ok = true;
24+
LEDcolor = 0;
25+
}
26+
}
27+
return result;
1028
}
1129
int ESP32CAN::CANStop()
1230
{

Software/ESP32CAN.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
#include "CAN_config.h"
55
#include "CAN.h"
6+
extern uint8_t LEDcolor;
67

78
class ESP32CAN
89
{
910
public:
11+
bool tx_ok = true;
1012
int CANInit();
11-
int CANConfigFilter(const CAN_filter_t* p_filter);
13+
int CANConfigFilter(const CAN_filter_t* p_filter);
1214
int CANWriteFrame(const CAN_frame_t* p_frame);
1315
int CANStop();
16+
void CANSetCfg(CAN_device_t *can_cfg);
1417
};
1518

1619
extern ESP32CAN ESP32Can;

Software/Software.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ uint16_t stat_batt_power = 0; //power going in/out of battery
8181
#define GREEN 0
8282
#define YELLOW 1
8383
#define RED 2
84+
#define BLUE 3
85+
8486
Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
8587
static uint8_t brightness = 0;
8688
static bool rampUp = true;
87-
const uint8_t maxBrightness = 255;
89+
const uint8_t maxBrightness = 100;
8890
uint8_t LEDcolor = GREEN;
8991

9092
//Contactor parameters
@@ -161,8 +163,6 @@ void setup()
161163

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

167167
//Inverter Setup
168168
#ifdef SOLAX_CAN
@@ -508,8 +508,11 @@ void handle_LED_state()
508508
case YELLOW:
509509
pixels.setPixelColor(0, pixels.Color(brightness, brightness, 0)); // Yellow pulsing LED
510510
break;
511+
case BLUE:
512+
pixels.setPixelColor(0, pixels.Color(0, 0, brightness)); //Blue pulsing LED
513+
break;
511514
case RED:
512-
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red LED full brightness
515+
pixels.setPixelColor(0, pixels.Color(150, 0, 0)); // Red LED full brightness
513516
break;
514517
default:
515518
break;

0 commit comments

Comments
 (0)