本帖最后由 求学问路 于 2015-4-23 15:48 编辑
只有50分,谢谢!
设备功能: 每隔0.5S以250K波特率往CAN 总线上发送数据,(无须接收数据以及其他问题处理)
异常干扰: 人为加入干扰,(例如以高优先级ID,以500K波特率往总线发数据)消除干扰后设备不再发送数据,单步调试后发现transmit_mailbox = CAN_NO_MB;导致无法发送数据,请教各位是什么原因?
使用ST库函数
uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
{
uint8_t transmit_mailbox = 0;
/* Check the parameters */
assert_param(IS_CAN_ALL_PERIPH(CANx));
assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
assert_param(IS_CAN_RTR(TxMessage->RTR));
assert_param(IS_CAN_DLC(TxMessage->DLC));
/* Select one empty transmit mailbox */
test4 = CANx->TSR; //测试观察用的变量,正常时test=0x1c000003,异常时test=0x82000008
if ((CANx->TSR&TSR_TME0) == TSR_TME0)
{
transmit_mailbox = 0;
}
else if ((CANx->TSR&TSR_TME1) == TSR_TME1)
{
transmit_mailbox = 1;
}
else if ((CANx->TSR&TSR_TME2) == TSR_TME2)
{
transmit_mailbox = 2;
}
else
{
transmit_mailbox = CAN_NO_MB; //进入这一步导致以下程序不执行无法发送数据
}
if (transmit_mailbox != CAN_NO_MB)
{
/* Set up the Id */
CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
if (TxMessage->IDE == CAN_ID_STD)
{
assert_param(IS_CAN_STDID(TxMessage->StdId));
CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | TxMessage->RTR);
}
else
{
assert_param(IS_CAN_EXTID(TxMessage->ExtId));
CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId<<3) | TxMessage->IDE |
TxMessage->RTR);
}
/* Set up the DLC */
TxMessage->DLC &= (uint8_t)0x0000000F;
CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
/* Set up the data field */
CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
((uint32_t)TxMessage->Data[2] << 16) |
((uint32_t)TxMessage->Data[1] << 8) |
((uint32_t)TxMessage->Data[0]));
CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
((uint32_t)TxMessage->Data[6] << 16) |
((uint32_t)TxMessage->Data[5] << 8) |
((uint32_t)TxMessage->Data[4]));
/* Request transmission */
CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
}
return transmit_mailbox;
}
|