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:
volatile uint32_t g_ul_recv_status = 0;
void CAN1_Handler(void)
{
uint32_t ul_status;
ul_status = can_mailbox_get_status(
CAN1, 0);
can1_mailbox.ul_mb_idx = 0;
can_mailbox_read(
CAN1, &can1_mailbox);
g_ul_recv_status = 1;
}
}
can_reset_all_mailbox(
CAN0);
can_reset_all_mailbox(
CAN1);
can0_mailbox.ul_mb_idx = 0;
can0_mailbox.uc_obj_type = CAN_MB_PRODUCER_MODE;
can0_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
can_mailbox_init(
CAN0, &can0_mailbox);
can0_mailbox.ul_datah = 0x44332211;
can_mailbox_write(
CAN0, &can0_mailbox);
can1_mailbox.ul_mb_idx = 0;
can1_mailbox.uc_obj_type = CAN_MB_CONSUMER_MODE;
can1_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
can_mailbox_init(
CAN1, &can1_mailbox);
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:
ul_status = can_mailbox_get_status(
CAN1, 0);
- In CAN1_Handler(), check whether the mailbox 0 has received a data frame:
can1_mailbox.ul_mb_idx = 0;
can_mailbox_read(
CAN1, &can1_mailbox);
g_ul_recv_status = 1;
}
- In CAN1_Handler(), if mailbox 0 is ready, read the received data from CAN1 mailbox 0:
can1_mailbox.ul_mb_idx = 0;
can_mailbox_read(
CAN1, &can1_mailbox);
- 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:
can_reset_all_mailbox(
CAN0);
can_reset_all_mailbox(
CAN1);
- Initialize CAN0 mailbox 0 as PRODUCER:
can0_mailbox.ul_mb_idx = 0;
can0_mailbox.uc_obj_type = CAN_MB_PRODUCER_MODE;
can0_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
can_mailbox_init(
CAN0, &can0_mailbox);
- Prepare the response information when it receives a remote frame:
can0_mailbox.ul_datah = 0x44332211;
can_mailbox_write(
CAN0, &can0_mailbox);
- Initialize CAN1 mailbox 0 as CONSUMER:
can1_mailbox.ul_mb_idx = 0;
can1_mailbox.uc_obj_type = CAN_MB_CONSUMER_MODE;
can1_mailbox.
ul_id = CAN_MID_MIDvA(0x0b);
can_mailbox_init(
CAN1, &can1_mailbox);
- Enable the CAN1 mailbox 0 interrupt:
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) {
}