[Atria AT32WB415 series Bluetooth BLE 5.0 MCU] + CAN communication
[Copy link]
[Atria AT32WB415 series Bluetooth BLE 5.0 MCU] + CAN communication
- CAN communication hardware
- According to the AT32WB415 data sheet, this chip has a CAN communication interface (2.0B active) and built-in 256 bytes of dedicated SRAM.
- Controller Area Network (CAN)
1 CAN interface compatible with specifications 2.0A and 2.0B (active), with a bit rate of up to 1 Mbit/s. It can receive and send standard frames with 11-bit identifiers and extended frames with 29-bit identifiers. It has 3 transmit mailboxes, 2 receive FIFOs with 3 levels of depth, and 14 adjustable filters. The CAN controller is allocated 256 bytes of dedicated SRAM, which is not shared with other hardware peripherals.
-
- CAN interface, default PA11, PA12 or remap PB8, PB9. Because PA11PA12 is occupied by USB interface, PB8/PB9 should be selected.
- There is no CAN PHY chip on board, so an external one is required. . . .
- Can communication program example
2.1. Open candemo. In CAN initialization, there is baudrate = pclk/(baudrate_div *(1 + bts1_size + bts2_size)). The baud rate calculation formula is not very strict for CAN communication frequency. You can use it normally if you get the approximate range.
2.2、communication_mode program examples are shown in the following two figures.
Its function is to send a set of data in a loop, but LED2 will light up or go out when receiving data from the computer.
2.3. Filter routine
Automatically send out a group of data, including standard frames and extended frames.
2.4、loopback_mode routine
Data is sent in a loop, and LED3 will light up and then go out when the host computer sends data.
3.1. The original sending code needs to be slightly changed. I made adjustments in the simplest way and just call this function for subsequent data sending.
static void can_transmit(unsigned int id,unsigned char *buf)
{
uint8_t transmit_mailbox;
can_tx_message_type tx_message_struct;
tx_message_struct.standard_id = id; // Standard frame ID
tx_message_struct.extended_id = 0; // Extended frame ID
tx_message_struct.id_type = CAN_ID_STANDARD; // Standard frame or extended frame
tx_message_struct.frame_type = CAN_TFT_DATA; // Frame type data frame or remote frame
tx_message_struct.dlc = 8; // data length
tx_message_struct.data[0] = buf[0];
tx_message_struct.data[1] = buf[1];
tx_message_struct.data[2] = buf[2];
tx_message_struct.data[3] = buf[3];
tx_message_struct.data[4] = buf[4];
tx_message_struct.data[5] = buf[5];
tx_message_struct.data[6] = buf[6];
tx_message_struct.data[7] = buf[7];
transmit_mailbox = can_message_transmit(CAN1, &tx_message_struct); // data sending function
while(can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL); //Get whether the transmission is completed
}
3.2. Initialization code
/* can base init */
can_default_para_init(&can_base_struct); //
can_base_struct.mode_selection = CAN_MODE_COMMUNICATE; //can mode selection
can_base_struct.ttc_enable = FALSE; // Enable/disable time-triggered communication mode
can_base_struct.aebo_enable = TRUE; // Automatically exit offline mode enable
can_base_struct.aed_enable = TRUE; // Automatically exit sleep mode enable
can_base_struct.prsf_enable = FALSE; // Disable retransmission when transmission fails
can_base_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED; // Message discard rule selection when receiving overflow
can_base_struct.mmssr_selection = CAN_SENDING_BY_ID; // Multiple message sending order rule selection
can_base_init(CAN1, &can_base_struct); //
|