#include "MCU_CAN.h" #include "i15765.h" /* receive and transmit buffer sizes */ #define CAN_BUF_RX_SIZE 10 #define CAN_BUF_TX_SIZE 8 /* total number of channels */ #define CAN_PORTS 2 /* used for FIFO buffers */ can_t can_buf_rx[ CAN_PORTS ][ CAN_BUF_RX_SIZE ]; can_t can_buf_tx[ CAN_PORTS ][ CAN_BUF_TX_SIZE ]; uint8_t volatile can_buf_rx_head[ CAN_PORTS ]; uint8_t volatile can_buf_rx_cnt[ CAN_PORTS ]; uint8_t volatile can_buf_tx_tail[ CAN_PORTS ]; uint8_t volatile can_buf_tx_cnt[ CAN_PORTS ]; uint8_t can_buf_rx_tail[ CAN_PORTS ]; uint8_t can_buf_tx_head[ CAN_PORTS ]; uint8_t can_tx_is_active[ CAN_PORTS ]; /* ** Called by application layer to receive a CAN message. ** RETURN 0: for success ** 1: for failure */ uint8_t can_rx(uint8_t p, can_t *frame) { uint8_t ret = 1; /* since the below global memory is shared with the rx isr, disable isrs */ // CAN_LOCK(); /* if there is something in the buffer, return it */ if(can_buf_rx_cnt[p]) { /* update buffer size */ can_buf_rx_cnt[p]--; /* copy over the new can frame from current buffer location */ *frame = can_buf_rx[p][can_buf_rx_tail[p]]; /* move on to next index in buffer */ if(++can_buf_rx_tail[p] >= CAN_BUF_RX_SIZE) can_buf_rx_tail[p] = 0; ret = 0; } // CAN_UNLOCK(); return ret; } /* ** Internal transmit interrupt. */ void can_tx_isr_i(uint8_t p) { /* if there is nothing to transmit, then exit */ if(can_buf_tx_cnt[p]) { /* we will have one less */ can_buf_tx_cnt[p]--; /* load the mailbox */ // can_mbox_write(p, &can_buf_tx[p][can_buf_tx_tail[p]]); /* move on to next index in buffer */ if(++can_buf_tx_tail[p] >= CAN_BUF_TX_SIZE) can_buf_tx_tail[p] = 0; } else { /* the buffer is empty, so we're not transmission is no longer active */ can_tx_is_active[p] = 0; } } /* ** Internal receive interrupt. */ void can_rx_isr_i(uint8_t p,uint32_t msgId,uint8_t * data, uint8_t len) { can_t *frame; can_t tmp_frame; /* is there room in the buffer and is it extended? */ if(can_buf_rx_cnt[p] < CAN_BUF_RX_SIZE) { /* mark we have one more in the buffer */ can_buf_rx_cnt[p]++; /* get pointer to the next open index */ frame = (can_t *)&can_buf_rx[p][ can_buf_rx_head[p] ]; /* move on to next index in buffer */ if(++can_buf_rx_head[p] >= CAN_BUF_RX_SIZE) can_buf_rx_head[p] = 0; } else { /* point to empty location because our rx buffer is full (always do a read) */ frame = (can_t *)&tmp_frame; } //qiaoxu Get ID �� data lenth and data //ID frame->id = msgId; /* get dlc */ frame->buf_len = len; /* * @return The function will return: * - 0: When RX message box hasn't received new data * - 1: When RX data are stored in the data buffer * - 3: When RX data are stored in the data buffer and a message was lost */ if(frame->buf_len > 0) { frame->buf[0] = data[0]; frame->buf[1] = data[1]; frame->buf[2] = data[2]; frame->buf[3] = data[3]; frame->buf[4] = data[4]; frame->buf[5] = data[5]; frame->buf[6] = data[6]; frame->buf[7] = data[7]; } }