8/17/2019 Stm32f4 Labs
1/429
STM32F4 Labs
T.O.M.A.S – Technically Oriented Microcontroller Application ServicesV1.07
8/17/2019 Stm32f4 Labs
2/429
CONTENT 1/3
1. GPIO lab
2. EXTI lab
3. SLEEP lab
4. STOP lab
5. STANDBY lab
6. DMA Poll lab
7. DMA Interrupt lab
8. RTC Alarm lab
9. UART Poll lab
10. UART Interrupt lab
11. UART DMA lab
2
20/10/2015STM32F42xx Technical Training
8/17/2019 Stm32f4 Labs
3/429
CONTENT 2/3
12. SPI Poll lab
13. SPI Interrupt lab
14. SPI DMA lab
15. TIM Interrupt lab
16. TIM PWM out lab
17. TIM DMA lab
18. TIM Counter lab
19. DAC wave generation lab
20. ADC Poll lab
21. ADC Interrupt lab
22. ADC DMA lab
3
20/10/2015STM32F42xx Technical Training
8/17/2019 Stm32f4 Labs
4/429
CONTENT 3/3
23. WWDG lab
24. IWDG lab
25. FMC SDRAM BSP lab
26. LCD BSP Print text lab
27. I2C BSP EEPROM lab
28. SPI BSP GYROSCOPE lab
4
20/10/2015STM32F42xx Technical Training
8/17/2019 Stm32f4 Labs
5/429
GPIO Lab 1
20/10/2015STM32F42xx Technical Training
8/17/2019 Stm32f4 Labs
6/429
Configure GPIO for LED toggling
• Objective• Learn how to setup pin and GPIO port in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Goal• Configure GPIO pin in CubeMX and Generate Code
• Add in to project HAL_Delay function and HAL_GPIO_Toggle function
• Verify the correct functionality on toggling LED
6
1
8/17/2019 Stm32f4 Labs
7/429
Configure GPIO for LED toggling
• Create project in CubeMX
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• Configure LED pin as GPIO_Output
7
1
8/17/2019 Stm32f4 Labs
8/429
Configure GPIO for LED toggling
• For debug purpose is recommended to select debug pinsSWD or JTAG
• Select can be done in TAB>Pinout>SYS
• On discovery is available only SWD option• If SWD/JTAG is not selected and the Set all free pins as analog
(MENU>Project>Settings>TAB>Code Generator) is selected, debug is not possible
8
1
8/17/2019 Stm32f4 Labs
9/429
Configure GPIO for LED toggling
• Clock Configuration
• TAB>Clock Configuration
9
1
We can easilysetup STM32
clocks
8/17/2019 Stm32f4 Labs
10/429
Configure GPIO for LED toggling
• The Clock configuration tree is interactive version of tree from RM
10
1
RM0090 Chapter 6Reset and clock
controlPage 151
8/17/2019 Stm32f4 Labs
11/429
Configure GPIO for LED toggling
Clock Configuration overview 1
• Clock sources
• Internal oscillators
11
1
Internaloscillators
8/17/2019 Stm32f4 Labs
12/429
Configure GPIO for LED toggling
Clock Configuration overview 3
• Clock sources
• Internal oscillators
• External clock sources
12
1
External clocksources
8/17/2019 Stm32f4 Labs
13/429
Configure GPIO for LED toggling
Clock Configuration overview 4
• Clock sources
• Internal oscillators
• LSI
• External clock sources
13
1
1. Low speed internaloscillator
8/17/2019 Stm32f4 Labs
14/429
Configure GPIO for LED toggling
Clock Configuration overview 5
• Clock sources
• Internal oscillators
• LSI
• HSI
• External clock sources
14
1
1. Low speed internaloscillator
2. High speed internaloscillator
8/17/2019 Stm32f4 Labs
15/429
Configure GPIO for LED toggling
Clock Configuration overview 6
• Clock sources
• Internal oscillators
• LSI
• HSI
• External clock sources
• LSE
15
1
1. Low speed internaloscillator
2. High speed internaloscillator
3. Low speed externalclocks
External 32,768Hzcrystal or externalsignal in bypass
mode
8/17/2019 Stm32f4 Labs
16/429
Configure GPIO for LED toggling
Clock Configuration overview 7
• Clock sources
• Internal oscillators
• LSI
• HSI
• External clock sources
• LSE – crystal or external signal in bypass mode
• HSE
16
1
1. Low speed internaloscillator
2. High speed internaloscillator
3. Low speed externalclocks
4. High speed externalclocks
External crystal
4-26MHz orexternal signal 1-50Mhz
in bypass mode
8/17/2019 Stm32f4 Labs
17/429
Configure GPIO for LED toggling
Clock Configuration overview 8
• Clock sources
• Internal oscillators
• LSI
• HSI
• External clock sources
• LSE – crystal or external signal in bypass mode
• HSE – crystal 4-26MHz or external signal 1-50MHzin bypass mode
17
1
1. Low speed internaloscillator
2. High speed internaloscillator
Disabled clocksHow to enable?
3. Low speed externalclocks
4. High speed externalclocks
8/17/2019 Stm32f4 Labs
18/429
Configure GPIO for LED toggling
Clock Configuration overview 9
• Clock sources
• Internal oscillators
• LSI
• HSI
• External clock sources
• LSE – crystal or external signal in bypass mode
• HSE – crystal 4-26MHz or external signal 1-50MHzin bypass mode
18
1
1. Low speed internaloscillator
2. High speed internaloscillator
Disabled clocksHow to enable?
3. Low speed externalclocks
4. High speed externalclocks
8/17/2019 Stm32f4 Labs
19/429
Configure GPIO for LED toggling
Clock Configuration overview 10
• External clock enabling
• TAB>Pinout
• Select HSE and LSE clocks
• Bypass or crystal
19
1
1. Set HSE crystal
2. CubeMX reserve pins
8/17/2019 Stm32f4 Labs
20/429
Configure GPIO for LED toggling
Clock Configuration overview 11
• The HSE is now available
• TAB>Clock configuration
• Click on Blue square andchange frequency on 8MHz
20
1
8/17/2019 Stm32f4 Labs
21/429
Configure GPIO for LED toggling
Clock Configuration overview 12
Clock tree for core description
• System multiplexer
• PLL
• PLL source multiplexer
21
1Frequency of
Core, AHBbridge, andmemories
Multiplexer selectclock source for
system
PLL multipliesfrequency
Clock sourcefor PLL
8/17/2019 Stm32f4 Labs
22/429
Configure GPIO for LED toggling
Clock Configuration overview 13
• Core clocked from HSI, default option after reset
22
1
HSI crystalHSI as system
source
System frequency
16MHz
AHB prescaler
divide by 1
Core frequencyis now 16MHz
8/17/2019 Stm32f4 Labs
23/429
Configure GPIO for LED toggling
Clock Configuration overview 14
• Core clocked from HSE
23
1
HSE crystalor external
signal
HSE as systemsource
System frequency
8MHz
AHB prescaler
divide by 1
Core frequencyis now 8MHz
8/17/2019 Stm32f4 Labs
24/429
Configure GPIO for LED toggling
Clock Configuration overview 15
• Core clocked from PLL and HSI
24
1PLL as system
source
System frequency
180MHz
AHB prescaler
divide by 1
Core frequencyis now 180MHz
HSI crystal
PLL out P dividerby 2PLL multiplier Nby 360
PLL in M dividerby 16
PLL clock sourceHSI
8/17/2019 Stm32f4 Labs
25/429
Configure GPIO for LED toggling
Clock Configuration overview 16
• Core clocked from PLL and HSE
25
1PLL as system
source
System frequency
180MHz
AHB prescaler
divide by 1
Core frequencyis now 180MHz
PLL out P dividerby 2PLL multiplier Nby 360
PLL in M dividerby 8
PLL clock sourceHSE
HSE crystalor external
signal
8/17/2019 Stm32f4 Labs
26/429
Configure GPIO for LED toggling
Clock Configuration overview 17
• AHB, APB prescalers and peripheral speed
26
1
Core clocks
Core, AHB, memoryDMA clock
8/17/2019 Stm32f4 Labs
27/429
Configure GPIO for LED toggling
Clock Configuration overview 18
• AHB, APB prescalers and peripheral speed
27
1
Core clocks
Systick clockSystick divider
8/17/2019 Stm32f4 Labs
28/429
Configure GPIO for LED toggling
Clock Configuration overview 19
• APB prescalers and peripheral speed
28
1
Core clocks
External memorycontroller clock
8/17/2019 Stm32f4 Labs
29/429
Configure GPIO for LED toggling
Clock Configuration overview 20
• AHB, APB prescalers and peripheral speed
29
1
Core clocks
APB1 prescaler APB1 peripheralsclock
APB1 timers clockAPB1 timers multiplier
8/17/2019 Stm32f4 Labs
30/429
Configure GPIO for LED toggling
Clock Configuration overview 21
• AHB, APB prescalers and peripheral speed
30
1
Core clocks
APB2 prescaler
APB2 peripheralsclock
APB2 timers clockAPB2 timers multiplier
8/17/2019 Stm32f4 Labs
31/429
Configure GPIO for LED toggling
Clock Configurationoverview 22
• Data sheet
Figure 4
31
1
APB2 bus clock
APB1 bus clock
AHB, Core, memory
clock
8/17/2019 Stm32f4 Labs
32/429
Configure GPIO for LED toggling
Clock Configurationoverview 23
32
1
APB2 bus clock
APB1 bus clockAHB, Core, memoryclock
8/17/2019 Stm32f4 Labs
33/429
Configure GPIO for LED toggling 33
1
APB2 bus clock APB1 bus clock
Clock Configurationoverview 23
Core clock
AHB clock
External memory
clock
DMA clock
8/17/2019 Stm32f4 Labs
34/429
Configure GPIO for LED toggling
Clock Configuration overview 24
• Enable clocks which are gray
• How to enable gray features?
34
1
MCO output?
RTC and IWDG
clock tree?
USB FS tree?
8/17/2019 Stm32f4 Labs
35/429
Configure GPIO for LED toggling
Clock Configuration overview 25
• MCO1 output
• TAB>Pinout
• RCC>MCO1 checkbox• TAB>Clock Configuration
• Now the MCO1 output can be set
35
1 1. DisabledMCO
2. MCO1 enable
3. CubeMX assign pin
4. Set MCO1
8/17/2019 Stm32f4 Labs
36/429
Configure GPIO for LED toggling
Clock Configuration overview 26
• RTC
• TAB>Pinout
• RCC>RTC set RTC feature• TAB>Clock Configuration
• Now the RTC can be set
36
1 1. RTC disabled
2. RTC set
3. Set RTC
8/17/2019 Stm32f4 Labs
37/429
Configure GPIO for LED toggling
Clock Configuration overview 26
• USB
• TAB>Pinout
• RCC>USB_OTG_FSset feature
• TAB>Clock Configuration
• Now the USB clock can be set
371
3. CubeMX assign pins
1. USB disabled
4. USB clock can be set now
2. USB enable
8/17/2019 Stm32f4 Labs
38/429
Configure GPIO for LED toggling
• GPIO Configuration
• TAB>Configuration>System>GPIO
381
8/17/2019 Stm32f4 Labs
39/429
8/17/2019 Stm32f4 Labs
40/429
Configure GPIO for LED toggling
• GPIO(Pin) output speed configuration
• Change the rising and falling edge when pin change state from high to low or low to high
• Higher GPIO speed increase EMI noise from STM32 and increase STM32 consumption
• It is good to adapt GPIO speed with periphery speed. Ex.: Toggling GPIO on 1Hz is LOW
optimal settings, but SPI on 45MHz the HIGH must be set
401
LOW
HIGH
LOW
HIGH
LOW
HIGH
LOW
HIGH
GPIO output LOW speed GPIO output MEDIUM speed
GPIO output HIGH speedGPIO output FAST speed
8/17/2019 Stm32f4 Labs
41/429
Configure GPIO for LED toggling
• Now we set the project details for generation
• Menu > Project > Project Settings
• Set the project name
• Project location
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
411
8/17/2019 Stm32f4 Labs
42/429
8/17/2019 Stm32f4 Labs
43/429
Configure GPIO for LED toggling
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Into infinite loop while(1){ }
• For toggling we need to use this functions
• HAL_HAL_Delay which create specific delay
• HAL_GPIO_WritePin or HAL_GPIO_TogglePin
431
/* USER CODE BEGIN 3 *//* Infinite loop */
while (1)
{
HA!G"IO!#$ite"in(G"IOG% G"IO!"IN!1&% G"IO!"IN!SE')
HA!Del(+,,)
HA!G"IO!#$ite"in(G"IOG% G"IO!"IN!1&% G"IO!"IN!RESE')
HA!Del(+,,)
-
/* USER CODE END 3 */
8/17/2019 Stm32f4 Labs
44/429
EXTI lab 2
20/10/2015STM32F42xx Technical Training
2
8/17/2019 Stm32f4 Labs
45/429
Configure EXTI which turns on LED
• Objective• Learn how to setup input pin with EXTI in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Goal• Configure GPIO and EXTI pin in CubeMX and Generate Code
• Add into project Callback function and function which turn on led
• Verify the correct functionality by pressing button which turns on LED
452
2
8/17/2019 Stm32f4 Labs
46/429
Configure EXTI which turns on LED
• Create project in CubeMX
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• Configure LED pin as GPIO_Output• Configure Button pin as GPIO_EXTIX
462
2
8/17/2019 Stm32f4 Labs
47/429
Configure EXTI which turns on LED
• Create project in CubeMX
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• Configure LED pin as GPIO_Output• Configure Button pin as GPIO_EXTIX
472
C fi EXTI hi h LED2
8/17/2019 Stm32f4 Labs
48/429
Configure EXTI which turns on LED
• In order to run on maximum frequency, setup clock system
• Details in lab 0
482
C fi EXTI hi h LED2
8/17/2019 Stm32f4 Labs
49/429
Configure EXTI which turns on LED
• GPIO Configuration
• TAB>Configuration>System>GPIO
492
C fi EXTI hi h t LED2
8/17/2019 Stm32f4 Labs
50/429
Configure EXTI which turns on LED
• GPIO(Pin) Configuration
• Select External Interrupt Mode with Rising edge trigger detection
• No pull-up or pull-down
• PG14 can be let in default
settings
• Button OK
502
C fi EXTI hi h t LED2
8/17/2019 Stm32f4 Labs
51/429
Configure EXTI which turns on LED
• NVIC Configuration
• We need to enable interrupts for EXTI
• TAB>Configuration>System>NVIC
512
C fi EXTI hi h t LED2
8/17/2019 Stm32f4 Labs
52/429
Configure EXTI which turns on LED
• NVIC Configuration
• Enable interrupt forEXTI Line0
• Button OK
522
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
53/429
Configure EXTI which turns on LED
• Now we set the project details for generation
• Menu > Project > Project Settings
• Set the project name
• Project location
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
532
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
54/429
Configure EXTI which turns on LED
HAL Library work flow 1
542
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
Generated byCubeMX
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
55/429
Configure EXTI which turns on LED
HAL Library work flow 2
552
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
MX_GPIO_Initinside main.c
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
56/429
Configure EXTI which turns on LED
HAL Library working flow 3
562
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
insidestm32f4xx_it.c
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
57/429
Configure EXTI which turns on LED
HAL Library work flow 4
572
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
User must define Callbackit is declared by default as
empty weak
Configure EXTI which turns on LED2
8/17/2019 Stm32f4 Labs
58/429
Configure EXTI which turns on LED
HAL Library work flow 5
582
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
Usually in main.c between/* USER CODE BEGIN */
tags
Configure EXTI which turns on LED 592
8/17/2019 Stm32f4 Labs
59/429
Configure EXTI which turns on LED
HAL Library work flow summary
592
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
2. init GPIO
1. init NVIC
3. createedge
4. EXTI interrupthandler
5. HAL EXTIinterrupt handler
6. HAL EXTIcallback
HAL files clearing
flags, check errors,…
Configure EXTI which turns on LED 602
8/17/2019 Stm32f4 Labs
60/429
Configure EXTI which turns on LED
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags
• We create function which will handle the EXTI interrupts
• The HAL callback function for EXTI
• void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
• For LED turn on we need to use this functions
• HAL_GPIO_WritePin
602
Configure EXTI which turns on LED 612
8/17/2019 Stm32f4 Labs
61/429
Configure EXTI which turns on LED
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags
• We create function which will handle the EXTI interrupts
• The HAL callback function for EXTI
• void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
• For LED turn on we need to use this functions
• HAL_GPIO_WritePin
612
/* USER CODE BEGIN & */
.oi HA!G"IO!E0'I!Cll2(4int15!t G"IO!"in)
{
if(G"IO!"in 66 G"IO!"IN!,) {
HA!G"IO!#$ite"in(G"IOG% G"IO!"IN!1&% G"IO!"IN!SE')- el7e {
!!NO"()
-
-
/* USER CODE END & */
8/17/2019 Stm32f4 Labs
62/429
Low Power mode SLEEP lab 3
20/10/2015STM32F42xx Technical Training
Use SLEEP mode with EXTI 633
8/17/2019 Stm32f4 Labs
63/429
Use SLEEP mode with EXTI
• Objective• We use the EXTI setup from lab 1
• Learn how to setup SLEEP in HAL
• Create simple project with SLEEP mode with wake up on pin press
• Goal• Use project from EXTI lab
• Learn how to setup the SLEEP in HAL, which events can wake up you• Verify the correct functionality by measuring consumption
633
SLEEP Mode
8/17/2019 Stm32f4 Labs
64/429
SLEEP Mode
DMA
CoreCM4
Powerregulator
RAM
Clock
USART
GP timers
DAC
USB
SPI
ADC
I2C
GPIO’s
RTC/backup reg.
IWDG
Reset
• Core is stopped
• Peripherals are running
HSE
HSI
LSE
LSI
FLASH
Use SLEEP mode with EXTI 653
8/17/2019 Stm32f4 Labs
65/429
Use SLEEP mode with EXTI
HAL Library work flow summary
3
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
2. init GPIO
1. init NVIC
4. createedge
6. HAL EXTIinterrupt handler
7. HAL EXTIcallback
HAL files clearingflags, check errors,
…
Enter into SLEEP mode(WFI)3. Use WFI
5. EXTI interrupthandler
Use SLEEP mode with EXTI 663
8/17/2019 Stm32f4 Labs
66/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Function to enter SLEEP• HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
• We can measure consumption
3
To be able to reprogramthe STM32 which is in
LP mode, useconnection during
reset option
Use SLEEP mode with EXTI 673
8/17/2019 Stm32f4 Labs
67/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Function to enter SLEEP
• HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
• We can measure consumption
3
/* USER CODE BEGIN 3 */
/* Infinite loop */while (1)
{
HA!Del(1,,,)
HA!"#R!Ente$SEE"8oe("#R!O#"O#ERREGUA'OR!ON%"#R!SEE"EN'R9!#:I)-
/* USER CODE END 3 */
Use SLEEP mode with EXTI 683
8/17/2019 Stm32f4 Labs
68/429
• Consumption still to high?
• Is STM32 really in SLEEP?
• Is the Systick disabled?
• Is this better?
3
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HA!Del(1,,,)HA!S47pen'i2()
HA!"#R!Ente$SEE"8oe("#R!O#"O#ERREGUA'OR!ON%"#R!SEE"EN'R9!#:I)
HA!Re74;e'i2()
-
/* USER CODE END 3 */
8/17/2019 Stm32f4 Labs
69/429
Low Power mode STOP lab 4
20/10/2015STM32F42xx Technical Training
Use STOP mode with EXTI 704
8/17/2019 Stm32f4 Labs
70/429
• Objective• We use the EXTI setup from lab 1
• Learn how to setup STOP in HAL
• Create simple project with STOP mode with wake up on pin press
• Goal• Use project from EXTI lab
• Learn how to setup the STOP in HAL, which events can wake up you• Verify the correct functionality by measuring consumption
STOP Mode
8/17/2019 Stm32f4 Labs
71/429
Clock
DMA
CoreCM4
Powerregulator
FLASH
RAM
USART
GP timers
DAC
USB
SPI
ADC
I2C
GPIO’s
RTC/backup reg.
IWDG
Reset
HSE
HSI
LSE
LSI
• Core is stopped
• HSE, MSI clocks are OFF
• SRAM and registers
content is preserved
• Peripherals with HSI, LSI,LSE clock option can beON
• GPIO’s keep their setup
Use STOP mode with EXTI 724
8/17/2019 Stm32f4 Labs
72/429
HAL Library work flow summary
Configure the GPIO to generate interrupt onrising or falling edge
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler
Edge detection callbackHAL_GPIO_EXTI_Callback
EXTI0_IRQHandler
2. init GPIO
1. init NVIC
4. createedge
6. HAL EXTIinterrupt handler
7. HAL EXTIcallback
HAL files clearingflags, check errors,
…
Enter into STOP mode(WFI) 3. Use WFI
5. EXTI interrupthandler
Clock reconfigure
8. Reconfigureclock afterwakeup
Use STOP mode with EXTI 734
8/17/2019 Stm32f4 Labs
73/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Function to enter SLEEP
• HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• We can measure consumption
To be able to reprogramthe STM32 which is in
LP mode, useconnection during
reset option
Use STOP mode with EXTI 744
8/17/2019 Stm32f4 Labs
74/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Function to enter SLEEP
• HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• We can measure consumption
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HA!Del(1,,,)
HA!"#R!Ente$S'O"8oe("#R!O#"O#ERREGUA'OR!ON%"#R!S'O"EN'R9!#:I)
S7te;Clo2!Confi
8/17/2019 Stm32f4 Labs
75/429
• Or different function
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HA!Del(1,,,)HA!"#RE=!Ente$Une$D$i.eS'O"8oe("#R!O#"O#ERREGUA'OR!UNDERDRI>E!ON%"#R!S'O"EN'R9!#:I)
S7te;Clo2!Confi
8/17/2019 Stm32f4 Labs
76/429
Low Power mode STANDBY lab 5
20/10/2015STM32F42xx Technical Training
Use STANDBY mode 775
8/17/2019 Stm32f4 Labs
77/429
• Objective• For this lab create empty CubeMX project
• Learn how to setup STANDBY in HAL
• Create simple project with STANDBY mode with wake up on pin press
• Goal• Learn how to setup the STANDBY in HAL, which events can wake up you
• Verify the correct functionality by measuring consumption
STANDBY Mode
8/17/2019 Stm32f4 Labs
78/429
Clock
DMA
CoreCM4
Powerregulator
FLASH
RAM
USART
GP timers
DAC
USB
SPI
ADC
I2C
GPIO’s
RTC/backup reg.
IWDG
Reset
HSE
HSI
LSE
LSI
• Core and all peripheralsare OFF, except RTC andIWDG if enabled
• HSE, MSI, HSI clocks areOFF, LSI LSE can be ON
• SRAM and registerscontent is lost, except
RTC, and standby circuitry• GPIO’s are in high Z,
except Reset, RTC OUTand WKUP 1,2,3
Reset RTC OUT WKUP 1,2,3
Use STANDBY mode 795
8/17/2019 Stm32f4 Labs
79/429
HAL Library work flow summary
Enable Wake-up pin
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations2. Standby
1. init NVIC
4. create
edgeEnter into STANDBY mode(WFI) 3. Use WFI
Reset5. Wake up
STM with reset
Use STANDBY mode 805
8/17/2019 Stm32f4 Labs
80/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• For Wake up we need to setup wake up pin
• HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
• Function to enter STANDBY
• HAL_PWR_EnterSTANDBYMode();
• We can measure consumption
To be able to reprogramthe STM32 which is in
LP mode, useconnection during
reset option
Use STANDBY mode 815
8/17/2019 Stm32f4 Labs
81/429
• Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Function to enter SLEEP
• HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
• We can measure consumption
/* USER CODE BEGIN 3 */
/* Infinite loop */while (1)
{
HA!G"IO!'o
8/17/2019 Stm32f4 Labs
82/429
• We cannot go into STANDBY again?
• Try to clear wake up flag
• __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* USER CODE BEGIN ? */
!!HA!"#R!CEAR!:AG("#R!:AG!#U)
/* USER CODE END ? */
8/17/2019 Stm32f4 Labs
83/429
Data transfer over DMA lab 6
20/10/2015STM32F42xx Technical Training
Use DMA in M2M transfer 846
8/17/2019 Stm32f4 Labs
84/429
• Objective• Learn how to setup DMA transfer in CubeMX
• Create simple DMA memory to memory transfer from RAM to RAM
• Goal• Use CubeMX and Generate Code with DMA
• Learn how to setup the DMA in HAL
• Verify the correct functionality by comparing transferred buffers
8/17/2019 Stm32f4 Labs
85/429
Use DMA in M2M transfer 866
8/17/2019 Stm32f4 Labs
86/429
• Create project in CubeMX
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• For DMA we don’t need to configure any pins
Use DMA in M2M transfer 876
8/17/2019 Stm32f4 Labs
87/429
• In order to run on maximum frequency, setup clock system
• Details in lab 0
Use DMA in M2M transfer 886
8/17/2019 Stm32f4 Labs
88/429
• DMA configuration
• TAB>Configuration
• System>DMA
• TAB>DMA2
• Button ADD
1. TAB >Configuration
2. System DMA
3. TAB>DMA 2
4. Add DMAchannel
Use DMA in M2M transfer 896DMA fi ti
8/17/2019 Stm32f4 Labs
89/429
• DMA configuration
• Select MEMTOMEMDMA request
• Normal mode
• Increment source anddestination address
• FIFO setup
• Byte data width
• Burst size
• Button OK
1. MEMTOMEM
2. Normalmode
3. Incrementaddresses
4. FIFO setup
5. Data width and
Burst
6. OK
Use DMA in M2M transfer
N t th j t d t il f ti
906
8/17/2019 Stm32f4 Labs
90/429
• Now we set the project details for generation
• Menu > Project > Project Settings
• Set the project name
• Project location
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
Use DMA in M2M transfer
Start process DMA (same for TIM ADC)
916
8/17/2019 Stm32f4 Labs
91/429
• Start process DMA (same for TIM, ADC)
• Non blocking start process
• The end of the process must be checked by polling
PeripheralInitializations
Start Process(HAL_DMA_Start)
Poll for process complete(HAL_DMA_PollForTransfer)
1. init DMA
2. Start DMA
3. DMA transfer data
4. Check if transfer is complete
Use DMA in M2M transfer
• Return values
926
8/17/2019 Stm32f4 Labs
92/429
• Return values
• Most of CubeMX functions have return values, which indicate, if operation was successful,timeout occurs of function end with error
• Is recommended handle this return values to be sure that program working as expected
Ex: Poll for process complete(HAL_DMA_PollForTransfer)
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY
DMA transfer was successfullyfinished and data was
transferred to destinationwithout error
Use DMA in M2M transfer
• Return values
936
8/17/2019 Stm32f4 Labs
93/429
• Return values
• Most of CubeMX functions have return values, which indicate, if operation was successful,timeout occurs of function end with error
• Is recommended handle this return values to be sure that program working as expected
Ex: Poll for process complete(HAL_DMA_PollForTransfer)
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY
Error occurs during DMAtransfer you use
HAL_DMA_GetError fordetails what happened
Use DMA in M2M transfer
• Return values
946
8/17/2019 Stm32f4 Labs
94/429
• Return values
• Most of CubeMX functions have return values, which indicate, if operation was successful,timeout occurs of function end with error
• Is recommended handle this return values to be sure that program working as expected
Ex: Poll for process complete(HAL_DMA_PollForTransfer)
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY
DMA transfer in progress,user can only abort the
transfer
Use DMA in M2M transfer
• Now we open the project in our IDE
956
8/17/2019 Stm32f4 Labs
95/429
Now we open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 2 */ and /* USER CODE END 2 */ tags
• HAL functions for DMA
• HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress,uint32_t DataLength)
• HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_tTimeout)
Use DMA in M2M transfer
• We create two buffers
966
8/17/2019 Stm32f4 Labs
96/429
We create two buffers
• One with source data
• Second as destination buffer
/* USER CODE BEGIN , */4int!t B4ffe$!S$26{,%1%?%3%&%+%5%%%-
4int!t B4ffe$!De7t1,
/* USER CODE END , */
Use DMA in M2M transfer
• HAL DMA Start start the M2M data transfer
976
8/17/2019 Stm32f4 Labs
97/429
HAL_DMA_Start start the M2M data transfer
• HAL_DMA_PollForTransfer check if the transfer ends successfully
/* USER CODE BEGIN ? */
HA!D8A!St$t(Fh;!;e;to;e;!;?!7t$e;,% (4int3?!t) (B4ffe$!S$2)% (4int3?!t) (B4ffe$!De7t)% 1,)
while(HA!D8A!"oll:o$'$n7fe$(Fh;!;e;to;e;!;?!7t$e;,% HA!D8A!:U!'RANS:ER% 1,,) 6 HA!O@)
{
!!NO"()
-/* USER CODE END ? */
8/17/2019 Stm32f4 Labs
98/429
Data transfer over DMA with interrupt
lab 7
20/10/2015STM32F42xx Technical Training
Use DMA M2M transfer with interrupt
• Objective
997
8/17/2019 Stm32f4 Labs
99/429
Objective• Learn how to setup DMA transfer with interrupt in CubeMX
• Create simple DMA memory to memory transfer from RAM to RAM
• Goal• Use CubeMX and Generate Code with DMA
• Learn how to setup the DMA in HAL
• Verify the correct functionality by comparing transferred buffers
Use DMA M2M transfer with interrupt
• Create project in CubeMX
1007
8/17/2019 Stm32f4 Labs
100/429
p j
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• For DMA we don’t need to configure any pins
Use DMA M2M transfer with interrupt
• In order to run on maximum frequency, setup clock system
1017
8/17/2019 Stm32f4 Labs
101/429
• Details in lab 0
Use DMA M2M transfer with interrupt 1027• DMA configuration
8/17/2019 Stm32f4 Labs
102/429
• TAB>Configuration
• System>DMA
• TAB>DMA2
• Button ADD
1. TAB >Configuration
2. System DMA
3. TAB>DMA 2
4. Add DMAchannel
Use DMA M2M transfer with interrupt 1037• DMA configuration
1 MEMTOMEM
8/17/2019 Stm32f4 Labs
103/429
• Select MEMTOMEMDMA request
• Normal mode
• Increment source and
destination address
• FIFO setup
• Byte data width
• Burst size
• Button OK
1. MEMTOMEM
2. Normalmode
3. Incrementaddresses
4. FIFO setup
5. Data width andBurst
6. OK
Use DMA M2M transfer with interrupt 1047• DMA configuration 1. TAB >
8/17/2019 Stm32f4 Labs
104/429
• System > NVIC
• Enable DMA2 Stream interrupt
• Button OK
Configuration
3. Enable DMA2interrupts
2. System > NVIC
4. OK
Use DMA M2M transfer with interrupt
• Now we set the project details for generation
1057
8/17/2019 Stm32f4 Labs
105/429
• Menu > Project > Project Settings
• Set the project name
• Project location
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1067
8/17/2019 Stm32f4 Labs
106/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1077
8/17/2019 Stm32f4 Labs
107/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Generated by CubeMX
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1087
8/17/2019 Stm32f4 Labs
108/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Defined by user
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1097
8/17/2019 Stm32f4 Labs
109/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Generated in main.c
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1107
8/17/2019 Stm32f4 Labs
110/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
We recommend to use itin main.c
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1117
8/17/2019 Stm32f4 Labs
111/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
User defined functions. The
user must define functions byhimself and put function namesinto DMA structure
8/17/2019 Stm32f4 Labs
112/429
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1137
8/17/2019 Stm32f4 Labs
113/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Defined instm32f4xx_hal_dma.c
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1147
8/17/2019 Stm32f4 Labs
114/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Start DMA buffer transferNot blocking function
program can continue
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1157
Interrupt indicate DMA
8/17/2019 Stm32f4 Labs
115/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Interrupt indicate DMAprocess is half/complete
or error was detected
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1167
8/17/2019 Stm32f4 Labs
116/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Process interruptinformation
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1177
8/17/2019 Stm32f4 Labs
117/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Data correctly transferredComplete callback
function
Use DMA M2M transfer with interrupt
HAL Library DMA with IT flow
1187
DMA I i i li i
8/17/2019 Stm32f4 Labs
118/429
Start process with interrupt generation at endof process
HAL_DMA_Start_IT
DMA Initializationsincluding peripheral interrupt NVIC
initializations
HAL_DMA_IRQHandler
end of process callbackDMA_XferCpltCallback
process Error callbackDMA_XferErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY DMA2_Stream0_IRQHandler
Error was detectedError callback function
8/17/2019 Stm32f4 Labs
119/429
Use DMA M2M transfer with interrupt
• Now we open the project in our IDE• The functions we want to put into main.c
1207
8/17/2019 Stm32f4 Labs
120/429
• Between /* USER CODE BEGIN 2 */ and /* USER CODE END 2 */ tags
• DMA callback function
• We need to add the name of callback function into DMA structure
• HAL functions for DMA
• HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress,uint32_t DataLength)
Use DMA M2M transfer with interrupt
• We create two buffers• One with source data
S d d i i b ff
1217
8/17/2019 Stm32f4 Labs
121/429
• Second as destination buffer
/* USER CODE BEGIN , */
4int!t B4ffe$!S$26{,%1%?%3%&%+%5%%%-
4int!t B4ffe$!De7t1,
/* USER CODE END , */
Use DMA M2M transfer with interrupt
• DMA callback creation function prototype
1227
/* USER CODE BEGIN , */
i t t B ff S {, 1 ? 3 & + 5 -
8/17/2019 Stm32f4 Labs
122/429
• DMA complete callback with nop where we can put breakpoint/* USER CODE BEGIN & */
.oi 0fe$CpltCll2(D8A!Hnle'peDef *h;)
{
!!NO"()//we $e2h thi7 onl if D8A t$n7fe$ w7 2o$$e2t
-/* USER CODE END & */
4int!t B4ffe$!S$26{,%1%?%3%&%+%5%%%-
4int!t B4ffe$!De7t1,
.oi 0fe$CpltCll2(D8A!Hnle'peDef *h;)
/* USER CODE END , */
Use DMA M2M transfer with interrupt
• DMA Start• Before we start the DMA with interrupt we need to set the callback into DMA structure
Then is possible use the HA D8A St$t I' to begin DMA transfer
1237
8/17/2019 Stm32f4 Labs
123/429
• Then is possible use the HA!D8A!St$t!I' to begin DMA transfer
/* USER CODE BEGIN ? */
h;!;e;to;e;!;?!7t$e;,0fe$CpltCll26F0fe$CpltCll2HA!D8A!St$t!I'(Fh;!;e;to;e;!;?!7t$e;,%(4int3?!t)B4ffe$!S$2%(4int3?!t)B4ffe$!De7t%1,)
/* USER CODE END ? */
8/17/2019 Stm32f4 Labs
124/429
Use RTC Alarm lab 8
20/10/2015STM32F42xx Technical Training
Use RTC and Alarm with interrupt
• Objective• Learn how to setup RTC with interrupt in CubeMX
1258
8/17/2019 Stm32f4 Labs
125/429
• Create simple RTC project with periodic alarm interrupt
• Goal• Use CubeMX and Generate Code with RTC
• Learn how to setup the RTC in HAL
• Verify the correct functionality by periodic RTC alarm interrupts
Use RTC and Alarm with interrupt
• Create project in CubeMX• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
1268
8/17/2019 Stm32f4 Labs
126/429
Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• Set Internal Alarm on Alarm A or Alarm B
• Set GPIO to toggle with LED as alarm indication
Use RTC and Alarm with interrupt
• In order to run on maximum frequency, setup clock system
• Details in lab 0
1278
8/17/2019 Stm32f4 Labs
127/429
Use RTC and Alarm with interrupt
• RTC Configuration• TAB>Configuration
• Control > RTC
1288
1. TAB > Configuration
8/17/2019 Stm32f4 Labs
128/429
• Set parameters which you want
2. RTC
3. Check configuration
Use RTC and Alarm with interrupt
• RTC Configuration NVIC• TAB>NVIC Setup
• Enable Alarm interrupt
12981. TAB > NVIC
8/17/2019 Stm32f4 Labs
129/429
p
• Button OK
2. Enable RTC Alarm
3. Button OK
Use RTC and Alarm with interrupt
• Now we set the project details for generation• Menu > Project > Project Settings
• Set the project name
1308
8/17/2019 Stm32f4 Labs
130/429
• Project location
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
Use RTC and Alarm with interrupt
• The RTC can be preserved during RESET(ok LP modes)• CubeMX not enable the RTC by default
• We need to add HAL_PWR_EnableBkUpAccess() and __HAL_RCC_RTC_ENABLE() before
1318
8/17/2019 Stm32f4 Labs
131/429
we call MX_RTC_Init()
• Set the first alarm to 1s
• In MX_RTC_Init
• We create the RTC interrupt handler and we reconfigure the Alarm A time
• HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
• HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm,uint32_t Format)
• HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_tFormat)
• RTC alarm indication will be done by LED
• HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
Use RTC and Alarm with interrupt
• RTC enable
1328
/* Initilie ll 2onfi
8/17/2019 Stm32f4 Labs
132/429
• In MX_RTC_Init we set first Alarm to 1s
!!HA!RCC!R'C!ENABE()//Enle R'C not 2$ete 24e e247e the R'C 2n $4n
80!G"IO!Init()
80!R'C!Init()
/**Enle the Al$; A
*/
7Al$;Al$;'i;eHo4$7 6 ,7Al$;Al$;'i;e8in4te7 6 ,
7Al$;Al$;'i;eSe2on7 6 1
7Al$;Al$;'i;eS4Se2on7 6 ,
Use RTC and Alarm with interrupt
• RTC enable
1338
/* USER CODE BEGIN & */
.oi HA!R'C!Al$;AE.entCll2(R'C!Hnle'peDef *h$t2){
R'C Al$;'peDef 7Al$;
8/17/2019 Stm32f4 Labs
133/429
R'C!Al$;'peDef 7Al$;
HA!R'C!GetAl$;(h$t2%F7Al$;%R'C!AAR8!A%:OR8A'!BIN)
if(7Al$;Al$;'i;eSe2on7J+){
7Al$;Al$;'i;eSe2on76,-el7e{
7Al$;Al$;'i;eSe2on767Al$;Al$;'i;eSe2on7K1
-
while(HA!R'C!SetAl$;!I'(h$t2% F7Al$;% :OR8A'!BIN)6HA!O@){-
HA!G"IO!'o
8/17/2019 Stm32f4 Labs
134/429
• We only need to modify the Alarm mask to ignore Days, Hours and Minutes
/**Enle the Al$; A
*/
7Al$;Al$;'i;eHo4$7 6 ,7Al$;Al$;'i;e8in4te7 6 ,
7Al$;Al$;'i;eSe2on7 6 1
7Al$;Al$;'i;eS4Se2on7 6 ,
7Al$;Al$;'i;e'i;e:o$;t 6 R'C!HOUR:OR8A'1?!A8
7Al$;Al$;'i;eDiING!NONE
7Al$;Al$;'i;eSto$eOpe$tion 6 R'C!S'OREO"ERA'ION!RESE'sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS|RTC_ALARMMASK_MINUTES;7Al$;Al$;S4Se2on87 6 R'C!AAR8SUBSECOND8AS@!A
7Al$;Al$;Dte#eeDSel 6 R'C!AAR8DA'E#EE@DA9SE!DA'E
7Al$;Al$;Dte#eeD 6 1
7Al$;Al$; 6 R'C!AAR8!A
HA!R'C!SetAl$;!I'(Fh$t2% F7Al$;% :OR8A'!BCD)
8/17/2019 Stm32f4 Labs
135/429
UART Poll lab 9
20/10/2015STM32F42xx Technical Training
Simple UART communication
• Objective• Learn how to setup UART in CubeMX
• How to Generate Code in CubeMX and use HAL functions
1369
8/17/2019 Stm32f4 Labs
136/429
How to Generate Code in CubeMX and use HAL functions
• Work in pairs, one will create transmitter and second receiver
• Goal• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART without interrupts
• Verify the correct functionality
Simple UART communication
• Create project in CubeMX• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
1379
8/17/2019 Stm32f4 Labs
137/429
• Pin selection
• We are looking for free pins where is possible to create wire loopback connection
Simple UART communication
• Pin selection• We are looking for free pins
where is possible to createwire loopback connection
1389
8/17/2019 Stm32f4 Labs
138/429
Image fromSTM32F429-Discovery
user manual
Simple UART communication
• Pin selection• We are looking for free pins where is possible to create wire loopback connection
1399
8/17/2019 Stm32f4 Labs
139/429
Image fromSTM32F429
datasheet
Simple UART communication
• Hardware preparation• We connect selected pins together by jumper, this help us to create loopback on UART
1409
8/17/2019 Stm32f4 Labs
140/429
Hardware
connectionRX RX
Simple UART communication
• Create project in CubeMX• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
C b MX UART l i
1419
8/17/2019 Stm32f4 Labs
141/429
• CubeMX UART selection
• Select USART1 in asynchronous mode• Select PA9 and PA10 for USART1 if weren't selected
Simple UART communication
• In order to run on maximum frequency, setup clock system• Details in lab 0
1429
8/17/2019 Stm32f4 Labs
142/429
Simple UART communication
• CubeMX UART configuration• Tab>Configuration>Connectivity>USART1
1439
8/17/2019 Stm32f4 Labs
143/429
Simple UART communication
• CubeMX USART configuration check:• BaudRate
• World length
• Parity
1449
8/17/2019 Stm32f4 Labs
144/429
y
• Stop bits
• Data direction
• Oversampling
Simple UART communication
• CubeMX USART GPIO configuration check:• On high baud rate set the
GPIO speed to HIGH
1459
8/17/2019 Stm32f4 Labs
145/429
• TAB>Configuration>System>
>GPIO• Set the HIGH output speed
Button OK
Simple UART communication
• Now we set the project details for generation• Menu > Project > Project Settings
• Set the project name
• Project location
1469
8/17/2019 Stm32f4 Labs
146/429
• Type of toolchain
• Now we can Generate Code
• Menu > Project > Generate Code
Simple UART communication
HAL Library init flow
1479
Peripheral InitializationsCubeMX UART init start in
8/17/2019 Stm32f4 Labs
147/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();
main.c file
Simple UART communication
HAL Library init flow
1489
Peripheral Initializations
1. We need initUART1
8/17/2019 Stm32f4 Labs
148/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();
Simple UART communication
HAL Library init flow
1499
Peripheral Initializations
1. We need initUART1
8/17/2019 Stm32f4 Labs
149/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();CubeMX create for usfunction which handle
UART initialization
Simple UART communication
HAL Library init flow
1509
Peripheral Initializations
1. We need initUART1
2 C ll UART1 i it
8/17/2019 Stm32f4 Labs
150/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
Simple UART communication
HAL Library init flow
1519
Peripheral Initializations
1. We need initUART1
2 Call UART1 init
8/17/2019 Stm32f4 Labs
151/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
CubeMX fill the UARTstructure with parameters
which we choose in
Configuration window
Simple UART communication
HAL Library init flow
1529
Peripheral Initializations
1. We need initUART1
2 Call UART1 init
8/17/2019 Stm32f4 Labs
152/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
3. Store UART1 configurationinto structure
Simple UART communication
HAL Library init flow
1539
Peripheral Initializations
1. We need initUART1
2 Call UART1 init
8/17/2019 Stm32f4 Labs
153/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
3. Store UART1 configurationinto structure
Function wroteparameters from structureinto UART1 registers
Simple UART communication
HAL Library init flow
1549
Peripheral Initializations
1. We need initUART1
2 Call UART1 init
8/17/2019 Stm32f4 Labs
154/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
3. Store UART1 configurationinto structure
4. Write to UART1 registers
Optional callback fromHAL_UART_Init function,be default empty weak
function
Simple UART communication
HAL Library init flow
1559
Peripheral Initializations
1. We need initUART1
2. Call UART1 init
8/17/2019 Stm32f4 Labs
155/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();2. Call UART1 init
function
3. Store UART1 configurationinto structure
4. Write to UART1 registers
CubeMX configure hereUART1 GPIOs and
enable UART1 clock
system
Simple UART communication
HAL Library init flow
1569
Peripheral Initializations
1. We need initUART1
2. Call UART1 init
8/17/2019 Stm32f4 Labs
156/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();function
3. Store UART1 configurationinto structure
4. Write to UART1 registers
CubeMX configure here
UART1 GPIOs andenable UART1 clock
system and NVIC
5. UART1 init callback
Simple UART communication
HAL Library init flow
1579
Peripheral Initializations
1. We need initUART1
2. Call UART1 init
8/17/2019 Stm32f4 Labs
157/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();function
3. Store UART1 configurationinto structure
4. Write to UART1 registers
5. UART1 init callback
6. UART1 GPIOS, NVIC and RCC init
Simple UART communication
HAL Library init flow
1589
Peripheral Initializations
MX USART1 UART I it()
1. We need initUART1
2. Call UART1 init
8/17/2019 Stm32f4 Labs
158/429
Init GPIO and NVIC for UART
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Init UART1 structure
MX_USART1_UART_Init();function
3. Store UART1 configurationinto structure
4. Write to UART1 registers
5. UART1 init callback
6. UART1 GPIOS, NVIC and RCC init
7. Next periph init or user code
Simple UART communication
HAL Library transmit flow
1599
Generated by
Peripheral Initializations
8/17/2019 Stm32f4 Labs
159/429
CubeMX
HAL_TIMEOUTHAL_TIMEOUT HAL_ERRORHAL_ERRORHAL_OKHAL_OK
Function blocksPolling with timeout
Function blocksPolling with timeout
Polling processHAL_UART_Transmit
HAL_BUSYHAL_BUSY
Simple UART communication
HAL Library transmit flow
1609
Created by user
Peripheral Initializations
8/17/2019 Stm32f4 Labs
160/429
HAL_TIMEOUTHAL_TIMEOUT HAL_ERRORHAL_ERRORHAL_OKHAL_OK
Function blocksPolling with timeout
Function blocksPolling with timeout
Polling processHAL_UART_Transmit
HAL_BUSYHAL_BUSY
Simple UART communication
HAL Library receive flow
1619
Generated by
Peripheral Initializations
8/17/2019 Stm32f4 Labs
161/429
CubeMX
HAL_TIMEOUTHAL_TIMEOUT HAL_ERRORHAL_ERRORHAL_OKHAL_OK
Function blocksPolling with timeout
Function blocksPolling with timeout
Polling processHAL_UART_Receive
HAL_BUSYHAL_BUSY
Simple UART communication
HAL Library receive flow
1629
Created by user
Peripheral Initializations
8/17/2019 Stm32f4 Labs
162/429
HAL_TIMEOUTHAL_TIMEOUT HAL_ERRORHAL_ERRORHAL_OKHAL_OK
Function blocksPolling with timeout
Function blocksPolling with timeout
Polling processHAL_UART_Receive
HAL_BUSYHAL_BUSY
Simple UART communication
• Open the project in our IDE• The functions we want to put into main.c
• Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
• Into infinite while function
1639
8/17/2019 Stm32f4 Labs
163/429
• For transmit use function• HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
• For receive use function• HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
Simple UART communication
• Transmit solution• Create data structure for data
1649
/* USER CODE BEGIN , */
4int!t t6{,%1%?%3%&%+%5%%%-
8/17/2019 Stm32f4 Labs
164/429
• Call transmit function from while loop
/* USER CODE BEGIN 3 *//* Infinite loop */
while (1)
{
HA!UAR'!'$n7;it(Fh4$t1%t%1,%1,,,)
-/* USER CODE END 3 */
/* USER CODE END , */
8/17/2019 Stm32f4 Labs
165/429
8/17/2019 Stm32f4 Labs
166/429
UART Interrupt lab 10
20/10/2015STM32F42xx Technical Training
Use UART with interrupt
• Objective• Learn how to setup UART with interrupts in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple loopback example with interrupts
16710
8/17/2019 Stm32f4 Labs
167/429
• Goal• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART with interrupts
• Verify the correct functionality
8/17/2019 Stm32f4 Labs
168/429
Use UART with interrupt
• Hardware preparation
• We connect selected pins together by jumper, this help us to create loopback on UART
16910
8/17/2019 Stm32f4 Labs
169/429
Hardwareloopback
Use UART with interrupt
• In order to run on maximum frequency, setup clock system
• Details in lab 0
17010
8/17/2019 Stm32f4 Labs
170/429
Use UART with interrupt
• CubeMX UART configuration
• Tab>Configuration>Connectivity>USART1
17110
8/17/2019 Stm32f4 Labs
171/429
Use UART with interrupt
• CubeMX UART configuration check:
• BaudRate
• World length
• Parity
• Stop bits
D t di ti
17210
8/17/2019 Stm32f4 Labs
172/429
• Data direction• Oversampling
Use UART with interrupt
• CubeMX USART configuration NVIC settings
• TAB>NVIC Settings
• Enable interrupts
• OK
17310
8/17/2019 Stm32f4 Labs
173/429
8/17/2019 Stm32f4 Labs
174/429
8/17/2019 Stm32f4 Labs
175/429
Use UART with interrupt
HAL Library UART with IT transmit flow
176
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
176/429
Start process with interrupt generation at endof processHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT transmit flow
177
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Generated by CubeMX
10
8/17/2019 Stm32f4 Labs
177/429
Start process with interrupt generation at endof processHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
178
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
178/429
p p gof processHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Defined by user
Use UART with interrupt
HAL Library UART with IT receive flow
179
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Generated in main.c andstm32f4xx_hal_msp.c
10
8/17/2019 Stm32f4 Labs
179/429
of processHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
180
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
180/429
pHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
We recommend to use itin main.c
Use UART with interrupt
HAL Library UART with IT receive flow
181
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
181/429
pHAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Defined as __weak you canfind name of this functions in
stm32f4xx_hal_uart.c
Use UART with interrupt
HAL Library UART with IT receive flow
182
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
182/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Generated instm32f4xx_it.c
Use UART with interrupt
HAL Library UART with IT receive flow
183
Start process with interrupt generation at endof process
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
183/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Defined instm32f4xx_hal_uart.c
Use UART with interrupt
HAL Library UART with IT receive flow
184
Start process with interrupt generation at endof process
HAL UART T it IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Send buffer over UARTNot blocking functionprogram can continue
10
8/17/2019 Stm32f4 Labs
184/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
185
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Interrupt indicate the dataregister is empty we cansend more data or error
was detected
10
8/17/2019 Stm32f4 Labs
185/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
186
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Process interruptinformation
10
8/17/2019 Stm32f4 Labs
186/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
187
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
Send more data if is buffernot empty
10
8/17/2019 Stm32f4 Labs
187/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Use UART with interrupt
HAL Library UART with IT receive flow
188
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
188/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callbackHAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
If data buffer is empty useComplete callback
function
Use UART with interrupt
HAL Library UART with IT receive flow
189
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
10
8/17/2019 Stm32f4 Labs
189/429
HAL_UART_Transmit_IT
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callback
HAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
Or if error was detecteduse Error callback
function
Use UART with interrupt
HAL Library UART with IT receive flow
190
Start process with interrupt generation at endof process
HAL UART Transmit IT
Peripheral Initializationsincluding peripheral interrupt NVIC
initializations
1. UART1 init
2. UART1 send
buffer
10
8/17/2019 Stm32f4 Labs
190/429
_ _ _
HAL_UART_IRQHandler
end of process callbackHAL_UART_TxCpltCallback
process Error callback
HAL_UART_ErrorCallback
HAL_ERRORHAL_ERRORHAL_OKHAL_OK HAL_BUSYHAL_BUSY USART1_IRQHandler
3. UART1 Txregister empty
4. Send more
data or manageerror5. Buffer sent
5. Transmit error
Use UART with interrupt
• Open the project in our IDE
• The functions we want to put into main.c
• Between /* USER CODE BEGIN 2 */ and /* USER CODE END 2 */ tags
• For transmit use function• HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
• For receive use function
19110
8/17/2019 Stm32f4 Labs
191/429
• For receive use function• HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
Use UART with interrupt
• Buffer definition
192
/* USER CODE BEGIN , */
4int!t t=!4ff6{,%1%?%3%&%+%5%%%-
4int!t $=!4ff1,
/* USER CODE END , */
10
8/17/2019 Stm32f4 Labs
192/429
• Sending and receiving methods
/* USER CODE BEGIN ? */
HA!UAR'!Re2ei.e!I'(Fh4$t1%$=!4ff%1,)HA!UAR'!'$n7;it!I'(Fh4$t1%t=!4ff%1,)
/* USER CODE END ? */
Use UART with interrupt
• Complete callback check
• We can put brakepoints on NOPs to watch if we send or receive complete buffer
193
/* USER CODE BEGIN & */
.oi HA!UAR'!R=CpltCll2(UAR'!Hnle'peDef *h4$t)
{NO"()//te7t if we $e2h thi7 po7ition
10
8/17/2019 Stm32f4 Labs
193/429
!!NO"()//te7t if we $e2h thi7 po7ition
-
.oi HA!UAR'!'=CpltCll2(UAR'!Hnle'peDef *h4$t)
{
!!NO"()//te7t if we $e2h thi7 po7ition
-
/* USER CODE END & */
Use UART with interrupt
• !!!!!!!! F4 cube 1.3.0 error in TX and RX. If we use loopback mode with
interrupt will not work correctly
194
/* USER CODE BEGIN & */
.oi HA!UAR'!R=CpltCll2(UAR'!Hnle'peDef *h4$t){
NO"() //t t if h thi iti
10
8/17/2019 Stm32f4 Labs
194/429
!!NO"()//te7t if we $e2h thi7 po7ition
-
.oi HA!UAR'!'=CpltCll2(UAR'!Hnle'peDef *h4$t){
!!NO"()//te7t if we $e2h thi7 po7ition
-
/* USER CODE END & */
8/17/2019 Stm32f4 Labs
195/429
UART DMA lab 11
20/10/2015STM32F42xx Technical Training
Use UART with DMA transfer
• Objective• Learn how to setup UART with DMA in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple loopback example with DMA
• GoalConfigure UART in CubeMX and Generate Code
19611
8/17/2019 Stm32f4 Labs
196/429
• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART with DMA
• Verify the correct functionality
Use UART with DMA transfer
• Create project in CubeMX
• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• Pin selection
• It will be same as previous lab we use again PA9 and PA10
19711
8/17/2019 Stm32f4 Labs
197/429
Use UART with DMA transfer
• Hardware preparation
• We connect selected pins together by jumper, this help us to create loopback on UART
19811
8/17/2019 Stm32f4 Labs
198/429
Hardwareloopback
Use UART with DMA transfer
• In order to run on maximum frequency, setup clock system
• Details in lab 0
19911
8/17/2019 Stm32f4 Labs
199/429
Use UART with DMA transfer
• CubeMX UART configuration
• Tab>Configuration>Connectivity>USART1
20011
8/17/2019 Stm32f4 Labs
200/429
Use UART with DMA transfer
• CubeMX USART configuration check:
• BaudRate
• World length
• Parity
• Stop bits
• Data direction
• Oversampling
20111
8/17/2019 Stm32f4 Labs
201/429
Use UART with DMA transfer
• CubeMX USART configuration DMA settings
• TAB>DMA Settings
• Button ADD
20211
8/17/2019 Stm32f4 Labs
202/429
Use UART with DMA transfer
• CubeMX USART configuration DMA Tx settings
• Set USART1_TX request
• Memory to peripheral direction
• Normal mode
• Byte data width
• Increment memory address
20311
8/17/2019 Stm32f4 Labs
203/429
Use UART with DMA transfer
• CubeMX USART configuration DMA Rx settings
• Button ADD
• Set USART1_RX request
• Peripheral to memory direction
• Normal mode
• Byte data width
• Increment memory address
20411
8/17/2019 Stm32f4 Labs
204/429
Use UART with DMA transfer• CubeMX USART configuration NVIC settings
• TAB>NVIC Settings• Enable DMA2 interrupts for
USART1
• Button OK
20511
8/17/2019 Stm32f4 Labs
205/429
Use UART with DMA transfer
• Now we set the project details for generation
• Menu > Project > Project Settings
• Set the project name
• Project location
• Type of toolchain
• Now we can Generate Code• Menu > Project > Generate Code
20611
8/17/2019 Stm32f4 Labs
206/429
Use UART with DMA transfer
HAL Library UART with DMA RX flow
207
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
11
8/17/2019 Stm32f4 Labs
207/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
Use UART with DMA transfer
HAL Library UART with DMA RX flow
208
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
Generated by CubeMX
11
8/17/2019 Stm32f4 Labs
208/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
8/17/2019 Stm32f4 Labs
209/429
Use UART with DMA transfer
HAL Library UART with DMA RX flow
210
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
Generated in main.c andstm32f4xx_hal_msp.c
11
8/17/2019 Stm32f4 Labs
210/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
8/17/2019 Stm32f4 Labs
211/429
Use UART with DMA transfer
HAL Library UART with DMA RX flow
212
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL OK
HAL OK HAL ERROR
HAL ERROR HAL ERROR
HAL ERROR
11
8/17/2019 Stm32f4 Labs
212/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
Generated instm32f4xx_it.c
Use UART with DMA transfer
HAL Library UART with DMA RX flow
213
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL OK
HAL OK HAL ERROR
HAL ERROR HAL ERROR
HAL ERROR
11
8/17/2019 Stm32f4 Labs
213/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
Defined instm32f4xx_hal_dma.c and
then linked tostm32f4xx_hal_uart.c
Use UART with DMA transfer
HAL Library UART with DMA RX flow
214
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL OK
HAL OK HAL ERROR
HAL ERROR HAL ERROR
HAL ERROR
Receive buffer over UARTwith DMA
Not blocking functionprogram can continue
11
8/17/2019 Stm32f4 Labs
214/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
Use UART with DMA transfer
HAL Library UART with DMA RX flow
215
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL OK
HAL OK HAL ERROR
HAL ERROR HAL ERROR
HAL ERROR
Interrupt indicate the DMAreceive half/complete
buffer or DMA error wasdetected
11
8/17/2019 Stm32f4 Labs
215/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR HAL_ERRORHAL_ERROR
8/17/2019 Stm32f4 Labs
216/429
Use UART with DMA transfer
HAL Library UART with DMA RX flow
217
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL OK
HAL OK HAL ERROR
HAL ERROR HAL ERROR
HAL ERROR
11
8/17/2019 Stm32f4 Labs
217/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
HAL_OKHAL_OK HAL_ERRORHAL_ERROR __
If data buffer is full HALuse Complete callback
function
Use UART with DMA transfer
HAL Library UART with DMA RX flow
218
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL_OK
HAL_OK HAL_ERROR
HAL_ERROR HAL_ERROR
HAL_ERROR
11
8/17/2019 Stm32f4 Labs
218/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
__ __
Or if DMA error wasdetected use Errorcallback function
Use UART with DMA transfer
HAL Library UART with DMA RX flow
219
Start process with DMA end of transfer
interrupt generation at end of processHAL_UART_Receive_DMA
Peripheral Initializationsincluding DMA stream initializations
HAL_OK
HAL_OK HAL_ERROR
HAL_ERROR HAL_ERROR
HAL_ERROR
1. UART1 andDMA2 init
2. UART1
receive buffer
11
8/17/2019 Stm32f4 Labs
219/429
HAL_DMA_IRQHandler
end of process callbackHAL_UART_RxCpltCallback
DMA_Stream_IRQHandler
process Error callbackHAL_UART_ErrorCallback
3. DMA bufferfull
4. Processinterrupt5. Buffer
received
5. Receive error
Use UART with DMA transfer
• Open the project in our IDE
• The functions we want to put into main.c
• Between /* USER C
Top Related