0% found this document useful (0 votes)
86 views109 pages

Microcontrollers - STM32C0 in Person Workshop - V1.8

Uploaded by

dany.developper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views109 pages

Microcontrollers - STM32C0 in Person Workshop - V1.8

Uploaded by

dany.developper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

STM32C0 MCU Series

Your next 8-bit MCU is a 32-bit – STM32C0!

Workshop

Daniel Isacson – Field Application Engineer


Mena Roumbakis – Product Marketing Engineer
Agenda

Overview of STM32C0 family

Lab 1 Blinky : Blink an LED by software

Lab 2 PWM : Use hardware (PWM timer) to blink an LED

Lab 3 EXTI : External Interrupt using a user push button

Lab 4 Printf : Printf debugging using UART communication

Lab 5 LL Driver : Example using Low Layer Driver

Optional Lab 6 ADC : ADC example using DMA

2
STM32C0 Labs: Introduction
STM32C0 Labs Introduction
• You have received by email a link to download a file containing:
• The pre-requisites (host machine, operating system…)
• The Installation Procedure (STM32CubeIDE, STM32Cube_FW_C0…)
• The source code to be added (to copy and paste code to the Labs)

• Make sure you are meeting the Pre-requisite criteria as explained in the
document

• Make sure you have installed the tools as described in the document

• Have the document open in the “Code to be added section” at the end of the
document to copy and paste source code when needed during the Labs.
STM32C0 Labs Introduction
• For these Labs you will need:

• The NUCLEO-C031C6:

• A Micro–USB Cable:

• Optional: one single jumper cable for the ADC Lab:


Introducing the STM32C0
ST’s most affordable 32-bit MCU
Streamline costs without comprising performance

Helps you reduce costs thanks to an attractive price point


Affordability and an optimized BOM

Reliability Benefits from proven STM32 quality & reliability

Consistent pinout with STM32G0


Continuity Shares same technological platform
STM32 MCUs and MPUs portfolio
STM32MP1
MPU 4158 CoreMark
Up to 800 MHz Cortex-A7
209 MHz Cortex-M4

STM32F2 STM32F4 STM32H5 STM32F7 STM32H7


High Perf Up to 398 CoreMark Up to 608 CoreMark 1023 CoreMark 1082 CoreMark Up to 3224 CoreMark
MCUs 120 MHz Cortex-M3 180 MHz Cortex-M4 250 MHz Cortex-M33 216 MHz Cortex-M7 Up to 550 MHz Cortex -M7
240 MHz Cortex -M4

STM32F3 STM32G4
245 CoreMark 569 CoreMark
72 MHz Cortex-M4 170 MHz Cortex-M4
Mainstream Mixed-signal MCUs

MCUs STM32C0 STM32F0 STM32G0 STM32F1


114 CoreMark 106 CoreMark 142 CoreMark 177 CoreMark
48MHz Cortex M0+ 48 MHz Cortex-M0 64 MHz Cortex-M0+ 72 MHz Cortex-M3

Ultra-low Power STM32L0 STM32L1 STM32L4 STM32L4+ STM32L5 STM32U5


75 CoreMark 93 CoreMark 273 CoreMark 409 CoreMark 443 CoreMark 651 CoreMark
MCUs 32 MHz Cortex-M0+ 32 MHz Cortex-M3 80 MHz Cortex-M4 120 MHz Cortex-M4 110 MHz Cortex-M33 160 MHz Cortex-M33

STM32WL STM32WB STM32WBA


Wireless 162 CoreMark 216 CoreMark 407 CoreMark
MCUs 48 MHz Cortex-M4 64 MHz Cortex-M4 100 MHz Cortex-M33
48 MHz Cortex-M0+ 32 MHz Cortex-M0+

More than 60,000 customers


Latest product generation Radio co-processor only
Over 10 billion STM32 shipped since 2007
Perfect fit for applications
typically served by 8-bit/16-bit MCUs

Smart homes Industrial devices Consumer devices

Fridges Industrial pumps Smoke detectors PC peripherals


Ovens Fan control Fire detectors & accessories
Coffee machines Circuit breakers Alarms

9
Affordability

Attractive price point Reduced BOM costs

Most cost-effective STM32 MCU • Smallest package: max I/O count

• Fewer surrounding components:


• accurate internal high-speed
clock 1% RC
Compact • only one power supply pair
9 tiny packages down to:
• 3 x 3 mm 20-pin QFN
• WLCSP12
• 8-pin SO8N
Optimized BOM cost

The STM32C0 series lets designers do more with less

Vdd Vss

• Smaller package: fewer


surrounding components
Vdd

Vss • Platform optimized with


Vdd
clock
Standard 1 power supply pair only
MCU Vss
Vdd STM32C0 • Embedded high-speed clock
Vss with high accuracy
Compact
Multiple packages

SO8N
Easy handling TSSOP-20
LQFP32/ 48

Low thickness and tiny


20-pin UFQFPN 3 x 3 mm 9
28/32/48-pin UFQFPN
packages

Lowest thickness, tiniest form factor WLCSP12


1.70 x 1.42 mm

12
The STM32 Continuum
The STM32C0 series uses the same 90nm technology as STM32G0,
ensuring high quality standards

• Arm® Cortex®-M0+ running at 48MHz


• Delivers 44DMIPS instruction throughput
with 114CoreMark performance
• Continuum with STM32G0 series
• Consistent pinout
STM32C0 STM32G0
• Same IP platform
• Same technology platform
✓ Safe in deliveries: 10-year longevity program
• Renewed commitment every year
Easy porting with STM32G0
Consistent pinout with STM32G0 leaves room for future product
upgrades

PB7/PB8/PB9/PC14-OSC32_IN PB5/PB6/PA14-BOOT0/PC15/PA15

VDD/VDDA PA13
8-pin Consistent I/O footprint
VSS/VSSA SO8N PA12[PA10]

PA0/PA1/PA2/PF2-NRST PA8/PA11[PA9]/PB0/PB1

PB7/PB8 PB3/PB4/PB5/PB6
Common pin location
PB9/PC14-OSC32_IN PA14-BOOT0/PA15 for alternate functions & system
PC15-OSC32_OUT PA13

VDD/VDDA PA12[PA10]

VSS/VSSA PA11[PA9]
20-pin
PF2-NRST PA8/PB0/PB1/PB2
TSSOP20
PA0 PA7

PA1 PA6 Maximum I/O ratio vs pin count


PA2 PA5

PA3 PA4

Common signals on STM32C011 and STM32G031 Legend: Common signals - STM32G031 only - STM32C011 only
Low-power modes for better efficiency

Excellent dynamic consumption

Wake-up time

385 µs SHUTDOWN 20 nA Wake-up sources: reset pin, few I/Os

23 µs STANDBY 8µA Wake-up sources: + BOR, IWDG

Flash-RTC off-off/off-on/on-off
2.7 µs Wake-up sources:
STOP 80 µA
+ RTC, all I/Os, I²C, UART

RUN at 48 MHz 80µA / MHz


Conditions: 25°C, VDD = 3V
Performance benchmark
STM32C0 & STM32G0

64MHz 3.6V 3.6V 125°C 125°C 100-pin

48MHz

48-pin

2.0V

1.7V -40°C -40°C 8-pin 8-pin

STM32G0 STM32C0 STM32G0 STM32C0 STM32G0 STM32C0 STM32G0 STM32C0

Frequency Voltage Temperature Package


STM32C011 / C031 Block Diagram

• 32-bit Arm Cortex-M0+ core • Timers 16-bit with Motor Control


features
• 2 to 3.6V power supply
• Communication peripherals incl.
• I/O ports maximization
• 2xUSART
• One supply pair
• Real-time Clock
• 1% internal clock
• 12-bit Ultra-fast ADC
• All clock sources
• Low speed 32kHZ • Safety features

• High speed • Excellent dynamic consumption


• Internal / External 80µA/MHz

• Direct Memory Access (DMA) • SRAM size:

• STM32C011: 6Kbytes

• STM32C031: 12Kbytes
STM32C0 portfolio

Same feature-set, different RAM size and packages


Development tools for STM32C0 series

Speed-up evaluation, prototyping and design

$10.32 $17 $11

STM32 Nucleo with C031 Discovery kit for C031 Discovery kit for C011
Prototyping QFP48 Mini evaluation board Ready to use wired sample
NUCLEO-C031C6 Full voltage range 2.0 ~ 3.6 V Daughter board QFN20/DIP20
Standalone fast STLinkV3-Minie STM32C0116-DK
STM32C0316-DK
Recommended Resell Price (RRP)
Leveraging STM32Cube software suite
Complemented with
Software Tools Embedded Software
Microsoft Azure RTOS

Configuration Packages Azure RTOS


Azure RTOS ThreadX
Real-time operating system

Development User application


Azure RTOS FileX
FAT file system, fault tolerant
Middleware

Drivers Azure RTOS NetX/NetX Duo


TCP/IP stacks
Programming

Azure RTOS USBX


Expansions USB stack, host and device

Monitor
Free-of-charge production License
STM32Cube configuration tool

Software MCU / board / example selector


Tools Code generation

Pinout configuration

Clock tree initialization

Software components
parameters
Peripheral configuration I²C SPI UART

21
Software tools for STM32C0

Complete support of Arm Cortex-M0+ architecture

IDEs STM32 Programming


STM32CubeMX
Compile and Debug & Monitoring tools
Graphical tool Simple, powerful solutions STM32CubeProg
for easy configuration STM32CubeMonitor
• Configure and generate code • Partners IDE (Arm KEIL) • Device and memory configuration
• Peripherals and middleware configuration • IDE based on Eclipse • Program the application
• RTOS aware debug • Monitor variables at run-time
Releasing your creativity

/STM32

@ST_World

community.st.com

www.st.com/STM32C0

wiki.st.com/stm32mcu

github.com/stm32-hotspot

www.st.com/stm32-mcu-developer-zone

23
STM32C0 Lab 1: Blinky
Lab: blinky

Objective:

• The objective of this lab is to generate a simple project using


STM32CubeIDE Software.

• In this example we are going to blink one of the LEDs present on the
STM32C031 Nucleo board, connected to PA5 of the STM32C0 MCU.

25
Run STM32CubeIDE

26
Create new project

• Click the Information Center icon 1

• Start new STM32 project 2

2
Create new project

• Click Access To MCU Selector 1


2

• Type: “STM32C031C6T6” in the


Part Number Search 2
3
• Then Select STM32C031C6T6
• LQFP48, 32 kB Flash 3

• Click “Next” 4

4
Give a name to the project

29
Pin configuration
• In this example we are going to use one of the LEDs present on the STM32C031
Nucleo board (connected to PA5 as seen in the schematic below)
• Search for PA5 in the search window at the bottom right
• Left-click PA5 and set it to GPIO_Output mode

From Nucleo schematics

30
Enter a User Label name for PA5
• Add a label to PA5 like “LED”
• Right click on PA5 on the Pinout View like this and click “Enter User Label”:

• Enter “LED” and press Enter:

31
Generate source code

• Project -> Generate Code or ALT + K or Save the Project

32
Change Perspective

• Click Yes

33
Step 4: toggle the LED
• In the Project Explorer Window:
• Expand the file tree and open the main.c file
• Add the following code inside the while(1) loop in
“main.c” between the “USER CODE BEGIN
WHILE” and “USER CODE END WHILE”
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(100);

Note: Code within the “USER CODE BEGIN WHILE” / “USER CODE END WHILE” section will be preserved
after regeneration by STM32CubeIDE

34
Build the project

• Project -> Build All or CTR +B

35
Debug

• Run -> Debug As -> STM32 Cortex-M C/C++ Application

36
ST-LINK Firmware Update (if needed)

37
Debug

• Run -> Debug As -> STM32 Cortex-M C/C++ Application

38
Check configuration

Note: It might ask you to upgrade the ST-LINK Driver


once you press OK, do the upgrade as requested 39
Switch the perspective

• Click Switch

40
Run the code

• Click on the Resume Icon or F8

Enjoy the LED (LD4) blinking!


41
Exit the debugger

42
Close the project

43
STM32C0 Lab 2: PWM
Lab: PWM timer

Objective:

• Now let’s use a more advanced peripheral like the Timer.

• In this lab we are going to configure a Timer in a PWM (Pulse Width


Modulation) mode to blink the LED that we previously controlled with a
GPIO.

• PA5 has an alternate Timer channel alternate function which is Timer 1


Channel 1: TIM1_CH1 that we will be using.

45
Timer - overview
ETR • Multiple timer units providing timing
CK_INT Trigger
ITR 1 Trigger / clock output resources
ITR 2 controller
ITR 3 • Internally (triggers and time-bases)
ITR 4
• Externally, for outputs or inputs:
16-bit prescaler • For waveform generation (PWM)
• For signal monitoring or measurement
Auto-reload
(frequency or timing)
+/- 16/32-bit counter

Inputs Outputs

CH1 CH1 Application benefits


• Versatile operating modes reducing CPU burden
CH2 CH2
and minimizing interfacing circuitry needs
Capture compare
… … • A single architecture for all timer instances offers
scalability and ease-of-use
CHn CHn
• Also fully featured for motor control and digital power
conversion applications
46
Lab: new project
• In STM32CubeIDE Create a New Project

47
Create new project

• Click Access To MCU Selector 1


2

• Type: “STM32C031C6T6” in the


Part Number Search 2
3
• Then Select STM32C031C6T6
• LQFP48, 32 kB Flash 3

• Click “Next” 4

4
Give a name to the project

49
Lab: Timer 1 CH1 configuration

• In this STM32CubeIDE project we are going to add Timer 1 Channel 1 to blink


LD4 (PA5) on the Nucleo board.
• In the Pinout & Configuration tab, Expand Timers Categories, then click on TIM1
peripheral and select “Internal Clock” for Source Clock and set Channel1 to “PWM
Generation CH1”.

1 4

2
50
Remapping Timer 1 CH1 output to PA5

• By default, the tool will configure Timer 1 CH1 to PA0


• We want to remap it to PA5
• NOTE: PA5 is connected to LD4 (Green LED)
• Hold “Ctrl” button and left mouse click on PA0
• Then drag the mouse pointer to PA5 and then release

51
Timer parameters calculation

• We want the Timer’s PWM output channel to be:


• T = 1 second period (1 Hz)
• D = 50% duty cycle (0.5)
• Timer input clock frequency (TPCLK) is set to 6 MHz.
• Prescaler for the Timer is set to 128. The resulting timer counter clock is:
• CK_CNT = TPCLK / Prescaler = 6 MHz / 128 = 46875 Hz
• To get T=1 Hz (or 1 sec period) the Counter Period needs to be set to: 46875
• Counter Period = CK_CNT / T = 46875 / 1 = 46875
• To get D=50% duty cycle the Pulse needs to be set to: 23437
• Pulse = Counter Period / 2 = 46875 / 2 = 23437

52
Clock tree configuration

Enter 6 for 6 MHz and press Enter

TPCLK

53
TIM1 configuration – 4 steps
• Select the Pinout & Configuration
1
• In Parameters Settings of the TIM1 1

• Configure 1 Hz timer 2

• PSC Prescaler -1 = 127 2


3

• Counter Period = 46875 3

• Set CH1 PWM


• Pulse = 23437 4

Note about Prescaler: prescaler = PSC +1 4

54
Generate source code and add code

• Generate Code by saving the project (File -> Save)


• Open the main.c, Add the following code before the while(1) loop in order to start
the PWM Timer:
Note : within “USER CODE BEGIN 2” / “USER CODE END 2” section

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

55
Build and run the code

• Build
• Enter debug session
• Run the code
• Enjoy the flashing LED (LD4)!
• LD4 is flashing using the PWM Timer

56
Exit the debugger

57
Close the project

58
STM32C0 Lab 3: EXTI
Lab: NVIC + EXTernal Interrupts

Objective:

• In this project we are going to configure the GPIO that is connected to the
user button as External Interrupt (EXTI) with rising edge trigger.

• We will also configure the Interrupt Controller: the NVIC.

60
EXTI - key features

• Wake-up from Stop mode, interrupts and events generation


• Independent interrupt and event masks

• Configurable events • Direct events


• Active edge selection • Status flag provided by related peripheral
• Dedicated pending flag • Linked to:
• Trigger-able by software • RTC, TAMP, I2C1, USARTx, LSE_CSS

• Linked to:
• GPIO

• P[x]y (where x = {A,B,C …} like as PA0, PB0, PC0… are linked to the same interrupt event EXTIy
• Groups of EXTI inputs with the same interrupt vector: (EXTI0_1, EXTI2_3, EXTI4_15)

61
EXTI - block diagram
AHB Lite Control and status registers

16
GPIO port A Configurable
16 events
GPIO port B 16 exti[15:0]
GPIO port C
16 EXTI Peripherals
16
GPIO port D mux
4
GPIO port F sys_wakeup

c_wakeup PWR
Configurable events
Peripherals without
Event
interrupt flags Event
Masking Cortex®-M0+
trigger
EVG Event input
Direct events
Peripherals with
interrupt flags

EXTI NVIC

EVG: EVent Generator

62
NVIC - overview

• The NVIC (Nested vector Interrupt Controller) is integrated in the Cortex®-M0+ CPU:
• 32 maskable interrupt channels
• 4 programmable priority levels
• Low-latency exception and interrupt handling
• Power management control
Application benefits
• Supports prioritization levels with
dynamic control
• Fast response to interrupt requests
• Relocatable vector table

63
Lab: pinout configuration
• In STM32CubeIDE create a new project “STM32C0_EXTI”.
• Add configuration of the IO that is connected to the User Button (connected to
PC13) and to toggle the LED LD4 (connected to PA5) on the STM32C0 Nucleo
board.
• Left-click on PC13 and set it to GPIO_EXTI13 mode.
• PA5 to be configured as GPIO Output push-pull.

From Nucleo schematics 64


GPIO configuration
• Select GPIO under System View 1

• Click on Pin Name PC13 2

• Make sure GPIO mode is “External


Interrupt Mode with Rising edge
trigger detection” 3

65
NVIC configuration
• Select NVIC under System View

• Enable “EXTI line 4 to 15 interrupts” (by checking the box) 1

66
Generate source code

• Open main.c, add the following code:


• within “USER CODE BEGIN PV” / “USER CODE END PV” section

• Generate Code

uint8_t PC13_flag = 0;

67
Add EXTI rising edge callback function
• Also, in main.c add the following code,
• within “USER CODE BEGIN 4” / “USER CODE END 4” section
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
PC13_flag++;
if ( ( PC13_flag & 0x01 ) == 0x01 )
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}
}

68
Build and run the code

• Click the “Build” button

• Push the Blue “USER” button to toggle the LED LD4!

69
STM32C0 Lab 4: Printf debugging using UART
Lab: printf() debugging using UART

Objective:

• Redirect “printf “ output to USART2 which is connected to the ST-LINK


Virtual COM port on the Nucleo board

• Using the Terminal in the STM32CubeIDE to view the printf output.

71
debugging settings
USART2 debug will be used via the ST-LINK Virtual-COM port

Set up additional GPIO / Clocks:


PA2 – USART2, “USART2-TX”
PA3 – USART2, “USART2-RX”
USART2 Clock = PCLK1 (48 MHz)

USART2 settings:
Asynchronous Mode
115200
8/N/1
No HW Flow control
Tx/Rx,
No advanced features

We will use the terminal in STM32CubeIDE.

USART2 is routed to the ST-LINK’s USART, and


brought via the USB Virtual-COM port class
(SB27/32 located on the back on the board have
been soldered)
72
72
Create a new project and GPIO configuration additions
• Click on USART2 dialog (under Connectivity), and select Asynchronous mode:

• Make sure PA2 & PA3 are selected for Tx / Rx pins:

73
Clock configuration
• Run the STM32C0 at 48 MHz for this lab, the USART2 clock also at 48 MHz.

74
USART2 configuration
• Click on the Configuration tab and select USART2

• Parameter Settings tab


• 115200 Bits/s
• 8-bit word length
• No parity bit
• 1 Stop bit
• Keep Default settings for the rest

75
Generate code and adding printf redirecting code in
main.C
1- Add following code in the section below:
/* USER CODE BEGIN PFP */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* USER CODE END PFP */
2- Add following function in the section below:
/* USER CODE BEGIN 4 */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END 4 */
76
Adding application code in main.C

Add application code in main loop:

/* USER CODE BEGIN WHILE */


while (1)
{
printf("** Hello World ** \n\r");
HAL_Delay(1000);
/* USER CODE END WHILE */

77
Build the project and run the application

• Build Project

• Enter Debug session

• Click “Resume” button to run the code

78
Configure the Terminal inside STM32CubeIDE
• Open a Command Shell Console

• In Select Remote Connection, select Serial Port for Connection Type, ISO for Encoding and then
Press the “New…” icon below:

79
Configure the Terminal inside STM32CubeIDE
• In the New serial Port connection, give a name to the connection, select the COM port of your ST-LINK
(COM91 for me) and use the following settings, then clock Finish:

• You should see the printf message being displayed in the Console Tab.

80
STM32C0 : LL Drivers
Overview

• STM32Cube HAL & LL are complementary and covers a wide range


of applications requirements

• HAL offers high level and functionalities-oriented APIs, with high


portability level and hide product/IPs complexity to end user

• LL offers low level APIs at registers level, w/ better optimization in


code size and code execution but LL are less portability and require
deep knowledge of the product specification

82
Low Layer (LL) library features

• The Low Layer (LL) Library offers the following services:


• Set of static inline functions for direct register access (provided in *.h
files only)
• One-shot operations that can be used by the HAL drivers or from
application level.
• Independent from HAL and can be used standalone (without HAL
drivers)
• Full feature coverage of the supported peripherals

83
STM32Cube FW package block view

Evaluation boards Discovery boards Nucleo boards


Board Demonstrations

Middleware level examples

Networking
RTOS
LwIP/NetX USB Graphics File system
FreeRTOS/ Utilities
TCP/IP & Polar Host & Device TouchGFX FATFS/FileX
SSL Azure
Middleware

HAL level examples Low Layer Examples HAL + LL Examples


CMSIS

Hardware Abstraction Layer API Boards Support Packages

Drivers Low Layer Drivers

STM3C0 STM32F0 STM32F1 STM32F2 STM32F3 STM32F7

STM32G0 STM32L0 STM32L1 STM32L4 STM32F4 STM32H7

STM32 Series

84
Benchmark- USART transmit example

• The below data are based on the “USART Transmitter IT” example:
• Configure GPIO & USART peripheral for sending characters to HyperTerminal (PC) in
Asynchronous mode using IT
HAL Drivers Low layer
• Using below configuration: Drivers
read-only code memory ROM Size
• Platform: STM32L486xx divided by ~4
(Bytes) 7206 2154
• Compiler : IAR
read-only data memory
• Optimization : High Size (Bytes) 204 94
• Heap Size = 512 Bytes RAM Size
read write data memory
• Stack Size = 512 Bytes reduction
(Bytes) (*) 1408 1093

• LL offer smaller footprint & high performance but less portability & require expertise
• HAL offer high level API (hide complexity) & portability but higher footprint & less performance
(*) to add Heap and Stack size for total RAM

85
STM32C0 Lab 5: Using the low layer (LL) drivers
Lab: Using the low layer drivers

Objective:

• Generate a project with STM32CubeIDE using the Low Layer


Drivers and check how much improvement we get compared to a
HAL project in term of Flash and RAM usage

87
Project

• In STM32CubeIDE create a project you can call “STM32C0_LL”

• Set GPIO PA5 as Output Push-Pull like we did for the “STM32C0_Blinky” lab

88
Configuration
• In Project Manager Tab
• In the Advanced Settings Tab
• Instead of HAL drivers select LL (Low Level) for RCC, GPIO and CORTEX_M0+

89
Generate source code

• Generate Code (ALT + K)

90
Add code to toggle the LED
• Expand the “Drivers” and notice that now the drivers are low layer (_ll):

• Expand the file tree and open the main.c file


• Add the following code inside the while(1) loop
• Add within “USER CODE BEGIN WHILE” / “USER CODE END WHILE” section (this will preserve
your code after code regeneration)
LL_GPIO_TogglePin(GPIOA, LL_GPIO_PIN_5);
// Delay 100 ms
LL_mDelay(100);
91
Build the project

• Click the “Build” button

• Click the “Start/Stop Debug Session” button

• Click “Run” button

• The Green LED (LD4) should be blinking just like the blinky example with the HAL
drivers

92
Let’s compare the differences between HAL and LL
projects
• In the console window of the “STM32C0_Blinky” project that is using HAL drivers,
after building we see:

93
Let’s compare the map files between HAL and LL
projects
• In the console window of the “STM32C0_Blinky_LL” project that is using LL
drivers, after building we see:

94
Comparison table between HAL and LL for our “blinky”
code
• Using below configuration:
• Heap Size = 512 Bytes / Stack Size = 512 Bytes
• STM32Cube C0 1.0.0
• CubeIDE 1.10.1

HAL Drivers Low layer


Drivers ROM Size with
read-only code memory 20%
(Bytes) 4440 3608 improvment

read write data memory RAM Size


(Bytes) 20 12 reduction
divided ~ by 2

95
STM32C0 Lab 6: ADC + DMA + TIM
Lab: ADC+DMA+TIM

Objective:

• The objective of this lab is to learn about the ADC (Analog to Digital
Converter) with the DMA (Direct Memory Access) and TIM (Timer) of the
STM32C0.

• We will convert a signal on PA12 which is ADC1 Channel 12 and store the
converted value in a buffer in RAM thanks to DMA and Timer.

97
Block Diagram

STM32C0

buffer
PA12
(ADC1 channel12)
Signal DMA
ADC1 8bit data

Trigger
1Hz

TIM3
Example description

• ADC1 is measuring 8bit data on PA12 (ADC1_IN12 channel) each 1 second


triggered by TIM3 (working as timebase) on its update event
• Data from ADC1 are transferred by DMA (DMA1_Channel2) to the buffer
• DMA1_Channel2 is configured in Circular mode using bytes (both sides)

99
TIM3 configuration
Goal: Configure TIM3 as upcounter using an internal
clock which would overflow each 1 second with TRGO
signal genaration on this event.

• Select TIM3 within Timers group


• In Mode part:
• Clock Source = Internal Clock
• In Configuration part:
• Prescaler = 11999
• Counter Period = 999
• Trigger Event Selection TRGO = Update Event
Goal: Configure ADC1 to convert signal on ADC1_IN12 line (PA12)
on each trigger coming from TIM3 TRGO line, using 12.5 cycles ADC1 configuration
sample time, 8bit resolution and transferring data over DMA.

• Select ADC1 within Analog group


• Activate IN12: ADC1 Channel 12.
ADC1 – DMA configuration
Goal: Add DMA support on ADC1 transfer complete
events. Transfer using bytes using circular buffer

• Select DMA Settings tab within ADC


Configuration
• Click on Add button and select ADC1 as
DMA request
• Set the following DMA Settings:
• Mode = Circular
• Increment address = on Memory side
• Data width = Byte on both sides
Goal: Configure ADC1 to convert signal on ADC1_IN12 line (PA12)
on each trigger coming from TIM3 TRGO line, using 12.5 cycles ADC1 configuration
sample time, 8bit resolution and transferring data over DMA.

• In Configuration part select Parameter Settings tab and


change:
• Resolution = 8 bits
• DMA Continuous Requests = Enabled
• SamplingTime Common1= 12.5 Cycles
• Ext Trigger Conv Source = Timer 3 Trigger Out event (TRGO)
• Ext Trigger Conversion Edge = rising edge
• Rank1, Sampling time = Sampling time common 1
Clock Configuration
default settings

• Default configuration
on HSI 12MHz
• No need to change
• System clock 12MHz
common for ADC1,
TIM3
Project generation
STM32CubeIDE
• Select „Project Manager” section
• Specify project location and
project name if not yet done
• Generate the project by one of
the following actions:
• Pressing icon
• Saving project by „Ctrl+S”
• Select Project -> Generate project
• Click on Alt+K
• After project generation open
main.c file

105
Coding
main.c file

• Data buffer declaration


/* USER CODE BEGIN PV */
uint8_t buffer[8];

• All peripherals start

/* USER CODE BEGIN 2 */


HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)buffer, 8);
HAL_TIM_Base_Start(&htim3);

106
PA12 location on your Nucleo board

107
Run the application
• Compile the code (i.e using „hammer”
icon)
• Start debug session (i.e using „bug”
icon)
• Add buffer table into „live expressions”
• Run the application
• As a result you should see each second
new data coming to buffer table within
live expressions if you change PA12
input signal

In case it is not working, please have a


look whether DMA is initialized BEFORE
any other peripheral which is using it.

108
© STMicroelectronics - All rights reserved.
ST logo is a trademark or a registered trademark of STMicroelectronics International NV or its affiliates in the EU and/or other countries.
For additional information about ST trademarks, please refer to www.st.com/trademarks.
All other product or service names are the property of their respective owners.

You might also like