CAN发送函数与CAN接收函数:
void CAN0SendFrame(CAN_MESSAGE Msg/*, unsigned long id,unsigned char length,unsigned char *txdata */)
{
INT8U index; /*number for read message*/
INT8U tbsel ; /* symbol for CAN0TBSEL*/
CAN0TBSEL = CAN0T**; /* Select lowest empty buffer */
tbsel = CAN0TBSEL; /* Backup selected buffer */
*((INT32U *) ((INT32U)(&CAN0TXIDR0)))= Msg.ulID.ulVal; /*lint !e923 MISRA 2004 Rule 11.3 cast from pointer to unsigned long*/
if(Msg.ubLen > 8U)
{
Msg.ubLen = 8U;
}
for (index = 0U;index < Msg.ubLen; index++)
{
*(&CAN0TXDSR0 + index) = Msg.ubData[index]; /* Load data to Tx buffer Data Segment Registers(ONLY 8 BYTES?) */
}
CAN0TXDLR = Msg.ubLen; /* Set Data Length Code */
CAN0TXTBPR = 0U; /* Set Priority */
CAN0T** = tbsel; /* Start transmission */
}
/* NOTE: The routine should include the following actions to obtain
correct functionality of the hardware.
The ISR is invoked by RXF flag. The RXF flag is cleared
if a "1" is written to the flag in CAN0R** register.
Example: CAN0R** = CAN0R**_RXF_MASK;
*/
void CAN0_RX_ISR_Handler(void)
{
if(CAN0R**_RXF) /*接收缓冲区满 */
{
if(g_can0RxBuf.RxCounter < CAN0_RXBUF_SIZE)
{
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ulID.ubDB[0] = CAN0RXIDR0; /*ID */
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ulID.ubDB[1] = CAN0RXIDR1 ;
#if BMS_CAN0_IDE > 0
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ulID.ubDB[2] = CAN0RXIDR2;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ulID.ubDB[3] = CAN0RXIDR3 ;
#endif
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[0] = CAN0RXDSR0; /*Data */
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[1] = CAN0RXDSR1;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[2] = CAN0RXDSR2;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[3] = CAN0RXDSR3;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[4] = CAN0RXDSR4;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[5] = CAN0RXDSR5;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[6] = CAN0RXDSR6;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubData[7] = CAN0RXDSR7;
g_can0RxBuf.Msg[g_can0RxBuf.RxInptr].ubLen = CAN0RXDLR & 0x0fU; /*Length */
g_can0RxBuf.RxInptr ++;
if(g_can0RxBuf.RxInptr >= CAN0_RXBUF_SIZE)
{
g_can0RxBuf.RxInptr = 0U;
}
g_can0RxBuf.RxCounter ++ ;
}
}
rte_release_Semaphore(rte_can0_MsgRxSem);
CAN0R**_RXF = 1U;/*清中断标志*/
}
|