hal_can.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "hal_can.h"
  2. #define MCAN_RX_INT_SEL (MCAN_INT_RX_FIFO0_NEW_MSG|MCAN_INT_RX_FIFO0_FULL |MCAN_INT_RX_FIFO1_NEW_MSG |MCAN_INT_RX_FIFO1_FULL |MCAN_INT_RX_BUF_NEW_MSG)
  3. #define MCAN_TX_INT_SEL (MCAN_INT_TX_CPLT |MCAN_INT_TX_EVT_FIFO_NEW_DATA |MCAN_INT_BUS_OFF)
  4. #define MCAN_INT0_SEL MCAN_RX_INT_SEL
  5. #define MCAN_INT1_SEL MCAN_TX_INT_SEL
  6. /* Message RAM */
  7. /* Each standard filter element size is 4 bytes */
  8. #define MCAN_STD_FILTER_NUM (1U)
  9. /* Each extended filter element size is 8 bytes */
  10. #define MCAN_EXT_FILTER_NUM (1U)
  11. /* Each Rx FIFO0 element size is 8+8 bytes */
  12. #define MCAN_RX_FIFO0_NUM (10U)
  13. #define MCAN_RX_FIFO0_WATERMARK (8U)
  14. #define MCAN_RX_FIFO0_DATA_FIELD_SIZE MCAN_DATA_SIZE_8BYTE
  15. /* Each Rx FIFO1 element size is 8+8 bytes */
  16. #define MCAN_RX_FIFO1_NUM (10U)
  17. #define MCAN_RX_FIFO1_WATERMARK (7U)
  18. #define MCAN_RX_FIFO1_DATA_FIELD_SIZE MCAN_DATA_SIZE_8BYTE
  19. /* Each Rx buffer element size is 8+8 bytes */
  20. #define MCAN_RX_BUF_NUM (6U)
  21. #define MCAN_RX_BUF_DATA_FIELD_SIZE MCAN_DATA_SIZE_8BYTE
  22. /* Each Tx buffer element size is 8+8 bytes */
  23. #define MCAN_TX_BUF_NUM (12U)
  24. #define MCAN_TX_FIFO_NUM (6U)
  25. #define MCAN_TX_BUF_DATA_FIELD_SIZE MCAN_DATA_SIZE_8BYTE
  26. #define MCAN_TX_NOTIFICATION_BUF ((1UL << (MCAN_TX_BUF_NUM + MCAN_TX_FIFO_NUM)) - 1U)
  27. /* Each extended filter element size is 8 bytes */
  28. #define MCAN_TX_EVT_NUM (12U)
  29. /* Filter */
  30. #define MCAN_CFG_IGNOR (0U)
  31. /* Accept standard frames with ID from 0x110 to 0x11F and store to Rx FIFO0 */
  32. #define MCAN_STD_FILTER0 {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 0U, .u32FilterType = MCAN_FILTER_RANGE, \
  33. .u32FilterConfig = MCAN_FILTER_TO_RX_FIFO0, .u32FilterId1 = 0x00UL, \
  34. .u32FilterId2 = 0x7FFUL,}
  35. ///* Accept standard frames with ID 0x130~0x132 and store to dedicated Rx buffer 0~2 */
  36. //#define MCAN_STD_FILTER1 {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 1U, .u32FilterType = MCAN_CFG_IGNOR, \
  37. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x130UL, \
  38. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF0}
  39. //#define MCAN_STD_FILTER2 {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 2U, .u32FilterType = MCAN_CFG_IGNOR, \
  40. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x131UL, \
  41. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF1}
  42. //#define MCAN_STD_FILTER3 {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 3U, .u32FilterType = MCAN_CFG_IGNOR, \
  43. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x132UL, \
  44. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF2}
  45. /* Accept extended frames with ID from 0x12345110 to 0x1234511F and store to Rx FIFO1 */
  46. #define MCAN_EXT_FILTER0 {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 0U, .u32FilterType = MCAN_FILTER_RANGE, \
  47. .u32FilterConfig = MCAN_FILTER_TO_RX_FIFO1, .u32FilterId1 = 0x0UL, \
  48. .u32FilterId2 = 0x1FFFFFFFUL,}
  49. ///* Accept extended frames with ID 0x12345130~0x12345132 and store to dedicated Rx buffer 3~5 */
  50. //#define MCAN_EXT_FILTER1 {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 1U, .u32FilterType = MCAN_CFG_IGNOR, \
  51. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345130UL, \
  52. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF3}
  53. //#define MCAN_EXT_FILTER2 {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 2U, .u32FilterType = MCAN_CFG_IGNOR, \
  54. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345131UL, \
  55. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF4}
  56. //#define MCAN_EXT_FILTER3 {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 3U, .u32FilterType = MCAN_CFG_IGNOR, \
  57. // .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345132UL, \
  58. // .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF5}
  59. QueueHandle_t can1_recieve1_data_queue;
  60. QueueHandle_t can1_recieve2_data_queue;
  61. QueueHandle_t can2_recieve1_data_queue;
  62. /***************************************CAN1****************************************************/
  63. void bsp_can1_init(void)
  64. {
  65. GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_01, GPIO_FUNC_56);//CAN1 TX PB01
  66. GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_00, GPIO_FUNC_57);//CAN1 RX PB00
  67. /* SET STB pin high before output */
  68. stc_gpio_init_t stcGpioInit;
  69. GPIO_StructInit(&stcGpioInit);
  70. stcGpioInit.u16PinState = PIN_STAT_SET;
  71. stcGpioInit.u16PinDir = PIN_DIR_OUT;
  72. GPIO_Init(GPIO_PORT_B, GPIO_PIN_02, &stcGpioInit);
  73. GPIO_ResetPins(GPIO_PORT_B,GPIO_PIN_02);
  74. stc_mcan_init_t stcMcanInit;
  75. stc_mcan_filter_t stcStdFilterList[MCAN_STD_FILTER_NUM] = {
  76. MCAN_STD_FILTER0/*, MCAN_STD_FILTER1, MCAN_STD_FILTER2, MCAN_STD_FILTER3*/
  77. };
  78. stc_mcan_filter_t stcExtFilterList[MCAN_EXT_FILTER_NUM] = {
  79. MCAN_EXT_FILTER0/*, MCAN_EXT_FILTER1, MCAN_EXT_FILTER2, MCAN_EXT_FILTER3*/
  80. };
  81. CLK_SetCANClockSrc(CLK_MCAN1, CLK_MCANCLK_SYSCLK_DIV5);//5分频,200/5=40MHZ给到CAN,
  82. MCAN_StructInit(&stcMcanInit);
  83. stcMcanInit.u32Mode = MCAN_MD_NORMAL;
  84. stcMcanInit.u32FrameFormat = MCAN_FRAME_CLASSIC;
  85. /* Classic CAN. Baudrate 1Mbps, sample point 80% */ //40MHZ
  86. stcMcanInit.stcCanClassic.u32Prescaler = 8U; //波特率改这里
  87. stcMcanInit.stcCanClassic.u32TimeSeg1 = 16U;
  88. stcMcanInit.stcCanClassic.u32TimeSeg2 = 4U;
  89. stcMcanInit.stcCanClassic.u32SyncJumpWidth = 4U;
  90. /* Message RAM */
  91. stcMcanInit.stcMsgRam.u32AddrOffset = 0U;
  92. stcMcanInit.stcMsgRam.u32StdFilterNum = MCAN_STD_FILTER_NUM;
  93. stcMcanInit.stcMsgRam.u32ExtFilterNum = MCAN_EXT_FILTER_NUM;
  94. stcMcanInit.stcMsgRam.u32RxFifo0Num = MCAN_RX_FIFO0_NUM;
  95. stcMcanInit.stcMsgRam.u32RxFifo0DataSize = MCAN_RX_FIFO0_DATA_FIELD_SIZE;
  96. stcMcanInit.stcMsgRam.u32RxFifo1Num = MCAN_RX_FIFO1_NUM;
  97. stcMcanInit.stcMsgRam.u32RxFifo1DataSize = MCAN_RX_FIFO1_DATA_FIELD_SIZE;
  98. stcMcanInit.stcMsgRam.u32RxBufferNum = MCAN_RX_BUF_NUM;
  99. stcMcanInit.stcMsgRam.u32RxBufferDataSize = MCAN_RX_BUF_DATA_FIELD_SIZE;
  100. stcMcanInit.stcMsgRam.u32TxBufferNum = MCAN_TX_BUF_NUM;
  101. stcMcanInit.stcMsgRam.u32TxFifoQueueNum = MCAN_TX_FIFO_NUM;
  102. stcMcanInit.stcMsgRam.u32TxFifoQueueMode = MCAN_TX_FIFO_MD;
  103. stcMcanInit.stcMsgRam.u32TxDataSize = MCAN_TX_BUF_DATA_FIELD_SIZE;
  104. stcMcanInit.stcMsgRam.u32TxEventNum = MCAN_TX_EVT_NUM;
  105. /* Acceptance filter */
  106. stcMcanInit.stcFilter.pstcStdFilterList = stcStdFilterList;
  107. stcMcanInit.stcFilter.pstcExtFilterList = stcExtFilterList;
  108. stcMcanInit.stcFilter.u32StdFilterConfigNum = stcMcanInit.stcMsgRam.u32StdFilterNum;
  109. stcMcanInit.stcFilter.u32ExtFilterConfigNum = stcMcanInit.stcMsgRam.u32ExtFilterNum;
  110. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_MCAN1, DISABLE);
  111. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_MCAN1, ENABLE);
  112. MCAN_Init(CM_MCAN1, &stcMcanInit);
  113. /* Watermark if needed */
  114. MCAN_SetFifoWatermark(CM_MCAN1, MCAN_WATERMARK_RX_FIFO0, MCAN_RX_FIFO0_WATERMARK);
  115. MCAN_SetFifoWatermark(CM_MCAN1, MCAN_WATERMARK_RX_FIFO1, MCAN_RX_FIFO1_WATERMARK);
  116. /* Configure Rx FIFO0 operation mode if needed.
  117. If new message received when Rx FIFO0 is full, the new message
  118. will not be stored and Message Lost Interrupt will be generated. */
  119. MCAN_RxFifoOperationModeConfig(CM_MCAN1, MCAN_RX_FIFO0, MCAN_RX_FIFO_BLOCKING);
  120. /* Configure Rx FIFO1 operation mode if needed.
  121. If new message received when Rx FIFO1 is full, the new received message
  122. will overwrite the oldest received message and RF1N Interrupt generated. */
  123. MCAN_RxFifoOperationModeConfig(CM_MCAN1, MCAN_RX_FIFO1, MCAN_RX_FIFO_BLOCKING);
  124. /* Configures timestamp if needed */
  125. MCAN_TimestampCounterConfig(CM_MCAN1, 1U);
  126. MCAN_TimestampCounterCmd(CM_MCAN1, ENABLE);
  127. /* The Tx buffer can cause transmission completed completed interrupt
  128. only when its own transmission completed interrupt is enabled. */
  129. MCAN_TxBufferNotificationCmd(CM_MCAN1, MCAN_TX_NOTIFICATION_BUF, MCAN_INT_TX_CPLT, ENABLE);
  130. MCAN_IntCmd(CM_MCAN1, MCAN_INT0_SEL, MCAN_INT_LINE0, ENABLE);
  131. NVIC_ClearPendingIRQ(MCAN1_INT0_IRQn);
  132. NVIC_SetPriority(MCAN1_INT0_IRQn, DDL_IRQ_PRIO_06);
  133. NVIC_EnableIRQ(MCAN1_INT0_IRQn);
  134. MCAN_Start(CM_MCAN1);
  135. }
  136. void MCAN1_INT0_Handler(void)
  137. {
  138. uint32_t u32RxBuffer;
  139. uint32_t u32TxBufferIndex;
  140. uint32_t u32TxBuffer = 0U;
  141. stc_mcan_rx_msg_t stcRxMsg;
  142. stc_mcan_tx_msg_t stcTxMsg;
  143. stc_mcan_rx_msg_t stcRxMsgRxFifo1[MCAN_RX_FIFO1_NUM];
  144. static uint32_t pxHigherPriorityTaskWoken = pdFALSE;
  145. /* Rx FIFO0 related interrupts */
  146. if (MCAN_GetStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO0_NEW_MSG) == SET) {
  147. MCAN_ClearStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO0_NEW_MSG);
  148. /* Messages stored in Rx FIFO0 can be read here */
  149. printf("Rx FIFO_0 new message->\r\n");
  150. if (MCAN_GetRxMsg(CM_MCAN1, MCAN_RX_FIFO0, &stcRxMsg) == LL_OK) {
  151. //printf("stcRxMsg.IDE=%d,stcRxMsg.ID=%x\r\n",stcRxMsg.IDE,stcRxMsg.ID);
  152. for(int i =0;i <8;i++)
  153. {
  154. printf("%x ",stcRxMsg.au8Data[i]);
  155. }
  156. hc32_can_rx_msg_t hc32_can_rx_msg;
  157. hc32_can_rx_msg.ide =stcRxMsg.IDE;
  158. hc32_can_rx_msg.id = stcRxMsg.ID;
  159. hc32_can_rx_msg.len = 8;//stcRxMsg.DLC;//直接赋值
  160. memcpy(hc32_can_rx_msg.data,stcRxMsg.au8Data,8);
  161. if(stcRxMsg.ID==MSG_RECEIVE_ID_STD)
  162. xQueueSendFromISR(can1_recieve1_data_queue,&stcRxMsg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  163. else
  164. xQueueOverwriteFromISR(can1_recieve2_data_queue,&hc32_can_rx_msg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  165. portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
  166. }
  167. }
  168. if (MCAN_GetStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO0_FULL) == SET)
  169. {
  170. MCAN_ClearStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO0_FULL);
  171. }
  172. /* Rx FIFO1 related interrupts */
  173. if (MCAN_GetStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO1_NEW_MSG) == SET)
  174. {
  175. MCAN_ClearStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO1_NEW_MSG);
  176. printf("Rx FIFO_1 new message->\r\n");
  177. if (MCAN_GetRxMsg(CM_MCAN1, MCAN_RX_FIFO1, &stcRxMsg) == LL_OK) {
  178. //printf("stcRxMsg.IDE=%d,stcRxMsg.ID=%x\r\n",stcRxMsg.IDE,stcRxMsg.ID);
  179. for(int i =0;i <8;i++)
  180. {
  181. printf("%x ",stcRxMsg.au8Data[i]);
  182. }
  183. hc32_can_rx_msg_t hc32_can_rx_msg;
  184. hc32_can_rx_msg.ide =stcRxMsg.IDE;
  185. hc32_can_rx_msg.id = stcRxMsg.ID;
  186. hc32_can_rx_msg.len = 8;//stcRxMsg.DLC;//直接赋值
  187. memcpy(hc32_can_rx_msg.data,stcRxMsg.au8Data,8);
  188. if(stcRxMsg.ID==MSG_RECEIVE_ID_STD)
  189. xQueueOverwriteFromISR(can1_recieve1_data_queue,&stcRxMsg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  190. else
  191. xQueueOverwriteFromISR(can1_recieve2_data_queue,&hc32_can_rx_msg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  192. portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
  193. }
  194. }
  195. if (MCAN_GetStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO1_FULL) == SET) {
  196. MCAN_ClearStatus(CM_MCAN1, MCAN_FLAG_RX_FIFO1_FULL);
  197. /* All dedicated Tx buffer are filled. Add transmission requests. */
  198. MCAN_EnableTxBufferRequest(CM_MCAN1, u32TxBuffer);
  199. }
  200. /* Dedicated Rx buffer related interrupts */
  201. if (MCAN_GetStatus(CM_MCAN1, MCAN_FLAG_RX_BUF_NEW_MSG) == SET) {
  202. MCAN_ClearStatus(CM_MCAN1, MCAN_FLAG_RX_BUF_NEW_MSG);
  203. }
  204. }
  205. ///***************************************CAN2****************************************************/
  206. void bsp_can2_init(void)
  207. {
  208. // GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_00, GPIO_FUNC_56);//CAN1 TX PB00
  209. // GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_01, GPIO_FUNC_57);//CAN1 RX PB01
  210. /*测试脚*/
  211. GPIO_SetFunc(GPIO_PORT_C, GPIO_PIN_12, GPIO_FUNC_56);//CAN1 TX PC12
  212. GPIO_SetFunc(GPIO_PORT_D, GPIO_PIN_00, GPIO_FUNC_57);//CAN1 RX PD00
  213. /* SET STB pin high before output */
  214. stc_gpio_init_t stcGpioInit;
  215. GPIO_StructInit(&stcGpioInit);
  216. stcGpioInit.u16PinState = PIN_STAT_RST;
  217. stcGpioInit.u16PinDir = PIN_DIR_OUT;
  218. GPIO_Init(GPIO_PORT_B, GPIO_PIN_02, &stcGpioInit);
  219. GPIO_ResetPins(GPIO_PORT_B,GPIO_PIN_02);
  220. stc_mcan_init_t stcMcanInit;
  221. stc_mcan_filter_t stcStdFilterList[MCAN_STD_FILTER_NUM] = {
  222. MCAN_STD_FILTER0/*, MCAN_STD_FILTER1, MCAN_STD_FILTER2, MCAN_STD_FILTER3*/
  223. };
  224. stc_mcan_filter_t stcExtFilterList[MCAN_EXT_FILTER_NUM] = {
  225. MCAN_EXT_FILTER0/*, MCAN_EXT_FILTER1, MCAN_EXT_FILTER2, MCAN_EXT_FILTER3*/
  226. };
  227. CLK_SetCANClockSrc(CLK_MCAN2, CLK_MCANCLK_SYSCLK_DIV5);//5分频,200/5=40MHZ给到CAN,
  228. MCAN_StructInit(&stcMcanInit);
  229. stcMcanInit.u32Mode = MCAN_MD_NORMAL;
  230. stcMcanInit.u32FrameFormat = MCAN_FRAME_CLASSIC;
  231. /* Classic CAN. Baudrate 1Mbps, sample point 80% */ //40MHZ
  232. stcMcanInit.stcCanClassic.u32Prescaler = 8U; //波特率改这里
  233. stcMcanInit.stcCanClassic.u32TimeSeg1 = 16U;
  234. stcMcanInit.stcCanClassic.u32TimeSeg2 = 4U;
  235. stcMcanInit.stcCanClassic.u32SyncJumpWidth = 4U;
  236. /* Message RAM */
  237. stcMcanInit.stcMsgRam.u32AddrOffset = 0U;
  238. stcMcanInit.stcMsgRam.u32StdFilterNum = MCAN_STD_FILTER_NUM;
  239. stcMcanInit.stcMsgRam.u32ExtFilterNum = MCAN_EXT_FILTER_NUM;
  240. stcMcanInit.stcMsgRam.u32RxFifo0Num = MCAN_RX_FIFO0_NUM;
  241. stcMcanInit.stcMsgRam.u32RxFifo0DataSize = MCAN_RX_FIFO0_DATA_FIELD_SIZE;
  242. stcMcanInit.stcMsgRam.u32RxFifo1Num = MCAN_RX_FIFO1_NUM;
  243. stcMcanInit.stcMsgRam.u32RxFifo1DataSize = MCAN_RX_FIFO1_DATA_FIELD_SIZE;
  244. stcMcanInit.stcMsgRam.u32RxBufferNum = MCAN_RX_BUF_NUM;
  245. stcMcanInit.stcMsgRam.u32RxBufferDataSize = MCAN_RX_BUF_DATA_FIELD_SIZE;
  246. stcMcanInit.stcMsgRam.u32TxBufferNum = MCAN_TX_BUF_NUM;
  247. stcMcanInit.stcMsgRam.u32TxFifoQueueNum = MCAN_TX_FIFO_NUM;
  248. stcMcanInit.stcMsgRam.u32TxFifoQueueMode = MCAN_TX_FIFO_MD;
  249. stcMcanInit.stcMsgRam.u32TxDataSize = MCAN_TX_BUF_DATA_FIELD_SIZE;
  250. stcMcanInit.stcMsgRam.u32TxEventNum = MCAN_TX_EVT_NUM;
  251. /* Acceptance filter */
  252. stcMcanInit.stcFilter.pstcStdFilterList = stcStdFilterList;
  253. stcMcanInit.stcFilter.pstcExtFilterList = stcExtFilterList;
  254. stcMcanInit.stcFilter.u32StdFilterConfigNum = stcMcanInit.stcMsgRam.u32StdFilterNum;
  255. stcMcanInit.stcFilter.u32ExtFilterConfigNum = stcMcanInit.stcMsgRam.u32ExtFilterNum;
  256. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_MCAN2, DISABLE);
  257. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_MCAN2, ENABLE);
  258. MCAN_Init(CM_MCAN2, &stcMcanInit);
  259. /* Watermark if needed */
  260. MCAN_SetFifoWatermark(CM_MCAN2, MCAN_WATERMARK_RX_FIFO0, MCAN_RX_FIFO0_WATERMARK);
  261. MCAN_SetFifoWatermark(CM_MCAN2, MCAN_WATERMARK_RX_FIFO1, MCAN_RX_FIFO1_WATERMARK);
  262. /* Configure Rx FIFO0 operation mode if needed.
  263. If new message received when Rx FIFO0 is full, the new message
  264. will not be stored and Message Lost Interrupt will be generated. */
  265. MCAN_RxFifoOperationModeConfig(CM_MCAN2, MCAN_RX_FIFO0, MCAN_RX_FIFO_BLOCKING);
  266. /* Configure Rx FIFO1 operation mode if needed.
  267. If new message received when Rx FIFO1 is full, the new received message
  268. will overwrite the oldest received message and RF1N Interrupt generated. */
  269. MCAN_RxFifoOperationModeConfig(CM_MCAN2, MCAN_RX_FIFO1, MCAN_RX_FIFO_BLOCKING);
  270. /* Configures timestamp if needed */
  271. MCAN_TimestampCounterConfig(CM_MCAN2, 1U);
  272. MCAN_TimestampCounterCmd(CM_MCAN2, ENABLE);
  273. /* The Tx buffer can cause transmission completed completed interrupt
  274. only when its own transmission completed interrupt is enabled. */
  275. MCAN_TxBufferNotificationCmd(CM_MCAN2, MCAN_TX_NOTIFICATION_BUF, MCAN_INT_TX_CPLT, ENABLE);
  276. MCAN_IntCmd(CM_MCAN2, MCAN_INT0_SEL, MCAN_INT_LINE0, ENABLE);
  277. NVIC_ClearPendingIRQ(MCAN2_INT0_IRQn);
  278. NVIC_SetPriority(MCAN2_INT0_IRQn, DDL_IRQ_PRIO_06);
  279. NVIC_EnableIRQ(MCAN2_INT0_IRQn);
  280. }
  281. void MCAN2_INT0_Handler(void)
  282. {
  283. uint32_t u32RxBuffer;
  284. uint32_t u32TxBufferIndex;
  285. uint32_t u32TxBuffer = 0U;
  286. stc_mcan_rx_msg_t stcRxMsg;
  287. stc_mcan_tx_msg_t stcTxMsg;
  288. stc_mcan_rx_msg_t stcRxMsgRxFifo1[MCAN_RX_FIFO1_NUM];
  289. static uint32_t pxHigherPriorityTaskWoken = pdFALSE;
  290. /* Rx FIFO0 related interrupts */
  291. if (MCAN_GetStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO0_NEW_MSG) == SET) {
  292. MCAN_ClearStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO0_NEW_MSG);
  293. /* Messages stored in Rx FIFO0 can be read here */
  294. printf("Rx FIFO_0 new message->\r\n");
  295. if (MCAN_GetRxMsg(CM_MCAN2, MCAN_RX_FIFO0, &stcRxMsg) == LL_OK) {
  296. //printf("stcRxMsg.IDE=%d,stcRxMsg.ID=%x\r\n",stcRxMsg.IDE,stcRxMsg.ID);
  297. for(int i =0;i <8;i++)
  298. {
  299. printf("%x ",stcRxMsg.au8Data[i]);
  300. }
  301. hc32_can_rx_msg_t hc32_can_rx_msg;
  302. hc32_can_rx_msg.ide =stcRxMsg.IDE;
  303. hc32_can_rx_msg.id = stcRxMsg.ID;
  304. hc32_can_rx_msg.len = 8;//stcRxMsg.DLC;//直接赋值
  305. memcpy(hc32_can_rx_msg.data,stcRxMsg.au8Data,8);
  306. xQueueOverwriteFromISR(can2_recieve1_data_queue,&hc32_can_rx_msg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  307. portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
  308. }
  309. }
  310. if (MCAN_GetStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO0_FULL) == SET)
  311. {
  312. MCAN_ClearStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO0_FULL);
  313. }
  314. /* Rx FIFO1 related interrupts */
  315. if (MCAN_GetStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO1_NEW_MSG) == SET)
  316. {
  317. MCAN_ClearStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO1_NEW_MSG);
  318. printf("Rx FIFO_1 new message->\r\n");
  319. if (MCAN_GetRxMsg(CM_MCAN2, MCAN_RX_FIFO1, &stcRxMsg) == LL_OK) {
  320. //printf("stcRxMsg.IDE=%d,stcRxMsg.ID=%x\r\n",stcRxMsg.IDE,stcRxMsg.ID);
  321. for(int i =0;i <8;i++)
  322. {
  323. printf("%x ",stcRxMsg.au8Data[i]);
  324. }
  325. hc32_can_rx_msg_t hc32_can_rx_msg;
  326. hc32_can_rx_msg.ide =stcRxMsg.IDE;
  327. hc32_can_rx_msg.id = stcRxMsg.ID;
  328. hc32_can_rx_msg.len = 8;//stcRxMsg.DLC;//直接赋值
  329. memcpy(hc32_can_rx_msg.data,stcRxMsg.au8Data,8);
  330. xQueueOverwriteFromISR(can2_recieve1_data_queue,&hc32_can_rx_msg,(BaseType_t*)&pxHigherPriorityTaskWoken);
  331. portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
  332. }
  333. }
  334. if (MCAN_GetStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO1_FULL) == SET) {
  335. MCAN_ClearStatus(CM_MCAN2, MCAN_FLAG_RX_FIFO1_FULL);
  336. /* All dedicated Tx buffer are filled. Add transmission requests. */
  337. MCAN_EnableTxBufferRequest(CM_MCAN2, u32TxBuffer);
  338. }
  339. /* Dedicated Rx buffer related interrupts */
  340. if (MCAN_GetStatus(CM_MCAN2, MCAN_FLAG_RX_BUF_NEW_MSG) == SET) {
  341. MCAN_ClearStatus(CM_MCAN2, MCAN_FLAG_RX_BUF_NEW_MSG);
  342. }
  343. }
  344. int32_t can_send_msg(CM_MCAN_TypeDef *MCANx,stc_mcan_tx_msg_t *pTxMsg)
  345. {
  346. return MCAN_AddMsgToTxFifoQueue(MCANx,pTxMsg);
  347. }