dev_can.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "dev_can.h"
  2. #include "dev_iap.h"
  3. #include "queue.h"
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. double pow_branch(double x, long long N)
  8. {
  9. double ans = 1.0;
  10. // 贡献的初始值为 x
  11. double x_contribute = x;
  12. // 在对 N 进行二进制拆分的同时计算答案
  13. while (N > 0)
  14. {
  15. if (N % 2 == 1)
  16. {
  17. // 如果 N 二进制表示的最低位为 1,那么需要计入贡献
  18. ans *= x_contribute;
  19. }
  20. // 将贡献不断地平方
  21. x_contribute *= x_contribute;
  22. // 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
  23. N = N >> 1;
  24. }
  25. return ans;
  26. }
  27. double pow(double x, double N)
  28. {
  29. return N >= 0 ? pow_branch(x, N) : 1.0 / pow_branch(x, -N);
  30. }
  31. uint8_t recv_can_id = 0;
  32. static uint64_t base_data;
  33. void data_bit_move(uint8_t start_bit, uint8_t bit_len, uint64_t data)
  34. {
  35. uint64_t mask = 0;
  36. uint64_t source_data = (uint64_t)data;
  37. mask = pow(2, bit_len) - 1;
  38. base_data |= (mask & source_data) << start_bit;
  39. }
  40. void product_array(uint8_t send_array[8])
  41. {
  42. send_array[0] = (uint8_t)base_data;
  43. send_array[1] = (uint8_t)(base_data >> 8);
  44. send_array[2] = (uint8_t)(base_data >> 16);
  45. send_array[3] = (uint8_t)(base_data >> 24);
  46. send_array[4] = (uint8_t)(base_data >> 32);
  47. send_array[5] = (uint8_t)(base_data >> 40);
  48. send_array[6] = (uint8_t)(base_data >> 48);
  49. send_array[7] = (uint8_t)(base_data >> 56);
  50. base_data = 0;
  51. }
  52. uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
  53. {
  54. pdu_tag response_msg;
  55. response_msg.id.r = id;
  56. response_msg.reg.dlc = len;
  57. memcpy(&response_msg.data.u8_buf[0], p_data, len);
  58. if (id > 0x7FF)
  59. {
  60. response_msg.reg.ide = CAN_Id_Extended;
  61. }
  62. else
  63. {
  64. response_msg.reg.ide = CAN_Id_Standard;
  65. }
  66. if (en_queue(&can_tx_queue, response_msg) != Q_OK)
  67. {
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. static uint8_t can_tx_frame(pdu_tag can_message)
  73. {
  74. uint8_t result = CAN_TxStatus_NoMailBox;
  75. result = drv_can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
  76. return result;
  77. }
  78. void can_tx_callback(void)
  79. {
  80. pdu_tag tx_data;
  81. if (de_queue(&can_tx_queue, &tx_data) == Q_OK) // 返回值为1代表读取成功
  82. {
  83. can_tx_frame(tx_data);
  84. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  85. }
  86. else
  87. {
  88. CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);
  89. }
  90. }
  91. void can_rx_callback(CanRxMsg rx_message)
  92. {
  93. pdu_tag data;
  94. uint8_t ps;
  95. uint8_t pf;
  96. switch (rx_message.ExtId)
  97. {
  98. case 0x18DFF4E1:
  99. data.id.r = 0x18DFF4E1;
  100. data.reg.dlc = rx_message.DLC;
  101. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  102. en_queue(&can_rx_queue, data);
  103. break;
  104. default:
  105. return;
  106. }
  107. }
  108. can_rx_tab can_tab[] = {
  109. 0x18DFF4E1,
  110. iap_rec_handler,
  111. };
  112. void can_start_send(void)
  113. {
  114. pdu_tag tx_msg;
  115. if (RESET == CAN_GetITStatus(CAN1, CAN_IT_TME))
  116. {
  117. if (de_queue(&can_tx_queue, &tx_msg) == Q_OK) // 返回值为1代表读取成功
  118. {
  119. can_tx_frame(tx_msg);
  120. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  121. }
  122. }
  123. }
  124. static uint8_t can_rx_process(void)
  125. {
  126. uint8_t i;
  127. uint8_t flg = 0;
  128. pdu_tag rec_msg;
  129. for (uint8_t j = 0; j < 15; j++)
  130. {
  131. if (Q_OK == de_queue(&can_rx_queue, &rec_msg))
  132. {
  133. for (i = 0; i < ARR_SIZE(can_tab); i++)
  134. {
  135. if (can_tab[i].id == rec_msg.id.r)
  136. {
  137. can_tab[i].p_func(rec_msg);
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. void dev_can_network_init(void)
  145. {
  146. drv_can_init();
  147. drv_can_rx_back_init(can_rx_callback);
  148. drv_can_tx_back_init(can_tx_callback);
  149. }
  150. void can_process(void)
  151. {
  152. can_rx_process();
  153. can_start_send();
  154. }