Advertisement

Quick Links

HT32F49xxx MCU CAN Quick Start Guide
D/N: AN0658EN
Introduction
The CAN (Controller Area Network) is a distributed serial communication protocol which
implements real-time and reliable data communication between nodes, compliant with the CAN 2.0
specification (2.0A and 2.0B). This application note introduces the CAN protocol, HT32F49xxx
MCU CAN application flow and CAN application examples.
CAN Overview
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 ISO 11898 and ISO 11519, which gain popularity in the
automotive network domain in Europe. It has been recognized for high performance and reliability
and widely used in industrial automation, ships, medical facilities and industrial equipment, etc.
CAN Protocol Features
Multi-master control
When the bus is idle, all nodes can start sending messages. When multiple nodes start sending
at the same time, the bus proceeds arbitration according to the identifier (ID), and the one with
the lowest ID (highest priority) wins and gets the right to send while all other nodes (with lower
priority) switch to a receiving mode. It should be noted that ID does not the node address but
the priority of message to be sent.
Flexibility
As mentioned, there is no address information of nodes on the CAN bus. Therefore, adding or
removing a node does not affect the hardware and software of other nodes on the CAN bus.
High reliability
The CAN protocol features are error detection, error notification, fault confinement and error
handling. Every node on the CAN bus can detect an error (error detection) in a message. If any
error is found, the discovering node will transmit an error frame to notify other nodes (error
notification). Each node has an internal error counter that accumulates error count value every
time an error is detected. When the accumulative error count value of one node is greater than
or equal to 256, this fault node is disconnected from the CAN bus to avoid affecting other nodes
AN0658EN V1.00
HT32F49xxx MCU CAN Quick Start Guide
1 / 29
December 25, 2023

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the HT32F49 Series and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Summary of Contents for Holtek HT32F49 Series

  • Page 1 HT32F49xxx MCU CAN Quick Start Guide HT32F49xxx MCU CAN Quick Start Guide D/N: AN0658EN Introduction The CAN (Controller Area Network) is a distributed serial communication protocol which implements real-time and reliable data communication between nodes, compliant with the CAN 2.0 specification (2.0A and 2.0B).
  • Page 2 HT32F49xxx MCU CAN Quick Start Guide (fault confinement). If the node detects an error when sending a message, the erroneous message will be retransmitted after the fault is cleared (error handling). Fast and long-range communication  The rate can reach 1Mbps (communication distance < 40m) and the communication distance can reach 10km (rate <...
  • Page 3: Frame Type

    HT32F49xxx MCU CAN Quick Start Guide CAN Bus Physical Layer Features As shown in Figure 2, the dominant level corresponds to logical “0” (voltage difference between CANH and CANL is about 2.5V) and the recessive level corresponds to logical “1” (voltage difference between CANH and CANL is 0V).
  • Page 4: Frame Structure

    HT32F49xxx MCU CAN Quick Start Guide The structure of each frame type is shown in Figure 3. Data Frame (Standard Identifier) Inter-frame space or Inter-frame space overload frame 44+8×N ACK Field Data Field CRC Field Arbitration Field Control Field 8×N Delimiter Bit Data Frame ( Extended Identifier) Inter-frame space or...
  • Page 5: Bit Stuffing

    HT32F49xxx MCU CAN Quick Start Guide The CAN standard 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; therefore, the dominant SOF bit marks the start of a message on the node. ...
  • Page 6 HT32F49xxx 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. Bit Format One bit of the HT32F49xxx CAN can be divided into the following 3 segments: Synchronization segment (SYNC_SEG)
  • Page 7 HT32F49xxx MCU CAN Quick Start Guide Synchronization Mechanism Hard Synchronization After a hard synchronization, the bit time is restarted with the end of synchronization segment. Thus the 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 8: Error Type

    HT32F49xxx MCU CAN Quick Start Guide transmitter compares the level of the bit transmitted with the level that is monitored on the bus. If these levels are equal, the node may continue to send. When a recessive level is sent, but a dominant level is monitored, the node has lost arbitration and must withdraw without sending any further bits, refer to the “CAN Bus Physical Layer Features”...
  • Page 9: Error State

    HT32F49xxx MCU CAN Quick Start Guide Form Error  A form error is detected when a fixed format field contains one or more illegal bits. For example, if a dominant bit is detected in the CRC delimiter or ACK delimiter, it is a form error. There is an exception that the dominant bit during the last bit at the end of frame of the receivers is not considered as a frame error.
  • Page 10: Function Overview

    HT32F49xxx MCU CAN Quick Start Guide TEC or REC≥128 Error Active Error Passive TEC and REC<128 128 occurrences of TEC>255 11 consecutive recessive bits Bus-off Figure 8. CAN Node Error States HT32F49xxx CAN The HT32F49xxx 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.
  • Page 11 HT32F49xxx MCU CAN Quick Start Guide Mail Mail Mail Receive FIFO 14 filter banks [1:0] Bit timing control CAN 1 RX/TX Email [2:0] Send arbitration Mail Mail Mail Receive FIFO 14 filter banks [1:0] RX/TX CAN 2 Bit timing control Email [2:0] Send arbitration Figure 9.
  • Page 12 HT32F49xxx MCU CAN Quick Start Guide Figure 10 shows the following flag bits and operation bits: Empty TMxTCF=X TMxTSF=X TMxEF=1 Pending TMxTCF=0 TMxTSF=0 TMxEF=0 Empty Scheduled TMxCT=1 TMxTCF=1 TMxTCF=0 TMxTSF=0 TMxTSF=0 TMxEF=1 TMxEF=0 Sending TMxTCF=0 TMxTSF=0 TMxEF=0 Empty TMxTCF=1 TMxTSF=1 TMxEF=1 ...
  • Page 13 HT32F49xxx MCU CAN Quick Start Guide Valid Message When a message is received correctly (no error occurs until the last bit of the EOF field) and has passed identifier filtering, it is regarded as a valid message. The filter is described in the next section. If the user does not participate in the message receiving process i.e., neither read the valid message nor release mailbox, the hardware completes the following process: Step 1: Valid message received;...
  • Page 14: Filtering Mode

    HT32F49xxx MCU CAN Quick Start Guide Filter In the CAN protocol, the message ID does not represent the node address but is related to the message content. Therefore, the transmitter broadcasts the message to all receivers, and the node determines whether this message is required by software according to the ID value. If required, this message is stored in the receive FIFO, which can be obtained by users by reading the receive mailbox register;...
  • Page 15 HT32F49xxx MCU CAN Quick Start Guide CAN_FiFB1[15:5] CAN_FiFB1[4:0] Mask CAN_FiFB1[31:21] CAN_FiFB1[20:16] CAN_FiFB2[15:5] CAN_FiFB2[4:0] Mask CAN_FiFB2[31:21] CAN_FiFB2[20:16] Mapping SID[10:0] EID[17:15] Figure 14. 16-bit Identifier Mask Mode CAN_FiFB1[15:8] CAN_FiFB1[7:0] CAN_FiFB1[31:24] CAN_FiFB1[23:16] CAN_FiFB2[15:8] CAN_FiFB2[7:0] CAN_FiFB2[31:24] CAN_FiFB2[23:16] Mapping SID[10:0] EID[17:15] Figure 15. 16-bit Identifier List Mode For details about filter match number and priority rules, refer to the corresponding User Manual.
  • Page 16: Firmware Library

    ≤500kbps 87.5% Table 3. Recommended Sample Point Settings Firmware Library Before using the example program, prepare the latest Holtek HT32 Firmware Library which can be obtained from the following HT32 development resource link. The development resources are categorised core series.
  • Page 17: Software Design

    This compressed file contains several folders which can be categorised as Document, Firmware Library, Tools, etc. The Firmware_Library folder contains the required compressed file named “HT32[ICFAMILY]_FWLib_Vm.n.r_s.zip”. Download link: https://mcu.holtek.com/ht32/resource/ Application Example 1: CAN Communication in Normal Mode Function Implement communication between two CAN nodes. Take the HT32F49395 as an example.
  • Page 18 HT32F49xxx MCU CAN Quick Start Guide 2. Code  main function int main(void) system_clock_config(); /* Configure system clock */ ht32_board_init();/* Board basic settings, such as LED initialisation, delay initialisation, etc. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); /* Configure interrupt group priority */ can_gpio_config();/* Configure the GPIO corresponding to CAN1 */ can_configuration();...
  • Page 19 HT32F49xxx MCU CAN Quick Start Guide /* CAN interrupt configuration */ nvic_irq_enable(CAN1_SE_IRQn, 0x00, 0x00); /* Interrupt priority setting and enable: CAN1 status change/error interrupt */ nvic_irq_enable(USBFS_L_CAN1_RX0_IRQn, 0x00, 0x00); /* Interrupt priority setting and enable: CAN1 FIFO0 receive interrupt */ can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE); /* CAN1 FIFO0 non-empty interrupt enable: An interrupt is generated when FIFO0 receives a frame of valid data */ can_interrupt_enable(CAN1, CAN_ETRIEN_INT, TRUE);...
  • Page 20: Test Result

    HT32F49xxx MCU CAN Quick Start Guide gpio_init_struct.gpio_mode = GPIO_MODE_MUX; /* GPIO mode: multiplexed */ gpio_init_struct.gpio_pins = GPIO_PINS_9; /* GPIO pin: pin9 */ gpio_init_struct.gpio_pull = GPIO_PULL_NONE; /* GPIO pull configuration: no pull-up, no pull-down gpio_init(GPIOB, &gpio_init_struct); /* Write the above configuration into the corresponding register */ /* Configure the CAN RX pin */ gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;...
  • Page 21 HT32F49xxx MCU CAN Quick Start Guide Software Design 1. Configuration process  Configure GPIO pins corresponding to CAN1 TX and RX pins  Configure CAN basic settings  Configure CAN baud rate  Configure CAN filter  Configure CAN interrupt 2.
  • Page 22 HT32F49xxx MCU CAN Quick Start Guide message with the smallest identifier is first transmitted */ can_base_init(CAN1, &can_base_struct); /* Write the above base configurations into CAN master control register */ /* CAN baud rate configuration: can baudrate, set baudrate = pclk/(baudrate_div *(1 + bts1_size + bts2_size)) pclk=120M;...
  • Page 23 HT32F49xxx MCU CAN Quick Start Guide 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; /* 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;...
  • Page 24 HT32F49xxx MCU CAN Quick Start Guide tx_message_struct.data[2] = 0x33; /* 3rd byte: 0x33 */ tx_message_struct.data[3] = 0x44; /* 4th byte: 0x44 */ tx_message_struct.data[4] = 0x55; /* 5th byte: 0x55 */ tx_message_struct.data[5] = 0x66; /* 6th byte: 0x66 */ tx_message_struct.data[6] = 0x77; /* 7th byte: 0x77 */ tx_message_struct.data[7] = 0x88;...
  • Page 25 HT32F49xxx MCU CAN Quick Start Guide Test Result If the data transmission is correct, the LED2/3/4 on the ESK32-31401 board toggle once, indicating four frames of data (ID=FILTER_EXT_ID1, FILTER_EXT_ID2, FILTER_STD_ID1 and FILTER_STD_ID2) are received. Application Example 3: CAN Debugging in Loopback Mode Function Implement CAN loopback communication.
  • Page 26 HT32F49xxx MCU CAN Quick Start Guide 2. Code  main function int main(void) system_clock_config(); /* Configure system clock */ ht32_board_init(); /* Board basic settings, such as LED initialisation, delay initialisation, etc.*/ nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); /* Configure interrupt group priority */ can_gpio_config(); /* Configure the GPIO corresponding to CAN1(Not required in loopback mode; if it is configured, CAN_TX pin outputs data but CAN_RX pin does not receive external data, as shown in Figure 18) */ can_configuration();...
  • Page 27 HT32F49xxx MCU CAN Quick Start Guide can_filter_init(CAN1, &can_filter_init_struct); /* Write the above base configurations into relevant CAN filter registers */ /* CAN interrupt configuration */ nvic_irq_enable(CAN1_SE_IRQn, 0x00, 0x00); /* Interrupt priority setting and enable: CAN1 status change/error interrupt */ nvic_irq_enable(USBFS_L_CAN1_RX0_IRQn, 0x00, 0x00); /* Interrupt priority setting and enable: CAN1 FIFO0 receive interrupt */ can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);...
  • Page 28: Reference Material

    The LED2 on the ESK32-31401 board blinks, indicating a message in standard data format with ID=0x400 is received, and the LED4 keeps blinking, indicating the program is operating properly. Reference Material Refer to the HT32F49xxx MCU User Manual. For more information consult the Holtek website: https://www.holtek.com/. Version and Modification Information Date Author...
  • Page 29: Limitation Of Liability

    Information displayed at such sites. Hyperlinks to other websites are at your own risk. Limitation of Liability In no event shall Holtek Limited be liable to any other party for any loss or damage whatsoever or howsoever caused directly or indirectly in connection with your access to or use of this website, the content thereon or any goods, materials or services.

Table of Contents