MCU_CAN.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "MCU_CAN.h"
  2. #include "i15765.h"
  3. /* receive and transmit buffer sizes */
  4. #define CAN_BUF_RX_SIZE 10
  5. #define CAN_BUF_TX_SIZE 8
  6. /* total number of channels */
  7. #define CAN_PORTS 2
  8. /* used for FIFO buffers */
  9. can_t can_buf_rx[ CAN_PORTS ][ CAN_BUF_RX_SIZE ];
  10. can_t can_buf_tx[ CAN_PORTS ][ CAN_BUF_TX_SIZE ];
  11. uint8_t volatile can_buf_rx_head[ CAN_PORTS ];
  12. uint8_t volatile can_buf_rx_cnt[ CAN_PORTS ];
  13. uint8_t volatile can_buf_tx_tail[ CAN_PORTS ];
  14. uint8_t volatile can_buf_tx_cnt[ CAN_PORTS ];
  15. uint8_t can_buf_rx_tail[ CAN_PORTS ];
  16. uint8_t can_buf_tx_head[ CAN_PORTS ];
  17. uint8_t can_tx_is_active[ CAN_PORTS ];
  18. /*
  19. ** Called by application layer to receive a CAN message.
  20. ** RETURN 0: for success
  21. ** 1: for failure
  22. */
  23. uint8_t can_rx(uint8_t p, can_t *frame)
  24. {
  25. uint8_t ret = 1;
  26. /* since the below global memory is shared with the rx isr, disable isrs */
  27. // CAN_LOCK();
  28. /* if there is something in the buffer, return it */
  29. if(can_buf_rx_cnt[p])
  30. {
  31. /* update buffer size */
  32. can_buf_rx_cnt[p]--;
  33. /* copy over the new can frame from current buffer location */
  34. *frame = can_buf_rx[p][can_buf_rx_tail[p]];
  35. /* move on to next index in buffer */
  36. if(++can_buf_rx_tail[p] >= CAN_BUF_RX_SIZE)
  37. can_buf_rx_tail[p] = 0;
  38. ret = 0;
  39. }
  40. // CAN_UNLOCK();
  41. return ret;
  42. }
  43. /*
  44. ** Internal transmit interrupt.
  45. */
  46. void can_tx_isr_i(uint8_t p)
  47. {
  48. /* if there is nothing to transmit, then exit */
  49. if(can_buf_tx_cnt[p])
  50. {
  51. /* we will have one less */
  52. can_buf_tx_cnt[p]--;
  53. /* load the mailbox */
  54. // can_mbox_write(p, &can_buf_tx[p][can_buf_tx_tail[p]]);
  55. /* move on to next index in buffer */
  56. if(++can_buf_tx_tail[p] >= CAN_BUF_TX_SIZE)
  57. can_buf_tx_tail[p] = 0;
  58. }
  59. else
  60. {
  61. /* the buffer is empty, so we're not transmission is no longer active */
  62. can_tx_is_active[p] = 0;
  63. }
  64. }
  65. /*
  66. ** Internal receive interrupt.
  67. */
  68. void can_rx_isr_i(uint8_t p,uint32_t msgId,uint8_t * data, uint8_t len)
  69. {
  70. can_t *frame;
  71. can_t tmp_frame;
  72. /* is there room in the buffer and is it extended? */
  73. if(can_buf_rx_cnt[p] < CAN_BUF_RX_SIZE)
  74. {
  75. /* mark we have one more in the buffer */
  76. can_buf_rx_cnt[p]++;
  77. /* get pointer to the next open index */
  78. frame = (can_t *)&can_buf_rx[p][ can_buf_rx_head[p] ];
  79. /* move on to next index in buffer */
  80. if(++can_buf_rx_head[p] >= CAN_BUF_RX_SIZE)
  81. can_buf_rx_head[p] = 0;
  82. }
  83. else
  84. {
  85. /* point to empty location because our rx buffer is full (always do a read) */
  86. frame = (can_t *)&tmp_frame;
  87. }
  88. //qiaoxu Get ID �� data lenth and data
  89. //ID
  90. frame->id = msgId;
  91. /* get dlc */
  92. frame->buf_len = len;
  93. /*
  94. * @return The function will return:
  95. * - 0: When RX message box hasn't received new data
  96. * - 1: When RX data are stored in the data buffer
  97. * - 3: When RX data are stored in the data buffer and a message was lost
  98. */
  99. if(frame->buf_len > 0)
  100. {
  101. frame->buf[0] = data[0];
  102. frame->buf[1] = data[1];
  103. frame->buf[2] = data[2];
  104. frame->buf[3] = data[3];
  105. frame->buf[4] = data[4];
  106. frame->buf[5] = data[5];
  107. frame->buf[6] = data[6];
  108. frame->buf[7] = data[7];
  109. }
  110. }