can.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <stdio.h>
  2. #include "os/queue.h"
  3. #include "bsp/bsp.h"
  4. #include "bsp/can.h"
  5. #include "libs/utils.h"
  6. #include "libs/circle_buffer.h"
  7. #define CAN_RX_MESSAGE_RX_ID 1
  8. #define CAN_SEND_QUEUE_SIZE 32
  9. #define RX_ID_OFFSET 16
  10. #define CAN_SEND_OK 0
  11. #define CAN_SEND_ERROR -1
  12. #define CAN_SEND_WAIT_TIMEOUT -2
  13. #define TX_NUM 32
  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(can_trasnmit_message_struct) * TX_NUM + 1];
  18. static uint8_t _g_rx_buffer[sizeof(can_receive_message_struct) * RX_NUM + 1];
  19. static int shark_send_can0_data(can_trasnmit_message_struct *P_message);
  20. static uint8_t can_get_mailbox(uint32_t can_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. can_receive_message_struct 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.rx_efid;
  31. handle_can_frame(can_id, message.rx_data, message.rx_dlen);
  32. return ;
  33. }
  34. void can_tx_poll(void){
  35. can_trasnmit_message_struct can_tr_m;
  36. while (can_get_mailbox(CAN0) != CAN_NOMAILBOX) {
  37. if (circle_get_data(&g_tx_circle, (uint8_t * )&can_tr_m, sizeof(can_tr_m)) != sizeof(can_tr_m)) {
  38. break;
  39. }
  40. can_message_transmit(CAN0,&can_tr_m);
  41. }
  42. }
  43. static u32 _can_poll_task(void *args) {
  44. can_rx_poll();
  45. can_tx_poll();
  46. return 0;
  47. }
  48. static __inline__ void can_fifo_recv(int fifo){
  49. can_receive_message_struct Rxmessage;
  50. can_message_receive(CAN0, fifo, &Rxmessage);
  51. circle_put_data(&g_rx_circle, (uint8_t *)&Rxmessage, sizeof(Rxmessage));
  52. }
  53. void USBD_LP_CAN0_RX0_IRQHandler(void)
  54. {
  55. can_fifo_recv(CAN_FIFO0);
  56. }
  57. void CAN0_RX1_IRQHandler(void)
  58. {
  59. can_fifo_recv(CAN_FIFO1);
  60. }
  61. static void shark_can0_txrx_pin_config(void){
  62. /* enable can clock */
  63. rcu_periph_clock_enable(RCU_CAN0);
  64. rcu_periph_clock_enable(RCU_GPIOA);
  65. rcu_periph_clock_enable(RCU_AF);
  66. //gpio_pin_remap_config(GPIO_CAN_FULL_REMAP,DISABLE);
  67. //gpio_pin_remap_config(GPIO_CAN_PARTIAL_REMAP,ENABLE);
  68. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
  69. gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
  70. }
  71. static void shark_can0_config(void)
  72. {
  73. can_parameter_struct can_parameter;
  74. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  75. /* initialize CAN register */
  76. can_deinit(CAN0);
  77. //can_deinit(CAN1);
  78. /* initialize CAN parameters */
  79. can_parameter.time_triggered = DISABLE;
  80. can_parameter.auto_bus_off_recovery = ENABLE;
  81. can_parameter.auto_wake_up = DISABLE;
  82. can_parameter.rec_fifo_overwrite = DISABLE;
  83. can_parameter.trans_fifo_order = ENABLE;
  84. can_parameter.no_auto_retrans = ENABLE;
  85. #if CAN0_SPEED==250 //250k bps
  86. if (SystemCoreClock == 120000000) {
  87. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  88. can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
  89. can_parameter.time_segment_2 = CAN_BT_BS2_4TQ;
  90. } else {
  91. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  92. can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
  93. can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
  94. }
  95. #endif
  96. can_parameter.working_mode = CAN_NORMAL_MODE;
  97. can_parameter.prescaler = 24;
  98. /* initialize CAN */
  99. can_init(CAN0, &can_parameter);
  100. /* recv my can ID, use fifo0 */
  101. can_filter_mask_mode_init(CAN_MY_ADDRESS, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO0, 0);
  102. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  103. nvic_irq_enable(USBD_LP_CAN0_RX0_IRQn,2,0);
  104. /* enable can receive FIFO0 not empty interrupt */
  105. can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
  106. /* recv broadcast, fifo1 */
  107. can_filter_mask_mode_init(CAN_NODE_ADDR_FF, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 1);
  108. /* recv ccu aux fifo2*/
  109. can_filter_mask_mode_init(CAN_NODE_ADDR_CCU_AUX, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 2);
  110. /* recv ble fifo3*/
  111. can_filter_mask_mode_init(CAN_NODE_ADDR_BLE, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 3);
  112. can_filter_mask_mode_init(CAN_NODE_ADDR_ACU, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 4);
  113. nvic_irq_enable(CAN0_RX1_IRQn,10,0);
  114. /* enable can receive FIFO1 not empty interrupt */
  115. can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE1);
  116. }
  117. static int shark_send_can0_data(can_trasnmit_message_struct *P_message){
  118. if (circle_put_data(&g_tx_circle, (u8 *)P_message, sizeof(can_trasnmit_message_struct))){
  119. return CAN_SEND_OK;
  120. }
  121. return CAN_SEND_ERROR;
  122. }
  123. static uint8_t can_get_mailbox(uint32_t can_periph)
  124. {
  125. uint8_t mailbox_number = CAN_MAILBOX0;
  126. /* select one empty mailbox */
  127. if(CAN_TSTAT_TME0 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME0)){
  128. mailbox_number = CAN_MAILBOX0;
  129. }else if(CAN_TSTAT_TME1 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME1)){
  130. mailbox_number = CAN_MAILBOX1;
  131. }else if(CAN_TSTAT_TME2 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME2)){
  132. mailbox_number = CAN_MAILBOX2;
  133. }else{
  134. mailbox_number = CAN_NOMAILBOX;
  135. }
  136. /* return no mailbox empty */
  137. if(CAN_NOMAILBOX == mailbox_number){
  138. return CAN_NOMAILBOX;
  139. }
  140. return mailbox_number;
  141. }
  142. int shark_can0_send_message(uint32_t can_id, const void*buff, int len){
  143. can_trasnmit_message_struct trasnmit_msg;
  144. can_id_t can_frame_id;
  145. u32 total_frames = ((len + 7) >> 3);
  146. int send_len = len;
  147. u32 frame_id = 1;
  148. can_frame_id.id = can_id;
  149. can_frame_id.total = total_frames;
  150. while(send_len > 0){
  151. can_frame_id.idx = frame_id;
  152. trasnmit_msg.tx_sfid = 0;
  153. trasnmit_msg.tx_efid = can_frame_id.id;
  154. trasnmit_msg.tx_ft = CAN_FT_DATA;
  155. trasnmit_msg.tx_ff = CAN_FF_EXTENDED;
  156. trasnmit_msg.tx_dlen = min(CAN_DATA_SIZE,send_len);
  157. memcpy((char *)trasnmit_msg.tx_data, (char *)buff + (len - send_len), trasnmit_msg.tx_dlen);
  158. send_len -= trasnmit_msg.tx_dlen;
  159. frame_id ++;
  160. if (shark_send_can0_data(&trasnmit_msg) != CAN_SEND_OK){
  161. return CAN_SEND_ERROR;
  162. }
  163. }
  164. return CAN_SEND_OK;
  165. }
  166. void shark_can0_reset(void){
  167. shark_can0_txrx_pin_config();
  168. shark_can0_config();
  169. }
  170. void shark_can0_deinit(void){
  171. can_deinit(CAN0);
  172. rcu_periph_clock_disable(RCU_CAN0);
  173. gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_2MHZ, GPIO_PIN_9);
  174. }
  175. void shark_can0_init(void){
  176. circle_buffer_init(&g_tx_circle, _g_tx_buffer, sizeof(_g_tx_buffer));
  177. circle_buffer_init(&g_rx_circle, _g_rx_buffer, sizeof(_g_rx_buffer));
  178. shark_task_create(_can_poll_task, NULL);
  179. shark_can0_txrx_pin_config();
  180. shark_can0_config();
  181. }