Robobo
Quickstart guide for SAM CAN module.

This is the quickstart guide for the SAM CAN module, with step-by-step instructions on how to configure and use the drivers in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

In this basic use case, as CAN module needs to work in network, two CAN modules need to be configured. CAN0 mailbox 0 is configured as transmitter, and CAN1 mailbox 0 is configured as receiver. The communication baudrate is 1Mbit/s.

Setup steps

Prerequisites

  • Power Management Controller driver
  • CAN transceiver driver

Example code

Add to application initialization:

can_mb_conf_t can0_mailbox;
can_mb_conf_t can1_mailbox;
can_init(CAN0, ul_sysclk, CAN_BPS_1000K);
can_init(CAN1, ul_sysclk, CAN_BPS_1000K);
can_reset_all_mailbox(CAN0);
can_reset_all_mailbox(CAN1);
can1_mailbox.ul_mb_idx = 0;
can1_mailbox.uc_obj_type = CAN_MB_RX_MODE;
can1_mailbox.ul_id = CAN_MID_MIDvA(0x07);
can_mailbox_init(CAN1, &can1_mailbox);
can0_mailbox.ul_mb_idx = 0;
can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
can0_mailbox.uc_tx_prio = 15;
can0_mailbox.uc_id_ver = 0;
can0_mailbox.ul_id_msk = 0;
can_mailbox_init(CAN0, &can0_mailbox);
can0_mailbox.ul_id = CAN_MID_MIDvA(0x07);
can0_mailbox.ul_datal = 0x12345678;
can0_mailbox.ul_datah = 0x87654321;
can0_mailbox.uc_length = 8;
can_mailbox_write(CAN0, &can0_mailbox);

Workflow

  1. Define the CAN0 and CAN1 Transfer mailbox structure:
  2. Enable the module clock for CAN0 and CAN1:
  3. Initialize CAN0 and CAN1, baudrate is 1Mb/s:
  4. Reset all CAN0 and CAN1 mailboxes:
    • can_reset_all_mailbox(CAN0);
      can_reset_all_mailbox(CAN1);
  5. Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07:
  6. Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15:
    • can0_mailbox.ul_mb_idx = 0;
      can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
      can0_mailbox.uc_tx_prio = 15;
      can0_mailbox.uc_id_ver = 0;
      can0_mailbox.ul_id_msk = 0;
      can_mailbox_init(CAN0, &can0_mailbox);
  7. Prepare transmit ID, data and data length in CAN0 mailbox 0:
    • can0_mailbox.ul_id = CAN_MID_MIDvA(0x07);
      can0_mailbox.ul_datal = 0x12345678;
      can0_mailbox.ul_datah = 0x87654321;
      can0_mailbox.uc_length = 8;
      can_mailbox_write(CAN0, &can0_mailbox);

Usage steps

Example code

Add to, e.g., main loop in application C-file:

can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
while (!(can_mailbox_get_status(CAN1, 0) & CAN_MSR_MRDY)) {
}
can_mailbox_read(CAN1, &can1_mailbox);

Workflow

  1. Send out data in CAN0 mailbox 0:
  2. Wait for CAN1 mailbox 0 to receive the data:
  3. Read the received data from CAN1 mailbox 0:
    • can_mailbox_read(CAN1, &can1_mailbox);

Advanced use cases

For more advanced use of the CAN driver, see the following use cases:

  • Use case #1 : Two CAN modules work in PRODUCER and CONSUMER mode respectively, use CAN interrupt handler to check whether the communication has been completed.