PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. MIT License
  3. Copyright (c) 2018 Antonio Alexander Brewer (tonton81) - https://github.com/tonton81
  4. Designed and tested for PJRC Teensy 4.0.
  5. Forum link : https://forum.pjrc.com/threads/56035-FlexCAN_T4-FlexCAN-for-Teensy-4?highlight=flexcan_t4
  6. Thanks goes to skpang, mjs513, and collin for tech/testing support
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. #if !defined(_FLEXCAN_T4_H_)
  24. #define _FLEXCAN_T4_H_
  25. #include "Arduino.h"
  26. #include "circular_buffer.h"
  27. #include "imxrt_flexcan.h"
  28. typedef struct CAN_message_t {
  29. uint32_t id = 0; // can identifier
  30. uint16_t timestamp = 0; // FlexCAN time when message arrived
  31. uint8_t idhit = 0; // filter that id came from
  32. struct {
  33. bool extended = 0; // identifier is extended (29-bit)
  34. bool remote = 0; // remote transmission request packet type
  35. bool overrun = 0; // message overrun
  36. bool reserved = 0;
  37. } flags;
  38. uint8_t len = 8; // length of data
  39. uint8_t buf[8] = { 0 }; // data
  40. uint8_t mb = 0; // used to identify mailbox reception
  41. uint8_t bus = 0; // used to identify where the message came from when events() is used.
  42. bool seq = 0; // sequential frames
  43. } CAN_message_t;
  44. typedef struct CANFD_message_t {
  45. uint32_t id = 0; // can identifier
  46. uint16_t timestamp = 0; // FlexCAN time when message arrived
  47. uint8_t idhit = 0; // filter that id came from
  48. bool brs = 1; // baud rate switching for data
  49. bool esi = 0; // error status indicator
  50. bool edl = 1; // extended data length (for RX, 0 == CAN2.0, 1 == FD)
  51. struct {
  52. bool extended = 0; // identifier is extended (29-bit)
  53. bool overrun = 0; // message overrun
  54. bool reserved = 0;
  55. } flags;
  56. uint8_t len = 8; // length of data
  57. uint8_t buf[64] = { 0 }; // data
  58. uint8_t mb = 0; // used to identify mailbox reception
  59. uint8_t bus = 0; // used to identify where the message came from when events() is used.
  60. bool seq = 0; // sequential frames
  61. } CANFD_message_t;
  62. typedef void (*_MB_ptr)(const CAN_message_t &msg); /* mailbox / global callbacks */
  63. typedef void (*_MBFD_ptr)(const CANFD_message_t &msg); /* mailbox / global callbacks */
  64. typedef enum FLEXCAN_PINS {
  65. ALT = 0,
  66. DEF = 1,
  67. } FLEXCAN_PINS;
  68. typedef enum FLEXCAN_MAILBOX {
  69. MB0 = 0,
  70. MB1 = 1,
  71. MB2 = 2,
  72. MB3 = 3,
  73. MB4 = 4,
  74. MB5 = 5,
  75. MB6 = 6,
  76. MB7 = 7,
  77. MB8 = 8,
  78. MB9 = 9,
  79. MB10 = 10,
  80. MB11 = 11,
  81. MB12 = 12,
  82. MB13 = 13,
  83. MB14 = 14,
  84. MB15 = 15,
  85. MB16 = 16,
  86. MB17 = 17,
  87. MB18 = 18,
  88. MB19 = 19,
  89. MB20 = 20,
  90. MB21 = 21,
  91. MB22 = 22,
  92. MB23 = 23,
  93. MB24 = 24,
  94. MB25 = 25,
  95. MB26 = 26,
  96. MB27 = 27,
  97. MB28 = 28,
  98. MB29 = 29,
  99. MB30 = 30,
  100. MB31 = 31,
  101. MB32 = 32,
  102. MB33 = 33,
  103. MB34 = 34,
  104. MB35 = 35,
  105. MB36 = 36,
  106. MB37 = 37,
  107. MB38 = 38,
  108. MB39 = 39,
  109. MB40 = 40,
  110. MB41 = 41,
  111. MB42 = 42,
  112. MB43 = 43,
  113. MB44 = 44,
  114. MB45 = 45,
  115. MB46 = 46,
  116. MB47 = 47,
  117. MB48 = 48,
  118. MB49 = 49,
  119. MB50 = 50,
  120. MB51 = 51,
  121. MB52 = 52,
  122. MB53 = 53,
  123. MB54 = 54,
  124. MB55 = 55,
  125. MB56 = 56,
  126. MB57 = 57,
  127. MB58 = 58,
  128. MB59 = 59,
  129. MB60 = 60,
  130. MB61 = 61,
  131. MB62 = 62,
  132. MB63 = 63,
  133. FIFO = 99
  134. } FLEXCAN_MAILBOX;
  135. typedef enum FLEXCAN_RXTX {
  136. TX,
  137. RX,
  138. LISTEN_ONLY
  139. } FLEXCAN_RXTX;
  140. typedef enum FLEXCAN_FDRATES {
  141. CAN_1M_2M,
  142. CAN_1M_4M,
  143. CAN_1M_6M,
  144. CAN_1M_8M
  145. } FLEXCAN_FDRATES;
  146. typedef enum FLEXCAN_CLOCK {
  147. CLK_OFF,
  148. CLK_8MHz = 8,
  149. CLK_16MHz = 16,
  150. CLK_20MHz = 20,
  151. CLK_24MHz = 24,
  152. CLK_30MHz = 30,
  153. CLK_40MHz = 40,
  154. CLK_60MHz = 60,
  155. CLK_80MHz = 80
  156. } FLEXCAN_CLOCK;
  157. typedef struct CANFD_timings_t {
  158. double baudrate = 1000000;
  159. double baudrateFD = 2000000;
  160. double propdelay = 190;
  161. double bus_length = 1;
  162. double sample = 75;
  163. FLEXCAN_CLOCK clock = CLK_24MHz;
  164. } CANFD_timings_t;
  165. typedef enum FLEXCAN_IDE {
  166. NONE = 0,
  167. EXT = 1,
  168. RTR = 2,
  169. STD = 3,
  170. INACTIVE
  171. } FLEXCAN_IDE;
  172. typedef enum FLEXCAN_FLTEN {
  173. ACCEPT_ALL = 0,
  174. REJECT_ALL = 1
  175. } FLEXCAN_FLTEN;
  176. typedef enum FLEXCAN_FILTER_TABLE {
  177. FLEXCAN_MULTI = 1,
  178. FLEXCAN_RANGE = 2,
  179. FLEXCAN_TABLE_B_MULTI = 3,
  180. FLEXCAN_TABLE_B_RANGE = 4
  181. } FLEXCAN_FILTER_TABLE;
  182. typedef enum FLEXCAN_FIFOTABLE {
  183. A = 0,
  184. B = 1,
  185. C = 2
  186. } FLEXCAN_FIFOTABLE;
  187. typedef enum FLEXCAN_RXQUEUE_TABLE {
  188. RX_SIZE_2 = (uint16_t)2,
  189. RX_SIZE_4 = (uint16_t)4,
  190. RX_SIZE_8 = (uint16_t)8,
  191. RX_SIZE_16 = (uint16_t)16,
  192. RX_SIZE_32 = (uint16_t)32,
  193. RX_SIZE_64 = (uint16_t)64,
  194. RX_SIZE_128 = (uint16_t)128,
  195. RX_SIZE_256 = (uint16_t)256,
  196. RX_SIZE_512 = (uint16_t)512,
  197. RX_SIZE_1024 = (uint16_t)1024
  198. } FLEXCAN_RXQUEUE_TABLE;
  199. typedef enum FLEXCAN_DLC_SIZE {
  200. DLC_SIZE_8 = (uint16_t)8,
  201. DLC_SIZE_12 = (uint16_t)12,
  202. DLC_SIZE_16 = (uint16_t)16,
  203. DLC_SIZE_20 = (uint16_t)20,
  204. DLC_SIZE_24 = (uint16_t)24,
  205. DLC_SIZE_32 = (uint16_t)32,
  206. DLC_SIZE_48 = (uint16_t)48,
  207. DLC_SIZE_64 = (uint16_t)64
  208. } FLEXCAN_DLC_SIZE;
  209. typedef enum FLEXCAN_RFFN_TABLE {
  210. RFFN_8 = (uint8_t)0,
  211. RFFN_16 = (uint8_t)1,
  212. RFFN_24 = (uint8_t)2,
  213. RFFN_32 = (uint8_t)3,
  214. RFFN_40 = (uint8_t)4,
  215. RFFN_48 = (uint8_t)5,
  216. RFFN_56 = (uint8_t)6,
  217. RFFN_64 = (uint8_t)7,
  218. RFFN_72 = (uint8_t)8,
  219. RFFN_80 = (uint8_t)9,
  220. RFFN_88 = (uint8_t)10,
  221. RFFN_96 = (uint8_t)11,
  222. RFFN_104 = (uint8_t)12,
  223. RFFN_112 = (uint8_t)13,
  224. RFFN_120 = (uint8_t)14,
  225. RFFN_128 = (uint8_t)15
  226. } FLEXCAN_RFFN_TABLE;
  227. typedef enum FLEXCAN_TXQUEUE_TABLE {
  228. TX_SIZE_2 = (uint16_t)2,
  229. TX_SIZE_4 = (uint16_t)4,
  230. TX_SIZE_8 = (uint16_t)8,
  231. TX_SIZE_16 = (uint16_t)16,
  232. TX_SIZE_32 = (uint16_t)32,
  233. TX_SIZE_64 = (uint16_t)64,
  234. TX_SIZE_128 = (uint16_t)128,
  235. TX_SIZE_256 = (uint16_t)256,
  236. TX_SIZE_512 = (uint16_t)512,
  237. TX_SIZE_1024 = (uint16_t)1024
  238. } FLEXCAN_TXQUEUE_TABLE;
  239. typedef enum CAN_DEV_TABLE {
  240. #if defined(__IMXRT1062__)
  241. CAN1 = (uint32_t)0x401D0000,
  242. CAN2 = (uint32_t)0x401D4000,
  243. CAN3 = (uint32_t)0x401D8000
  244. #endif
  245. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  246. CAN0 = (uint32_t)0x40024000,
  247. CAN1 = (uint32_t)0x400A4000,
  248. #endif
  249. } CAN_DEV_TABLE;
  250. #define FCTP_CLASS template<CAN_DEV_TABLE _bus, FLEXCAN_RXQUEUE_TABLE _rxSize = RX_SIZE_16, FLEXCAN_TXQUEUE_TABLE _txSize = TX_SIZE_16>
  251. #define FCTP_FUNC template<CAN_DEV_TABLE _bus, FLEXCAN_RXQUEUE_TABLE _rxSize, FLEXCAN_TXQUEUE_TABLE _txSize>
  252. #define FCTP_OPT FlexCAN_T4<_bus, _rxSize, _txSize>
  253. #define FCTPFD_CLASS template<CAN_DEV_TABLE _bus, FLEXCAN_RXQUEUE_TABLE _rxSize = RX_SIZE_16, FLEXCAN_TXQUEUE_TABLE _txSize = TX_SIZE_16>
  254. #define FCTPFD_FUNC template<CAN_DEV_TABLE _bus, FLEXCAN_RXQUEUE_TABLE _rxSize, FLEXCAN_TXQUEUE_TABLE _txSize>
  255. #define FCTPFD_OPT FlexCAN_T4FD<_bus, _rxSize, _txSize>
  256. class FlexCAN_T4_Base {
  257. public:
  258. virtual void flexcan_interrupt() = 0;
  259. virtual void setBaudRate(uint32_t baud = 1000000, FLEXCAN_RXTX listen_only = TX) = 0;
  260. virtual uint64_t events() = 0;
  261. virtual int write(const CANFD_message_t &msg) = 0;
  262. virtual int write(const CAN_message_t &msg) = 0;
  263. virtual bool isFD() = 0;
  264. virtual uint8_t getFirstTxBoxSize();
  265. };
  266. #if defined(__IMXRT1062__)
  267. static FlexCAN_T4_Base* _CAN1 = nullptr;
  268. static FlexCAN_T4_Base* _CAN2 = nullptr;
  269. static FlexCAN_T4_Base* _CAN3 = nullptr;
  270. #endif
  271. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  272. static FlexCAN_T4_Base* _CAN0 = nullptr;
  273. static FlexCAN_T4_Base* _CAN1 = nullptr;
  274. #endif
  275. extern void ext_outputFD1(const CANFD_message_t &msg); // Interrupt data output, not filtered, for external libraries
  276. extern void ext_outputFD2(const CANFD_message_t &msg);
  277. extern void ext_outputFD3(const CANFD_message_t &msg);
  278. extern void ext_output1(const CAN_message_t &msg); // Interrupt data output, not filtered, for external libraries
  279. extern void ext_output2(const CAN_message_t &msg);
  280. extern void ext_output3(const CAN_message_t &msg);
  281. FCTPFD_CLASS class FlexCAN_T4FD : public FlexCAN_T4_Base {
  282. public:
  283. FlexCAN_T4FD();
  284. bool isFD() { return 1; }
  285. void begin();
  286. void setTX(FLEXCAN_PINS pin = DEF);
  287. void setRX(FLEXCAN_PINS pin = DEF);
  288. void enableFIFO(bool status = 1);
  289. void disableFIFO() { enableFIFO(0); }
  290. int read(CANFD_message_t &msg);
  291. int readMB(CANFD_message_t &msg);
  292. int write(const CANFD_message_t &msg); /* use any available mailbox for transmitting */
  293. int write(const CAN_message_t &msg) { return 0; } /* to satisfy base class for external pointers */
  294. int write(FLEXCAN_MAILBOX mb_num, const CANFD_message_t &msg); /* use a single mailbox for transmitting */
  295. bool setMB(const FLEXCAN_MAILBOX &mb_num, const FLEXCAN_RXTX &mb_rx_tx, const FLEXCAN_IDE &ide = STD);
  296. uint8_t setRegions(uint8_t size); /* 8, 16, 32 or 64 bytes */
  297. uint8_t setRegions(uint8_t mbdsr0, uint8_t mbdsr1); /* set both regions independantly */
  298. bool setBaudRateAdvanced(CANFD_timings_t config, uint8_t nominal_choice, uint8_t flexdata_choice, FLEXCAN_RXTX listen_only = TX);
  299. bool setBaudRate(CANFD_timings_t config, FLEXCAN_RXTX listen_only = TX);
  300. void setBaudRate(FLEXCAN_FDRATES input, FLEXCAN_RXTX listen_only = TX);
  301. void enableMBInterrupt(const FLEXCAN_MAILBOX &mb_num, bool status = 1);
  302. void disableMBInterrupt(const FLEXCAN_MAILBOX &mb_num) { enableMBInterrupt(mb_num, 0); }
  303. void enableMBInterrupts(bool status = 1);
  304. void disableMBInterrupts() { enableMBInterrupts(0); }
  305. uint64_t events();
  306. void onReceive(const FLEXCAN_MAILBOX &mb_num, _MBFD_ptr handler); /* individual mailbox callback function */
  307. void onReceive(_MBFD_ptr handler); /* global callback function */
  308. void setMBFilter(FLEXCAN_FLTEN input); /* enable/disable traffic for all MBs (for individual masking) */
  309. void setMBFilter(FLEXCAN_MAILBOX mb_num, FLEXCAN_FLTEN input); /* set specific MB to accept/deny traffic */
  310. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1); /* input 1 ID to be filtered */
  311. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2); /* input 2 ID's to be filtered */
  312. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3); /* input 3 ID's to be filtered */
  313. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4); /* input 4 ID's to be filtered */
  314. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5); /* input 5 ID's to be filtered */
  315. bool setMBFilterRange(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2); /* filter a range of ids */
  316. void mailboxStatus();
  317. void enhanceFilter(FLEXCAN_MAILBOX mb_num);
  318. void distribute(bool state = 1) { distribution = state; }
  319. void enableDMA(bool state = 1);
  320. void disableDMA() { enableDMA(0); }
  321. uint8_t getFirstTxBoxSize();
  322. void setMBFilterProcessing(FLEXCAN_MAILBOX mb_num, uint32_t filter_id, uint32_t calculated_mask);
  323. private:
  324. uint64_t readIFLAG() { return (((uint64_t)FLEXCANb_IFLAG2(_bus) << 32) | FLEXCANb_IFLAG1(_bus)); }
  325. uint32_t mailbox_offset(uint8_t mailbox, uint8_t &maxsize);
  326. void writeTxMailbox(uint8_t mb_num, const CANFD_message_t &msg);
  327. bool setBaudRate(CANFD_timings_t config, uint8_t nominal_choice, uint8_t flexdata_choice, FLEXCAN_RXTX listen_only = TX, bool advanced = 0);
  328. int getFirstTxBox();
  329. uint32_t mb_filter_table[64][7];
  330. Circular_Buffer<uint8_t, (uint32_t)_rxSize, sizeof(CANFD_message_t)> rxBuffer;
  331. Circular_Buffer<uint8_t, (uint32_t)_txSize, sizeof(CANFD_message_t)> txBuffer;
  332. void FLEXCAN_ExitFreezeMode();
  333. void FLEXCAN_EnterFreezeMode();
  334. void reset() { softReset(); } /* reset flexcan controller (needs register restore capabilities...) */
  335. void flexcan_interrupt();
  336. void writeIFLAG(uint64_t value);
  337. void writeIFLAGBit(uint8_t mb_num);
  338. uint8_t max_mailboxes();
  339. uint64_t readIMASK() { return (((uint64_t)FLEXCANb_IMASK2(_bus) << 32) | FLEXCANb_IMASK1(_bus)); }
  340. void frame_distribution(CANFD_message_t &msg);
  341. void setBaudRate(uint32_t baud = 1000000, FLEXCAN_RXTX listen_only = TX) { ; } // unused, CAN2.0 only (needed for base class existance)
  342. void setClock(FLEXCAN_CLOCK clock = CLK_24MHz);
  343. uint32_t getClock();
  344. void softReset();
  345. void writeIMASK(uint64_t value);
  346. void writeIMASKBit(uint8_t mb_num, bool set = 1);
  347. uint8_t busNumber;
  348. uint8_t mailbox_reader_increment = 0;
  349. uint32_t nvicIrq = 0;
  350. uint8_t dlc_to_len(uint8_t val);
  351. uint8_t len_to_dlc(uint8_t val);
  352. uint32_t setBaudRateFD(CANFD_timings_t config, uint32_t flexdata_choice, bool advanced); /* internally used */
  353. void mbCallbacks(const FLEXCAN_MAILBOX &mb_num, const CANFD_message_t &msg);
  354. _MBFD_ptr _mbHandlers[64]; /* individual mailbox handlers */
  355. _MBFD_ptr _mainHandler; /* global mailbox handler */
  356. void filter_store(FLEXCAN_FILTER_TABLE type, FLEXCAN_MAILBOX mb_num, uint32_t id_count, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5, uint32_t mask);
  357. bool filter_match(FLEXCAN_MAILBOX mb_num, uint32_t id);
  358. void struct2queueTx(const CANFD_message_t &msg);
  359. void struct2queueRx(const CANFD_message_t &msg);
  360. bool distribution = 0;
  361. };
  362. FCTP_CLASS class FlexCAN_T4 : public FlexCAN_T4_Base {
  363. public:
  364. FlexCAN_T4();
  365. bool isFD() { return 0; }
  366. void begin();
  367. uint32_t getBaudRate() { return currentBitrate; }
  368. void setTX(FLEXCAN_PINS pin = DEF);
  369. void setRX(FLEXCAN_PINS pin = DEF);
  370. void setBaudRate(uint32_t baud = 1000000, FLEXCAN_RXTX listen_only = TX);
  371. void reset() { softReset(); } /* reset flexcan controller (needs register restore capabilities...) */
  372. void setMaxMB(uint8_t last);
  373. void enableFIFO(bool status = 1);
  374. void disableFIFO() { enableFIFO(0); }
  375. void enableFIFOInterrupt(bool status = 1);
  376. void disableFIFOInterrupt() { enableFIFOInterrupt(0); }
  377. void mailboxStatus();
  378. int read(CAN_message_t &msg);
  379. int readMB(CAN_message_t &msg);
  380. int readFIFO(CAN_message_t &msg);
  381. bool setMB(const FLEXCAN_MAILBOX &mb_num, const FLEXCAN_RXTX &mb_rx_tx, const FLEXCAN_IDE &ide = STD);
  382. void enableMBInterrupt(const FLEXCAN_MAILBOX &mb_num, bool status = 1);
  383. void disableMBInterrupt(const FLEXCAN_MAILBOX &mb_num) { enableMBInterrupt(mb_num, 0); }
  384. void enableMBInterrupts(bool status = 1);
  385. void disableMBInterrupts() { enableMBInterrupts(0); }
  386. void setMRP(bool mrp = 1); /* mailbox(1)/fifo(0) priority */
  387. void setRRS(bool rrs = 1); /* store remote frames */
  388. void onReceive(const FLEXCAN_MAILBOX &mb_num, _MB_ptr handler); /* individual mailbox callback function */
  389. void onReceive(_MB_ptr handler); /* global callback function */
  390. void setMBFilter(FLEXCAN_FLTEN input); /* enable/disable traffic for all MBs (for individual masking) */
  391. void setMBFilter(FLEXCAN_MAILBOX mb_num, FLEXCAN_FLTEN input); /* set specific MB to accept/deny traffic */
  392. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1); /* input 1 ID to be filtered */
  393. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2); /* input 2 ID's to be filtered */
  394. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3); /* input 3 ID's to be filtered */
  395. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4); /* input 4 ID's to be filtered */
  396. bool setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5); /* input 5 ID's to be filtered */
  397. bool setMBFilterRange(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2); /* filter a range of ids */
  398. int write(const CAN_message_t &msg); /* use any available mailbox for transmitting */
  399. int write(const CANFD_message_t &msg) { return 0; } /* to satisfy base class for external pointers */
  400. int write(FLEXCAN_MAILBOX mb_num, const CAN_message_t &msg); /* use a single mailbox for transmitting */
  401. uint64_t events();
  402. uint8_t setRFFN(FLEXCAN_RFFN_TABLE rffn = RFFN_8); /* Number Of Rx FIFO Filters (0 == 8 filters, 1 == 16 filters, etc.. */
  403. uint8_t setRFFN(uint8_t rffn) { return setRFFN((FLEXCAN_RFFN_TABLE)constrain(rffn, 0, 15)); }
  404. void setFIFOFilterTable(FLEXCAN_FIFOTABLE letter);
  405. void setFIFOFilter(const FLEXCAN_FLTEN &input);
  406. bool setFIFOFilter(uint8_t filter, uint32_t id1, const FLEXCAN_IDE &ide, const FLEXCAN_IDE &remote = NONE); /* single ID per filter */
  407. bool setFIFOFilter(uint8_t filter, uint32_t id1, uint32_t id2, const FLEXCAN_IDE &ide, const FLEXCAN_IDE &remote = NONE); /* 2 ID's per filter */
  408. bool setFIFOFilterRange(uint8_t filter, uint32_t id1, uint32_t id2, const FLEXCAN_IDE &ide, const FLEXCAN_IDE &remote = NONE); /* ID range per filter */
  409. bool setFIFOFilter(uint8_t filter, uint32_t id1, const FLEXCAN_IDE &ide1, const FLEXCAN_IDE &remote1, uint32_t id2, const FLEXCAN_IDE &ide2, const FLEXCAN_IDE &remote2); /* TableB 2 ID / filter */
  410. bool setFIFOFilter(uint8_t filter, uint32_t id1, uint32_t id2, const FLEXCAN_IDE &ide1, const FLEXCAN_IDE &remote1, uint32_t id3, uint32_t id4, const FLEXCAN_IDE &ide2, const FLEXCAN_IDE &remote2); /* TableB 4 minimum ID / filter */
  411. bool setFIFOFilterRange(uint8_t filter, uint32_t id1, uint32_t id2, const FLEXCAN_IDE &ide1, const FLEXCAN_IDE &remote1, uint32_t id3, uint32_t id4, const FLEXCAN_IDE &ide2, const FLEXCAN_IDE &remote2); /* TableB dual range based IDs */
  412. void struct2queueTx(const CAN_message_t &msg);
  413. void struct2queueRx(const CAN_message_t &msg);
  414. #if defined(__IMXRT1062__)
  415. void setClock(FLEXCAN_CLOCK clock = CLK_24MHz);
  416. #endif
  417. void enhanceFilter(FLEXCAN_MAILBOX mb_num);
  418. void distribute(bool state = 1) { distribution = state; }
  419. void enableDMA(bool state = 1);
  420. void disableDMA() { enableDMA(0); }
  421. uint8_t getFirstTxBoxSize(){ return 8; }
  422. void setMBFilterProcessing(FLEXCAN_MAILBOX mb_num, uint32_t filter_id, uint32_t calculated_mask);
  423. private:
  424. void writeTxMailbox(uint8_t mb_num, const CAN_message_t &msg);
  425. uint64_t readIMASK();// { return (((uint64_t)FLEXCANb_IMASK2(_bus) << 32) | FLEXCANb_IMASK1(_bus)); }
  426. void flexcan_interrupt();
  427. void flexcanFD_interrupt() { ; } // dummy placeholder to satisfy base class
  428. Circular_Buffer<uint8_t, (uint32_t)_rxSize, sizeof(CAN_message_t)> rxBuffer;
  429. Circular_Buffer<uint8_t, (uint32_t)_txSize, sizeof(CAN_message_t)> txBuffer;
  430. #if defined(__IMXRT1062__)
  431. uint32_t getClock();
  432. #endif
  433. void FLEXCAN_ExitFreezeMode();
  434. void FLEXCAN_EnterFreezeMode();
  435. volatile uint32_t fifo_filter_table[32][6];
  436. volatile uint32_t mb_filter_table[64][6];
  437. volatile bool fifo_filter_match(uint32_t id);
  438. volatile void frame_distribution(CAN_message_t &msg);
  439. void filter_store(FLEXCAN_FILTER_TABLE type, FLEXCAN_MAILBOX mb_num, uint32_t id_count, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5);
  440. void fifo_filter_store(FLEXCAN_FILTER_TABLE type, uint8_t filter, uint32_t id_count, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5);
  441. volatile bool filter_match(FLEXCAN_MAILBOX mb_num, uint32_t id);
  442. volatile bool distribution = 0;
  443. uint8_t getNumMailBoxes() { return FLEXCANb_MAXMB_SIZE(_bus); }
  444. uint8_t mailboxOffset();
  445. void softReset();
  446. int getFirstTxBox();
  447. _MB_ptr _mbHandlers[64]; /* individual mailbox handlers */
  448. _MB_ptr _mainHandler; /* global mailbox handler */
  449. uint64_t readIFLAG();// { return (((uint64_t)FLEXCANb_IFLAG2(_bus) << 32) | FLEXCANb_IFLAG1(_bus)); }
  450. void writeIFLAG(uint64_t value);
  451. void writeIFLAGBit(uint8_t mb_num);
  452. void writeIMASK(uint64_t value);
  453. void writeIMASKBit(uint8_t mb_num, bool set = 1);
  454. uint32_t nvicIrq = 0;
  455. uint32_t currentBitrate = 0UL;
  456. uint8_t mailbox_reader_increment = 0;
  457. uint8_t busNumber;
  458. void mbCallbacks(const FLEXCAN_MAILBOX &mb_num, const CAN_message_t &msg);
  459. };
  460. #include "FlexCAN_T4.tpp"
  461. #if defined(__IMXRT1062__)
  462. #include "FlexCAN_T4FD.tpp"
  463. #include "FlexCAN_T4FDTimings.tpp"
  464. #endif
  465. #endif