1st twai version with send/receive

This commit is contained in:
andreas 2023-08-24 17:32:13 +02:00
parent f025fddc71
commit 7baa5688ef
3 changed files with 119 additions and 4 deletions

View File

@ -0,0 +1,81 @@
#include "Nmea2kTwai.h"
#include "driver/gpio.h"
#include "driver/twai.h"
#include "GwLog.h"
#define TWAI_DEBUG 1
#if TWAI_DEBUG == 1
#define TWAI_LOG(...) LOG_DEBUG(__VA_ARGS__)
#define TWAI_LDEBUG(...)
#else
#if TWAI_DEBUG == 2
#define TWAI_LOG(...) LOG_DEBUG(__VA_ARGS__)
#define TWA_LDEBUG(...) LOG_DEBUG(__VA_ARGS__)
#else
#define TWAI_LOG(...)
#define TWAI_LDEBUG(...)
#endif
#endif
Nmea2kTwai::Nmea2kTwai(gpio_num_t _TxPin, gpio_num_t _RxPin,GwLog *l):
tNMEA2000(),RxPin(_RxPin),TxPin(_TxPin),logger(l)
{
}
bool Nmea2kTwai::CANSendFrame(unsigned long id, unsigned char len, const unsigned char *buf, bool wait_sent)
{
twai_message_t message;
message.identifier = id;
message.extd = 1;
message.data_length_code = len;
memcpy(message.data,buf,len);
esp_err_t rt=twai_transmit(&message,0);
if (rt != ESP_OK){
TWAI_LOG(GwLog::DEBUG,"twai transmit for %ld failed: %d",id,(int)rt);
return false;
}
TWAI_LDEBUG(GwLog::DEBUG,"twai transmit id %ld, len %d",id,(int)len);
return true;
}
bool Nmea2kTwai::CANOpen()
{
esp_err_t rt=twai_start();
if (rt != ESP_OK){
LOG_DEBUG(GwLog::ERROR,"CANOpen failed: %d",(int)rt);
return false;
}
else{
LOG_DEBUG(GwLog::LOG,"CANOpen ok");
}
return true;
}
bool Nmea2kTwai::CANGetFrame(unsigned long &id, unsigned char &len, unsigned char *buf)
{
twai_message_t message;
esp_err_t rt=twai_receive(&message,0);
if (rt != ESP_OK){
return false;
}
id=message.identifier;
len=message.data_length_code;
TWAI_LDEBUG(GwLog::DEBUG,"twai rcv id=%ld,len=%d, ext=%d",message.identifier,message.data_length_code,message.extd);
memcpy(buf,message.data,message.data_length_code);
return true;
}
// This will be called on Open() before any other initialization. Inherit this, if buffers can be set for the driver
// and you want to change size of library send frame buffer size. See e.g. NMEA2000_teensy.cpp.
void Nmea2kTwai::InitCANFrameBuffers()
{
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TxPin,RxPin, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
esp_err_t rt=twai_driver_install(&g_config, &t_config, &f_config);
if (rt == ESP_OK) {
LOG_DEBUG(GwLog::LOG,"twai driver initialzed, rx=%d,tx=%d",(int)RxPin,(int)TxPin);
}
else{
LOG_DEBUG(GwLog::ERROR,"twai driver init failed: %d",(int)rt);
}
tNMEA2000::InitCANFrameBuffers();
}

View File

@ -0,0 +1,27 @@
#ifndef _NMEA2KTWAI_H
#define _NMEA2KTWAI_H
#include "NMEA2000.h"
#include "GwLog.h"
class Nmea2kTwai : public tNMEA2000{
public:
Nmea2kTwai(gpio_num_t _TxPin, gpio_num_t _RxPin,GwLog *logger);
protected:
// Virtual functions for different interfaces. Currently there are own classes
// for Arduino due internal CAN (NMEA2000_due), external MCP2515 SPI CAN bus controller (NMEA2000_mcp),
// Teensy FlexCAN (NMEA2000_Teensy), NMEA2000_avr for AVR, NMEA2000_mbed for MBED and NMEA2000_socketCAN for e.g. RPi.
virtual bool CANSendFrame(unsigned long id, unsigned char len, const unsigned char *buf, bool wait_sent=true);
virtual bool CANOpen();
virtual bool CANGetFrame(unsigned long &id, unsigned char &len, unsigned char *buf);
// This will be called on Open() before any other initialization. Inherit this, if buffers can be set for the driver
// and you want to change size of library send frame buffer size. See e.g. NMEA2000_teensy.cpp.
virtual void InitCANFrameBuffers();
private:
gpio_num_t TxPin;
gpio_num_t RxPin;
GwLog *logger;
};
#endif

View File

@ -12,6 +12,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "GwAppInfo.h" #include "GwAppInfo.h"
#define USE_TWAI
// #define GW_MESSAGE_DEBUG_ENABLED // #define GW_MESSAGE_DEBUG_ENABLED
//#define FALLBACK_SERIAL //#define FALLBACK_SERIAL
//#define CAN_ESP_DEBUG //#define CAN_ESP_DEBUG
@ -65,17 +66,14 @@ const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
#include "GwTcpClient.h" #include "GwTcpClient.h"
#include "GwChannel.h" #include "GwChannel.h"
#include "GwChannelList.h" #include "GwChannelList.h"
#include <NMEA2000_esp32.h> // forked from https://github.com/ttlappalainen/NMEA2000_esp32
#ifdef FALLBACK_SERIAL #ifdef FALLBACK_SERIAL
#ifdef CAN_ESP_DEBUG #ifdef CAN_ESP_DEBUG
#define CDBS &Serial #define CDBS &Serial
#else #else
#define CDBS NULL #define CDBS NULL
#endif #endif
tNMEA2000 &NMEA2000=*(new tNMEA2000_esp32(ESP32_CAN_TX_PIN,ESP32_CAN_RX_PIN,CDBS));
#else #else
tNMEA2000 &NMEA2000=*(new tNMEA2000_esp32()); #define CDBS NULL
#endif #endif
@ -111,6 +109,15 @@ typedef std::map<String,String> StringMap;
GwLog logger(LOGLEVEL,NULL); GwLog logger(LOGLEVEL,NULL);
GwConfigHandler config(&logger); GwConfigHandler config(&logger);
#ifndef USE_TWAI
#include <NMEA2000_esp32.h> // forked from https://github.com/ttlappalainen/NMEA2000_esp32
tNMEA2000 &NMEA2000=*(new tNMEA2000_esp32(ESP32_CAN_TX_PIN,ESP32_CAN_RX_PIN,CDBS));
#else
#include "Nmea2kTwai.h"
tNMEA2000 &NMEA2000=*(new Nmea2kTwai(ESP32_CAN_TX_PIN,ESP32_CAN_RX_PIN,&logger));
#endif
#ifdef GWBUTTON_PIN #ifdef GWBUTTON_PIN
bool fixedApPass=false; bool fixedApPass=false;
#else #else