From 10f7d8d2aaa22776533e91c7088404007bdda6e5 Mon Sep 17 00:00:00 2001 From: "peter.pinter" Date: Sun, 20 Jun 2021 19:37:49 +0200 Subject: [PATCH] Replaced CAN library with built in one from ESP-IDF --- PSAVanCanBridge/PSAVanCanBridgeMain.cpp | 4 +- .../src/Can/AbstractCanMessageSender.h | 4 +- .../src/Can/CanMessageSenderEsp32Arduino.h | 109 ---------------- .../src/Can/CanMessageSenderEsp32Idf.cpp | 122 ++++++++++++++++++ .../src/Can/CanMessageSenderEsp32Idf.h | 34 +++++ platformio.ini | 2 - readme.md | 2 - 7 files changed, 161 insertions(+), 116 deletions(-) delete mode 100644 PSAVanCanBridge/src/Can/CanMessageSenderEsp32Arduino.h create mode 100644 PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.cpp create mode 100644 PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.h diff --git a/PSAVanCanBridge/PSAVanCanBridgeMain.cpp b/PSAVanCanBridge/PSAVanCanBridgeMain.cpp index 0d4247b..f8742d5 100644 --- a/PSAVanCanBridge/PSAVanCanBridgeMain.cpp +++ b/PSAVanCanBridge/PSAVanCanBridgeMain.cpp @@ -13,7 +13,7 @@ #include "src/SerialPort/HardwareSerialAbs.h" #endif -#include "src/Can/CanMessageSenderEsp32Arduino.h" +#include "src/Can/CanMessageSenderEsp32Idf.h" #include "src/Van/VanMessageReaderEsp32Rmt.h" #include "src/Helpers/VinFlashStorageEsp32.h" #include "src/Helpers/GetDeviceInfoEsp32.h" @@ -291,7 +291,7 @@ void setup() } //CANInterface = new CanMessageSender(CAN_RX_PIN, CAN_TX_PIN); - CANInterface = new CanMessageSenderEsp32Arduino(CAN_RX_PIN, CAN_TX_PIN, true); + CANInterface = new CanMessageSenderEsp32Idf(CAN_RX_PIN, CAN_TX_PIN, false, serialPort); CANInterface->Init(); #if POPUP_HANDLER == 1 diff --git a/PSAVanCanBridge/src/Can/AbstractCanMessageSender.h b/PSAVanCanBridge/src/Can/AbstractCanMessageSender.h index 1e60a12..364c81e 100644 --- a/PSAVanCanBridge/src/Can/AbstractCanMessageSender.h +++ b/PSAVanCanBridge/src/Can/AbstractCanMessageSender.h @@ -3,10 +3,12 @@ #ifndef _AbstractCanMessageSender_h #define _AbstractCanMessageSender_h +#include + class AbstractCanMessageSender { public: virtual void Init() = 0; // The '= 0;' makes whole class "pure virtual" - virtual void SendMessage(uint16_t canId, byte ext, byte sizeOfByteArray, unsigned char *byteArray) = 0; // The '= 0;' makes whole class "pure virtual" + virtual uint8_t SendMessage(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, uint8_t *byteArray) = 0; // The '= 0;' makes whole class "pure virtual" virtual void ReadMessage(uint16_t* canId, uint8_t* len, uint8_t* buf) = 0; //virtual unsigned long GetCanId(void) = 0; //virtual unsigned long Start(byte speedset, const byte clockset) = 0; diff --git a/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Arduino.h b/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Arduino.h deleted file mode 100644 index e6e18b1..0000000 --- a/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Arduino.h +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once - -#ifndef _CanMessageSenderEsp32Arduino_h -#define _CanMessageSenderEsp32Arduino_h - -#include -#include -#include "AbstractCanMessageSender.h" - -class CanMessageSenderEsp32Arduino : public AbstractCanMessageSender -{ -private: - const int rx_queue_size = 10; - - uint16_t _prevCanId; - unsigned long _prevCanIdTime; - CAN_device_t _CAN_cfg; - bool _enableThrottling; - - SemaphoreHandle_t canSemaphore; - -public: - CanMessageSenderEsp32Arduino(uint8_t rxPin, uint8_t txPin, bool enableThrottling) - { - _enableThrottling = enableThrottling; - _prevCanId = 0; - _CAN_cfg.speed = CAN_SPEED_125KBPS; - _CAN_cfg.tx_pin_id = (gpio_num_t)txPin; - _CAN_cfg.rx_pin_id = (gpio_num_t)rxPin; - - canSemaphore = xSemaphoreCreateMutex(); - } - - virtual void Init() - { - _CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); - ESP32Can.CANInit(_CAN_cfg); - } - - virtual void SendMessage(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, unsigned char *byteArray) - { - CAN_frame_t tx_frame; - tx_frame.FIR.B.FF = CAN_frame_std; - tx_frame.MsgID = canId; - tx_frame.FIR.B.DLC = sizeOfByteArray; - - for (size_t i = 0; i < sizeOfByteArray; i++) - { - tx_frame.data.u8[i] = byteArray[i]; - } - - xSemaphoreTake(canSemaphore, portMAX_DELAY); - - //workaround to avoid weird errors on screen - if (_enableThrottling) - { - unsigned long currentTime = millis(); - if (_prevCanId != canId) - { - if (currentTime - _prevCanIdTime < 10) - { - delay(10); - } - _prevCanIdTime = currentTime; - _prevCanId = canId; - } - } - - ESP32Can.CANWriteFrame(&tx_frame); - xSemaphoreGive(canSemaphore); - } - - virtual void ReadMessage(uint16_t *canId, uint8_t *len, uint8_t *buf) - { - CAN_frame_t rx_frame; - - if (xQueueReceive(_CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) - { - - if (rx_frame.FIR.B.FF == CAN_frame_std) - { - //printf("New standard frame"); - } - else - { - //printf("New extended frame"); - } - - if (rx_frame.FIR.B.RTR == CAN_RTR) - { - //printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); - } - else - { - *canId = rx_frame.MsgID; - *len = rx_frame.FIR.B.DLC; - //printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); - for (int i = 0; i < rx_frame.FIR.B.DLC; i++) - { - buf[i] = rx_frame.data.u8[i]; - //printf("0x%02X ", rx_frame.data.u8[i]); - } - //printf("\n"); - } - } - } -}; - -#endif diff --git a/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.cpp b/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.cpp new file mode 100644 index 0000000..d285282 --- /dev/null +++ b/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.cpp @@ -0,0 +1,122 @@ +#include "CanMessageSenderEsp32Idf.h" +#include "driver/gpio.h" +#include "driver/can.h" + +void CanMessageSenderEsp32Idf::PrintToSerial(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, uint8_t *byteArray) +{ + // + /* + if (canId != 0x1e3) + { + return; + } + //*/ + char tmp[3]; + + _serialPort->print(canId, HEX); + _serialPort->print(",1,"); + _serialPort->print(sizeOfByteArray, DEC); + _serialPort->print(","); + + for (size_t i = 0; i < sizeOfByteArray; i++) + { + snprintf(tmp, 3, "%02X", byteArray[i]); + if (i != sizeOfByteArray - 1) + { + _serialPort->print(tmp); + _serialPort->print(""); + } + } + _serialPort->println(tmp); +} + +CanMessageSenderEsp32Idf::CanMessageSenderEsp32Idf(uint8_t rxPin, uint8_t txPin, bool enableThrottling, AbsSer *serialPort) +{ + _serialPort = serialPort; + _enableThrottling = enableThrottling; + _prevCanId = 0; + + can_general_config_t g_config = {.mode = CAN_MODE_NORMAL, + .tx_io = (gpio_num_t)txPin, .rx_io = (gpio_num_t)rxPin, + .clkout_io = CAN_IO_UNUSED, .bus_off_io = CAN_IO_UNUSED, + .tx_queue_len = 10, .rx_queue_len = 10, + .alerts_enabled = CAN_ALERT_NONE, .clkout_divider = 0, + .intr_flags = ESP_INTR_FLAG_LEVEL1}; + + can_timing_config_t t_config = CAN_TIMING_CONFIG_125KBITS(); + can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL(); + + esp_err_t result = can_driver_install(&g_config, &t_config, &f_config); + + canSemaphore = xSemaphoreCreateMutex(); +} + +void CanMessageSenderEsp32Idf::Init() +{ + esp_err_t result = can_start(); +} + +uint8_t CanMessageSenderEsp32Idf::SendMessage(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, uint8_t *byteArray) +{ + //PrintToSerial(canId, ext, sizeOfByteArray, byteArray); + + can_message_t message; + message.identifier = canId; + message.flags = CAN_MSG_FLAG_SS; + message.data_length_code = sizeOfByteArray; + for (int i = 0; i < sizeOfByteArray; i++) { + message.data[i] = byteArray[i]; + } + + //workaround to avoid weird errors on screen + if (_enableThrottling) + { + unsigned long currentTime = millis(); + if (_prevCanId != canId) + { + unsigned long delayFromLastTransmission = currentTime - _prevCanIdTime; + if (delayFromLastTransmission < 15) + { + delay(15 - delayFromLastTransmission); + } + _prevCanIdTime = currentTime; + _prevCanId = canId; + } + } + + uint8_t result = -1; + + if (xSemaphoreTake(canSemaphore, portMAX_DELAY) == pdTRUE) + { + if (can_transmit(&message, pdMS_TO_TICKS(10)) == ESP_OK) { + //_serialPort->println("Message queued for transmission"); + result = 0; + } else { + //_serialPort->println("Failed to queue message for transmission"); + //return -1; + result = -1; + } + xSemaphoreGive(canSemaphore); + } + return result; +} + +void CanMessageSenderEsp32Idf::ReadMessage(uint16_t *canId, uint8_t *len, uint8_t *buf) +{ + can_message_t message; + if (can_receive(&message, pdMS_TO_TICKS(10)) == ESP_OK) { + if (message.flags == CAN_MSG_FLAG_NONE || message.flags == CAN_MSG_FLAG_SS) + { + *canId = message.identifier; + *len = message.data_length_code; + for (int i = 0; i < message.data_length_code; i++) + { + buf[i] = message.data[i]; + } + //PrintToSerial(*canId, 0, *len, buf); + } + } else { + //printf("Failed to receive message\n"); + return; + } +} diff --git a/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.h b/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.h new file mode 100644 index 0000000..a9750f7 --- /dev/null +++ b/PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.h @@ -0,0 +1,34 @@ +#pragma once + +#ifndef _CanMessageSenderEsp32Idf_h + #define _CanMessageSenderEsp32Idf_h + +#include "AbstractCanMessageSender.h" +#include "../SerialPort/AbstractSerial.h" + +class CanMessageSenderEsp32Idf : public AbstractCanMessageSender +{ +private: + const int rx_queue_size = 10; + + uint16_t _prevCanId; + unsigned long _prevCanIdTime; + bool _enableThrottling; + + SemaphoreHandle_t canSemaphore; + + AbsSer *_serialPort; + + void PrintToSerial(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, uint8_t *byteArray); + +public: + CanMessageSenderEsp32Idf(uint8_t rxPin, uint8_t txPin, bool enableThrottling, AbsSer *serialPort); + + void Init() override; + + uint8_t SendMessage(uint16_t canId, uint8_t ext, uint8_t sizeOfByteArray, uint8_t *byteArray) override; + + virtual void ReadMessage(uint16_t *canId, uint8_t *len, uint8_t *buf) override; +}; + +#endif diff --git a/platformio.ini b/platformio.ini index a19ad09..8660071 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,5 +26,3 @@ lib_deps = # Accept new functionality in a backwards compatible manner and patches morcibacsi/ESP32 RMT Peripheral VAN bus reader library @ ^1.0.0 morcibacsi/Atmel TSS463C VAN bus Datalink Controller library @ ^2.0.2 - - https://github.com/morcibacsi/ESP32-Arduino-CAN#fix/can_cfg_to_constructor diff --git a/readme.md b/readme.md index 2e99f1b..9f2c1ef 100644 --- a/readme.md +++ b/readme.md @@ -125,7 +125,6 @@ Follow these steps to build the project: - C:\Users\YOUR_NAME\Documents\Arduino\libraries\esp32_arduino_rmt_van_rx\ - C:\Users\YOUR_NAME\Documents\Arduino\libraries\tss463_van\ - C:\Users\YOUR_NAME\Documents\Arduino\libraries\Queue\ - - C:\Users\YOUR_NAME\Documents\Arduino\libraries\esp32_arduino_can\ - Extract the contents of the zip file - Open the empty **PSAVanCanBridge\PSAVanCanBridge.ino** file from the Arduino IDE *(do not rename any file or whatsoever)* - Select ESP32 Dev module from Tools\Board menu @@ -146,7 +145,6 @@ You can also open the project from PlatformIO. It will download the necessary li [lib_abstract_serial]: https://github.com/computergeek125/arduino-abstract-serial [lib_tss463c_van]: https://github.com/morcibacsi/arduino_tss463_van [lib_esp32_van_rx]: https://github.com/morcibacsi/esp32_rmt_van_rx -[lib_esp32_can]: https://github.com/morcibacsi/ESP32-Arduino-CAN/tree/fix/can_cfg_to_constructor [lib_queue]: https://github.com/SMFSW/Queue [psavancanbridgehw]: https://github.com/morcibacsi/PSAVanCanBridgeHW [history]: https://github.com/morcibacsi/PSAVanCanBridge/wiki/History