soft_can.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "board.h"
  2. #include "soft_can.h"
  3. #include "hard_can.h"
  4. #include "params.h"
  5. #include "rkfifo.h"
  6. #include "soft_gs.h"
  7. #include "soft_time.h"
  8. #include "ver_config.h"
  9. #include "soft_can_yy.h"
  10. #define CAN_DEV BOARD_APP_CAN_BASE
  11. #define CAN_IRQn BOARD_APP_CAN_IRQn
  12. #define CAN_RX_FIFO_SIZE 16
  13. #define CAN_TX_FIFO_SIZE 16
  14. /* can 接收结构体 fifo */
  15. static can_receive_buf_t canRxMsgBuffer[CAN_RX_FIFO_SIZE] = {0};
  16. static rkfifo_t canRxFifo = {0};
  17. /* can 发送结构体 FIFO */
  18. static can_transmit_buf_t canTxMsgBuffer[CAN_TX_FIFO_SIZE] = {0};
  19. static rkfifo_t canTxFifo = {0};
  20. static void CAN_FilterConfig(void);
  21. /**
  22. * @brief can tx rx fifo 初始化
  23. *
  24. */
  25. static void canTxRxFifoInit(void)
  26. {
  27. rkfifo_init(&canRxFifo, &canRxMsgBuffer, sizeof(canRxMsgBuffer),
  28. sizeof(can_receive_buf_t));
  29. rkfifo_init(&canTxFifo, &canTxMsgBuffer, sizeof(canTxMsgBuffer),
  30. sizeof(can_transmit_buf_t));
  31. }
  32. /**
  33. * @brief CAN bus 总线初始化
  34. *
  35. */
  36. void CAN_BusInit(void)
  37. {
  38. canTxRxFifoInit();
  39. uint32_t baudrate = 1000 * 1000;
  40. CAN1_Mode_Init(baudrate);
  41. CAN_FilterConfig();
  42. }
  43. /**
  44. * @brief CAN 过滤器初始化
  45. *
  46. */
  47. void CAN_FilterConfig(void)
  48. {
  49. // 放在硬件CAN里去处理了
  50. }
  51. int canSendMsg(can_transmit_buf_t *pTxMsg)
  52. {
  53. int ret = 0;
  54. if (can_send_message_nonblocking(CAN_DEV, pTxMsg) != status_success ) // 发送失败
  55. {
  56. if (rkfifo_in(&canTxFifo, pTxMsg, 1) != 1)
  57. {
  58. ret = -1;
  59. }
  60. }
  61. return ret;
  62. }
  63. int canSendVkMsg(unsigned char *data, unsigned char length, unsigned int extid,
  64. uint16_t time_out_ms)
  65. {
  66. // 计算总帧数
  67. unsigned char frame_num = (length - 1) / 8 + 1;
  68. uint32_t start_time = micros();
  69. uint8_t sequence = 0;
  70. int ret_val = 0;
  71. can_transmit_buf_t TxMessage;
  72. // 设置固定属性
  73. TxMessage.extend_id = 1; // 扩展帧
  74. TxMessage.remote_frame = 0; // 数据帧
  75. TxMessage.canfd_frame = 0; // 标准CAN,不是CANFD
  76. while (sequence < frame_num)
  77. {
  78. // 基于原始extid计算当前帧的ID
  79. uint32_t current_id = extid;
  80. // 添加帧标志
  81. if (sequence == 0)
  82. current_id |= CAN_MSG_STA; // 起始帧标志
  83. if (sequence == frame_num - 1)
  84. current_id |= CAN_MSG_END; // 结束帧标志
  85. // 添加序列号 (左移3位,保留3位空间给标志位)
  86. current_id = current_id | (sequence << 3);
  87. // 右移3位,去除标志位占用的低位
  88. current_id = current_id >> 3;
  89. // 确保是29位扩展ID (0x1FFFFFFF = 29位全1)
  90. TxMessage.id = current_id & 0x1FFFFFFF;
  91. // 扩展ID帧未使用标准ID
  92. // TxMessage.StdId = TxMessage.ExtId & 0x07FF0000;
  93. // 设置数据长度
  94. if (length - (sequence + 1) * 8 >= 0)
  95. TxMessage.dlc = 8;
  96. else
  97. TxMessage.dlc = length - sequence * 8;
  98. // 复制数据
  99. for (int i = 0; i < TxMessage.dlc; i++)
  100. {
  101. TxMessage.data[i] = *(data + sequence * 8 + i);
  102. }
  103. // 发送消息
  104. if (canSendMsg(&TxMessage) == 0)
  105. {
  106. sequence++;
  107. }
  108. // 超时检查
  109. if (micros() - start_time > time_out_ms * 1000)
  110. {
  111. ret_val = -1;
  112. break;
  113. }
  114. // 等待发送完成(如果需要)
  115. // while (!has_sent_out && (micros() - start_time <= time_out_ms * 1000));
  116. // has_sent_out = false;
  117. }
  118. return ret_val;
  119. }
  120. void CanTxRxServicePoll(void)
  121. {
  122. can_receive_buf_t msg;
  123. while (rkfifo_out(&canRxFifo, &msg, 1) == 1)
  124. {
  125. // YY_can_rx_decode(&msg);
  126. }
  127. YY_tx_loop();
  128. }
  129. static volatile bool has_sent_out;
  130. SDK_DECLARE_EXT_ISR_M(CAN_IRQn, can0_isr)
  131. void can0_isr(void)
  132. {
  133. can_transmit_buf_t can_TxMessage;
  134. can_receive_buf_t can_RxMessage;
  135. uint8_t flags = can_get_tx_rx_flags(CAN_DEV);
  136. /* 处理接收中断 */
  137. if ((flags & CAN_EVENT_RECEIVE) != 0) {
  138. /* 直接读取到应用层消息结构(现在就是HPM格式) */
  139. can_read_received_message(CAN_DEV, &can_RxMessage);
  140. /* 存入FIFO */
  141. rkfifo_in(&canRxFifo, &can_RxMessage, 1);
  142. }
  143. /* 处理发送完成中断 */
  144. if ((flags & (CAN_EVENT_TX_PRIMARY_BUF | CAN_EVENT_TX_SECONDARY_BUF)) != 0) {
  145. /* 设置发送完成标志 */
  146. has_sent_out = true;
  147. /* 检查发送FIFO中是否有待发送消息 */
  148. if (rkfifo_out(&canTxFifo, &can_TxMessage, 1) == 1) {
  149. /* 直接发送(已经是HPM格式) */
  150. can_send_message_nonblocking(CAN_DEV, &can_TxMessage);
  151. }
  152. }
  153. /* 清除发送接收标志 */
  154. can_clear_tx_rx_flags(CAN_DEV, flags);
  155. /* 处理错误中断 */
  156. uint8_t error_flags = can_get_error_interrupt_flags(CAN_DEV);
  157. if (error_flags != 0) {
  158. // 可以在这里处理错误
  159. can_clear_error_interrupt_flags(CAN_DEV, error_flags);
  160. }
  161. }