本帖最后由 wjandsq 于 2018-6-29 16:17 编辑
/*
* @brief: Send data via CAN to the specified mailbox with the specified message id
* @param mailbox : Destination mailbox number
* @param messageId : Message ID
* @param data : Pointer to the TX data
* @param len : Length of the TX data
* @return : None
*/
void SendCANData(uint32_t mailbox, uint32_t messageId, uint8_t * data, uint32_t len)
{
/* Set information about the data to be sent
* - 1 byte in length
* - Standard message ID
* - Bit rate switch enabled to use a different bitrate for the data segment
* - Flexible data rate enabled
* - Use zeros for FD padding
*/
flexcan_data_info_t dataInfo =
{
.data_length = len,
.msg_id_type = FLEXCAN_MSG_ID_STD,
.enable_brs = true,
.fd_enable = true,
.fd_padding = 0U
};
/* Configure TX message buffer with index TX_MSG_ID and TX_MAILBOX*/
FLEXCAN_DRV_ConfigTxMb(INST_CANCOM1, mailbox, &dataInfo, messageId);
/* Execute send non-blocking */
FLEXCAN_DRV_Send(INST_CANCOM1, mailbox, &dataInfo, messageId, data);
}
这个发送函数,似乎没有问题,但进入SDK函数FLEXCAN_DRV_ConfigTxMb中,看到
/*FUNCTION**********************************************************************
*
* Function Name : FLEXCAN_DRV_ConfigTxMb
* Description : Configure a Tx message buffer.
* This function will first check if RX FIFO is enabled. If RX FIFO is enabled,
* the function will make sure if the MB requested is not occupied by RX FIFO
* and ID filter table. Then this function will set up the message buffer fields,
* configure the message buffer code for Tx buffer as INACTIVE, and enable the
* Message Buffer interrupt.
*
* Implements : FLEXCAN_DRV_ConfigTxMb_Activity
*END**************************************************************************/
status_t FLEXCAN_DRV_ConfigTxMb(
uint8_t instance,
uint8_t mb_idx,
const flexcan_data_info_t *tx_info,
uint32_t msg_id)
{
DEV_ASSERT(instance < CAN_INSTANCE_COUNT);
DEV_ASSERT(tx_info != NULL);
flexcan_msgbuff_code_status_t cs;
CAN_Type * base = g_flexcanBase[instance];
/* Initialize transmit mb*/
cs.dataLen = tx_info->data_length;
cs.msgIdType = tx_info->msg_id_type;
if (tx_info->is_remote)
{
cs.code = (uint32_t)FLEXCAN_TX_REMOTE;
}
else
{
cs.code = (uint32_t)FLEXCAN_TX_INACTIVE;
}
return FLEXCAN_SetTxMsgBuff(base, mb_idx, &cs, msg_id, NULL);
}
cs.code 数据信息中包含有FLEXCAN_TX_REMOTE和FLEXCAN_TX_INACTIVE的区别,而这个变量在
上层函数根本没有设置!利用 ZLG USBCAN-II 做CAN收发调试,USBCAN-II 发送标准帧数据, S32K144回复的CAN数据总有个别为远程帧,而将上位程序中的dataInfo.is_remote 变量赋值 为false后(dataInfo.is_remote = false;),问题解决!
为了避免发生仲裁,can 接收和发送均设置了软件FIFO,为以后能利用CAN-FD,没有使用Flexcan的
硬件FIFO。修改后的 SendCANData 函数如下:
typedef struct {
uint32_t msgId; /*!< Message Buffer ID*/
uint8_t data[64]; /*!< Data bytes of the FlexCAN message*/
uint8_t dataLen; /*!< Length of data in bytes */
flexcan_msgbuff_id_type_t msg_id_type; /*!< Type of message ID (standard or extended)*/
} flexcan_msg_tx_buf;
/* Flex_CAN Send function ( for Max 64 byte data)*/
status_t SendCANData(uint32_t mailbox, flexcan_msg_tx_buf * can_tx_pstr)
{
/* Set information about the data to be sent
* - 1 byte in length
* - Standard message ID
* - Bit rate switch enabled to use a different bitrate for the data segment
* - Flexible data rate enabled
* - Use zeros for FD padding
*/
flexcan_data_info_t dataInfo;
dataInfo.data_length = can_tx_pstr->dataLen;
dataInfo.msg_id_type = can_tx_pstr->msg_id_type;
dataInfo.enable_brs = false;
dataInfo.fd_enable = false;
dataInfo.fd_padding = 0;
dataInfo.is_remote = false;
status_t result;
/* Configure TX message buffer with index TX_MSG_ID and TX_MAILBOX*/
FLEXCAN_DRV_ConfigTxMb(INST_CANCOM1, mailbox, &dataInfo, can_tx_pstr->msgId);
/* Execute send non-blocking */
result = FLEXCAN_DRV_Send(INST_CANCOM1, mailbox, &dataInfo, can_tx_pstr->msgId, can_tx_pstr->data);
return result;
}
修改后的函数测试正常!如下图所示,连续发送32个不同ID的报文,S32K144设置了较大的接收和发送的 软件FIFO,没有丢一个报文。
|