In this use case, CAN0 mailbox 0 works in PRODUCER mode, and CAN1 mailbox 0 works in CONSUMER mode.
While CAN1 mailbox 0 receives a data frame from the bus, an interrupt is triggered.
Setup steps
Prerequisites
- Power Management Controller driver
- CAN transceiver driver
Example code
Add to application C-file:
{
uint32_t ul_status;
if ((ul_status & CAN_MSR_MRDY) == CAN_MSR_MRDY) {
g_ul_recv_status = 1;
}
}
can0_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
can1_mailbox.
ul_id_msk = CAN_MID_MIDvA_Msk | CAN_MID_MIDvB_Msk;
can1_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
NVIC_EnableIRQ(CAN1_IRQn);
Workflow
- Define the CAN0 and CAN1 Transfer mailbox structure:
- Define the receive flag that is changed in CAN1 ISR handler:
volatile uint32_t g_ul_recv_status = 0;
- Define the CAN1 ISR handler in the application:
- In CAN1_Handler(), get CAN1 mailbox 0 status:
- In CAN1_Handler(), check whether the mailbox 0 has received a data frame:
if ((ul_status & CAN_MSR_MRDY) == CAN_MSR_MRDY) {
g_ul_recv_status = 1;
}
- In CAN1_Handler(), if mailbox 0 is ready, read the received data from CAN1 mailbox 0:
- In CAN1_Handler(), if mailbox 0 is ready, set up the receive flag:
- Enable the module clock for CAN0 and CAN1:
- Initialize CAN0 and CAN1, baudrate is 1Mb/s:
- Note
- The CAN transceiver should be configured before initializing the CAN module.
- Reset all CAN0 and CAN1 mailboxes:
- Initialize CAN0 mailbox 0 as PRODUCER:
can0_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
- Prepare the response information when it receives a remote frame:
- Initialize CAN1 mailbox 0 as CONSUMER:
can1_mailbox.
ul_id_msk = CAN_MID_MIDvA_Msk | CAN_MID_MIDvB_Msk;
can1_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
- Enable the CAN1 mailbox 0 interrupt:
NVIC_EnableIRQ(CAN1_IRQn);
Usage steps
Example code
while (!g_ul_recv_status) {
}
Workflow
- Enable CAN0 mailbox 0 to receive remote frame and respond it:
- Enable CAN1 mailbox 0 to send out a remote frame and then receive data frame from bus:
- Wait for the communication to be completed.
while (!g_ul_recv_status) {
}