Table of Contents

Advertisement

Quick Links

Introduction
The CAN (Controller Area Network) is a distributed serial communication protocol which realizes real-
time and reliable data communication between nodes, compliant with the CAN 2.0 specification (2.0A
and 2.0B). This application note introduces CAN protocol, AT32 MCU CAN application flow and
examples of AT32 MCU CAN application.
Note: The corresponding code in this application note is developed on the basis of V2.x.x BSP
provided by Artery. For other versions of BSP, please pay attention to the differences in usage.
Applicable products:
Part number
2022.7.19
AT32 MCU CAN Quick Start Guide
AT32 MCU CAN Quick Start Guide
AT32Fxx
AT32Lxx
1
AN0095
Application Note
Ver 2.0.1

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the AT32 MCU CAN and is the answer not in the manual?

Questions and answers

Summary of Contents for ARTERY AT32 MCU CAN

  • Page 1 AT32 MCU CAN application. Note: The corresponding code in this application note is developed on the basis of V2.x.x BSP provided by Artery. For other versions of BSP, please pay attention to the differences in usage. Applicable products:...
  • Page 2: Table Of Contents

    AT32 MCU CAN Quick Start Guide Contents Introduction to CAN ....................6 CAN protocol ......................7 CAN bus topology ....................7 CAN bus physical layer features ................7 Frame type ....................... 8 Frame structure ...................... 10 Bit stuffing....................... 10 Bit format ......................... 11 Synchronization mechanism ...................
  • Page 3 AT32 MCU CAN Quick Start Guide Application case 2: CAN receive filter ............... 28 Function ......................... 28 Resources ......................28 Software design ...................... 28 Test result ....................... 35 Application case 3: CAN debugging in loopback mode ........36 Function ......................... 36 Resources ......................
  • Page 4 AT32 MCU CAN Quick Start Guide List of tables Table 1 CAN frame types ........................8 Table 2 Description of bit segments ....................11 Table 3 Sample point settings ......................21 Table 4 Document revision history ..................... 41 2022.7.19 Ver 2.0.1...
  • Page 5 AT32 MCU CAN Quick Start Guide List of figures Figure 1 CAN bus topology ........................7 Figure 2 CAN bus level characteristics ....................8 Figure 3 CAN frame structure ......................9 Figure 4 CAN standard data frame ....................10 Figure 5 Normal bit timing ........................12 Figure 6 Resynchronization jump ......................
  • Page 6: Introduction To Can

    AT32 MCU CAN Quick Start Guide Introduction to CAN The Controller Area Network (CAN) is designed to efficiently process a large amount of messages with minimum CPU usage. It is a serial bus protocol created in 1986 by the German company BOSCH and ISO standardized with ISO11898 and ISO11519, which gain popularity in the automotive network domain in Europe.
  • Page 7: Can Protocol

    AT32 MCU CAN Quick Start Guide CAN protocol This section mainly introduces the CAN bus topology, characteristics of physical layers, CAN frame types, frame structure, bit stuffing mechanism, bit format, synchronization mechanism, arbitration mechanism and error handling mechanism. For more details on CAN protocol, refer to BOSCH CAN specification.
  • Page 8: Frame Type

    AT32 MCU CAN Quick Start Guide Figure 2 CAN bus level characteristics CANH line CAN bus physical layer signal CANL line Recessive Dominant Recessive level level level CAN bus logical value Frame type There are five types of frames in CAN protocol, as shown in Table 1. The data frame and remote...
  • Page 9: Figure 3 Can Frame Structure

    AT32 MCU CAN Quick Start Guide Figure 3 CAN frame structure Inter-frame Inter-frame space or Data frame (standard identifier) space overload frame 44 + 8* N Ack field Arbitration field Control field Data field CRC field 8* N Inter-frame Inter-frame...
  • Page 10: Frame Structure

    AT32 MCU CAN Quick Start Guide Frame structure This section mainly introduces standard data frame. For other frame types, refer to Figure 3 for better understanding. The CAN data frame is composed of seven fields as below: SOF (Start of frame): It is dominant of 1-bit. The CAN bus level is recessive when the CAN bus is in an idle state;...
  • Page 11: Bit Format

    AT32 MCU CAN Quick Start Guide example, the original data stream is “0000000111110001…” and the data stream sent to CAN bus after bit stuffing is “000001001111100001…”, in which the underlined bits are stuff bits. Bit stuffing is added from the SOF to CRC field (CRC delimiter not included), as shown in Figure 4.
  • Page 12: Synchronization Mechanism

    AT32 MCU CAN Quick Start Guide Figure 5 Normal bit timing Nomal Bit Timing SYNC_SEG BSEG1 BSEG2 BSEG2 BSEG1 SYNC_SEG Translate Sample Synchronization mechanism Hard synchronization After a hard synchronization, the bit time is restarted with the end of synchronization segment. Thus hard synchronization forces the edge which has caused the hard synchronization to lie within the synchronization segment of the restarted bit time, as shown in Figure 6.
  • Page 13: Arbitration Mechanism

    AT32 MCU CAN Quick Start Guide Figure 6 Resynchronization jump Ideal falling edge Sample point SYNC_SEG BSEG1 BSEG2 Transmitting node 1bit Falling edge on BSEG1 SYNC_SEG BSEG1 BSEG2 delay delay Transmitting 1bit node Sample point Falling edge on BSEG2 SYNC_SEG...
  • Page 14: Can Error Handling Mechanism

    AT32 MCU CAN Quick Start Guide Figure 7 Arbitration mechanism Arbitration lost Node 1 Node 2 Bus level CAN error handling mechanism 2.9.1 Error type There are five types of error in CAN protocol.  Bit error CAN nodes transmitting a message to the bus also monitor the bus and compare the transmitted level bit by bit with the corresponding level on the bus.
  • Page 15: Error State

    AT32 MCU CAN Quick Start Guide CAN node is in a transmitting mode. 2.9.2 Error state When an error is detected on the CAN node, the transmit error counter (TEC[7:0]) /receive error counter (REC[7:0]) is increased by 1 or 8 (refer to BOSCH CAN protocol for details) according to the specific error state and type.
  • Page 16: At32 Can

    AT32 MCU CAN Quick Start Guide AT32 CAN AT32 CAN is compliant with the CAN 2.0 specification (2.0A and 2.0B), with some functions and configurable options added based on the compatible standard CAN protocol. The main differences between CAN 2.0A and 2.0B are as follows: CAN 2.0A only supports 11-bit IDs (i.e., standard frames) while CAN 2.0B supports 11-bit/29-bit IDs (i.e., standard and extended frames).
  • Page 17: Message Reception

    AT32 MCU CAN Quick Start Guide 6) Transmit; 7) Mailbox becomes empty. Note: Steps 1~7 are brief transmission procedures, and Figure 10 also demonstrates transmission cancelled, transmission failed, and automatic retransmission enabled / disenabled. Refer to message transmission in the Reference Manual for details.
  • Page 18 AT32 MCU CAN Quick Start Guide registers; 5) Release mailbox: Set RFxR=1 in the CAN_RFx register. Note: Steps 4~3 are performed by the user, and steps 1~3 are completed by hardware automatically. Valid message: When a message is received correctly (no error occurs in the whole EOF field) and has passed identifier filtering, it is regarded as a valid message.
  • Page 19: Filter

    AT32 MCU CAN Quick Start Guide Figure 11 Message reception process Empty RFxMN=0×00 Valid RFxOF=0 message received Release mailbox RFxR=1 Pending _ RFxMN=0×01 RFxOF=0 Valid message received Release Pending _ mailbox RFxMN=0×10 RFxR=1 RFxOF=0 Valid message received Release Pending mailbox _...
  • Page 20: Figure 12 32-Bit Identifier Mask Mode

    AT32 MCU CAN Quick Start Guide register. One 32-bit filter register CAN_FiFBx includes the SID[10:0], EID[17:0], IDT and RTR bits. Two 16-bit filter register CAN_FiFBx includes the SID[10:0], IDT, RTR and EID[17:15] bits. Filtering mode The filter can be configured in identifier mask mode or in identifier list mode by setting the FMSELx bit in the CAN_FMCFG register.
  • Page 21: Can Baud Rate And Sample Point

    AT32 MCU CAN Quick Start Guide CAN baud rate and sample point The nominal bit time of the CAN bus consists of three parts as follows: Synchronization segment (SYNC_SEG): This segment has one time unit (Tq) and its time duration is defined by the BRDIV[11:0] bit in the CAN_BTMG register.
  • Page 22: Baud Rate Configuration Tool

    AT32 MCU CAN Quick Start Guide >800Kbps >500Kbps <=500Kbps 87.5% 3.5.3 Baud rate configuration tool For the convenience of baud rate configuration, this application note introduces the CAN BitRate Configuration specially developed by Artery. Operation steps: Baud rate setting: High-speed CAN offers baud rates of up to 1 M, and the frequently used baud rates are 125K, 250K, 333K, 500K and 1M.
  • Page 23: Application Case 1: Can Communication In Normal Mode

    AT32 MCU CAN Quick Start Guide Application case 1: CAN communication in normal mode Note: All projects are built around keil 5. If users want to use them in other compiling environments, please refer to AT32xxx_Firmware_Library_V2.x.x\project\at_start_xxx\templates (such as IAR6/7, keil 4/5) for a simple change.
  • Page 24 AT32 MCU CAN Quick Start Guide int main(void) system_clock_config(); /* Configure system clock */ at32_board_init();/*AT-START BOARD basic settings, such as LED initialization, delay initialization, etc. */ nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); /*Configure interrupt group priority */ can_gpio_config();/*Configure the GPIO corresponding to CAN1 */ can_configuration(); /* Configure CAN, including CAN basic settings, baud rate, filter and interrupt */ while(1) can_transmit_data();...
  • Page 25 AT32 MCU CAN Quick Start Guide pclk=144M; boudrate = 144/(12*(1+8+3))=1Mbps can_baudrate_struct.baudrate_div = 12; /* CAN division: 12 */ can_baudrate_struct.rsaw_size = CAN_RSAW_1TQ; /* CAN sync jump width: 1Tq */ can_baudrate_struct.bts1_size = CAN_BTS1_8TQ; /* CAN bit segment 1: 8Tq */ can_baudrate_struct.bts2_size = CAN_BTS2_3TQ; /* CAN bit segment 2: 3Tq */ can_baudrate_set(CAN1, &can_baudrate_struct);...
  • Page 26 AT32 MCU CAN Quick Start Guide Data frame */ tx_message_struct.dlc = 8; /* To-be-transmitted data length (0~8): 8 */ tx_message_struct.data[0] = 0x11; /* 1st byte: 0x11 */ tx_message_struct.data[1] = 0x22; /* 2nd byte: 0x22 */ tx_message_struct.data[2] = 0x33; /* 3rd byte: 0x33 */ tx_message_struct.data[3] = 0x44;...
  • Page 27: Test Result

    AT32 MCU CAN Quick Start Guide gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; /* GPIO drive strength: strong */ gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; /* FPIO output mode: push-pull output */ gpio_init_struct.gpio_mode = GPIO_MODE_MUX; /* GPIO mode: multiplexed*/ gpio_init_struct.gpio_pins = GPIO_PINS_9 | GPIO_PINS_8; /* GPIO pins: pin8 & pin9 */ gpio_init_struct.gpio_pull = GPIO_PULL_NONE;...
  • Page 28: Application Case 2: Can Receive Filter

    AT32 MCU CAN Quick Start Guide Application case 2: CAN receive filter Note: All projects are built around keil 5. If users want to use them in other compiling environments, please refer to AT32xxx_Firmware_Library_V2.x.x\project\at_start_xxx\templates (such as IAR6/7, keil 4/5) for a simple change.
  • Page 29 AT32 MCU CAN Quick Start Guide /* extended identifier */ #define FILTER_EXT_ID1 ((uint32_t)0x18F5F100) #define FILTER_EXT_ID2 ((uint32_t)0x18F5F200) /* standard identifier */ #define FILTER_STD_ID1 ((uint16_t)0x04F6) #define FILTER_STD_ID2 ((uint16_t)0x04F7)  Main function int main(void) system_clock_config(); /* Configure system clock */ at32_board_init(); /* Configure AT-START BOARD basic settings, such as LED initialization, delay initialization, etc.
  • Page 30 AT32 MCU CAN Quick Start Guide can_base_struct.ttc_enable = FALSE; /* CAN time triggered communication mode (timestamp): Disabled */ can_base_struct.aebo_enable = TRUE; /* Automatic exit bus-off mode: Enabled */ can_base_struct.aed_enable = TRUE; /* Automatic exit doze mode: Enabled */ can_base_struct.prsf_enable = FALSE; /* Prohibit auto retransmission: Disabled (that is, auto retransmission enabled, as specified in CAN protocol) */ can_base_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED;...
  • Page 31 AT32 MCU CAN Quick Start Guide can_filter_init_struct.filter_fifo = CAN_FILTER_FIFO0; /* Filter relation FIFO (FIFO1/FIFO1) select FIFO0 */ can_filter_init_struct.filter_number = 1; /* Filter bank select (0~27):1 */ can_filter_init_struct.filter_bit = CAN_FILTER_32BIT; /* Filter bit width: 32 bits */ can_filter_init_struct.filter_id_high = FILTER_STD_ID1 << 5; /* Configure the standard ID that can pass filtering: FILTER_STD_ID1 */ can_filter_init_struct.filter_id_low = 0;...
  • Page 32 AT32 MCU CAN Quick Start Guide tx_message_struct.data[6] = 0x77; /* 7th byte: 0x77 */ tx_message_struct.data[7] = 0x88; /* 8th byte: 0x88 */ transmit_mailbox = can_message_transmit(CAN1, &tx_message_struct); /* Write the above to-be- transmitted message into the transmit mailbox and request to send */ while(can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);...
  • Page 33 AT32 MCU CAN Quick Start Guide tx_message_struct.data[6] = 0x77; /* 7th byte: 0x77 */ tx_message_struct.data[7] = 0x88; /* 8th byte: 0x88 */ transmit_mailbox = can_message_transmit(CAN1, &tx_message_struct); /* Write the above to-be- transmitted message into the transmit mailbox and request to send */ while(can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);...
  • Page 34 AT32 MCU CAN Quick Start Guide FILTER_STD_ID1)) test_result++; /* If a standard frame (ID= FILTER_STD_ID1) is received, the test_result flag is incremented by 1 */ else if((rx_message_struct.id_type == CAN_ID_STANDARD) && (rx_message_struct.standard_id == FILTER_STD_ID2)) test_result++;/* If a standard frame (ID= FILTER_STD_ID2) is received, the test_result flag is incremented by 1 */ else if((rx_message_struct.id_type == CAN_ID_EXTENDED) &&...
  • Page 35: Test Result

    AT32 MCU CAN Quick Start Guide Test result  If the data transmission is correct, LED2/3/4 on AT-START BOARD toggle once, indicating four frames of data (ID=FILTER_EXT_ID1, FILTER_EXT_ID2, FILTER_STD_ID1, FILTER_STD_ID2) are received. 2022.7.19 Ver 2.0.1...
  • Page 36: Application Case 3: Can Debugging In Loopback Mode

    AT32 MCU CAN Quick Start Guide Application case 3: CAN debugging in loopback mode Note: Note: All projects are built around keil 5. If users want to use them in other compiling environments, please refer to AT32xxx_Firmware_Library_V2.x.x\project\at_start_xxx\templates (such as IAR6/7, keil 4/5) for a simple change.
  • Page 37 AT32 MCU CAN Quick Start Guide  Configure CAN basic settings  Configure CAN baud rate  Configure CAN filter  Configure CAN interrupt Code  Main function int main(void) system_clock_config(); /* Configure system clock */ at32_board_init(); /* Configure AT-START BOARD basic settings, such as LED initialization, delay initialization, etc.
  • Page 38 AT32 MCU CAN Quick Start Guide discarding rule when overflow: the previous message is discarded */ can_base_struct.mmssr_selection = CAN_SENDING_BY_ID; /* Multiple message transmission priority: message with the smallest identifier is first transmitted */ can_base_init(CAN1, &can_base_struct); /* Write the above configuration into CAN master control...
  • Page 39 AT32 MCU CAN Quick Start Guide static void can_transmit_data(void) uint8_t transmit_mailbox; can_tx_message_type tx_message_struct; tx_message_struct.standard_id = 0x400; /* Set the to-be-transmit data ID=0x400 */ tx_message_struct.extended_id = 0; tx_message_struct.id_type = CAN_ID_STANDARD; /* To-be-transmitted data type (standard/extended): standard data frame */ tx_message_struct.frame_type = CAN_TFT_DATA; /* To-be-transmitted frame type (remote/data): data frame */ tx_message_struct.dlc = 8;...
  • Page 40: Test Result

    AT32 MCU CAN Quick Start Guide static void can_gpio_config(void) gpio_init_type gpio_init_struct; /* enable the gpio clock */ crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); /* Enable the corresponding GPIOB clock */ gpio_default_para_init(&gpio_init_struct); /* Initialize GPIO configuration structure */ /* configure the can tx, rx pin */ gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;...
  • Page 41: Revision History

    AT32 MCU CAN Quick Start Guide Revision history Table 4 Document revision history Date Version Revision note 2021.5.20 2.0.0 Initial release 1. Modified the CAN baud rate calculation tool as the 2022.7.19 2.0.1 Artery_CAN_BitRate_Configuration. 2022.7.19 Ver 2.0.1...
  • Page 42 No license, express or implied, to any intellectual property rights is granted under this document. If any part of this document deals with any third party products or services, it shall not be deemed a license grant by ARTERY for the use of such third party products or services, or any intellectual property contained therein, or considered as a warranty regarding the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.

Table of Contents