Skip to content

Commit

Permalink
Update Arduino driver
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed May 20, 2024
1 parent d7f4914 commit 5197166
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 409 deletions.
118 changes: 0 additions & 118 deletions inc/queue.h

This file was deleted.

23 changes: 19 additions & 4 deletions inc/xcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ extern "C" {

#if XCP_TRANSPORT_LAYER == XCP_ON_CAN

#if ((XCP_ENABLE_CAN_GET_SLAVE_ID == XCP_ON) || (XCP_ENABLE_CAN_GET_DAQ_ID == XCP_ON) || \
(XCP_ENABLE_CAN_SET_DAQ_ID == XCP_ON))
#if ((XCP_ENABLE_CAN_GET_SLAVE_ID == XCP_ON) || (XCP_ENABLE_CAN_GET_DAQ_ID == XCP_ON) || (XCP_ENABLE_CAN_SET_DAQ_ID == XCP_ON))
#define XCP_ENABLE_TRANSPORT_LAYER_CMD (XCP_ON)
#endif

Expand Down Expand Up @@ -270,7 +269,7 @@ extern "C" {
#define XCP_MIN_ST_PGM (0)
#endif /* XCP_MIN_ST_PGM */

#define XCP_DOWNLOAD_PAYLOAD_LENGTH ((XCP_MAX_CTO) - 2)
#define XCP_DOWNLOAD_PAYLOAD_LENGTH ((XCP_MAX_CTO)-2)

/*
* Packet Identifiers.
Expand Down Expand Up @@ -497,7 +496,7 @@ extern "C" {
/*
** XCPonCAN specific function-like macros.
*/
#define XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(i) (((i) & XCP_ON_CAN_EXT_IDENTIFIER) == XCP_ON_CAN_EXT_IDENTIFIER)
#define XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(i) (((i)&XCP_ON_CAN_EXT_IDENTIFIER) == XCP_ON_CAN_EXT_IDENTIFIER)
#define XCP_ON_CAN_STRIP_IDENTIFIER(i) ((i) & (~XCP_ON_CAN_EXT_IDENTIFIER))

/*
Expand All @@ -518,6 +517,22 @@ extern "C" {

#define XCP_ETH_HEADER_SIZE (4)

/*
* CAN Interfaces.
*/
#define XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD (0x01)
#define XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD (0x02)
#define XCP_CAN_IF_MKR_ZERO_CAN_SHIELD (0x03)

// Set defaults for MCP25XX CAN controllers.
#if !defined(XCP_CAN_IF_MCP25XX_PIN_CS)
#define XCP_CAN_IF_MCP25XX_PIN_CS UINT8(9)
#endif

#if !defined(XCP_CAN_IF_MCP25XX_PIN_INT)
#define XCP_CAN_IF_MCP25XX_PIN_INT UINT8(2)
#endif

/*
** Bounds-checking macros.
*/
Expand Down
112 changes: 85 additions & 27 deletions src/tl/can/arduino_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@

#include "xcp_config.h"

/*!!! START-INCLUDE-SECTION !!!*/
#include "queue.h"
/*!!! END-INCLUDE-SECTION !!!*/

#if XCP_TRANSPORT_LAYER == XCP_ON_CAN

#include <CAN.h>
#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)
#include <SPI.h>
#include <can-serial.h>
#include <mcp2515_can.h>
#include <mcp2515_can_dfs.h>
#include <mcp2518fd_can.h>
#include <mcp2518fd_can_dfs.h>
#include <mcp_can.h>
#else
// XCP_CAN_IF_MKR_ZERO_CAN_SHIELD
#include <CAN.h>
#endif

#include <stdint.h>

uint32_t filter_mask(uint32_t identifier);
extern const uint32_t Xcp_DaqIDs[];
extern const uint16_t Xcp_DaqIDCount;

Expand All @@ -42,10 +51,24 @@ static const char XCP_MAGIC[] = "XCP";
static bool connected = false;
static volatile bool XcpTl_FrameReceived{ false };

static char XcpTl_Buffer[64];
static int XcpTl_Dlc = 0;
static unsigned char XcpTl_Buffer[64];
static unsigned char XcpTl_Dlc = 0;
static int XcpTl_ID = 0;

#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)
static void on_receive();
#else
static void on_receive(int packetSize);
#endif

#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)
#if XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD
mcp2515_can CAN(XCP_CAN_IF_MCP25XX_PIN_CS);
#elif XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD
mcp2518fd CAN(XCP_CAN_IF_MCP25XX_PIN_CS);
#endif
#else
#endif

void XcpTl_Init(void) {
Serial.begin(9600);
Expand All @@ -54,14 +77,38 @@ void XcpTl_Init(void) {

Serial.println("Starting Blueparrot XCP...");

// start the CAN bus at 500 kbps
#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)
attachInterrupt(digitalPinToInterrupt(XCP_CAN_IF_MCP25XX_PIN_INT), on_receive, FALLING);

while (CAN_OK != CAN.begin(XCP_ON_CAN_FREQ)) {
Serial.println("CAN init fail, retry...");
delay(100);
}

Serial.println("CAN init OK!");
// #if 0
CAN.init_Mask(0, XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_INBOUND_IDENTIFIER), filter_mask(XCP_ON_CAN_INBOUND_IDENTIFIER));
CAN.init_Mask(
1, XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_BROADCAST_IDENTIFIER), filter_mask(XCP_ON_CAN_BROADCAST_IDENTIFIER)
);
// #endif
// CAN.init_Mask(0, 0, 0);
// CAN.init_Mask(1, 0, 0);

CAN.init_Filt(0, XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_INBOUND_IDENTIFIER), XCP_ON_CAN_INBOUND_IDENTIFIER);
CAN.init_Filt(1, XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_BROADCAST_IDENTIFIER), XCP_ON_CAN_BROADCAST_IDENTIFIER);
#else

// XCP_CAN_IF_MKR_ZERO_CAN_SHIELD

if (!CAN.begin(XCP_ON_CAN_FREQ)) {
Serial.println("Starting CAN failed!");
while (1) {
}
}
CAN.setTimeout(1000);
CAN.onReceive(on_receive);
#endif
}

void XcpTl_DeInit(void) {
Expand All @@ -83,40 +130,36 @@ void XcpTl_MainFunction(void) {

// ARDUINO_API_VERSION

#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)
static void on_receive() {
#else
static void on_receive(int packetSize) {
uint_least8_t idx = 0;
XcpTl_Dlc = packetSize;
#endif

XcpTl_Dlc = packetSize;
XcpTl_FrameReceived = true;
#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)

// received a packet
// Serial.print("Received ");
if (CAN.checkReceive() == CAN_MSGAVAIL) {
XcpTl_FrameReceived = true;
CAN.readMsgBuf(&XcpTl_Dlc, static_cast<byte *>(XcpTl_Buffer));
// XcpTl_ID
// canBusPacket.id = CAN.getCanId();
}

#else
XcpTl_FrameReceived = true;
if (CAN.packetExtended()) {
// Serial.print("extended ");
}

if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
// Serial.print("RTR ");
}

// Serial.print("packet with id 0x");
// Serial.print(CAN.packetId(), HEX);

if (CAN.packetRtr()) {
// Serial.print(" and requested length ");
// Serial.println(CAN.packetDlc());
} else {
// Serial.print(" and length ");
// Serial.println(packetSize);

// only print packet data for non-RTR packets
while (CAN.available()) {
XcpTl_Buffer[idx++] = CAN.read();
}
}
// Serial.println();
#endif
}

void XcpTl_RxHandler(void) {
Expand All @@ -141,6 +184,12 @@ void XcpTl_Send(uint8_t const *buf, uint16_t len) {

can_id = XCP_ON_CAN_STRIP_IDENTIFIER(XCP_ON_CAN_OUTBOUND_IDENTIFIER);

#if (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_SHIELD) || (XCP_CAN_INTERFACE == XCP_CAN_IF_SEED_STUDIO_CAN_FD_SHIELD)

CAN.sendMsgBuf(can_id, XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_OUTBOUND_IDENTIFIER), len, buf);

#else

if (XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_OUTBOUND_IDENTIFIER)) {
CAN.beginExtendedPacket(can_id, len);
} else {
Expand All @@ -149,6 +198,15 @@ void XcpTl_Send(uint8_t const *buf, uint16_t len) {

CAN.write(buf, len);
CAN.endPacket();
#endif
}

uint32_t filter_mask(uint32_t identifier) {
if (XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(identifier)) {
return (2 << (29 - 1)) - 1;
} else {
return (2 << (11 - 1)) - 1;
}
}

void XcpTl_SaveConnection(void) {
Expand Down
Loading

0 comments on commit 5197166

Please sign in to comment.