CAN Bus Soldered Arduino Library 1.0.0
This is a library for the MCP2518 Can Bus Breakout by Soldered
Loading...
Searching...
No Matches
mcp_can_fd.h
Go to the documentation of this file.
1#ifndef _MCP_CAN_H_
2#define _MCP_CAN_H_
3
4#include <Arduino.h>
5#include <SPI.h>
6#include <inttypes.h>
7
8#define CAN_OK (0)
9#define CAN_FAILINIT (1)
10#define CAN_FAILTX (2)
11#define CAN_MSGAVAIL (3)
12#define CAN_NOMSG (4)
13#define CAN_CTRLERROR (5)
14#define CAN_GETTXBFTIMEOUT (6)
15#define CAN_SENDMSGTIMEOUT (7)
16#define CAN_FAIL (0xff)
17
18// clock
19typedef enum {
21 /* apply to MCP2515 */
24 /* apply to MCP2518FD */
25 MCP2518FD_40MHz = MCP_16MHz /* To compatible MCP2515 shield */,
29
52
53
55{
56public:
57 virtual void enableTxInterrupt(bool enable = true) = 0; // enable transmit interrupt
58 virtual void reserveTxBuffers(byte nTxBuf = 0) = 0;
59 virtual byte getLastTxBuffer() = 0;
60 /*
61 * speedset be in MCP_BITTIME_SETUP
62 * clockset be in MCP_CLOCK_T
63 */
64 virtual byte begin(uint32_t speedset, const byte clockset) = 0; // init can
65 virtual byte init_Mask(byte num, byte ext, unsigned long ulData) = 0; // init Masks
66 virtual byte init_Filt(byte num, byte ext, unsigned long ulData) = 0; // init filters
67 virtual void setSleepWakeup(byte enable) = 0; // Enable or disable the wake up interrupt
68 // (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only)
69 virtual byte sleep() = 0; // Put the MCP2515 in sleep mode
70 virtual byte wake() = 0; // Wake MCP2515 manually from sleep
71 virtual byte __setMode(byte opMode) = 0; // Set operational mode
72 virtual byte getMode() = 0; // Get operational mode
73 virtual byte checkError(uint8_t* err_ptr = NULL) = 0; // if something error
74
75 /* ---- receiving ---- */
76 virtual byte checkReceive(void) = 0; // if something received
77 virtual byte readMsgBufID(byte status,
78 volatile unsigned long *id, volatile byte *ext, volatile byte *rtr,
79 volatile byte *len, volatile byte *buf) = 0; // read buf with object ID
80 /* wrapper */
81 virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf) = 0;
82 virtual byte readMsgBuf(byte *len, byte *buf) = 0;
83
84 /* could be called after a successful readMsgBufID() */
85 unsigned long getCanId(void) { return can_id; }
86 byte isRemoteRequest(void) { return rtr; }
87 byte isExtendedFrame(void) { return ext_flg;}
88
89 /* ---- sending ---- */
90 virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtr,
91 byte len, const byte *buf, byte iTxBuf = 0xff) = 0; // as sendMsgBuf, but does not have any wait for free buffer
92 virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtr,
93 byte len, volatile const byte *buf) = 0; // send message buf by using parsed buffer status
94 virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit,
95 byte len, const byte *buf, bool wait_sent = true) = 0; // send message with wait
96
97 virtual void clearBufferTransmitIfFlags(byte flags = 0) = 0; // Clear transmit flags according to status
98 virtual byte readRxTxStatus(void) = 0; // read has something send or received
99 virtual byte checkClearRxStatus(byte *status) = 0; // read and clear and return first found rx status bit
100 virtual byte checkClearTxStatus(byte *status, byte iTxBuf = 0xff) = 0; // read and clear and return first found or buffer specified tx status bit
101 virtual bool mcpPinMode(const byte pin, const byte mode) = 0; // switch supported pins between HiZ, interrupt, output or input
102 virtual bool mcpDigitalWrite(const byte pin, const byte mode) = 0; // write HIGH or LOW to RX0BF/RX1BF
103 virtual byte mcpDigitalRead(const byte pin) = 0; // read HIGH or LOW from supported pins
104
105public:
106 MCP_CAN(byte _CS);
107 void init_CS(byte _CS); // define CS after construction before begin()
108 void setSPI(SPIClass *_pSPI);
109
110protected:
111 byte ext_flg; // identifier xxxID
112 // either extended (the 29 LSB) or standard (the 11 LSB)
113 unsigned long can_id; // can id
114 byte rtr; // is remote frame
115 byte SPICS;
116 SPIClass *pSPI;
117 byte mcpMode; // Current controller mode
118};
119
120#endif
SPIClass * pSPI
Definition mcp_can_fd.h:116
virtual byte getLastTxBuffer()=0
virtual byte init_Filt(byte num, byte ext, unsigned long ulData)=0
virtual byte __setMode(byte opMode)=0
unsigned long can_id
Definition mcp_can_fd.h:113
virtual byte readMsgBuf(byte *len, byte *buf)=0
virtual byte checkReceive(void)=0
virtual void enableTxInterrupt(bool enable=true)=0
virtual byte wake()=0
virtual byte mcpDigitalRead(const byte pin)=0
byte mcpMode
Definition mcp_can_fd.h:117
unsigned long getCanId(void)
Definition mcp_can_fd.h:85
virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtr, byte len, volatile const byte *buf)=0
byte ext_flg
Definition mcp_can_fd.h:111
void init_CS(byte _CS)
Definition mcp_can_fd.cpp:17
byte isExtendedFrame(void)
Definition mcp_can_fd.h:87
byte isRemoteRequest(void)
Definition mcp_can_fd.h:86
virtual byte checkError(uint8_t *err_ptr=NULL)=0
virtual byte begin(uint32_t speedset, const byte clockset)=0
virtual byte checkClearRxStatus(byte *status)=0
virtual byte getMode()=0
virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent=true)=0
virtual byte checkClearTxStatus(byte *status, byte iTxBuf=0xff)=0
byte SPICS
Definition mcp_can_fd.h:115
virtual byte init_Mask(byte num, byte ext, unsigned long ulData)=0
virtual byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf)=0
virtual void reserveTxBuffers(byte nTxBuf=0)=0
virtual byte readRxTxStatus(void)=0
void setSPI(SPIClass *_pSPI)
Definition mcp_can_fd.cpp:28
byte rtr
Definition mcp_can_fd.h:114
MCP_CAN(byte _CS)
Definition mcp_can_fd.cpp:7
virtual bool mcpDigitalWrite(const byte pin, const byte mode)=0
virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, const byte *buf, byte iTxBuf=0xff)=0
virtual void clearBufferTransmitIfFlags(byte flags=0)=0
virtual void setSleepWakeup(byte enable)=0
virtual bool mcpPinMode(const byte pin, const byte mode)=0
virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf)=0
virtual byte sleep()=0
MCP_BITTIME_SETUP
Definition mcp_can_fd.h:30
@ CAN_500KBPS
Definition mcp_can_fd.h:47
@ CAN_5KBPS
Definition mcp_can_fd.h:32
@ CAN_50KBPS
Definition mcp_can_fd.h:39
@ CAN_83K3BPS
Definition mcp_can_fd.h:41
@ CAN_NOBPS
Definition mcp_can_fd.h:31
@ CAN_125KBPS
Definition mcp_can_fd.h:44
@ CAN_200KBPS
Definition mcp_can_fd.h:45
@ CAN_1000KBPS
Definition mcp_can_fd.h:50
@ CAN_20KBPS
Definition mcp_can_fd.h:34
@ CAN_95KBPS
Definition mcp_can_fd.h:42
@ CAN_40KBPS
Definition mcp_can_fd.h:38
@ CAN_800KBPS
Definition mcp_can_fd.h:49
@ CAN_33KBPS
Definition mcp_can_fd.h:37
@ CAN_100KBPS
Definition mcp_can_fd.h:43
@ CAN_666KBPS
Definition mcp_can_fd.h:48
@ CAN_25KBPS
Definition mcp_can_fd.h:35
@ CAN_31K25BPS
Definition mcp_can_fd.h:36
@ CAN_80KBPS
Definition mcp_can_fd.h:40
@ CAN_250KBPS
Definition mcp_can_fd.h:46
@ CAN_10KBPS
Definition mcp_can_fd.h:33
MCP_CLOCK_T
Definition mcp_can_fd.h:19
@ MCP2518FD_20MHz
Definition mcp_can_fd.h:26
@ MCP_16MHz
Definition mcp_can_fd.h:22
@ MCP2518FD_40MHz
Definition mcp_can_fd.h:25
@ MCP_8MHz
Definition mcp_can_fd.h:23
@ MCP_NO_MHz
Definition mcp_can_fd.h:20
@ MCP2518FD_10MHz
Definition mcp_can_fd.h:27