123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "can_interface.h"
- #include "can.h"
- #include <string.h>
- CAN_TxHeaderTypeDef tx_header;
- CAN_RxHeaderTypeDef rx_header;
- pcan_rx pcan_rx_back = NULL;
- pcan_tx pcan_tx_back = NULL;
- void can_rx_back_init(pcan_rx p_fun)
- {
- pcan_rx_back = p_fun;
- }
- void can_tx_back_init(pcan_tx p_fun)
- {
- pcan_tx_back = p_fun;
- }
- uint8_t can_write(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len)
- {
- uint32_t TxMailbox;
- if (id_type)
- {
- tx_header.ExtId = id; // 设置扩展标示符(29位)
- }
- else
- {
- tx_header.StdId = id_type; // 标准标识符为0
- }
- tx_header.IDE = id_type; // 使用扩展标识符
- tx_header.DLC = len; // 发送两帧信息
- if (len == 0)
- {
- tx_header.RTR = CAN_RTR_REMOTE; // 消息类型为远程帧
- }
- else
- {
- tx_header.RTR = CAN_RTR_DATA; // 消息类型为数据帧
- }
- if (HAL_CAN_AddTxMessage(&hcan, &tx_header, p_data, &TxMailbox) != HAL_OK) /* 发送消息 */
- {
- return 1;
- }
- return 0;
- }
- void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan_interface)
- {
- uint8_t rxbuf[8];
- if (hcan_interface->Instance == hcan.Instance)
- {
- HAL_CAN_GetRxMessage(hcan_interface, CAN_RX_FIFO0, &rx_header, rxbuf);
- if (pcan_rx_back != NULL)
- {
- pcan_rx_back(rx_header, rxbuf);
- }
- }
- }
- void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan_interface)
- {
- if (hcan_interface->Instance == hcan.Instance)
- {
- if (pcan_tx_back != NULL)
- {
- pcan_tx_back();
- }
- }
- }
- void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan_interface)
- {
- if (hcan_interface->Instance == hcan.Instance)
- {
- if (pcan_tx_back != NULL)
- {
- pcan_tx_back();
- }
- }
- }
- void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan_interface)
- {
- if (hcan_interface->Instance == hcan.Instance)
- {
- if (pcan_tx_back != NULL)
- {
- pcan_tx_back();
- }
- }
- }
|