diff --git a/build-flags/build_flags_stm32-noip b/build-flags/build_flags_stm32-noip index 94b5161..fafe4e8 100644 --- a/build-flags/build_flags_stm32-noip +++ b/build-flags/build_flags_stm32-noip @@ -13,6 +13,9 @@ -DMULTIVENT_DISABLE -DMOTOR_DISABLE +-D CANDRV +-D THERMOSTAT_CHECK_PERIOD=5000 + -DENABLE_HWSERIAL1 -DdebugSerialPort=Serial1 @@ -21,8 +24,8 @@ #-DFLASH_DATA_SECTOR #-DFLASH_PAGE_NUMBER --D PIO_FRAMEWORK_ARDUINO_ENABLE_MASS_STORAGE --D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC_AND_MSC +# -D PIO_FRAMEWORK_ARDUINO_ENABLE_MASS_STORAGE +# -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC_AND_MSC #-DdebugSerialPort=SerialUSB #-DSerialPortType=USBSerial diff --git a/lighthub/candriver.cpp b/lighthub/candriver.cpp new file mode 100644 index 0000000..25e1d6d --- /dev/null +++ b/lighthub/candriver.cpp @@ -0,0 +1,322 @@ + +#ifdef CANDRV + +#include +#include +#include + +#if defined(ARDUINO_ARCH_STM32) +#include +STM32_CAN STMCan( CAN1, ALT, RX_SIZE_64, TX_SIZE_16 ); +#endif + +#if defined(ARDUINO_ARCH_ESP32) +#include +#endif +#if defined(__SAM3X8E__) +#include +#endif + +#include +//#include +extern systemConfig sysConf; + + +void printFrame(datagram_t * frame, uint8_t len ) { + + debugSerial.print(" Data: 0x"); + for (int count = 0; count < len; count++) { + debugSerial.print(frame->data[count], HEX); + debugSerial.print(" "); + } + debugSerial.println(); +} + + +bool canDriver::upTime(uint32_t ut) +{ + // return 0; + canid_t id; + datagram_t packet; + + id.reserve=0; + id.status=1; + id.payloadType=payloadType::metric; + id.deviceId=controllerId; + id.itemId=metricType::UpTime; + + packet.metric1=ut; + + debugSerial<<("UpTime")<=8) break; + } + debugSerialPort.println(); + + processPacket( CAN.packetId(), &packet,i); + } + } + #endif + + //DUE + #if defined(__SAM3X8E__) + CAN_FRAME incoming; + if (Can0.available() > 0) { + Can0.read(incoming); + printFrame(incoming); + } + + #endif + + +//State machine + +switch (state) +{ + case canState::MACLookup: + if (isTimeOver(responseTimer,millis(),1000UL)) + { + responseTimer=millisNZ(); + state=canState::Error; + errorSerial<<"CAN Timeout"<=6)) + { + debugSerial<<"Got Controller CAN addr: "<mac); + //debugSerial<<"ID requested"<8) size = 8; + + //STM32 + #if defined(ARDUINO_ARCH_STM32) + //if (!Can) return 0; + if (buf) for(uint8_t i=0;idata[i]; + CAN_TX_msg.id = msg_id; + CAN_TX_msg.flags.extended = 1; // To enable extended ID + CAN_TX_msg.len=size; + if (res=STMCan.write(CAN_TX_msg)) debugSerial<<("CAN Wrote ")<data,size); + //for(uint8_t i=0;i +#endif + +#include +//#include NO! + +typedef uint8_t macAddress[6]; +#pragma pack(push, 1) +typedef union +{ + uint32_t id; + struct + { + uint16_t itemId; + uint8_t deviceId; + uint8_t payloadType:4; + uint8_t status:1; + uint8_t reserve:3; //0 + + }; +} canid_t; + +enum payloadType +{ + itemCommand=1, + lookupMAC=2, + configFrame=3, + OTAFrame=4, + auth=5, + metric=6, + sysCmd=7 +}; + +enum metricType +{ + MAC=1, + IP=2, + NetMask=3, + GW=4, + DNS=5, + UpTime=6, + Salt=7 +}; + +enum commandType +{ + reboot=1, + get=2, + save=3, + load=4 +}; + + +#define MAXCANID 0x1FFFFFFF +// Request item status: id.status=1;deviceId=[0xFF | deviceId];itemId=x; RTR bit=true +// Request on config: id.status=0;deviceId=0;itemId=x payload.mac=mac;crc16=crc_current_config + + + +typedef union { + uint8_t data[8]; + struct { + itemCmdStore cmd; + itemArgStore param; + }; + struct { + macAddress mac; + uint16_t currentConfCRC; + }; + struct { + uint8_t sysCmd; + uint8_t sysCmdData[7]; + }; + struct { + uint32_t metric1; + uint32_t metric2; + }; +} datagram_t; + + +enum canState +{ +Unknown=0, +MACLookup=2, +HaveId=3, +ConfigFrameRequested=4, +ConfigFrameReceived=5, +ConfigLoaded=6, +Error=7 +}; +#pragma pack(pop) + +class canDriver +{ +public: +canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::Unknown;}; +bool upTime(uint32_t ut); +bool salt(uint32_t salt); +bool lookupMAC(); +bool sendRemoteID(macAddress mac); +bool begin(); +void Poll(); +bool processPacket(uint32_t rawid, datagram_t *packet, uint8_t len, bool rtr=false); +bool write(uint32_t msg_id, datagram_t * buf = NULL, uint8_t size=0); +private: + + + #if defined(ARDUINO_ARCH_STM32) + CAN_message_t CAN_RX_msg; + CAN_message_t CAN_TX_msg; + #endif + bool ready; + uint8_t controllerId; + canState state; + uint32_t responseTimer; + +}; +#endif // \ No newline at end of file diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 1411c53..fca9086 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -67,6 +67,11 @@ static char syslogDeviceHostname[16]; flashStream sysConfStream; systemConfig sysConf(&sysConfStream); + +#ifdef CANDRV +canDriver LHCAN; +#endif + extern long timer0_overflow_count; #ifdef WIFI_ENABLE WiFiClient ethClient; @@ -104,7 +109,7 @@ const char verval_P[] PROGMEM = QUOTE(PIO_SRC_REV); char cryptoKey[] = QUOTE(SHAREDSECRET); #endif -#if defined(__SAM3X8E__) +#if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32) UID UniqueID; #endif @@ -482,7 +487,7 @@ else return;// -7; } - +#endif //NOIP void printMACAddress() { //macAddress * mac = sysConf.getMAC(); @@ -494,7 +499,7 @@ void printMACAddress() { } } -#endif //NOIP + char* getStringFromConfig(aJsonObject * a, int i) { @@ -1677,18 +1682,7 @@ int cmdFunctionOTAPwd(int arg_cnt, char **args) return 200; } -int cmdFunctionSetMac(int arg_cnt, char **args) { - char dummy; - uint8_t mac[6]; - if (sscanf(args[1], "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], &dummy) < 6) { - errorSerial< HAVE_IP_ADDRESS) { // Begin network runners @@ -3042,8 +3096,17 @@ void thermoLoop(void) { if (!isTimeOver(timerThermostatCheck,millis(),THERMOSTAT_CHECK_PERIOD)) return; + +timerThermostatCheck = millis();// + THERMOSTAT_CHECK_PERIOD; +publishStat(); + +#ifndef DISABLE_FREERAM_PRINT + //(thermostatCheckPrinted) ? + debugSerial<child; thermoItem; thermoItem = thermoItem->next) { if ((thermoItem->type == aJson_Array) && (aJson.getArrayItem(thermoItem, I_TYPE)->valueint == CH_THERMO)) @@ -3092,17 +3155,11 @@ void thermoLoop(void) { } } else debugSerial< +#endif #ifdef _artnet extern Artnet *artnet; @@ -243,15 +246,17 @@ bool isNotRetainingStatus(); #if not defined (NOIP) void mqttCallback(char *topic, byte *payload, unsigned int length); -void printMACAddress(); lan_status lanLoop(); int loadConfigFromHttp(); void onInitialStateInitLAN(); void onMQTTConnect(); void ip_ready_config_loaded_connecting_to_broker(); -void setupMacAddress(); + #endif +void setupMacAddress(); +void printMACAddress(); + #ifndef OWIRE_DISABLE void Changed(int i, DeviceAddress addr, float currentTemp); #endif diff --git a/lighthub/options.h b/lighthub/options.h index 56daece..d117a17 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -129,7 +129,9 @@ #define INTERVAL_SLOW_POLLING 1000 //#define INTERVAL_POLLING 100 +#ifndef THERMOSTAT_CHECK_PERIOD #define THERMOSTAT_CHECK_PERIOD 30000 +#endif #ifndef OW_UPDATE_INTERVAL #define OW_UPDATE_INTERVAL 5000 diff --git a/lighthub/systemconfigdata.h b/lighthub/systemconfigdata.h index 5c582de..8772497 100644 --- a/lighthub/systemconfigdata.h +++ b/lighthub/systemconfigdata.h @@ -1,3 +1,4 @@ +#pragma once #define SYSCONF_OFFSET 0 #define EEPROM_offset_NotAlligned SYSCONF_OFFSET+sizeof(systemConfigData) #define SYSCONF_SIZE EEPROM_offsetJSON diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 669874c..908fb90 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -368,6 +368,21 @@ uint32_t ReadUniqueID( uint32_t * pdwUniqueID ) return *(uint32_t *)(IFLASH1_ADDR + 128); // dont remove: SAM defect workaround - MPU dont leave Unique Identifier mode until read flash out UID of range +#elif defined(ARDUINO_ARCH_STM32) +#define UID_BASE 0x1FFFF7E8 + +uint16_t *idBase0 = (uint16_t*)(UID_BASE); +uint16_t *idBase1 = (uint16_t*)(UID_BASE + 0x02); +uint32_t *idBase2 = (uint32_t*)(UID_BASE + 0x04); +uint32_t *idBase3 = (uint32_t*)(UID_BASE + 0x08); + +pdwUniqueID[0] = *idBase0; +pdwUniqueID[1] = *idBase1; +pdwUniqueID[2] = *idBase2; +pdwUniqueID[3] = *idBase3; + +return 1; + #else return 0; #endif diff --git a/platformio.ini b/platformio.ini index a1ee855..e06b7dc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -258,7 +258,7 @@ lib_deps = ;ArduinoMDNS ;ESPmDNS https://github.com/khoih-prog/TimerInterrupt_Generic.git - + https://github.com/sandeepmistry/arduino-CAN.git [env:due] ;Experimental target with universal Ethernet Library @@ -326,6 +326,8 @@ lib_deps = ArduinoMDNS https://github.com/khoih-prog/TimerInterrupt_Generic.git rweather/Crypto + collin80/can_common + collin80/due_can monitor_speed = 115200 [env:mega2560slim] @@ -977,6 +979,7 @@ lib_deps = ; ArduinoMDNS https://github.com/khoih-prog/TimerInterrupt_Generic.git ;https://github.com/anklimov/ModbusMaster + pazi88/STM32_CAN monitor_speed = 115200