can.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <stdio.h>
  2. #include "os/queue.h"
  3. #include "bsp/bsp_driver.h"
  4. #include "libs/utils.h"
  5. #include "libs/circle_buffer.h"
  6. #define CAN_RX_MESSAGE_RX_ID 1
  7. #define CAN_SEND_QUEUE_SIZE 32
  8. #define RX_ID_OFFSET 16
  9. #define CAN_SEND_OK 0
  10. #define CAN_SEND_ERROR -1
  11. #define CAN_SEND_WAIT_TIMEOUT -2
  12. #define CAN_DEV CAN1
  13. #define TX_NUM 64
  14. #define RX_NUM 64
  15. static c_buffer_t g_tx_circle;
  16. static c_buffer_t g_rx_circle;
  17. static uint8_t _g_tx_buffer[sizeof(CanTxMessage) * TX_NUM + 1];
  18. static uint8_t _g_rx_buffer[sizeof(CanRxMessage) * RX_NUM + 1];
  19. static int shark_send_can0_data(CanTxMessage *P_message);
  20. static uint8_t can_get_mailbox(CAN_Module *an_periph);
  21. /* this function can be overide by app, which need recv the can frame */
  22. __weak void handle_can_frame(can_id_t id, uint8_t *data, int len){
  23. }
  24. void can_rx_poll(void){
  25. CanRxMessage message;
  26. if (circle_get_data(&g_rx_circle, (uint8_t *)&message, sizeof(message)) != sizeof(message)) {
  27. return;
  28. }
  29. can_id_t can_id;
  30. can_id.id = message.ExtId;
  31. handle_can_frame(can_id, message.Data , message.DLC);
  32. return ;
  33. }
  34. void can_tx_poll(void){
  35. CanTxMessage can_tr_m;
  36. if (CAN_GetFlagSTS(CAN_DEV, CAN_FLAG_BOFFL)){
  37. shark_can0_reset();
  38. }
  39. while (can_get_mailbox(CAN_DEV) != CAN_TxSTS_NoMailBox) {
  40. if (circle_get_data(&g_tx_circle, (uint8_t * )&can_tr_m, sizeof(can_tr_m)) != sizeof(can_tr_m)) {
  41. break;
  42. }
  43. CAN_TransmitMessage(CAN_DEV,&can_tr_m);
  44. }
  45. }
  46. static u32 _can_poll_task(void *args) {
  47. can_rx_poll();
  48. can_tx_poll();
  49. return 0;
  50. }
  51. static __inline__ void can_fifo_recv(int fifo){
  52. CanRxMessage Rxmessage;
  53. CAN_ReceiveMessage(CAN_DEV, fifo, &Rxmessage);
  54. circle_put_data(&g_rx_circle, (uint8_t *)&Rxmessage, sizeof(Rxmessage));
  55. }
  56. void CAN_RX0_IRQHandler(void)
  57. {
  58. can_fifo_recv(CAN_FIFO0);
  59. }
  60. void CAN_RX1_IRQHandler(void)
  61. {
  62. can_fifo_recv(CAN_FIFO1);
  63. }
  64. static void shark_can0_txrx_pin_config(void){
  65. /* enable can clock */
  66. rcu_apb1_periph_clock_enable(RCU_CAN0);
  67. rcu_apb2_periph_clock_enable(CAN_PIN_RCU);
  68. rcu_apb2_periph_clock_enable(RCU_AF);
  69. #ifdef CAN_REMAP
  70. gpio_pin_remap_config(CAN_REMAP,ENABLE);
  71. #endif
  72. gpio_init(CAN_TX_GROUP, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CAN_TX_PIN);
  73. gpio_init(CAN_RX_GROUP, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, CAN_RX_PIN);
  74. }
  75. static void can_filter_init(u32 id, u32 mask, u8 fifo, u8 filter)
  76. {
  77. CAN_FilterInitType CAN_FilterInitStructure;
  78. /* CAN filter init */
  79. CAN_FilterInitStructure.Filter_Num = filter;
  80. CAN_FilterInitStructure.Filter_Mode = CAN_Filter_IdMaskMode;
  81. CAN_FilterInitStructure.Filter_Scale = CAN_Filter_32bitScale;
  82. CAN_FilterInitStructure.Filter_HighId = (uint16_t)id >> 13;
  83. CAN_FilterInitStructure.Filter_LowId = (0x003F8U & (uint16_t)(id << 3)) | (1U << 2);
  84. CAN_FilterInitStructure.FilterMask_HighId = (uint16_t)mask >> 13;
  85. CAN_FilterInitStructure.FilterMask_LowId = (0x003F8U & (uint16_t)(mask << 3)) | (1U << 2);
  86. CAN_FilterInitStructure.Filter_FIFOAssignment = fifo;
  87. CAN_FilterInitStructure.Filter_Act = ENABLE;
  88. CAN1_InitFilter(&CAN_FilterInitStructure);
  89. CAN_INTConfig(CAN_DEV, CAN_INT_FMP0, ENABLE);
  90. }
  91. static void shark_can0_config(void)
  92. {
  93. CAN_InitType CAN_InitStructure;
  94. /* CAN register init */
  95. CAN_DeInit(CAN_DEV);
  96. /* Struct init*/
  97. CAN_InitStruct(&CAN_InitStructure);
  98. /* CAN cell init */
  99. CAN_InitStructure.TTCM = DISABLE;
  100. CAN_InitStructure.ABOM = DISABLE;
  101. CAN_InitStructure.AWKUM = DISABLE;
  102. CAN_InitStructure.NART = ENABLE;
  103. CAN_InitStructure.RFLM = DISABLE;
  104. CAN_InitStructure.TXFP = ENABLE;
  105. CAN_InitStructure.OperatingMode = CAN_Normal_Mode;
  106. CAN_InitStructure.RSJW = CAN_RSJW_1tq;
  107. CAN_InitStructure.TBS1 = CAN_TBS1_4tq;
  108. CAN_InitStructure.TBS2 = CAN_TBS2_3tq;
  109. CAN_InitStructure.BaudRatePrescaler = 18;
  110. /*Initializes the CAN */
  111. CAN_Init(CAN_DEV, &CAN_InitStructure);
  112. /* recv my can ID, use fifo0 */
  113. can_filter_init(CAN_MY_ADDRESS, CAN_FILTER_DEST_MASK, CAN_FIFO0, 0);
  114. //nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  115. nvic_irq_enable(CAN_IRQ0,CAN_IRQ_PRIORITY,0);
  116. }
  117. static int shark_send_can0_data(CanTxMessage *P_message){
  118. can_tx_poll();
  119. if (circle_put_data(&g_tx_circle, (u8 *)P_message, sizeof(CanTxMessage))){
  120. return CAN_SEND_OK;
  121. }
  122. return CAN_SEND_ERROR;
  123. }
  124. static uint8_t can_get_mailbox(CAN_Module *can_periph)
  125. {
  126. uint8_t transmit_mailbox = 0;
  127. /* Check the parameters */
  128. /* Select one empty transmit mailbox */
  129. if ((CAN_DEV->TSTS & CAN_TSTS_TMEM0) == CAN_TSTS_TMEM0)
  130. {
  131. transmit_mailbox = 0;
  132. }
  133. else if ((CAN_DEV->TSTS & CAN_TSTS_TMEM1) == CAN_TSTS_TMEM1)
  134. {
  135. transmit_mailbox = 1;
  136. }
  137. else if ((CAN_DEV->TSTS & CAN_TSTS_TMEM2) == CAN_TSTS_TMEM2)
  138. {
  139. transmit_mailbox = 2;
  140. }
  141. else
  142. {
  143. transmit_mailbox = CAN_TxSTS_NoMailBox;
  144. }
  145. return transmit_mailbox;
  146. }
  147. int shark_can0_send_message(uint32_t can_id, const void*buff, int len){
  148. CanTxMessage trasnmit_msg;
  149. can_id_t can_frame_id;
  150. u32 total_frames = ((len + 7) >> 3);
  151. int send_len = len;
  152. u32 frame_id = 1;
  153. can_frame_id.id = can_id;
  154. can_frame_id.total = total_frames;
  155. while(send_len > 0){
  156. can_frame_id.idx = frame_id;
  157. trasnmit_msg.StdId = 0;
  158. trasnmit_msg.ExtId = can_frame_id.id;
  159. trasnmit_msg.RTR = CAN_RTRQ_DATA;
  160. trasnmit_msg.IDE = CAN_ID_EXT;
  161. trasnmit_msg.DLC = min(CAN_DATA_SIZE,send_len);
  162. memcpy((char *)trasnmit_msg.Data, (char *)buff + (len - send_len), trasnmit_msg.DLC);
  163. send_len -= trasnmit_msg.DLC;
  164. frame_id ++;
  165. if (shark_send_can0_data(&trasnmit_msg) != CAN_SEND_OK){
  166. return CAN_SEND_ERROR;
  167. }
  168. }
  169. return CAN_SEND_OK;
  170. }
  171. int shark_can0_send_ext_message(uint32_t can_id, const void*buff, int len){
  172. CanTxMessage trasnmit_msg;
  173. trasnmit_msg.StdId = 0;
  174. trasnmit_msg.ExtId = can_id;
  175. trasnmit_msg.RTR = CAN_RTRQ_DATA;
  176. trasnmit_msg.IDE = CAN_ID_EXT;
  177. trasnmit_msg.DLC = min(CAN_DLC_LENGTH,len);
  178. memcpy((char *)trasnmit_msg.Data, (char *)buff, trasnmit_msg.DLC);
  179. if (shark_send_can0_data(&trasnmit_msg) != CAN_SEND_OK){
  180. return CAN_SEND_ERROR;
  181. }
  182. return CAN_SEND_OK;
  183. }
  184. void shark_can0_reset(void){
  185. shark_can0_txrx_pin_config();
  186. shark_can0_config();
  187. }
  188. void shark_can0_deinit(void){
  189. CAN_DeInit(CAN_DEV);
  190. rcu_apb1_periph_clock_disable(RCU_CAN0);
  191. }
  192. void shark_can0_init(void){
  193. circle_buffer_init(&g_tx_circle, _g_tx_buffer, sizeof(_g_tx_buffer));
  194. circle_buffer_init(&g_rx_circle, _g_rx_buffer, sizeof(_g_rx_buffer));
  195. shark_task_create(_can_poll_task, NULL);
  196. shark_can0_txrx_pin_config();
  197. shark_can0_config();
  198. }