Hi Thomas,
First of all, sorry for the later reply.
I don't think there need to checking the TX and RX flags before copying data to a message box.
Please check below MCUXpresso SDK FlexCAN driver FLEXCAN_WriteTxMb() function,
There doesn't check the CAN_ESR1 the status of TX and the RX bit.
It just need to check MB itself status if the MB is active.
status_t FLEXCAN_WriteTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_frame_t *txFrame)
{
/* Assertion. */
assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
assert(txFrame);
assert(txFrame->length <= 8);
assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
uint32_t cs_temp = 0;
/* Check if Message Buffer is available. */
if (CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) != (base->MB[mbIdx].CS & CAN_CS_CODE_MASK))
{
/* Inactive Tx Message Buffer. */
base->MB[mbIdx].CS = (base->MB[mbIdx].CS & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
/* Fill Message ID field. */
base->MB[mbIdx].ID = txFrame->id;
/* Fill Message Format field. */
if (kFLEXCAN_FrameFormatExtend == txFrame->format)
{
cs_temp |= CAN_CS_SRR_MASK | CAN_CS_IDE_MASK;
}
/* Fill Message Type field. */
if (kFLEXCAN_FrameTypeRemote == txFrame->type)
{
cs_temp |= CAN_CS_RTR_MASK;
}
cs_temp |= CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) | CAN_CS_DLC(txFrame->length);
/* Load Message Payload. */
base->MB[mbIdx].WORD0 = txFrame->dataWord0;
base->MB[mbIdx].WORD1 = txFrame->dataWord1;
/* Activate Tx Message Buffer. */
base->MB[mbIdx].CS = cs_temp;
#if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
#endif
return kStatus_Success;
}
else
{
/* Tx Message Buffer is activated, return immediately. */
return kStatus_Fail;
}
}
You could download MCUXpresso SDK software from here.
Thank you for the attention.
Have a great day,
Mike
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------