Skip to content

Commit

Permalink
Replaced CAN library with built in one from ESP-IDF
Browse files Browse the repository at this point in the history
  • Loading branch information
morcibacsi committed Jun 20, 2021
1 parent 56be589 commit 10f7d8d
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 116 deletions.
4 changes: 2 additions & 2 deletions PSAVanCanBridge/PSAVanCanBridgeMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion PSAVanCanBridge/src/Can/AbstractCanMessageSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#ifndef _AbstractCanMessageSender_h
#define _AbstractCanMessageSender_h

#include <stdint.h>

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;
Expand Down
109 changes: 0 additions & 109 deletions PSAVanCanBridge/src/Can/CanMessageSenderEsp32Arduino.h

This file was deleted.

122 changes: 122 additions & 0 deletions PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions PSAVanCanBridge/src/Can/CanMessageSenderEsp32Idf.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 10f7d8d

Please sign in to comment.