can_interface.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "can_interface.h"
  2. #include <string.h>
  3. CanTxMsg tx_message;
  4. CanRxMsg rx_message;
  5. pcan_rx pcan_rx_back = NULL;
  6. pcan_tx pcan_tx_back = NULL;
  7. void can_rx_back_init(pcan_rx p_fun)
  8. {
  9. pcan_rx_back = p_fun;
  10. }
  11. void can_tx_back_init(pcan_tx p_fun)
  12. {
  13. pcan_tx_back = p_fun;
  14. }
  15. uint8_t can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len)
  16. {
  17. uint8_t flg = 0;
  18. if (id_type)
  19. {
  20. tx_message.ExtId = id; // 设置扩展标示符(29位)
  21. }
  22. else
  23. {
  24. tx_message.StdId = id_type; // 标准标识符为0
  25. }
  26. tx_message.IDE = id_type; // 使用扩展标识符
  27. tx_message.DLC = len; // 发送两帧信息
  28. if (len == 0)
  29. {
  30. tx_message.RTR = CAN_RTR_Remote; // 消息类型为远程帧
  31. }
  32. else
  33. {
  34. tx_message.RTR = CAN_RTR_Data; // 消息类型为数据帧
  35. memcpy(tx_message.Data, p_data, len);
  36. }
  37. flg = CAN_Transmit(CAN1, &tx_message);
  38. return flg;
  39. }
  40. void can_tx_iqr(void)
  41. {
  42. if (pcan_tx_back != NULL)
  43. {
  44. pcan_tx_back();
  45. }
  46. }
  47. void can_rx_iqr(void)
  48. {
  49. CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
  50. if (pcan_rx_back != NULL)
  51. {
  52. pcan_rx_back(rx_message);
  53. }
  54. }