Reference project of CAN bus analyzer based on STM32 MCU: part 1/2 - hardware

Document created by Szymon Panecki Employee on Jun 26, 2018
Version 1Show Document
  • View in full screen mode

Subject of this material is a reference project of CAN bus analyzer. Content guides reader through whole development process step by step. First article (out of two) covers hardware aspects: system's functions analysis, components selection and PCB schematics & layout design. Second article, which will come later, will cover firmware development and system validation by testing. We encourage to read this material everyone, who would like to:

  • increase his STM32 MCUs knowledge,
  • improve his STM32 MCUs hardware design and firmware development skills,
  • become more familiar with CAN in STM32 MCUs.

 

Introduction

CAN (Controller Area Network) is a popular communication interface used in many various industrial and automotive systems. These systems often consist of many devices, which use CAN bus to transmit and receive data. Such complex architecture of a systems makes software development and application debugging quite challenging. In order to decrease time and effort needed to finalize a system, a dedicated CAN tools are used. These tools are called CAN bus analyzers, CAN bus sniffers or CAN bus loggers. They allow to make a non-intrusive connection to a CAN bus and provide features related to CAN messages reception and transmission according to user’s configuration.

System’s functions analysis

In one sentence CAN bus analyzer can be described as a tool, which allows to send and receive CAN 2.0A/B messages as well as present them to the user. However in fact on the application level there are several different tasks (they can be also called as system's functions) hidden under this short definition. Let's try to distinguish them and then explain shortly their purpose. All tasks are also presented graphically on the Figure 1.

 

First task can be named as "CAN-to-USB". CAN bus analyzer listens to CAN traffic and receives every CAN message, which is sent within the CAN bus. When message is received, it is processed. This action means a conversion of the message content to ASCII (American Standard Code for Information Interchange) signs format. Finally these ASCII signs are sent by CAN bus analyzer to PC via USB interface, so the user is able to see CAN messages on PC’s screen.

Second task has the opposite meaning to the first one. It can be named as "USB-to-CAN". User sends content in form of ASCII signs from PC to CAN bus analyzer by USB interface. Immediately when data reception process is finished, CAN bus analyzer converts content to a CAN message and sends on a CAN bus.

All devices within one CAN bus have to send and receive messages with the same CAN baudrate (speed), otherwise they would be not able to communicate. As a result, CAN bus analyzer needs to have a function, which allows to change CAN baudrate to different value. This is third task ("CAN speed change") performed by a tool. It takes place when user decides to set different CAN speed for CAN bus analyzer. As a result ASCII signs, which represent new value of CAN baudrate are sent from PC to CAN bus analyzer by USB interface. CAN bus analyzer interprets these ASCII signs, changes them to value and sets new CAN baudrate.

Fourth and last task is related to previous three tasks. Sending and receiving of CAN messages as well as changing of CAN baudrate should be indicated somehow to user. Very convenient solution for this is blinking of LED and that is how this task can be named ("LEDs"). After each CAN message reception and transmission related LED ftom CAN bus analyzer makes one quick blink. When CAN baudrate is changed, both LEDs blink in the same time.

Fig. 1. Tasks executed by CAN bus analyzer together with arrows showing direction of data flow

System's components selection

Software implementation of functionalities described in previous chapter requires suitable hardware platform. This hardware platform needs to be fitted with the following ICs (Integrated Circuits): MCU (microcontroller), CAN transceiver, power management IC and USB protection IC. This chapter provides description of ICs selected for purpose of CAN bus analyzer.

First component to be selected was MCU. Author decided to use device from STM32L4 family offered by STMicroelectronics. STM32L4 are 32-bit MCUs based on Cortex-M4 core equipped with FPU (Floating Point Unit) and DSP (Digital Signal Processing) instructions. These devices also benefit from rich set of analog, timing and communication peripherals. Last but not least, STM32L4 are ultra-low power MCUs. This combination of features (performance, peripherals, low-power) makes STM32L4 a good solution for wide range of industrial applications. For the CAN bus analyzer key features were two peripherals, which are integrated in STM32l4 devices: CAN 2.0 A/B controller and USB 2.0 full-speed controller. Thanks to them one IC can perform all tasks of application: CAN communication, USB communication and LEDs control (these are controlled by GPIOs - General Purpose Input Output pins). A part number selected from STM32L4 family is STM32L476RGT6. Its detailed information is provided in Table 1.

Table 1. Detailed parameters of STM32L476RGT6

Name of parameter

Value of parameter

Core type and its maximum frequency

Cortex-M4F, 80 MHz

Memory capacity

1MB of Flash, 128 kB of SRAM

Analog peripherals

ADC, DAC, Comparator, Op. amplifier

Communication interfaces

CAN, I2C, LPUART, Quad SPI, SAI, SPI, U(S)ART, USB

Timing peripherals

LPTIM, RTC, TIM (16- and 32-bit), WDG

Other peripherals

CRC, LCD, RNG, Touch

Package and pinout

LQFP, 64

Operating temperature

-40 to 85°C

Supply voltage range

1.71...3.6 V

 

Second component to be selected was a CAN transceiver. This is an IC, which works as a signal translator between the CAN controller (signals on CAN TX and CAN RX lines) embedded in MCU and the physical CAN bus (differential signal between CAN High and CAN Low lines). Author decided to use SN65HVD230 CAN transceiver offered by Texas Instruments. For the CAN bus analyzer key features of SN65HVD230 were: low supply voltage (compatible with MCU), multiple protection features and high data transmission rate. More detailed information about SN65HVD230 is provided in Table 2.

Table 2. Detailed parameters of SN65HVD230 CAN transceiver

Name of parameter

Value of parameter

Maximum data transmission speed

1 Mbit/s

Protection features

Cross wire protection, loss of ground and overvoltage protection, overtemperature protection

Operating modes

High speed mode, slope control mode, low-power mode

Package and pinout

SOIC, 8

Operating temperature

40 to 85°C

Supply voltage range

3.0... 3.6V

 

Third component to be selected was power management IC. This IC is required for voltage conversion of 5 V, which comes from USB connector, to 3.3 V, which is needed to supply STM32L476RG MCU and SN65HVD230 CAN transceiver. Author decided to use device from LD1117 family of voltage regulators offered by STMicroelectronics. For the CAN bus analyzer key features of LD1117 were: version of device with fixed 3.3V output, high output current and small dimensions. A part number selected from LD1117 family is LD1117S33CTR. Detailed information about this device is provided in Table 3.

Table 3. Detailed parameters of LD1117S33CTR voltage regulator

Name of parameter

Value of parameter

Regulator type

Linear

Maximum input voltage

15 V

Output voltage

3.3 V

Dropout voltage

1 V for output current 100mA

Maximum output current

800 mA

Package

SOT-223

Operating temperature

-40 to 125°C

 

Last component to be selected was USB protection IC. This IC is required to provide ESD protection of USB data lines. Author decided to use device from USBLC6 family offered by STMicroelectronics. This device is based on TVS (Transient-Voltage-Suppression) diodes or in other words Transil. For the CAN bus analyzer key features of USBLC6 were: low capacitance between lines to GND for optimized data integrity and speed, high reliability thanks to monolithic integration and small dimensions. A part number selected from USBLC6 family is USBLC6-4. Detailed information about this device is provided in Table 4.

Table 4. Detailed parameters of USBLC6-4 ESD protection IC

Name of parameter

Value of parameter

ESD protection (peak pulse voltage)

IEC 61000-4-2 air discharge: 15 kV

IEC 61000-4-2 contact discharge: 15 kV

MIL STD883C-Method 3015-6: 25 kV

Number of channels

4

Capacitance between I/O and GND

3 pF

Package

SOT23-6L

Operating temperature

-40 to 125°C

 

Besides ICs, which were already mentioned, CAN bus analyzer consists of some additional components, which are: mechanical parts, LEDs, crystal oscillator, capacitors and resistors. Complete BOM (Bill Of Materials) is listed in Table 5.

Table 5. BOM of CAN bus analyzer

Component symbol

Name of component

Type of component

Volume of used

components

IC1

STM32L476RGT6

Microcontroller

1

IC2

LD1117S33CTR

Voltage regulator

1

IC3

SN65HVD230

CAN transceiver

1

IC4

USBLC6-4

ESD protection

1

Q1

EPSON FC-12M

32.768 kHz crystal oscillator

1

LED1

LED green

LED package 0805

1

LED2

LED red

LED package 0805

1

LED3

LED yellow

LED package 0805

1

SV1

ZX62-AB-5PA

Micro USB socket

1

SV2

1x6 male pin header

Pin header pitch 2.54

1

SV3, JP1

1x2 male pin header

Pin header pitch 2.54

2

R1, R2, R3, R4

Resistor 120R

Resistor package 0805

4

C1, C3, C7, C8, C9, C10, C13

Capacitor 100nF

Capacitor package 0805

6

C2

Capacitor 10uF

Capacitor package 0805

1

C4, C6

Capacitor 1uF

Capacitor package 0805

2

C5

Capacitor 10nF

Capacitor package 0805

1

C11, C12

Capacitor 12pF

Capacitor package 0805

2

System implementation: development tools

System development process was made based on three different, free-of-charge tools. For purpose of hardware design author used CadSoft (Autodesk) EAGLE tool. For purpose of firmware development author used two PC applications: STM32CubeMX and TrueSTUDIO, both provided by STMicroelectronics. Short description together with most important features of each of these tools is provided in this chapter.

 

EAGLE (Easy Applicable Graphical Layout Editor) is a PCB design software provided by CadSoft (Autodesk). This tool consists of three main modules: schematics editor, layout editor and autorouter. The schematics editor allows developer to create a symbolic, easy-to-read representation of the design. Layout editor allows developer to design layout of a PCB, which represents the physical reality of the design. Autorouter supports developer in PCB designing by replacing manual routing (of copper traces) with automatic routing. Author used a freeware, non-commercial edition of EAGLE, named EAGLE Light Edition (revision 7.3).

 

STM32CubeMX is a graphical tool that allows a very easy configuration of STM32 MCUs and the generation of the corresponding initialization C code through a step-by-step process. Step one consists in selecting the STM32 MCU that matches the required set of peripherals. Then in step two user must assign functions to pins, select active peripherals together with their parameters, configure clock tree. Finally in step three user launches the generation of the initialization C code based on the selected configuration. This code is ready to be used within several IDEs (Integrated Development Environment). The user code is kept at the next code generation.

 

TrueSTUDIO is an IDE for STM32 MCUs provided by Atollic, which was acquired by STMicroelectronics. TrueSTUDIO is based on Eclipse and GCC and GDB. It provides all tools necessary to carry on application development process like workspace for projects management, editor with syntax highlighter, C/C++ compiler, assembler, linker and debugger. On the top of that TrueSTUDIO embeds additional tools like serial terminal and provides advanced features for example static analysis on memory and stack usage, as well as visualizing the dynamic behavior of the running system. As a result developers, who use TrueSTUDIO benefit from professional IDE, which is in fact based on open standards. For purpose of this project author used TrueSTUDIO with revision 9.0.0.

Hardware design: PCB schematics

Complete schematics of the PCB, which was designed in Cadsoft EAGLE tool, is shown on Figure 2. As it consist of several components, the easiest way to analyze it is to divide it into functional blocks and describe each of them separately. Author identified these blocks: power supply block, clock domain block, debug block, reset block, booting block, LEDs block, USB communication block and CAN communication block.

 

Schematics description starts with introduction to power supply block. Supply voltage is provided to the PCB by USB cable. When it is attached to USB connector (SV1), it delivers voltage, which has value around 5V (positive potential is available on VB pin, while negative potential is available on GND pin). This voltage is applied to input pin (IN) of LD1117S33CTR voltage regulator (IC2), which generates 3.3V on its output pin (OUT). Voltage on both input and output pins is filtered by capacitor (C1 and C2 respectively). 3.3V is a suitable voltage level for supplying SN65HVD230 CAN transceiver (IC3). As a result 3.3V is applied to VCC pin of this device. Another device supplied from voltage regulator is STM32L476RG (IC1). 3.3V is applied to MCU’s supply pins according to this scheme:

  • VDDUSB pin (it supplies USB controller), decoupled by two capacitors (C3, C4),
  • three VDD pins (they supply embedded voltage regulator and digital part of MCU), each of them is decoupled by single capacitor (C7, C8, C9),
  • VBAT pin (it supplies MCU’s backup domain),
  • VDDA/VREF+ pin (it supplies analog peripherals of MCU and acts as reference voltage), decoupled by two capacitors (C5, C6).

Besides supply pins, care has to be taken about ground pins (VSS, VSSA/VREF-). MCU has four of such pins. All of them are connected to the same negative potential of supply voltage, which is applied to the components mentioned before: voltage regulator, CAN transceiver, ESD protection IC, decoupling capacitors and USB connector.

 

Second part of the schematics is a clock domain block, which means the external circuitry responsible for feeding the MCU with a clock signal. The key component here is a crystal oscillator (Q1), which generates 32.768 kHz clock signal. Crystal’s pins are connected directly to MCU’s clock input pins. In addition there is a load capacitor fitted between each crystal’s pin and ground plane (C11, C12). 32.768 kHz clock signal is used for trimming internal clock source (MSI – Multi-Speed Internal), which can generate signal frequency up to 48MHz and can act as system clock source, that feeds core, memory and peripherals, including USB and CAN.

 

Third part of the schematics is a debug block. In fact this block allows not only debugging, but also programming (in other words flashing) the MCU. STM32L4 MCUs support two debugging and programming interfaces: JTAG (Joint Test Action Group) and SWD (Serial Wire Debug). Historically JTAG is an older solution than SWD. As they both offer the same features, the main differentiating factor between them is number of required communication lines. For JTAG it is TCK, TDI, TDO, TMS and TRST (optional). For SWD it is only SWCLK and SWDIO. In this design SWD interface is used. Debug connector (SV2) gives access to SWDIO and SWCLK lines (MCU’s pins PA13 and PA14 respectively) as well as 3.3V, GND, NRST and SWO (PB3) lines. This connector can be used by debugging/programming probes for Cortex-M-based MCUs (like ST’s ST-Link, Segger’s J-Link or Keil’s ULINK).

 

Fourth part of the schematics is a reset block. MCU can be put into the reset state in two ways. First one is internal (software reset or reset generated by peripheral). Second one is external, by applying signal with logical low value to NRST pin. In order to ensure proper management of reset and to minimize risk of spurious resets a simple RC circuit is used. This is an internal pull-up resistor and external pull-down capacitor (C13), both connected directly to NRST pin.

 

Fifth part of the schematics is a booting block. It is responsible for selecting memory, from which MCU will execute an application. There are three available options: internal Flash, internal SRAM or system memory (factory bootloader). BOOT0 pin is a main selecting factor for MCU's booting. In this design it is connected to ground plane, which means that internal Flash is selected as a booting memory.

 

Sixth part of the schematics is a LEDs block. There are three LEDs on the PCB. One of them (LED1) is connected together with serial resistor (R2) between 3.3V and ground plane. It indicates presence of power supply. Two additional LEDs (LED2, LED3) are controlled by MCU. Each of them is connected together with serial resistor (R3 and R4 respectively) between MCU’s pin (each LED to different pin: PA1 and PA2) and ground plane. Signal with logical high value generated by MCU forces current flow through the LEDs, which leads to light emission.

 

Seventh part of the schematics is a USB communication block. USB connector (SV1) provides three lines of USB interface: USB D+, USB D- and USB ID. Usage of USB D+ and USB D- is mandatory, because they transmit data. These lines are connected to MCU’s pins, which have access to USB controller: PA11 and PA12. Usage of USB ID is optional. This line is connected to MCU’s PA10 pin. All three USB lines are connected to USBLC6-4 for ESD/transient protection.

 

Eighth and last part of the schematics is a CAN communication block. Key component here is a CAN transceiver (IC3), which converts serial CAN data to differential CAN signal and differential CAN signal to serial CAN data. Serial communication takes place between CAN transceiver and MCU via CAN RX and CAN TX lines. CAN RX line, which goes from CAN transceiver’s RXD pin to MCU’s PB8 pin (CAN data input) passes CAN messages received from the CAN bus to MCU. CAN TX line, which goes from MCU’s pin PB9 (CAN data output) to CAN transceiver’s TXD pin passes CAN messages generated by MCU to CAN transceiver (to send them in next step to the CAN bus). On the other hand communication using CAN differential signal takes place between CAN transceiver and other CAN nodes connected to the CAN bus outside the board. This differential signal is generated between CANH (CAN High) and CANL (CAN Low) transceiver pins. To simplify connecting CAN transceiver to CAN bus, CANH and CANL are available on dedicated connector (SV3). In addition there is a resistor (R1) and jumper (JP1) fitted between CANH and CANL. In case of the situation when CAN transceiver belongs to the first or the last CAN node on the CAN bus, jumper can be closed and this circuitry will act as a bus termination.

 

More detailed information related to designing of hardware based on STM32L4 MCUs (like power supply, clock domain, debug, boot and reset) can be found in Application Note AN4555 “Getting started with STM32L4 Series and STM32L4+ Series hardware development”.

Fig. 2. PCB’s schematics

Hardware design: PCB layout

Complete layout of the PCB, which was designed in EAGLE tool, is shown on Figure 3. Board is MCU-oriented, which means that MCU is in the center of the PCB and is surrounded by all components, which are present on the schematics. Such approach made signals routing relatively easy.

In the closest range to MCU passive components were located. These are MCU’s decoupling capacitors, which belong to power supply block.

 

On the left side of the PCB two functional blocks were located. First one is a LEDs block, which consists of three LEDs and three resistors. Second one is clock domain block: one crystal oscillator and two resistors.

 

On the upper side of PCB also two functional blocks were located. There is a CAN communication block, which consists of CAN transceiver, decoupling capacitor, resistor, jumper and connector. There is also debug block (connector).

 

On the right side of the PCB again two functional blocks were located. First one is USB communication block, which consists of USB connector and ESD protection IC. Second one is part of power supply block – voltage regulator with two filtering capacitors.

Fig. 3. PCB’s layout

EAGLE project (both PCB schematics and layout) are attached to this article.

2 people found this helpful

Outcomes