ZHCAF96 April 2025 MSPM0G3506 , MSPM0G3507 , MSPM0G3518 , MSPM0G3519
在 SDK 中,TX 在当前演示项目中配置为缓冲模式。下文描述了如何在 FIFO 模式下使用 TX。
uint8_t MCAN_send_frame(uint32_t id, uint8_t* data, uint16_t len)
{
frame_send_success = false;
DL_MCAN_TxBufElement txMsg;
DL_MCAN_TxFIFOStatus txfifoStatus;
/* Initialize message to transmit. */
/* Identifier Value. */
txMsg.id = id;
/* Transmit data frame. */
txMsg.rtr = 0U;
/* 11-bit standard identifier. */
txMsg.xtd = 0U;
/* ESI bit in CAN FD format depends only on error passive flag. */
txMsg.esi = 0U;
/* Transmitting 4 bytes. */
txMsg.dlc = encode_dlc(len);
/* CAN FD frames transmitted with bit rate switching. */
txMsg.brs = protocol_mode;
/* Frame transmitted in CAN FD format. */
txMsg.fdf = protocol_mode; //protocol_mode;
/* Store Tx events. */
txMsg.efc = 1U;
/* Message Marker. */
txMsg.mm = 0xAAU;
/* Data bytes. */
for (int i = 0; i < len; i++) txMsg.data[i] = data[i];
while (DL_MCAN_OPERATION_MODE_NORMAL != DL_MCAN_getOpMode(CANFD0))
;
/* Write Tx Message to the Message RAM (FIFO). */
DL_MCAN_writeMsgRam(CANFD0, DL_MCAN_MEM_TYPE_FIFO, 0, &txMsg);
/* Get put index and other TxFIFO details in txfifoStatus*/
DL_MCAN_getTxFIFOQueStatus(CANFD0, &txfifoStatus);
/* Enable Transmission interrupt.*/
DL_MCAN_TXBufTransIntrEnable(CANFD0, txfifoStatus.putIdx, 1U);
/* Add request for transmission. */
DL_MCAN_TXBufAddReq(CANFD0, txfifoStatus.putIdx);
if (txMsg.id == MCAN_HOST_ID) {
while (frame_send_success == false) {
;
}
}
return 0;
}
首先,通过调用 DL_MCAN_writeMsgRam() 将 TX 消息保存到 FIFO 类型的消息 RAM 中。然后,通过调用 DL_MCAN_getTxFIFOQueStatus() 获取该消息的索引,该索引指示已保存消息在 FIFO 中的位置。之后,通过调用 DL_MCAN_TXBufTransIntrEnable(),使用此消息的 put 索引信息启用传输中断。最后,通过调用 DL_MCAN_TXBufAddReq(),使用此消息的 put 索引信息调用传输用的添加请求。