Hi,
in Technical Reference Manual I didn't find a description of the right sequence to clear MsgLst bit of a CAN receive mailbox.
In section 21.12.9 TRM reports that "MsgLst will not be automatically reset", however there is no bit in IF register to clear it.
On the opposite, NewDat and IndPnd bit can be cleared by writing 1 to TXRQST and ClrIntPnd bit respectively.
I found the following sequence, that is based on a read-modify-write operation.
----------------- MY CODE ----------------
// local variable
uint32_t msgCtrl = 0;
//
// --- READ STEP ---
// Read Data A, Data B, Control informations and clear NewDat e IntPnd bits
//
HWREG_BP(CANB_BASE + CAN_O_IF2CMD) = (CAN_IF2CMD_DATA_A | CAN_IF2CMD_DATA_B |
CAN_IF2CMD_CONTROL | CAN_IF2CMD_TXRQST | CAN_IF2CMD_CLRINTPND | (objID & CAN_IF2CMD_MSG_NUM_M));
//
// Wait for busy bit to clear
//
while((HWREG_BP(CANB_BASE + CAN_O_IF2CMD) & CAN_IF2CMD_BUSY) != 0)
{
;
}
//
// Read out the IF control Register (saving in a local variable).
//
msgCtrl = HWREG_BP(CANB_BASE + CAN_O_IF2MCTL);
//
// --- MODIFY STEP ---
// Reset of MsgLst, NewDat and IntPnd bits in the local variable (NewDat and IntPnd are already cleared in the mailbox by the writing on IF2CMD in READ STEP).
//
msgCtrl &= ~(CAN_IF2MCTL_MSGLST | CAN_IF2MCTL_NEWDAT | CAN_IF2MCTL_INTPND);
//
// --- WRITE STEP ---
// Write Control image on the register
//
HWREG_BP(CANB_BASE + CAN_O_IF2MCTL) = msgCtrl;
//
// Transfer of CAN_IF2MCTL to the mailbox
//
HWREG_BP(CANB_BASE + CAN_O_IF2CMD) = (CAN_IF2CMD_DIR | CAN_IF2CMD_CONTROL | (objID & CAN_IF2CMD_MSG_NUM_M));
//
// Wait for busy bit to clear
//
while((HWREG_BP(CANB_BASE + CAN_O_IF2CMD) & CAN_IF2CMD_BUSY) != 0)
{
;
}
----------------- END OF MY CODE ----------------
The sequence is effective in the sense that after WRITE STEP MsgLst bit is equal to 0.
However I'm not sure this is a good way to clear MsgLst bit because of the following scenario.
Suppose a new message is stored in receive mailbox after READ STEP and before WRITE STEP: NewDat and IntPnd bit are set to 1 in the mailbox. However in the WRITE STEP I force both them to 0.
Then if I read NewDat again from mailbox I will find it equal to 0 so I don't know that a new message has arrived and it is lost!
Which is the correct way to clear MsgLst bit without message loss or other unwanted side effects?
Thank you.
Best regards,
Demis