mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-07-27 19:55:59 +00:00
223 lines
12 KiB
C++
223 lines
12 KiB
C++
|
|
|
|
#ifndef _MCP2518FD_H_
|
|
#define _MCP2518FD_H_
|
|
|
|
#include "mcp_can.h"
|
|
#include "mcp2518fd_can_def.h"
|
|
#include "mcp2518fd_can_dfs.h"
|
|
#include <Arduino.h>
|
|
#include <SPI.h>
|
|
|
|
#define MCP2518fd_OK (0)
|
|
#define MCP2518fd_FAIL (1)
|
|
#define CAN_OK (0)
|
|
#define CAN_FAILINIT (1)
|
|
#define CAN_FAILTX (2)
|
|
#define CAN_MSGAVAIL (3)
|
|
|
|
// *****************************************************************************
|
|
// *****************************************************************************
|
|
//! Reset DUT
|
|
|
|
// Code anchor for break points
|
|
#define Nop() asm("nop")
|
|
|
|
// Index to SPI channel
|
|
// Used when multiple MCP25xxFD are connected to the same SPI interface, but with different CS
|
|
#define SPI_DEFAULT_BUFFER_LENGTH 96
|
|
|
|
|
|
//extern SPIClass* pSPI;
|
|
#define spi_readwrite pSPI->transfer
|
|
#define spi_read() spi_readwrite(0x00)
|
|
#define spi_write(spi_val) spi_readwrite(spi_val)
|
|
#define SPI_BEGIN() pSPI->beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0))
|
|
#define SPI_END() pSPI->endTransaction();
|
|
|
|
// *****************************************************************************
|
|
// *****************************************************************************
|
|
// Section: Defines
|
|
|
|
#define CRCBASE 0xFFFF
|
|
#define CRCUPPER 1
|
|
|
|
|
|
//! Reverse order of bits in byte
|
|
const uint8_t BitReverseTable256[256] = {
|
|
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
|
|
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
|
|
0x04, 0x84, 0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0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
|
|
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
|
|
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
|
|
};
|
|
|
|
//! Look-up table for CRC calculation
|
|
const uint16_t crc16_table[256] = {
|
|
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
|
|
0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
|
|
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
|
|
0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
|
|
0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
|
|
0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
|
|
0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
|
|
0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
|
|
0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
|
|
0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
|
|
0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
|
|
0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
|
|
0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
|
|
0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
|
|
0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
|
|
0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
|
|
0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
|
|
0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
|
|
0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
|
|
0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
|
|
0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
|
|
0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
|
|
0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
|
|
0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
|
|
0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
|
|
0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
|
|
0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
|
|
0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
|
|
0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
|
|
0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
|
|
0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
|
|
0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
|
|
};
|
|
|
|
|
|
class SPIClass;
|
|
|
|
class mcp2518fd : public MCP_CAN
|
|
{
|
|
public:
|
|
mcp2518fd(byte _CS) : MCP_CAN(_CS), nReservedTx(0){};
|
|
|
|
public:
|
|
virtual void enableTxInterrupt(bool enable = true); // enable transmit interrupt
|
|
virtual void reserveTxBuffers(byte nTxBuf = 0)
|
|
{
|
|
nReservedTx = (nTxBuf < 3 ? nTxBuf : 3 - 1);
|
|
}
|
|
virtual byte getLastTxBuffer()
|
|
{
|
|
return 3 - 1; // read index of last tx buffer
|
|
}
|
|
virtual byte begin(byte speedset,const byte clockset = CAN_SYSCLK_40M); // init can
|
|
virtual byte init_Mask(byte num, byte ext, unsigned long ulData);
|
|
virtual byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
|
|
virtual void setSleepWakeup(const byte enable);
|
|
virtual byte sleep();
|
|
virtual byte wake();
|
|
virtual byte setMode(const byte opMode);
|
|
virtual byte getMode();
|
|
virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent = true);
|
|
virtual byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf, bool wait_sent = true);
|
|
virtual byte readMsgBuf(byte *len, byte *buf);
|
|
virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf);
|
|
virtual byte checkReceive(void);
|
|
virtual byte checkError(void);
|
|
virtual unsigned long getCanId(void);
|
|
virtual byte isRemoteRequest(void);
|
|
virtual byte isExtendedFrame(void);
|
|
virtual byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf); // read buf with object ID
|
|
virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, byte iTxBuf = 0xff);
|
|
virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte *buf);
|
|
virtual void clearBufferTransmitIfFlags(byte flags = 0);
|
|
virtual byte readRxTxStatus(void);
|
|
virtual byte checkClearRxStatus(byte *status);
|
|
virtual byte checkClearTxStatus(byte *status, byte iTxBuf = 0xff);
|
|
virtual bool mcpPinMode(const byte pin, const byte mode);
|
|
virtual bool mcpDigitalWrite(const byte pin, const byte mode);
|
|
virtual byte mcpDigitalRead(const byte pin);
|
|
|
|
private:
|
|
byte mcp2518fd_readMsgBufID(volatile byte *len, volatile byte *buf);
|
|
byte mcp2518fd_sendMsgBuf(const byte* buf,byte len,unsigned long id, byte ext,bool wait_sent); // send buf
|
|
byte mcp2518fd_sendMsg(const byte* buf,byte len,unsigned long id, byte ext,bool wait_sent);
|
|
int8_t mcp2518fd_receiveMsg();
|
|
|
|
private:
|
|
uint8_t mcp2518fd_init(byte speedset,const byte clock); // mcp2518fdinit
|
|
int8_t mcp2518fd_reset(void); //reset mcp2518fd
|
|
int8_t mcp2518fd_EccEnable(void); //Section: ECC
|
|
int8_t mcp2518fd_RamInit(uint8_t d);
|
|
int8_t mcp2518fd_ConfigureObjectReset(CAN_CONFIG* config);
|
|
int8_t mcp2518fd_Configure(CAN_CONFIG* config);
|
|
int8_t mcp2518fd_TransmitChannelConfigureObjectReset(CAN_TX_FIFO_CONFIG* config);
|
|
int8_t mcp2518fd_TransmitChannelConfigure(CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_CONFIG* config);
|
|
int8_t mcp2518fd_ReceiveChannelConfigureObjectReset(CAN_RX_FIFO_CONFIG* config);
|
|
int8_t mcp2518fd_ReceiveChannelConfigure(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_CONFIG* config);
|
|
int8_t mcp2518fd_FilterObjectConfigure(CAN_FILTER filter, CAN_FILTEROBJ_ID* id);
|
|
int8_t mcp2518fd_FilterMaskConfigure(CAN_FILTER filter, CAN_MASKOBJ_ID* mask);
|
|
int8_t mcp2518fd_FilterToFifoLink(CAN_FILTER filter, CAN_FIFO_CHANNEL channel, bool enable);
|
|
int8_t mcp2518fd_BitTimeConfigure(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode,CAN_SYSCLK_SPEED clk);
|
|
int8_t mcp2518fd_GpioModeConfigure(GPIO_PIN_MODE gpio0, GPIO_PIN_MODE gpio1);
|
|
int8_t mcp2518fd_TransmitChannelEventEnable(CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT flags);
|
|
int8_t mcp2518fd_ReceiveChannelEventEnable(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT flags);
|
|
int8_t mcp2518fd_ModuleEventEnable(CAN_MODULE_EVENT flags);
|
|
int8_t mcp2518fd_OperationModeSelect(CAN_OPERATION_MODE opMode);
|
|
CAN_OPERATION_MODE mcp2518fd_OperationModeGet();
|
|
void mcp2518fd_TransmitMessageQueue();
|
|
int8_t mcp2518fd_TransmitChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT* flags);
|
|
int8_t mcp2518fd_ErrorCountStateGet(uint8_t* tec, uint8_t* rec, CAN_ERROR_STATE* flags);
|
|
int8_t mcp2518fd_TransmitChannelLoad(CAN_FIFO_CHANNEL channel, CAN_TX_MSGOBJ* txObj,
|
|
uint8_t *txd, uint32_t txdNumBytes, bool flush);
|
|
int8_t mcp2518fd_ReceiveChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT* flags);
|
|
int8_t mcp2518fd_ReceiveMessageGet(CAN_FIFO_CHANNEL channel, CAN_RX_MSGOBJ* rxObj,uint8_t *rxd, uint8_t nBytes);
|
|
int8_t mcp2518fd_ReceiveChannelUpdate(CAN_FIFO_CHANNEL channel);
|
|
int8_t mcp2518fd_TransmitChannelUpdate(CAN_FIFO_CHANNEL channel, bool flush);
|
|
int8_t mcp2518fd_ReceiveChannelStatusGet(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_STATUS* status);
|
|
int8_t mcp2518fd_ErrorStateGet(CAN_ERROR_STATE* flags);
|
|
int8_t mcp2518fd_ModuleEventRxCodeGet(CAN_RXCODE* rxCode);
|
|
int8_t mcp2518fd_ModuleEventTxCodeGet(CAN_TXCODE* txCode);
|
|
|
|
int8_t mcp2518fd_LowPowerModeEnable();
|
|
int8_t mcp2518fd_LowPowerModeDisable();
|
|
|
|
int8_t mcp2518fd_ReadByte(uint16_t address, uint8_t *rxd);
|
|
int8_t mcp2518fd_WriteByte(uint16_t address, uint8_t txd);
|
|
int8_t mcp2518fd_ReadWord(uint16_t address,uint32_t *rxd);
|
|
int8_t mcp2518fd_WriteWord(uint16_t address,uint32_t txd);
|
|
int8_t mcp2518fd_ReadHalfWord(uint16_t address,uint16_t *rxd);
|
|
int8_t mcp2518fd_WriteHalfWord(uint16_t address,uint16_t txd);
|
|
int8_t mcp2518fd_ReadByteArray(uint16_t address,uint8_t *rxd, uint16_t nBytes);
|
|
int8_t mcp2518fd_WriteByteArray(uint16_t address,uint8_t *txd, uint16_t nBytes);
|
|
int8_t mcp2518fd_WriteByteSafe(uint16_t address,uint8_t txd);
|
|
int8_t mcp2518fd_WriteWordSafe(uint16_t address,uint32_t txd);
|
|
int8_t mcp2518fd_ReadByteArrayWithCRC(uint16_t address,uint8_t *rxd, uint16_t nBytes, bool fromRam, bool* crcIsCorrect);
|
|
int8_t mcp2518fd_WriteByteArrayWithCRC(uint16_t address,uint8_t *txd, uint16_t nBytes, bool fromRam);
|
|
int8_t mcp2518fd_ReadWordArray(uint16_t address,uint32_t *rxd, uint16_t nWords);
|
|
int8_t mcp2518fd_WriteWordArray(uint16_t address,uint32_t *txd, uint16_t nWords);
|
|
|
|
private:
|
|
int8_t mcp2518fd_BitTimeConfigureNominal40MHz(CAN_BITTIME_SETUP bitTime);
|
|
int8_t mcp2518fd_BitTimeConfigureData40MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode);
|
|
int8_t mcp2518fd_BitTimeConfigureNominal20MHz(CAN_BITTIME_SETUP bitTime);
|
|
int8_t mcp2518fd_BitTimeConfigureData20MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode);
|
|
int8_t mcp2518fd_BitTimeConfigureNominal10MHz(CAN_BITTIME_SETUP bitTime);
|
|
int8_t mcp2518fd_BitTimeConfigureData10MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode);
|
|
|
|
byte ext_flg; // identifier xxxID
|
|
// either extended (the 29 LSB) or standard (the 11 LSB)
|
|
unsigned long can_id; // can id
|
|
byte rtr; // rtr
|
|
byte nReservedTx; // Count of tx buffers for reserved send
|
|
CAN_OPERATION_MODE mcpMode = CAN_CLASSIC_MODE; // Current controller mode
|
|
|
|
};
|
|
#endif |