Skip to content

Commit

Permalink
Fix CAN problems with the DEYE inverter
Browse files Browse the repository at this point in the history
  • Loading branch information
shining-man committed Aug 3, 2023
1 parent 7951f0c commit 278d979
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
11 changes: 9 additions & 2 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "params_dt.h"

#define BSC_SW_VERSION "V0.4.13"
#define BSC_SW_VERSION "V0.4.16"
static const char COMPILE_DATE_TIME[] = "";

#define HTML_MINIFY
Expand All @@ -33,6 +33,7 @@ static const char COMPILE_DATE_TIME[] = "";
//#define WLAN_DEBUG
//#define WLAN_DEBUG2
//#define CAN_DEBUG
//#define CAN_DEBUG_STATUS
//#define WEBSET_DEBUG
//#define MAIN_DEBUG
//#define LOG_BMS_DATA
Expand All @@ -51,7 +52,8 @@ static const char COMPILE_DATE_TIME[] = "";
//#define MQTT_DEBUG
#define WLAN_DEBUG
#define WLAN_DEBUG2
#define CAN_DEBUG
//#define CAN_DEBUG
#define CAN_DEBUG_STATUS
//#define WEBSET_DEBUG
#define MAIN_DEBUG
//#define LOG_BMS_DATA
Expand Down Expand Up @@ -302,6 +304,11 @@ enum serialRxTxEn_e {serialRxTx_RxTxDisable, serialRxTx_TxEn, serialRxTx_RxEn};

#define ID_PARAM_BMS_BALUE_ADJUSTMENTS_SOC100_CELL_VOLTAGE 127

#define ID_PARAM_TEMP_ALARM_TEMP_QUELLE 128
#define ID_PARAM_TEMP_ALARM_BMS_QUELLE 129



//Auswahl Bluetooth Geräte
#define ID_BT_DEVICE_NB 0
#define ID_BT_DEVICE_NEEY4A 1
Expand Down
47 changes: 47 additions & 0 deletions src/Canbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

static const char *TAG = "CAN";

void readCanMessages();
void sendBmsCanMessages();
void sendCanMsg_35e_370_371();
void sendCanMsg_351();
Expand Down Expand Up @@ -120,6 +121,7 @@ void canSetup()

loadCanSettings();

CAN.setAlert(true);
esp_err_t err = CAN.begin(GPIO_NUM_5,GPIO_NUM_4,TWAI_SPEED_500KBPS);
BSC_LOGI(TAG, "%s", CAN.getErrorText(err).c_str());
}
Expand Down Expand Up @@ -197,16 +199,60 @@ void canTxCyclicRun()
if(WebSettings::getBool(ID_PARAM_BMS_CAN_ENABLE,0))
{
u8_mMqttTxTimer++;
readCanMessages();
sendBmsCanMessages();
if(u8_mMqttTxTimer>=15)u8_mMqttTxTimer=0;
}
}


void readCanMessages()
{
twai_message_t canMessage;
twai_status_info_t canStatus;

for(uint8_t i=0;i<5;i++)
{
canStatus = CAN.getStatus();
if(canStatus.msgs_to_rx==0) break;
CAN.read(&canMessage);
#ifdef CAN_DEBUG
BSC_LOGI(TAG,"RX ID: %i",canMessage.identifier);
#endif
}
}


void sendCanMsg(uint32_t identifier, uint8_t *buffer, uint8_t length)
{
esp_err_t err = CAN.write(TWAI_STD_FRAME,identifier,length,buffer);
if(err!=ESP_OK) BSC_LOGI(TAG, "%s", CAN.getErrorText(err).c_str());

#ifdef CAN_DEBUG_STATUS
twai_status_info_t canStatus = CAN.getStatus();

if(err!=ESP_OK || canStatus.state!=1 || canStatus.tx_error_counter!=0 || canStatus.tx_failed_count!=0 || canStatus.arb_lost_count!=0 || canStatus.bus_error_count!=0)
{
BSC_LOGE(TAG, "state=%i, msgs_to_tx=%i, msgs_to_rx=%i, tx_error=%i, rx_error=%i, tx_failed=%i, rx_missed=%i, rx_overrun=%i, arb_lost=%i, bus_error=%i", \
canStatus.state, canStatus.msgs_to_tx, canStatus.msgs_to_rx, canStatus.tx_error_counter, canStatus.rx_error_counter, \
canStatus.tx_failed_count, canStatus.rx_missed_count, canStatus.rx_overrun_count, canStatus.arb_lost_count, canStatus.bus_error_count);
}

/*
twai_state_t state; //< Current state of TWAI controller (Stopped/Running/Bus-Off/Recovery)
uint32_t msgs_to_tx; //< Number of messages queued for transmission or awaiting transmission completion
uint32_t msgs_to_rx; //< Number of messages in RX queue waiting to be read
uint32_t tx_error_counter; //< Current value of Transmit Error Counter
uint32_t rx_error_counter; //< Current value of Receive Error Counter
uint32_t tx_failed_count; //< Number of messages that failed transmissions
uint32_t rx_missed_count; //< Number of messages that were lost due to a full RX queue (or errata workaround if enabled)
uint32_t rx_overrun_count; //< Number of messages that were lost due to a RX FIFO overrun
uint32_t arb_lost_count; //< Number of instances arbitration was lost
uint32_t bus_error_count; //< Number of instances a bus error has occurred
*/
BSC_LOGI(TAG,"Alert=%i", CAN.getAlert());
#endif

vTaskDelay(pdMS_TO_TICKS(5));
}

Expand All @@ -219,6 +265,7 @@ void sendBmsCanMessages()
sendCanMsg_351();
sendCanMsg_355();
sendCanMsg_356();
vTaskDelay(pdMS_TO_TICKS(50));
sendCanMsg_35e_370_371();
sendCanMsg_359(); //Alarms
break;
Expand Down
2 changes: 1 addition & 1 deletion src/bscTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Arduino.h"
#include <NTP.h>
#include <WiFiUdp.h>
#include "Websettings.h"
#include "WebSettings.h"

static const char *TAG = "TIME";

Expand Down
2 changes: 1 addition & 1 deletion src/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "log.h"
#include "WebSettings.h"
#include "Canbus.h"
#include "Alarmrules.h"
#include "AlarmRules.h"
#include "BmsData.h"
#include "mcp23017.h"

Expand Down

0 comments on commit 278d979

Please sign in to comment.