Skip to content

Commit

Permalink
can bus support 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gemu2015 committed Oct 24, 2023
1 parent 2589cf3 commit b43ee05
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 31 deletions.
80 changes: 49 additions & 31 deletions tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5916,38 +5916,56 @@ extern char *SML_GetSVal(uint32_t index);
}
if (glob_script_mem.tcp_server) {
if (glob_script_mem.tcp_client.connected()) {
for (uint16_t cnt = 0; cnt < al; cnt++) {
switch (opts) {
case 0:
glob_script_mem.tcp_client.write((uint8_t)*fpd++);
break;
case 1:
{
uint16_t wval = *fpd++;
glob_script_mem.tcp_client.write(wval >> 8);
glob_script_mem.tcp_client.write(wval);
}
break;
case 2:
{
int16_t swval = *fpd++;
glob_script_mem.tcp_client.write(swval >> 8);
glob_script_mem.tcp_client.write(swval);
}
break;
case 3:
{
uint32_t lval = *(uint32_t*)fpd;
fpd++;
glob_script_mem.tcp_client.write(lval >> 24);
glob_script_mem.tcp_client.write(lval >> 16);
glob_script_mem.tcp_client.write(lval >> 8);
glob_script_mem.tcp_client.write(lval);
}
break;


uint8_t *abf = (uint8_t*)malloc(al * 4);
uint8_t *oabf = abf;
uint16_t dlen = 0;
if (abf) {
for (uint16_t cnt = 0; cnt < al; cnt++) {
switch (opts) {
case 0:
//glob_script_mem.tcp_client.write((uint8_t)*fpd++);
*abf++ = (uint8_t)*fpd++;
dlen++;
break;
case 1:
{
uint16_t wval = *fpd++;
//glob_script_mem.tcp_client.write(wval >> 8);
//glob_script_mem.tcp_client.write(wval);
*abf++ = (wval >> 8);
*abf++ = wval;
dlen += 2;
}
break;
case 2:
{
int16_t swval = *fpd++;
//glob_script_mem.tcp_client.write(swval >> 8);
//glob_script_mem.tcp_client.write(swval);
*abf++ = (swval >> 8);
*abf++ = swval;
dlen += 2;
}
break;
case 3:
{
uint32_t lval = *(uint32_t*)fpd;
fpd++;
//glob_script_mem.tcp_client.write(lval >> 24);
//glob_script_mem.tcp_client.write(lval >> 16);
//glob_script_mem.tcp_client.write(lval >> 8);
//glob_script_mem.tcp_client.write(lval);
*abf++ = (lval >> 24);
*abf++ = (lval >> 16);
*abf++ = (lval >> 8);
*abf++ = lval;
dlen += 4;
}
break;
}
}
glob_script_mem.tcp_client.write(oabf, dlen);
free(oabf);
}
}
}
Expand Down
100 changes: 100 additions & 0 deletions tasmota/tasmota_xsns_sensor/xsns_53_sml.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
#include "han_Parser.h"
#endif


#ifdef USE_SML_CANBUS
#include "mcp2515.h"
#endif

/* special options per meter
1:
special binary SML option for meters that use a bit in the status register to sign import or export like ED300L, AS2020 or DTZ541
Expand Down Expand Up @@ -479,13 +484,18 @@ struct METER_DESC {

#endif // USE_SML_TCP


#ifdef USE_SML_CANBUS
MCP2515 *mcp2515;
#endif
#ifdef ESP32
int8_t uart_index;
#endif
};




#define TCP_MODE_FLG 0x7f

struct METER_DESC meter_desc[MAX_METERS];
Expand Down Expand Up @@ -1480,6 +1490,7 @@ uint32_t meters;

for (meters = 0; meters < sml_globs.meters_used; meters++) {
struct METER_DESC *mp = &meter_desc[meters];
if (mp->type == 'C') continue;
if (mp->type != 'c') {
if (mp->srcpin != TCP_MODE_FLG) {
if (!mp->meter_ss) continue;
Expand Down Expand Up @@ -3171,6 +3182,29 @@ next_line:
InjektCounterValue(meters, RtcSettings.pulse_counter[cindex], 0.0);
cindex++;
}
} else if (mp->type == 'C') {
#ifdef USE_SML_CANBUS
if (TasmotaGlobal.spi_enabled) {
mp->mcp2515 = new MCP2515(mp->srcpin);
if (MCP2515::ERROR_OK != mp->mcp2515->reset()) {
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: Failed to reset module"));
return;
}
//mp->params

if (MCP2515::ERROR_OK != mp->mcp2515->setBitrate(CAN_100KBPS, MCP_8MHZ)) {
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: Failed to set module bitrate"));
return;
}
if (MCP2515::ERROR_OK != mp->mcp2515->setNormalMode()) {
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: Failed to set normal mode"));
return;
}
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: Initialized"));
} else {
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: SPI not configuered"));
}
#endif
} else {
// serial input, init
if (mp->srcpin == TCP_MODE_FLG) {
Expand Down Expand Up @@ -3559,6 +3593,68 @@ uint32_t ctime = millis();
}

#ifdef USE_SCRIPT

#ifdef USE_SML_CANBUS

char sml_c2h(char c) {
return "0123456789ABCDEF"[0x0F & (unsigned char)c];
}

#define SML_CAN_MAX_FRAMES 8

void SML_CANBUS_Read() {
uint8_t nCounter = 0;
bool checkRcv;
struct can_frame canFrame;

for (uint32_t meter = 0; meter < sml_globs.meters_used; meter++) {
struct METER_DESC *mp = &sml_globs.mp[meter];

if (mp->type != 'C') continue;

checkRcv = mp->mcp2515->checkReceive();

while (checkRcv && nCounter <= SML_CAN_MAX_FRAMES) {
mp->mcp2515->checkReceive();
nCounter++;
if (mp->mcp2515->readMessage(&canFrame) == MCP2515::ERROR_OK) {
//mp->mcp2515->lastFrameRecv = TasmotaGlobal.uptime;
char canMsg[17];
canMsg[0] = 0;
for (int i = 0; i < canFrame.can_dlc; i++) {
canMsg[i*2] = sml_c2h(canFrame.data[i]>>4);
canMsg[i*2+1] = sml_c2h(canFrame.data[i]);
}
if (canFrame.can_dlc > 0) {
canMsg[(canFrame.can_dlc - 1) * 2 + 2] = 0;
}
// AddLog(LOG_LEVEL_INFO, PSTR("CAN: Received message 0x%s from ID 0x%x"), canMsg, (uint32_t)canFrame.can_id);

// AddLog(LOG_LEVEL_INFO, PSTR("CAN: Received: ID: %d"), (uint32_t)canFrame.can_id);
// AddLog(LOG_LEVEL_INFO, PSTR("CAN: Received: LEN: %d"), (uint32_t)canFrame.can_dlc);
// for (int i = 0; i < canFrame.can_dlc; i++) {
// AddLog(LOG_LEVEL_INFO, PSTR("CAN: Received: DATA[%d]: %d"), i,canFrame.data[i]);
// }
Response_P(PSTR("{\"%s\":%d,\"%s\":%d"),
"ID",(uint32_t)canFrame.can_id,
"LEN",(uint32_t)canFrame.can_dlc
);
for (int i = 0; i < canFrame.can_dlc; i++) { ResponseAppend_P(PSTR(",\"D%d\":%d"),i,canFrame.data[i]); }
ResponseJsonEnd();

MqttPublishPrefixTopic_P(STAT, "CAN");
ResponseClear();
} else if (mp->mcp2515->checkError()) {
uint8_t errFlags = mp->mcp2515->getErrorFlags();
mp->mcp2515->clearRXnOVRFlags();
AddLog(LOG_LEVEL_INFO, PSTR("SML CAN: Received error %d"), errFlags);
break;
}
}
}
}
#endif

char *SML_Get_Sequence(char *cp,uint32_t index) {
if (!index) return cp;
uint32_t cindex = 0;
Expand Down Expand Up @@ -4056,6 +4152,10 @@ bool Xsns53(uint32_t function) {
SML_Check_Send();
}
}

#ifdef USE_SML_CANBUS
SML_CANBUS_Read();
#endif
break;
case FUNC_EVERY_SECOND:
if (bitRead(Settings->rule_enabled, 0)) {
Expand Down

0 comments on commit b43ee05

Please sign in to comment.