Microcontrollers - STM32C0 in Person Workshop - V1.8
Microcontrollers - STM32C0 in Person Workshop - V1.8
Workshop
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:
STM32F3 STM32G4
245 CoreMark 569 CoreMark
72 MHz Cortex-M4 170 MHz Cortex-M4
Mainstream Mixed-signal MCUs
9
Affordability
Vdd Vss
SO8N
Easy handling TSSOP-20
LQFP32/ 48
12
The STM32 Continuum
The STM32C0 series uses the same 90nm technology as STM32G0,
ensuring high quality standards
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
PA3 PA4
Common signals on STM32C011 and STM32G031 Legend: Common signals - STM32G031 only - STM32C011 only
Low-power modes for better efficiency
Wake-up time
Flash-RTC off-off/off-on/on-off
2.7 µs Wake-up sources:
STOP 80 µA
+ RTC, all I/Os, I²C, UART
48MHz
48-pin
2.0V
• STM32C011: 6Kbytes
• STM32C031: 12Kbytes
STM32C0 portfolio
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
Monitor
Free-of-charge production License
STM32Cube configuration tool
Pinout configuration
Software components
parameters
Peripheral configuration I²C SPI UART
21
Software tools for STM32C0
/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:
• 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
2
Create new project
• 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
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”:
31
Generate source code
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
35
Debug
36
ST-LINK Firmware Update (if needed)
37
Debug
38
Check configuration
• Click Switch
40
Run the code
42
Close the project
43
STM32C0 Lab 2: PWM
Lab: PWM timer
Objective:
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
47
Create new project
• Click “Next” 4
4
Give a name to the project
49
Lab: Timer 1 CH1 configuration
1 4
2
50
Remapping Timer 1 CH1 output to PA5
51
Timer parameters calculation
52
Clock tree configuration
TPCLK
53
TIM1 configuration – 4 steps
• Select the Pinout & Configuration
1
• In Parameters Settings of the TIM1 1
• Configure 1 Hz timer 2
54
Generate source code and add code
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.
60
EXTI - key features
• 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
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.
65
NVIC configuration
• Select NVIC under System View
66
Generate source code
• 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
69
STM32C0 Lab 4: Printf debugging using UART
Lab: printf() debugging using UART
Objective:
71
debugging settings
USART2 debug will be used via the ST-LINK Virtual-COM port
USART2 settings:
Asynchronous Mode
115200
8/N/1
No HW Flow control
Tx/Rx,
No advanced features
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
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
77
Build the project and run the application
• Build Project
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
82
Low Layer (LL) library features
83
STM32Cube FW package block view
Networking
RTOS
LwIP/NetX USB Graphics File system
FreeRTOS/ Utilities
TCP/IP & Polar Host & Device TouchGFX FATFS/FileX
SSL Azure
Middleware
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:
87
Project
• 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
90
Add code to toggle the LED
• Expand the “Drivers” and notice that now the drivers are low layer (_ll):
• 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
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
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.
• 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
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
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.