| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #include "board.h"
- #include "soft_can.h"
- #include "hard_can.h"
- #include "params.h"
- #include "rkfifo.h"
- #include "soft_gs.h"
- #include "soft_time.h"
- #include "ver_config.h"
- #include "soft_can_yy.h"
- // 宏定义与hard can同步修改
- #define CAN2 HPM_CAN2
- #define CAN2_IRQ IRQn_CAN2
- #define CAN2_CLK_NAME clock_can2
- #define BAUD_1000k (1000000)
- #define BAUD_500k (500000)
- #define CAN_RX_FIFO_SIZE 16
- #define CAN_TX_FIFO_SIZE 16
- /* can 接收结构体 fifo */
- static can_receive_buf_t canRxMsgBuffer[CAN_RX_FIFO_SIZE] = {0};
- static rkfifo_t canRxFifo = {0};
- /* can 发送结构体 FIFO */
- static can_transmit_buf_t canTxMsgBuffer[CAN_TX_FIFO_SIZE] = {0};
- static rkfifo_t canTxFifo = {0};
- static void CAN_FilterConfig(void);
- /**
- * @brief can tx rx fifo 初始化
- *
- */
- static void canTxRxFifoInit(void)
- {
- rkfifo_init(&canRxFifo, &canRxMsgBuffer, sizeof(canRxMsgBuffer),
- sizeof(can_receive_buf_t));
- rkfifo_init(&canTxFifo, &canTxMsgBuffer, sizeof(canTxMsgBuffer),
- sizeof(can_transmit_buf_t));
- }
- /**
- * @brief CAN bus 总线初始化
- *
- */
- void CAN_BusInit(void)
- {
- canTxRxFifoInit();
- uint32_t baudrate = 1000 * 1000;
- CAN1_Mode_Init(baudrate);
- CAN_FilterConfig();
- }
- /**
- * @brief CAN 过滤器初始化
- *
- */
- void CAN_FilterConfig(void)
- {
- // 放在硬件CAN里去处理了
- }
- int canSendMsg(can_transmit_buf_t *pTxMsg)
- {
- int ret = 0;
- if (can_send_message_nonblocking(CAN2, pTxMsg) != status_success ) // 发送失败
- {
- if (rkfifo_in(&canTxFifo, pTxMsg, 1) != 1)
- {
- ret = -1;
- }
- }
- return ret;
- }
- int canSendVkMsg(unsigned char *data, unsigned char length, unsigned int extid,
- uint16_t time_out_ms)
- {
- // 计算总帧数
- unsigned char frame_num = (length - 1) / 8 + 1;
- uint32_t start_time = micros();
- uint8_t sequence = 0;
- int ret_val = 0;
- can_transmit_buf_t TxMessage;
-
- // 设置固定属性
- TxMessage.extend_id = 1; // 扩展帧
- TxMessage.remote_frame = 0; // 数据帧
- TxMessage.canfd_frame = 0; // 标准CAN,不是CANFD
-
- while (sequence < frame_num)
- {
- // 基于原始extid计算当前帧的ID
- uint32_t current_id = extid;
-
- // 添加帧标志
- if (sequence == 0)
- current_id |= CAN_MSG_STA; // 起始帧标志
- if (sequence == frame_num - 1)
- current_id |= CAN_MSG_END; // 结束帧标志
-
- // 添加序列号 (左移3位,保留3位空间给标志位)
- current_id = current_id | (sequence << 3);
-
- // 右移3位,去除标志位占用的低位
- current_id = current_id >> 3;
-
- // 确保是29位扩展ID (0x1FFFFFFF = 29位全1)
- TxMessage.id = current_id & 0x1FFFFFFF;
- // 扩展ID帧未使用标准ID
- // TxMessage.StdId = TxMessage.ExtId & 0x07FF0000;
- // 设置数据长度
- if (length - (sequence + 1) * 8 >= 0)
- TxMessage.dlc = 8;
- else
- TxMessage.dlc = length - sequence * 8;
-
- // 复制数据
- for (int i = 0; i < TxMessage.dlc; i++)
- {
- TxMessage.data[i] = *(data + sequence * 8 + i);
- }
-
- // 发送消息
- if (canSendMsg(&TxMessage) == 0)
- {
- sequence++;
- }
-
- // 超时检查
- if (micros() - start_time > time_out_ms * 1000)
- {
- ret_val = -1;
- break;
- }
-
- // 等待发送完成(如果需要)
- // while (!has_sent_out && (micros() - start_time <= time_out_ms * 1000));
- // has_sent_out = false;
- }
-
- return ret_val;
- }
- void CanTxRxServicePoll(void)
- {
- can_receive_buf_t msg;
- while (rkfifo_out(&canRxFifo, &msg, 1) == 1)
- {
- // YY_can_rx_decode(&msg);
- }
- YY_tx_loop();
- }
- static volatile bool has_sent_out;
- SDK_DECLARE_EXT_ISR_M(CAN2_IRQ, can2_isr)
- void can2_isr(void)
- {
- can_transmit_buf_t can_TxMessage;
- can_receive_buf_t can_RxMessage;
- uint8_t flags = can_get_tx_rx_flags(CAN2);
-
- /* 处理接收中断 */
- if ((flags & CAN_EVENT_RECEIVE) != 0) {
- /* 直接读取到应用层消息结构(现在就是HPM格式) */
- can_read_received_message(CAN2, &can_RxMessage);
-
- /* 存入FIFO */
- rkfifo_in(&canRxFifo, &can_RxMessage, 1);
- }
-
- /* 处理发送完成中断 */
- if ((flags & (CAN_EVENT_TX_PRIMARY_BUF | CAN_EVENT_TX_SECONDARY_BUF)) != 0) {
- /* 设置发送完成标志 */
- has_sent_out = true;
-
- /* 检查发送FIFO中是否有待发送消息 */
- if (rkfifo_out(&canTxFifo, &can_TxMessage, 1) == 1) {
- /* 直接发送(已经是HPM格式) */
- can_send_message_nonblocking(CAN2, &can_TxMessage);
- }
- }
-
- /* 清除发送接收标志 */
- can_clear_tx_rx_flags(CAN2, flags);
-
- /* 处理错误中断 */
- uint8_t error_flags = can_get_error_interrupt_flags(CAN2);
- if (error_flags != 0) {
- // 可以在这里处理错误
- can_clear_error_interrupt_flags(CAN2, error_flags);
- }
- }
|