You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

1 year ago
1 year ago
5 months ago
1 year ago
5 months ago
5 months ago
1 year ago
1 year ago
5 months ago
1 year ago
5 months ago
1 year ago
1 year ago
  1. //
  2. // Created by zwsd
  3. //
  4. #pragma once
  5. #include "a8000_protocol/protocol.hpp"
  6. #include "sdk/components/api/zi_module.hpp"
  7. #include "sdk/os/zos.hpp"
  8. #include "type/zcan_rx_frame.hpp"
  9. #include "type/zcan_rx_frame_pool.hpp"
  10. #include "type/zcan_rx_frame_queue.hpp"
  11. #ifdef HAL_CAN_MODULE_ENABLED
  12. namespace iflytop {
  13. using namespace zcr;
  14. class IZCanRxProcesser {
  15. public:
  16. virtual bool filterPacket(ZcanRxframe *rx) = 0;
  17. virtual void processRxPacket(ZcanRxframe *rx) = 0;
  18. };
  19. class ZCanReceiver : public ZCanIRQListener {
  20. public:
  21. class CFG {
  22. public:
  23. uint8_t deviceId; //
  24. /*******************************************************************************
  25. * CANConfig *
  26. *******************************************************************************/
  27. CAN_HandleTypeDef *canHandle; // 默认使用CAN1
  28. int canFilterIndex0; // 过滤器0 接收,发给自身的消息
  29. int maxFilterNum; // 使用的过滤器数量,最大值14,默认为7
  30. int rxfifoNum; // 使用的FIFO,默认使用FIFO0
  31. int packetRxOvertime_ms; //
  32. };
  33. public:
  34. class LoopJobContext {
  35. public:
  36. bool hasDoneSomething;
  37. };
  38. private:
  39. CFG *m_config = NULL; // 配置
  40. bool m_canOnRxDataFlag = false; // 是否有数据接收,用于从中断上下文转移到MainLoop上下文
  41. uint32_t m_lastPacketTicket = 0; // 上一次接收到消息的时间,用于判断与主机是否断开连接
  42. HAL_StatusTypeDef m_lastTransmitStatus; // 上次调用can发送方法的返回值
  43. uint8_t txbuff[128];
  44. IZCanRxProcesser *rxprocesser;
  45. ZcanRxframePool m_rxFramePool; // 接收数据池
  46. ZcanRxframeQueue m_pendingMsgQueue; // 接收数据队列
  47. ZcanRxframe *curRxBuf = nullptr; // 当前接收数据
  48. int txPacketInterval_ms = 0;
  49. zmutex m_lock;
  50. int8_t m_reportIndex = 0;
  51. public:
  52. ZCanReceiver() {}
  53. CFG *createCFG(uint8_t deviceId);
  54. void initialize(CFG *cfg);
  55. uint8_t getDeviceId() { return m_config->deviceId; }
  56. void setTxPacketInterval(int interval_ms) { txPacketInterval_ms = interval_ms; }
  57. virtual void registerListener(IZCanRxProcesser *listener);
  58. virtual int32_t sendBufAck(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, int32_t len);
  59. virtual int32_t sendAck(zcr_cmd_header_t *rx_cmd_header, int32_t *ackvar, int32_t nack);
  60. virtual int32_t sendErrorAck(zcr_cmd_header_t *rx_cmd_header, int32_t errorcode);
  61. virtual int32_t triggerEvent(zcr_cmd_header_t *cmd_header, uint8_t *data, int32_t len);
  62. void loop();
  63. public:
  64. virtual void STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *can);
  65. virtual void STM32_HAL_onCAN_Error(CAN_HandleTypeDef *can);
  66. private:
  67. void sendPacket(uint8_t *packet, size_t len);
  68. bool sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems);
  69. void processRx();
  70. HAL_StatusTypeDef initializeFilter();
  71. HAL_StatusTypeDef activateRxIT();
  72. HAL_StatusTypeDef deactivateRxIT();
  73. bool getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/);
  74. };
  75. } // namespace iflytop
  76. #endif