PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. #include <FlexCAN_T4.h>
  24. #include "imxrt_flexcan.h"
  25. #include "Arduino.h"
  26. static void flexcan_isr_can3fd();
  27. FCTPFD_FUNC FCTPFD_OPT::FlexCAN_T4FD() {
  28. if ( _bus == CAN3 ) _CAN3 = this;
  29. }
  30. FCTPFD_FUNC void FCTPFD_OPT::setClock(FLEXCAN_CLOCK clock) {
  31. if ( clock == CLK_OFF ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(3) | CCM_CSCMR2_CAN_CLK_PODF(0);
  32. if ( clock == CLK_8MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(2) | CCM_CSCMR2_CAN_CLK_PODF(9);
  33. if ( clock == CLK_16MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(2) | CCM_CSCMR2_CAN_CLK_PODF(4);
  34. if ( clock == CLK_24MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(1) | CCM_CSCMR2_CAN_CLK_PODF(0);
  35. if ( clock == CLK_20MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(2) | CCM_CSCMR2_CAN_CLK_PODF(3);
  36. if ( clock == CLK_30MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(0) | CCM_CSCMR2_CAN_CLK_PODF(1);
  37. if ( clock == CLK_40MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(2) | CCM_CSCMR2_CAN_CLK_PODF(1);
  38. if ( clock == CLK_60MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(0) | CCM_CSCMR2_CAN_CLK_PODF(0);
  39. if ( clock == CLK_80MHz ) CCM_CSCMR2 = (CCM_CSCMR2 & 0xFFFFFC03) | CCM_CSCMR2_CAN_CLK_SEL(2) | CCM_CSCMR2_CAN_CLK_PODF(0);
  40. }
  41. FCTPFD_FUNC uint32_t FCTPFD_OPT::getClock() {
  42. const uint8_t clocksrc[4] = {60, 24, 80, 0};
  43. return clocksrc[(CCM_CSCMR2 & 0x300) >> 8];
  44. }
  45. FCTPFD_FUNC void FCTPFD_OPT::begin() {
  46. if ( !getClock() ) setClock(CLK_24MHz); /* no clock enabled, enable osc clock */
  47. CCM_CCGR0 |= CCM_CCGR0_LPUART3(CCM_CCGR_ON); /* hardware bug, FD is unable to operate without an LPUART clock online */
  48. if ( _bus == CAN3 ) {
  49. nvicIrq = IRQ_CAN3;
  50. _VectorsRam[16 + nvicIrq] = flexcan_isr_can3fd;
  51. CCM_CCGR7 |= 0x3C0;
  52. busNumber = 3;
  53. }
  54. setTX(); setRX();
  55. FLEXCANb_MCR(_bus) &= ~FLEXCAN_MCR_MDIS; /* enable module */
  56. FLEXCAN_EnterFreezeMode();
  57. FLEXCANb_CTRL1(_bus) = FLEXCAN_CTRL_LOM /*| (1UL << 5)*/; /* listen only mode, TSYN */
  58. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_FRZ; /* enable freeze bit */
  59. while (FLEXCANb_MCR(_bus) & FLEXCAN_MCR_LPM_ACK);
  60. softReset(); /* reset bus */
  61. while (!(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK));
  62. FLEXCANb_MCR(_bus) &= ~0x8000; // disable DMA
  63. FLEXCANb_MCR(_bus) |= (1UL << 16) | (1UL << 11) | (1UL << 17) | 0x3F; // IRMQ, FDEN, SRXDIS, 64MBs
  64. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_AEN; // TX ABORT FEATURE
  65. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_LPRIO_EN; // TX PRIORITY FEATURE
  66. FLEXCANb_CTRL2(_bus) |= FLEXCAN_CTRL2_RRS | // store remote frames
  67. FLEXCAN_CTRL2_EACEN | /* handles the way filtering works. Library adjusts to whether you use this or not */
  68. FLEXCAN_CTRL2_MRP | // mailbox > FIFO priority.
  69. FLEXCAN_CTRL2_ISOCANFDEN;
  70. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_WRN_EN;
  71. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_WAK_MSK;
  72. FLEXCANb_FDCTRL(_bus) = 0x80008300; // MBDSR0-MBDSR1 = 0; 32MBs each as default, TDCEN, FDRATE(BRS)
  73. disableFIFO();
  74. FLEXCAN_ExitFreezeMode();
  75. NVIC_ENABLE_IRQ(nvicIrq);
  76. }
  77. FCTPFD_FUNC uint8_t FCTPFD_OPT::setRegions(uint8_t size) {
  78. FLEXCAN_EnterFreezeMode();
  79. FLEXCANb_FDCTRL(_bus) &= ~0x1B0000; /* clear both memory region bits */
  80. if ( constrain(size, 8, 64) == 8 ) {
  81. size = 64;
  82. }
  83. else if ( constrain(size, 16, 64) == 16 ) {
  84. FLEXCANb_FDCTRL(_bus) |= 0x90000;
  85. size = 42;
  86. }
  87. else if ( constrain(size, 32, 64) == 32 ) {
  88. FLEXCANb_FDCTRL(_bus) |= 0x120000;
  89. size = 24;
  90. }
  91. else {
  92. FLEXCANb_FDCTRL(_bus) |= 0x1B0000;
  93. size = 14;
  94. }
  95. disableFIFO();
  96. FLEXCAN_ExitFreezeMode();
  97. return size;
  98. }
  99. FCTPFD_FUNC uint8_t FCTPFD_OPT::setRegions(uint8_t mbdsr0, uint8_t mbdsr1) {
  100. FLEXCAN_EnterFreezeMode();
  101. FLEXCANb_FDCTRL(_bus) &= ~0x1B0000; /* clear both memory region bits */
  102. if ( constrain(mbdsr0, 8, 64) == 8 ) {
  103. mbdsr0 = 32;
  104. }
  105. else if ( constrain(mbdsr0, 16, 64) == 16 ) {
  106. FLEXCANb_FDCTRL(_bus) |= 0x10000;
  107. mbdsr0 = 21;
  108. }
  109. else if ( constrain(mbdsr0, 32, 64) == 32 ) {
  110. FLEXCANb_FDCTRL(_bus) |= 0x20000;
  111. mbdsr0 = 12;
  112. }
  113. else {
  114. FLEXCANb_FDCTRL(_bus) |= 0x30000;
  115. mbdsr0 = 7;
  116. }
  117. if ( constrain(mbdsr1, 8, 64) == 8 ) {
  118. mbdsr1 = 32;
  119. }
  120. else if ( constrain(mbdsr1, 16, 64) == 16 ) {
  121. FLEXCANb_FDCTRL(_bus) |= 0x80000;
  122. mbdsr1 = 21;
  123. }
  124. else if ( constrain(mbdsr1, 32, 64) == 32 ) {
  125. FLEXCANb_FDCTRL(_bus) |= 0x100000;
  126. mbdsr1 = 12;
  127. }
  128. else {
  129. FLEXCANb_FDCTRL(_bus) |= 0x180000;
  130. mbdsr1 = 7;
  131. }
  132. disableFIFO();
  133. FLEXCAN_ExitFreezeMode();
  134. return mbdsr0 + mbdsr1;
  135. }
  136. FCTPFD_FUNC uint32_t FCTPFD_OPT::mailbox_offset(uint8_t mailbox, uint8_t &maxsize) {
  137. const uint8_t data_size[4] = { 8, 16, 32, 64 };
  138. const uint8_t mbx_total[4] = { 32, 21, 12, 7 };
  139. const uint8_t mbx_shift[4] = { 0x10, 0x18, 0x28, 0x48 };
  140. uint8_t region0 = (FLEXCANb_FDCTRL(_bus) & (3UL << 16)) >> 16;
  141. uint8_t region1 = (FLEXCANb_FDCTRL(_bus) & (3UL << 19)) >> 19;
  142. if ( mailbox < mbx_total[region0] ) {
  143. maxsize = data_size[region0];
  144. return _bus + 0x80 + (mbx_shift[region0] * mailbox);
  145. }
  146. else if ( mailbox < (mbx_total[region0] + mbx_total[region1]) ) {
  147. maxsize = data_size[region1];
  148. return _bus + 0x280 + (mbx_shift[region1] * (mailbox - mbx_total[region0]));
  149. }
  150. return _bus + 0x80;
  151. }
  152. FCTPFD_FUNC int FCTPFD_OPT::getFirstTxBox() {
  153. for (uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++) {
  154. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  155. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return i; // if TX
  156. }
  157. return -1;
  158. }
  159. FCTPFD_FUNC uint8_t FCTPFD_OPT::getFirstTxBoxSize() {
  160. uint8_t mbsize = 0;
  161. mailbox_offset(getFirstTxBox(), mbsize);
  162. return mbsize;
  163. }
  164. FCTPFD_FUNC void FCTPFD_OPT::setBaudRate(FLEXCAN_FDRATES input, FLEXCAN_RXTX listen_only) {
  165. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  166. FLEXCAN_EnterFreezeMode();
  167. FLEXCANb_FDCTRL(_bus) = (FLEXCANb_FDCTRL(_bus) & 0xFFFF60FF); /* clear TDC values */
  168. FLEXCANb_CBT(_bus) &= ~(1UL << 31); /* clear BTE bit to edit CTRL1 register */
  169. ( listen_only != LISTEN_ONLY ) ? FLEXCANb_CTRL1(_bus) &= ~FLEXCAN_CTRL_LOM : FLEXCANb_CTRL1(_bus) |= FLEXCAN_CTRL_LOM;
  170. if ( input == CAN_1M_2M ) { /* based on 24MHz and 70% sample point */
  171. setClock(CLK_24MHz);
  172. FLEXCANb_FDCTRL(_bus) |= (0x801B8300 & 0x9F00);
  173. FLEXCANb_FDCBT(_bus) = 0x31423;
  174. FLEXCANb_CBT(_bus) = 0x800624A6;
  175. }
  176. if ( input == CAN_1M_4M ) { /* based on 24MHz and 70% sample point */
  177. setClock(CLK_24MHz);
  178. FLEXCANb_FDCTRL(_bus) |= (0x80008300 & 0x9F00);
  179. FLEXCANb_FDCBT(_bus) = 0x10421;
  180. FLEXCANb_CBT(_bus) = 0x800624A6;
  181. }
  182. if ( input == CAN_1M_6M ) { /* based on 30MHz and 70% sample point */
  183. setClock(CLK_30MHz);
  184. FLEXCANb_FDCTRL(_bus) |= (0x80008300 & 0x9F00);
  185. FLEXCANb_FDCBT(_bus) = 0x401;
  186. FLEXCANb_CBT(_bus) = 0x80082CE8;
  187. }
  188. if ( input == CAN_1M_8M ) { /* based on 40MHz and 70% sample point */
  189. setClock(CLK_40MHz);
  190. FLEXCANb_FDCTRL(_bus) |= (0x80008300 & 0x9F00);
  191. FLEXCANb_FDCBT(_bus) = 0x401;
  192. FLEXCANb_CBT(_bus) = 0x800B3D4B;
  193. }
  194. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  195. }
  196. FCTPFD_FUNC uint8_t FCTPFD_OPT::max_mailboxes() {
  197. uint8_t mb_count = 0;
  198. uint8_t block0 = (FLEXCANb_FDCTRL(_bus) & (3UL << 16)) >> 16;
  199. uint8_t block1 = (FLEXCANb_FDCTRL(_bus) & (3UL << 19)) >> 19;
  200. const uint8_t sizes[4] = {32, 21, 12, 7};
  201. mb_count = sizes[block0] + sizes[block1];
  202. if ( mb_count > FLEXCANb_MAXMB_SIZE(_bus) ) return FLEXCANb_MAXMB_SIZE(_bus);
  203. return mb_count;
  204. }
  205. FCTPFD_FUNC int FCTPFD_OPT::write(FLEXCAN_MAILBOX mb_num, const CANFD_message_t &msg) {
  206. uint8_t mbsize = 0;
  207. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  208. if ( !((FLEXCAN_get_code(mbxAddr[0])) >> 3) ) return 0; /* not a transmit mailbox */
  209. if ( msg.seq && FLEXCAN_get_code(mbxAddr[0]) != FLEXCAN_MB_CODE_TX_INACTIVE ) return 0; /* non blocking resend sequential frames */
  210. uint32_t timeout = millis();
  211. while ( FLEXCAN_get_code(mbxAddr[0]) != FLEXCAN_MB_CODE_TX_INACTIVE ) {
  212. if ( millis() - timeout > 100 ) return 0;
  213. }
  214. writeTxMailbox(mb_num, msg);
  215. return 1; // transmit entry accepted //
  216. }
  217. FCTPFD_FUNC void FCTPFD_OPT::writeTxMailbox(uint8_t mb_num, const CANFD_message_t &frame) {
  218. CANFD_message_t msg = frame;
  219. writeIFLAGBit(mb_num);
  220. uint8_t mbsize = 0;
  221. uint32_t code = 0;
  222. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  223. mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE);
  224. mbxAddr[1] = (( msg.flags.extended ) ? ( msg.id & FLEXCAN_MB_ID_EXT_MASK ) : FLEXCAN_MB_ID_IDSTD(msg.id));
  225. if ( msg.flags.extended ) code |= (3UL << 21);
  226. for ( uint8_t i = 0; i < (mbsize >> 2); i++ ) mbxAddr[2 + i] = (msg.buf[0 + i * 4] << 24) | (msg.buf[1 + i * 4] << 16) | (msg.buf[2 + i * 4] << 8) | msg.buf[3 + i * 4];
  227. if ( msg.len > mbsize ) msg.len = mbsize;
  228. code |= len_to_dlc(msg.len) << 16;
  229. if ( msg.brs ) code |= (1UL << 30); // BRS
  230. if ( msg.edl ) code |= (1UL << 31); // EDL
  231. mbxAddr[0] = code | FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_ONCE);
  232. }
  233. FCTPFD_FUNC uint8_t FCTPFD_OPT::len_to_dlc(uint8_t val) {
  234. if ( val <= 8 );
  235. else if ( val <= 12 ) val = 9;
  236. else if ( val <= 16 ) val = 10;
  237. else if ( val <= 20 ) val = 11;
  238. else if ( val <= 24 ) val = 12;
  239. else if ( val <= 32 ) val = 13;
  240. else if ( val <= 48 ) val = 14;
  241. else if ( val <= 64 ) val = 15;
  242. return val;
  243. }
  244. FCTPFD_FUNC bool FCTPFD_OPT::setMB(const FLEXCAN_MAILBOX &mb_num, const FLEXCAN_RXTX &mb_rx_tx, const FLEXCAN_IDE &ide) {
  245. if ( mb_num >= max_mailboxes() ) return 0;
  246. writeIMASKBit(mb_num, 0); /* immediately disable mailbox interrupt */
  247. FLEXCAN_EnterFreezeMode();
  248. FLEXCANb_RXIMR(_bus, mb_num) = 0UL | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0); // CLEAR MAILBOX MASK
  249. FLEXCAN_ExitFreezeMode();
  250. uint8_t mbsize = 0;
  251. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num , mbsize)));
  252. FLEXCAN_get_code(mbxAddr[0]); // Reading Control Status atomically locks mailbox (if it is RX mode).
  253. for ( uint8_t i = 0; i < (mbsize >> 2); i++ ) mbxAddr[2 + i] = 0x0; /* clear mailbox data */
  254. if ( ide == INACTIVE ) mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_INACTIVE);
  255. else if ( mb_rx_tx == RX ) mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_EMPTY) | ((ide != EXT) ? 0 : FLEXCAN_MB_CS_SRR | FLEXCAN_MB_CS_IDE);
  256. else if ( mb_rx_tx == TX ) mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE);
  257. mbxAddr[1] = 0; // clear ID
  258. FLEXCANb_TIMER(_bus); /* reading timer unlocks individual mailbox */
  259. writeIFLAGBit(mb_num); /* clear mailbox flag */
  260. mb_filter_table[mb_num][0] = ( ((mbxAddr[0] & 0x600000) ? 1UL : 0UL) << 27); /* extended flag check */
  261. return 1;
  262. }
  263. FCTPFD_FUNC uint8_t FCTPFD_OPT::dlc_to_len(uint8_t val) {
  264. const uint8_t dlcTolen[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
  265. return dlcTolen[val];
  266. }
  267. FCTPFD_FUNC void FCTPFD_OPT::FLEXCAN_ExitFreezeMode() {
  268. FLEXCANb_MCR(_bus) &= ~FLEXCAN_MCR_HALT;
  269. while (FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  270. }
  271. FCTPFD_FUNC void FCTPFD_OPT::FLEXCAN_EnterFreezeMode() {
  272. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT;
  273. while (!(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK));
  274. }
  275. FCTPFD_FUNC void FCTPFD_OPT::softReset() {
  276. FLEXCANb_MCR(_bus) |= FLEXCAN_MCR_SOFT_RST;
  277. while (FLEXCANb_MCR(_bus) & FLEXCAN_MCR_SOFT_RST);
  278. }
  279. FCTPFD_FUNC void FCTPFD_OPT::setTX(FLEXCAN_PINS pin) {
  280. if ( _bus == CAN3 ) {
  281. if ( pin == DEF ) {
  282. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_36 = 0x19; // pin31 T3B2
  283. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_36 = 0x10B0; // pin31 T3B2
  284. }
  285. }
  286. if ( _bus == CAN2 ) {
  287. if ( pin == DEF ) {
  288. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_02 = 0x10; // pin 1 T4B1+B2
  289. IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_02 = 0x10B0; // pin 1 T4B1+B2
  290. }
  291. }
  292. if ( _bus == CAN1 ) {
  293. if ( pin == DEF ) {
  294. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_08 = 0x12; // pin 22 T4B1+B2
  295. IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_08 = 0x10B0; // pin 22 T4B1+B2
  296. }
  297. }
  298. }
  299. FCTPFD_FUNC void FCTPFD_OPT::setRX(FLEXCAN_PINS pin) {
  300. /* DAISY REGISTER CAN3
  301. 00 GPIO_EMC_37_ALT9 — Selecting Pad: GPIO_EMC_37 for Mode: ALT9
  302. 01 GPIO_AD_B0_15_ALT8 — Selecting Pad: GPIO_AD_B0_15 for Mode: ALT8
  303. 10 GPIO_AD_B0_11_ALT8 — Selecting Pad: GPIO_AD_B0_11 for Mode: ALT8
  304. */
  305. /* DAISY REGISTER CAN2
  306. 00 GPIO_EMC_10_ALT3 — Selecting Pad: GPIO_EMC_10 for Mode: ALT3
  307. 01 GPIO_AD_B0_03_ALT0 — Selecting Pad: GPIO_AD_B0_03 for Mode: ALT0
  308. 10 GPIO_AD_B0_15_ALT6 — Selecting Pad: GPIO_AD_B0_15 for Mode: ALT6
  309. 11 GPIO_B1_09_ALT6 — Selecting Pad: GPIO_B1_09 for Mode: ALT6
  310. */
  311. /* DAISY REGISTER CAN1
  312. 00 GPIO_SD_B1_03_ALT4 — Selecting Pad: GPIO_SD_B1_03 for Mode: ALT4
  313. 01 GPIO_EMC_18_ALT3 — Selecting Pad: GPIO_EMC_18 for Mode: ALT3
  314. 10 GPIO_AD_B1_09_ALT2 — Selecting Pad: GPIO_AD_B1_09 for Mode: ALT2
  315. 11 GPIO_B0_03_ALT2 — Selecting Pad: GPIO_B0_03 for Mode: ALT2
  316. */
  317. if ( _bus == CAN3 ) {
  318. if ( pin == DEF ) {
  319. IOMUXC_CANFD_IPP_IND_CANRX_SELECT_INPUT = 0x00;
  320. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_37 = 0x19; // pin30 T3B2
  321. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_37 = 0x10B0; // pin30 T3B2
  322. }
  323. }
  324. if ( _bus == CAN2 ) {
  325. if ( pin == DEF ) {
  326. IOMUXC_FLEXCAN2_RX_SELECT_INPUT = 0x01;
  327. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_03 = 0x10; // pin 0 T4B1+B2
  328. IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_03 = 0x10B0; // pin 0 T4B1+B2
  329. }
  330. }
  331. if ( _bus == CAN1 ) {
  332. if ( pin == DEF ) {
  333. IOMUXC_FLEXCAN1_RX_SELECT_INPUT = 0x02;
  334. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_09 = 0x12; // pin 23 T4B1+B2
  335. IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_09 = 0x10B0; // pin 23 T4B1+B2
  336. }
  337. }
  338. }
  339. FCTPFD_FUNC void FCTPFD_OPT::enableDMA(bool state) { /* only CAN3 supports this on 1062, untested */
  340. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  341. FLEXCAN_EnterFreezeMode();
  342. ( !state ) ? FLEXCANb_MCR(_bus) &= ~0x8000 : FLEXCANb_MCR(_bus) |= 0x8000;
  343. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  344. }
  345. FCTPFD_FUNC void FCTPFD_OPT::writeIFLAG(uint64_t value) {
  346. FLEXCANb_IFLAG2(_bus) = value >> 32;
  347. FLEXCANb_IFLAG1(_bus) = value;
  348. }
  349. FCTPFD_FUNC void FCTPFD_OPT::writeIFLAGBit(uint8_t mb_num) {
  350. if ( mb_num < 32 ) FLEXCANb_IFLAG1(_bus) |= (1UL << mb_num);
  351. else FLEXCANb_IFLAG2(_bus) |= (1UL << (mb_num - 32));
  352. }
  353. FCTPFD_FUNC void FCTPFD_OPT::writeIMASK(uint64_t value) {
  354. FLEXCANb_IMASK2(_bus) = value >> 32;
  355. FLEXCANb_IMASK1(_bus) = value;
  356. }
  357. FCTPFD_FUNC void FCTPFD_OPT::writeIMASKBit(uint8_t mb_num, bool set) {
  358. if ( mb_num < 32 ) (( set ) ? FLEXCANb_IMASK1(_bus) |= (1UL << mb_num) : FLEXCANb_IMASK1(_bus) &= ~(1UL << mb_num));
  359. else (( set ) ? FLEXCANb_IMASK2(_bus) |= (1UL << (mb_num - 32)) : FLEXCANb_IMASK2(_bus) &= ~(1UL << (mb_num - 32)));
  360. }
  361. static void flexcan_isr_can3fd() {
  362. if ( _CAN3 ) _CAN3->flexcan_interrupt();
  363. }
  364. FCTPFD_FUNC void FCTPFD_OPT::enableMBInterrupts(bool status) {
  365. FLEXCAN_EnterFreezeMode();
  366. for ( uint8_t mb_num = 0, mbsize = 0; mb_num < max_mailboxes(); mb_num++ ) {
  367. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  368. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) continue; // skip TX mailboxes
  369. enableMBInterrupt((FLEXCAN_MAILBOX)mb_num, status);
  370. }
  371. FLEXCAN_ExitFreezeMode();
  372. }
  373. FCTPFD_FUNC void FCTPFD_OPT::enableMBInterrupt(const FLEXCAN_MAILBOX &mb_num, bool status) {
  374. FLEXCAN_EnterFreezeMode();
  375. if ( status ) writeIMASKBit(mb_num); /* enable mailbox interrupt */
  376. else writeIMASKBit(mb_num, 0); /* disable mailbox interrupt */
  377. FLEXCAN_ExitFreezeMode();
  378. }
  379. FCTPFD_FUNC void FCTPFD_OPT::onReceive(const FLEXCAN_MAILBOX &mb_num, _MBFD_ptr handler) {
  380. if ( FIFO == mb_num ) {
  381. _mbHandlers[0] = handler;
  382. return;
  383. }
  384. _mbHandlers[mb_num] = handler;
  385. }
  386. FCTPFD_FUNC void FCTPFD_OPT::onReceive(_MBFD_ptr handler) {
  387. _mainHandler = handler;
  388. }
  389. FCTPFD_FUNC void FCTPFD_OPT::mbCallbacks(const FLEXCAN_MAILBOX &mb_num, const CANFD_message_t &msg) {
  390. if ( mb_num == FIFO ) {
  391. if ( _mbHandlers[0] ) _mbHandlers[0](msg);
  392. return;
  393. }
  394. if ( _mbHandlers[mb_num] ) _mbHandlers[mb_num](msg);
  395. }
  396. FCTPFD_FUNC void FCTPFD_OPT::flexcan_interrupt() {
  397. CANFD_message_t msg; // setup a temporary storage buffer
  398. uint64_t imask = readIMASK(), iflag = readIFLAG();
  399. for ( uint8_t mb_num = 0, mbsize = 0; mb_num < max_mailboxes(); mb_num++ ) {
  400. if (!(imask & (1ULL << mb_num))) continue; /* don't read non-interrupt mailboxes */
  401. if (!(iflag & (1ULL << mb_num))) continue; /* don't read unflagged mailboxes */
  402. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  403. uint32_t code = mbxAddr[0];
  404. if ( ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_FULL ) ||
  405. ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_OVERRUN ) ) {
  406. msg.flags.extended = (bool)(code & (1UL << 21));
  407. msg.edl = (bool)(code & (1UL << 31));
  408. msg.brs = (bool)(code & (1UL << 30));
  409. msg.esi = (bool)(code & (1UL << 29));
  410. msg.id = (mbxAddr[1] & 0x1FFFFFFF) >> ((msg.flags.extended) ? 0 : 18);
  411. if ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_OVERRUN ) msg.flags.overrun = 1;
  412. msg.len = dlc_to_len((code & 0xF0000) >> 16);
  413. msg.mb = mb_num;
  414. msg.timestamp = code & 0xFFFF;
  415. msg.bus = busNumber;
  416. for ( uint8_t i = 0; i < (mbsize >> 2); i++ ) for ( int8_t d = 0; d < 4 ; d++ ) msg.buf[(4 * i) + 3 - d] = (uint8_t)(mbxAddr[2 + i] >> (8 * d));
  417. mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_EMPTY) | ((msg.flags.extended) ? (FLEXCAN_MB_CS_SRR | FLEXCAN_MB_CS_IDE) : 0);
  418. (void)FLEXCANb_TIMER(_bus);
  419. writeIFLAGBit(mb_num);
  420. if ( filter_match((FLEXCAN_MAILBOX)mb_num, msg.id) ) struct2queueRx(msg); /* store frame in queue */
  421. frame_distribution(msg);
  422. ext_outputFD1(msg);
  423. ext_outputFD2(msg);
  424. ext_outputFD3(msg);
  425. }
  426. }
  427. FLEXCANb_ESR1(_bus) = FLEXCANb_ESR1(_bus);
  428. }
  429. FCTPFD_FUNC void FCTPFD_OPT::struct2queueRx(const CANFD_message_t &msg) {
  430. uint8_t buf[sizeof(CANFD_message_t)];
  431. memmove(buf, &msg, sizeof(msg));
  432. rxBuffer.push_back(buf, sizeof(CANFD_message_t));
  433. }
  434. FCTPFD_FUNC void FCTPFD_OPT::struct2queueTx(const CANFD_message_t &msg) {
  435. uint8_t buf[sizeof(CANFD_message_t)];
  436. memmove(buf, &msg, sizeof(msg));
  437. txBuffer.push_back(buf, sizeof(CANFD_message_t));
  438. }
  439. FCTPFD_FUNC int FCTPFD_OPT::write(const CANFD_message_t &msg) {
  440. if ( msg.seq ) {
  441. if ( !write((FLEXCAN_MAILBOX)getFirstTxBox(), msg) ) struct2queueTx(msg);
  442. return 1;
  443. }
  444. for (uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++) {
  445. if (readIMASK() & (1ULL << i)) continue; /* don't write interrupt enabled mailboxes */
  446. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  447. if ( FLEXCAN_get_code(mbxAddr[0]) == FLEXCAN_MB_CODE_TX_INACTIVE ) {
  448. writeTxMailbox(i, msg);
  449. return 1; /* transmit entry accepted */
  450. }
  451. }
  452. return 0; /* transmit entry failed, no mailboxes or queues available */
  453. }
  454. FCTPFD_FUNC uint64_t FCTPFD_OPT::events() {
  455. if ( rxBuffer.size() ) {
  456. CANFD_message_t frame;
  457. uint8_t buf[sizeof(CANFD_message_t)];
  458. rxBuffer.pop_front(buf, sizeof(CANFD_message_t));
  459. memmove(&frame, buf, sizeof(frame));
  460. if ( _mbHandlers[frame.mb] ) _mbHandlers[frame.mb](frame);
  461. if ( _mainHandler ) _mainHandler(frame);
  462. }
  463. if ( txBuffer.size() ) {
  464. CANFD_message_t frame;
  465. uint8_t buf[sizeof(CANFD_message_t)];
  466. txBuffer.peek_front(buf, sizeof(CANFD_message_t));
  467. memmove(&frame, buf, sizeof(frame));
  468. if ( write((FLEXCAN_MAILBOX)getFirstTxBox(), frame) ) txBuffer.pop_front();
  469. }
  470. return (uint64_t)(rxBuffer.size() << 12) | txBuffer.size();
  471. }
  472. FCTPFD_FUNC int FCTPFD_OPT::read(CANFD_message_t &msg) {
  473. return readMB(msg);
  474. }
  475. FCTPFD_FUNC int FCTPFD_OPT::readMB(CANFD_message_t &msg) {
  476. uint8_t cycle_limit = 3;
  477. for ( uint8_t mbsize = 0; mailbox_reader_increment <= max_mailboxes(); ++mailbox_reader_increment ) {
  478. if ( mailbox_reader_increment >= max_mailboxes() ) {
  479. mailbox_reader_increment = 0;
  480. if ( !--cycle_limit ) return 0;
  481. }
  482. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mailbox_reader_increment , mbsize)));
  483. if ((readIMASK() & (1ULL << mailbox_reader_increment))) continue; /* don't read interrupt enabled mailboxes */
  484. uint32_t code = mbxAddr[0];
  485. if ( (FLEXCAN_get_code(code) >> 3) ) continue; /* skip TX mailboxes */
  486. // if (!(code & 0x600000) && !(readIFLAG() & (1ULL << mailbox_reader_increment))) continue; /* don't read unflagged mailboxes, errata: extended mailboxes iflags do not work in poll mode, must check CS field */
  487. if ( ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_FULL ) ||
  488. ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_OVERRUN ) ) {
  489. msg.flags.extended = (bool)(code & (1UL << 21));
  490. msg.edl = (bool)(code & (1UL << 31));
  491. msg.brs = (bool)(code & (1UL << 30));
  492. msg.esi = (bool)(code & (1UL << 29));
  493. msg.id = (mbxAddr[1] & 0x1FFFFFFF) >> ((msg.flags.extended) ? 0 : 18);
  494. if ( FLEXCAN_get_code(code) == FLEXCAN_MB_CODE_RX_OVERRUN ) msg.flags.overrun = 1;
  495. msg.len = dlc_to_len((code & 0xF0000) >> 16);
  496. msg.mb = mailbox_reader_increment++;
  497. msg.timestamp = code & 0xFFFF;
  498. msg.bus = busNumber;
  499. for ( uint8_t i = 0; i < (mbsize >> 2); i++ ) for ( int8_t d = 0; d < 4 ; d++ ) msg.buf[(4 * i) + 3 - d] = (uint8_t)(mbxAddr[2 + i] >> (8 * d));
  500. mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_EMPTY) | ((msg.flags.extended) ? (FLEXCAN_MB_CS_SRR | FLEXCAN_MB_CS_IDE) : 0);
  501. (void)FLEXCANb_TIMER(_bus); /* Read free-running timer to unlock Rx Message Buffer. */
  502. writeIFLAGBit(msg.mb);
  503. frame_distribution(msg);
  504. if ( filter_match((FLEXCAN_MAILBOX)msg.mb, msg.id) ) return 1;
  505. }
  506. }
  507. return 0; /* no messages available */
  508. }
  509. FCTPFD_FUNC void FCTPFD_OPT::enableFIFO(bool status) {
  510. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  511. FLEXCAN_EnterFreezeMode();
  512. writeIMASK(0ULL); // disable all FIFO/MB Interrupts
  513. FLEXCANb_RXMGMASK(_bus) = FLEXCANb_RXFGMASK(_bus) = 0;
  514. writeIFLAG(readIFLAG()); // (all bits reset when written back)
  515. FLEXCANb_MCR(_bus) &= ~0x20008000; /* we disable DMA, Legacy FIFO (we never use this in FD mode!) */
  516. for (uint8_t i = 0; i < max_mailboxes(); i++ ) FLEXCANb_RXIMR(_bus, i) = 0 | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0); // CLEAR MAILBOX MASKS (RXIMR)
  517. for (uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++) { /* clear valid MB codes from memory regions */
  518. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  519. mbxAddr[0] = mbxAddr[1] = 0;
  520. }
  521. if ( status ) { /* enable FIFO */
  522. // TODO: FD FIFO, ### WARNING: NXP claims, after being tested to fail despite documented and advertised, that the 1062 does NOT support FD FIFO mode!!!
  523. }
  524. else { // FIFO disabled default setup of mailboxes, 0-7 RX, 8-15 TX
  525. for (uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++ ) { // clear all mailboxes
  526. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  527. if ( i < max_mailboxes() / 2 ) {
  528. mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_EMPTY) | ((i < (max_mailboxes() / 4)) ? 0 : FLEXCAN_MB_CS_IDE | FLEXCAN_MB_CS_SRR);
  529. FLEXCANb_RXIMR(_bus, i) = 0UL | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0); // (RXIMR)
  530. }
  531. else {
  532. mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE);
  533. }
  534. }
  535. }
  536. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  537. }
  538. FCTPFD_FUNC void FCTPFD_OPT::setMBFilterProcessing(FLEXCAN_MAILBOX mb_num, uint32_t filter_id, uint32_t calculated_mask) {
  539. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  540. FLEXCAN_EnterFreezeMode();
  541. uint8_t mbsize = 0;
  542. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  543. FLEXCANb_RXIMR(_bus, mb_num) = calculated_mask | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0);
  544. mbxAddr[1] = ((!(mbxAddr[0] & FLEXCAN_MB_CS_IDE)) ? FLEXCAN_MB_ID_IDSTD(filter_id) : FLEXCAN_MB_ID_IDEXT(filter_id));
  545. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  546. }
  547. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilterRange(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2) {
  548. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  549. uint8_t mbsize = 0;
  550. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  551. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  552. if ( id1 > id2 || ((id2 > id1) && (id2-id1>1000)) || !id1 || !id2 ) return 0; /* don't play around... */
  553. uint32_t stage1 = id1, stage2 = id1;
  554. for ( uint32_t i = id1 + 1; i <= id2; i++ ) {
  555. stage1 |= i; stage2 &= i;
  556. }
  557. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD( (stage1 ^ stage2) ^ 0x1FFFFFFF ) : FLEXCAN_MB_ID_IDEXT( (stage1 ^ stage2) ^ 0x1FFFFFFF );
  558. setMBFilterProcessing(mb_num,id1,mask);
  559. filter_store(FLEXCAN_RANGE, mb_num, 2, id1, id2, 0, 0, 0, mask);
  560. return 1;
  561. }
  562. FCTPFD_FUNC void FCTPFD_OPT::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) {
  563. mb_filter_table[mb_num][0] = mb_num; // first 7 bits reserved for MB
  564. mb_filter_table[mb_num][0] |= (id_count << 7); // we store the quantity of ids after the mailboxes
  565. /* bit 28: filter enabled */
  566. mb_filter_table[mb_num][0] |= (type << 29); // we reserve 3 upper bits for type
  567. uint8_t mbsize;
  568. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  569. mb_filter_table[mb_num][0] |= ( ((mbxAddr[0] & 0x600000) ? 1UL : 0UL) << 27); /* extended flag check */
  570. mb_filter_table[mb_num][1] = id1; // id1
  571. mb_filter_table[mb_num][2] = id2; // id2
  572. mb_filter_table[mb_num][3] = id3; // id3
  573. mb_filter_table[mb_num][4] = id4; // id4
  574. mb_filter_table[mb_num][5] = id5; // id5
  575. mb_filter_table[mb_num][6] = mask; // mask
  576. }
  577. FCTPFD_FUNC void FCTPFD_OPT::enhanceFilter(FLEXCAN_MAILBOX mb_num) {
  578. if ( !(mb_filter_table[mb_num][0] & 0xE0000000) ) return;
  579. mb_filter_table[mb_num][0] |= (1UL << 28); /* enable enhancement */
  580. }
  581. FCTPFD_FUNC bool FCTPFD_OPT::filter_match(FLEXCAN_MAILBOX mb_num, uint32_t id) {
  582. if ( !(mb_filter_table[mb_num][0] & 0x10000000) ) return 1;
  583. if ( (mb_filter_table[mb_num][0] >> 29) == FLEXCAN_MULTI ) {
  584. for ( uint8_t i = 0; i < ((mb_filter_table[mb_num][0] & 0x380) >> 7); i++) if ( id == mb_filter_table[mb_num][i+1] ) return 1;
  585. }
  586. else if ( (mb_filter_table[mb_num][0] >> 29) == FLEXCAN_RANGE ) {
  587. if ( id >= mb_filter_table[mb_num][1] && id <= mb_filter_table[mb_num][2] ) return 1;
  588. }
  589. return 0;
  590. }
  591. FCTPFD_FUNC void FCTPFD_OPT::frame_distribution(CANFD_message_t &msg) {
  592. if ( !distribution ) return; /* distribution not enabled */
  593. CANFD_message_t frame = msg;
  594. for ( uint8_t i = 0; i < max_mailboxes(); i++ ) {
  595. if ( frame.mb == i ) continue; // don't distribute to same mailbox
  596. if ( !(mb_filter_table[i][0] & 0xE0000000) ) continue; // skip unset filters
  597. if ( (bool)(mb_filter_table[i][0] & (1UL << 27)) != msg.flags.extended ) continue; /* extended flag check */
  598. if ( (mb_filter_table[i][0] >> 29) == FLEXCAN_MULTI ) {
  599. for ( uint8_t p = 0; p < ((mb_filter_table[i][0] & 0x380) >> 7); p++) {
  600. if ( frame.id == mb_filter_table[i][p+1] ) {
  601. frame.mb = i;
  602. struct2queueRx(frame);
  603. }
  604. }
  605. }
  606. else if ( (mb_filter_table[i][0] >> 29) == FLEXCAN_RANGE ) {
  607. if ( frame.id >= mb_filter_table[i][1] && frame.id <= mb_filter_table[i][2] ) {
  608. frame.mb = i;
  609. struct2queueRx(frame);
  610. }
  611. }
  612. }
  613. }
  614. FCTPFD_FUNC void FCTPFD_OPT::setMBFilter(FLEXCAN_FLTEN input) {
  615. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  616. FLEXCAN_EnterFreezeMode();
  617. for (uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++) {
  618. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  619. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) continue; /* skip TX mailboxes */
  620. if ( input == ACCEPT_ALL ) FLEXCANb_RXIMR(_bus, i) = 0UL | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0); // (RXIMR)
  621. if ( input == REJECT_ALL ) FLEXCANb_RXIMR(_bus, i) = ~0UL; // (RXIMR)
  622. mbxAddr[1] = 0UL;
  623. mb_filter_table[i][0] = ( ((mbxAddr[0] & 0x600000) ? 1UL : 0UL) << 27); /* extended flag check */
  624. }
  625. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  626. }
  627. FCTPFD_FUNC void FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, FLEXCAN_FLTEN input) {
  628. if ( mb_num >= max_mailboxes() ) return; /* mailbox not available */
  629. uint8_t mbsize = 0;
  630. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  631. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return; /* exit on TX mailbox */
  632. bool frz_flag_negate = !(FLEXCANb_MCR(_bus) & FLEXCAN_MCR_FRZ_ACK);
  633. FLEXCAN_EnterFreezeMode();
  634. if ( input == ACCEPT_ALL ) FLEXCANb_RXIMR(_bus, mb_num) = 0UL | ((FLEXCANb_CTRL2(_bus) & FLEXCAN_CTRL2_EACEN) ? (1UL << 30) : 0); // (RXIMR)
  635. if ( input == REJECT_ALL ) FLEXCANb_RXIMR(_bus, mb_num) = ~0UL; // (RXIMR)
  636. mbxAddr[1] = 0UL;
  637. mb_filter_table[mb_num][0] = ( ((mbxAddr[0] & 0x600000) ? 1UL : 0UL) << 27); /* extended flag check */
  638. if ( frz_flag_negate ) FLEXCAN_ExitFreezeMode();
  639. }
  640. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1) {
  641. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  642. uint8_t mbsize = 0;
  643. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  644. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  645. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD(((id1) ^ (id1)) ^ 0x7FF) : FLEXCAN_MB_ID_IDEXT(((id1) ^ (id1)) ^ 0x1FFFFFFF);
  646. setMBFilterProcessing(mb_num,id1,mask);
  647. filter_store(FLEXCAN_MULTI, mb_num, 1, id1, 0, 0, 0, 0, mask);
  648. return 1;
  649. }
  650. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2) {
  651. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  652. uint8_t mbsize = 0;
  653. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  654. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  655. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD(((id1 | id2) ^ (id1 & id2)) ^ 0x7FF) : FLEXCAN_MB_ID_IDEXT(((id1 | id2) ^ (id1 & id2)) ^ 0x1FFFFFFF);
  656. setMBFilterProcessing(mb_num,id1,mask);
  657. filter_store(FLEXCAN_MULTI, mb_num, 2, id1, id2, 0, 0, 0, mask);
  658. return 1;
  659. }
  660. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3) {
  661. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  662. uint8_t mbsize = 0;
  663. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  664. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  665. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD(((id1 | id2 | id3) ^ (id1 & id2 & id3)) ^ 0x7FF) : FLEXCAN_MB_ID_IDEXT(((id1 | id2 | id3) ^ (id1 & id2 & id3)) ^ 0x1FFFFFFF);
  666. setMBFilterProcessing(mb_num,id1,mask);
  667. filter_store(FLEXCAN_MULTI, mb_num, 3, id1, id2, id3, 0, 0, mask);
  668. return 1;
  669. }
  670. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4) {
  671. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  672. uint8_t mbsize = 0;
  673. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  674. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  675. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD(((id1 | id2 | id3 | id4) ^ (id1 & id2 & id3 & id4)) ^ 0x7FF) : FLEXCAN_MB_ID_IDEXT(((id1 | id2 | id3 | id4) ^ (id1 & id2 & id3 & id4)) ^ 0x1FFFFFFF);
  676. setMBFilterProcessing(mb_num,id1,mask);
  677. filter_store(FLEXCAN_MULTI, mb_num, 4, id1, id2, id3, id4, 0, mask);
  678. return 1;
  679. }
  680. FCTPFD_FUNC bool FCTPFD_OPT::setMBFilter(FLEXCAN_MAILBOX mb_num, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4, uint32_t id5) {
  681. if ( mb_num >= max_mailboxes() ) return 0; /* mailbox not available */
  682. uint8_t mbsize = 0;
  683. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
  684. if ( (FLEXCAN_get_code(mbxAddr[0]) >> 3) ) return 0; /* exit on TX mailbox */
  685. uint32_t mask = ( !(mbxAddr[0] & FLEXCAN_MB_CS_IDE) ) ? FLEXCAN_MB_ID_IDSTD(((id1 | id2 | id3 | id4 | id5) ^ (id1 & id2 & id3 & id4 & id5)) ^ 0x7FF) : FLEXCAN_MB_ID_IDEXT(((id1 | id2 | id3 | id4 | id5) ^ (id1 & id2 & id3 & id4 & id5)) ^ 0x1FFFFFFF);
  686. setMBFilterProcessing(mb_num,id1,mask);
  687. filter_store(FLEXCAN_MULTI, mb_num, 5, id1, id2, id3, id4, id5, mask);
  688. return 1;
  689. }
  690. FCTPFD_FUNC void FCTPFD_OPT::mailboxStatus() {
  691. Serial.print("FIFO Disabled\n\tMailboxes:\n");
  692. for ( uint8_t i = 0, mbsize = 0; i < max_mailboxes(); i++ ) {
  693. volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(i, mbsize)));
  694. switch ( FLEXCAN_get_code(mbxAddr[0]) ) {
  695. case 0b0000: {
  696. Serial.printf("\t\tMB%u%s\n", i, " code: RX_INACTIVE");
  697. break;
  698. }
  699. case 0b0100: {
  700. Serial.printf("\t\tMB%u%s%s\n", i, " code: RX_EMPTY",((mbxAddr[0] & FLEXCAN_MB_CS_IDE)?"\t(Extended Frame)":"\t(Standard Frame)"));
  701. break;
  702. }
  703. case 0b0010: {
  704. Serial.printf("\t\tMB%u%s\n", i, " code: RX_FULL");
  705. break;
  706. }
  707. case 0b0110: {
  708. Serial.printf("\t\tMB%u%s\n", i, " code: RX_OVERRUN");
  709. break;
  710. }
  711. case 0b1010: {
  712. Serial.printf("\t\tMB%u%s\n", i, " code: RX_ANSWER");
  713. break;
  714. }
  715. case 0b0001: {
  716. Serial.printf("\t\tMB%u%s\n", i, " code: RX_BUSY");
  717. break;
  718. }
  719. case 0b1000: {
  720. Serial.printf("\t\tMB%u%s\n", i, " code: TX_INACTIVE");
  721. break;
  722. }
  723. case 0b1001: {
  724. Serial.printf("\t\tMB%u%s\n", i, " code: TX_ABORT");
  725. break;
  726. }
  727. case 0b1100: {
  728. Serial.printf("\t\tMB%u%s", i, " code: TX_DATA (Transmitting)");
  729. uint32_t extid = (mbxAddr[0] & FLEXCAN_MB_CS_IDE);
  730. (extid) ? Serial.print("(Extended Frame)") : Serial.print("(Standard Frame)");
  731. uint32_t dataIn = mbxAddr[2];
  732. uint32_t id = (FLEXCANb_MBn_ID(_bus, i) & FLEXCAN_MB_ID_EXT_MASK);
  733. if (!extid) id >>= FLEXCAN_MB_ID_STD_BIT_NO;
  734. Serial.print("(ID: 0x"); Serial.print(id, HEX); Serial.print(")");
  735. Serial.print("(Payload: "); Serial.print((uint8_t)(dataIn >> 24), HEX);
  736. Serial.print(" "); Serial.print((uint8_t)(dataIn >> 16), HEX);
  737. Serial.print(" "); Serial.print((uint8_t)(dataIn >> 8), HEX);
  738. Serial.print(" "); Serial.print((uint8_t)dataIn, HEX);
  739. dataIn = mbxAddr[3];
  740. Serial.print(" "); Serial.print((uint8_t)(dataIn >> 24), HEX);
  741. Serial.print(" "); Serial.print((uint8_t)(dataIn >> 16), HEX);
  742. Serial.print(" "); Serial.print((uint8_t)(dataIn >> 8), HEX);
  743. Serial.print(" "); Serial.print((uint8_t)dataIn, HEX);
  744. Serial.println(")");
  745. break;
  746. }
  747. case 0b1110: {
  748. Serial.printf("\t\tMB%u%s\n", i, " code: TX_ANSWER");
  749. break;
  750. }
  751. }
  752. } // for loop
  753. }
  754. extern void __attribute__((weak)) ext_outputFD1(const CANFD_message_t &msg);
  755. extern void __attribute__((weak)) ext_outputFD2(const CANFD_message_t &msg);
  756. extern void __attribute__((weak)) ext_outputFD3(const CANFD_message_t &msg);