Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 422

STM32F4 Labs

T.O.M.A.S – Technically Oriented Microcontroller Application Services


V1.09
1 System Peripherals 2

1. GPIO & EXTI (1.1)


1. GPIO lab (1.1.1)
2. EXTI lab (1.1.2)

2. PWR (1.2)
1. SLEEP lab (1.2.1)
2. STOP lab (1.2.2)
3. STANDBY lab (1.2.3)

3. DMA (1.3)
1. DMA Poll lab (1.3.1)
2. DMA Interrupt lab (1.3.2)
2 Basic communication peripherals 3

1. UART (2.1)
1. UART Poll lab (2.1.1)
2. UART Interrupt lab (2.1.2)
3. UART DMA lab (2.1.3)

2. SPI (2.2)
1. SPI Poll lab (2.2.1)
2. SPI Interrupt lab (2.2.2)
3. SPI DMA lab (2.2.3)

3. I2C (2.3)
1. I2C Poll lab (2.3.1)
2. I2C Interrupt lab (2.3.2)
3. I2C DMA lab (2.3.3)
4. I2C Memory mode lab (2.3.4)
3 Timing peripherals 4

1. RTC (3.1)
1. RTC Alarm lab (3.1.1)

2. TIM (3.2)
1. TIM Interrupt lab (3.2.1)
2. TIM PWM out lab (3.2.2)
3. TIM DMA lab (3.2.3)
4. TIM Counter lab (3.2.4)

3. WDGs (3.3)
1. WWDG lab (3.3.1)
2. IWDG lab (3.3.2)
4 Analog peripherals 5

1. DAC (4.1)
1. DAC wave generation lab (4.1.1)

2. ADC (4.2)
1. ADC Poll lab (4.2.1)
2. ADC Interrupt lab (4.2.2)
3. ADC DMA lab (4.2.3)
5 5. BSP package 6

1. FMC SDRAM BSP lab (5.1)


2. LCD BSP Print text lab (5.2)
3. I2C BSP EEPROM lab (5.3)
4. SPI BSP GYROSCOPE lab (5.4)

STM32F42xx Technical Training 02/08/2016


1.1.1 GPIO Lab
1.1.1 Configure GPIO for LED toggling 11

• 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
1.1.1 Configure GPIO for LED toggling 12

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• Configure LED pin as GPIO_Output


1.1.1 Configure GPIO for LED toggling 13

• For debug purpose is recommended to select debug pins


SWD 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
1.1.1 Configure GPIO for LED toggling 14

Clock Configuration overview 10


• External clock enabling
• TAB>Pinout
• Select HSE and LSE clocks
• Bypass or crystal

1. Set HSE crystal

2. CubeMX reserve pins


1.1.1 Configure GPIO for LED toggling 15

• In order to run on maximum frequency, setup clock system


1.1.1 Configure GPIO for LED toggling 16

• GPIO Configuration
• TAB>Configuration>System>GPIO
1.1.1 Configure GPIO for LED toggling 17

• GPIO(Pin) Configuration
• Select Push Pull mode
• No pull-up and pull-down
• Output speed to HIGH
Is important for faster
peripheries like SPI, USART
• Button OK
1.1.1 Configure GPIO for LED toggling 18

• 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

GPIO output LOW speed GPIO output MEDIUM speed


LOW LOW

HIGH HIGH

GPIO output FAST speed GPIO output HIGH speed

LOW LOW

HIGH HIGH
1.1.1 Configure GPIO for LED toggling 19

• 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
1.1.1 Configure GPIO for LED toggling 20

• 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
1.1.1 Configure GPIO for LED toggling 21

• 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

/* USER CODE BEGIN 3 */


/* Infinite loop */
while (1)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(500);

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);


HAL_Delay(500);
}
/* USER CODE END 3 */
1.1.2 EXTI lab

STM32F42xx Technical Training 02/08/2016


1.1.2 Configure EXTI to turn on LED 23

• 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
1.1.2 Configure EXTI to turn on LED 24

• 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
1.1.2 Configure EXTI to turn on LED 25

• 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
1.1.2 Configure EXTI to turn on LED 26

• In order to run on maximum frequency, setup clock system


• Details in lab 0
1.1.2 Configure EXTI to turn on LED 27

• GPIO Configuration
• TAB>Configuration>System>GPIO
1.1.2 Configure EXTI to turn on LED 28

• 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
1.1.2 Configure EXTI to turn on LED 29

• NVIC Configuration
• We need to enable interrupts for EXTI
• TAB>Configuration>System>NVIC
1.1.2 Configure EXTI to turn on LED 30

• NVIC Configuration
• Enable interrupt for
EXTI Line0
• Button OK
1.1.2 Configure EXTI to turn on LED 31

• 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
1.1.2 Configure EXTI to turn on LED 32

HAL Library work flow 1

Peripheral Initializations
including peripheral interrupt NVIC
initializations Generated by
CubeMX
Configure the GPIO to generate interrupt on
rising or falling edge

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback
1.1.2 Configure EXTI to turn on LED 33

HAL Library work flow 2

Peripheral Initializations
including peripheral interrupt NVIC
initializations MX_GPIO_Init
inside main.c
Configure the GPIO to generate interrupt on
rising or falling edge

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback
1.1.2 Configure EXTI to turn on LED 34

HAL Library working flow 3

Peripheral Initializations
including peripheral interrupt NVIC
initializations inside
stm32f4xx_it.c
Configure the GPIO to generate interrupt on
rising or falling edge

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback
1.1.2 Configure EXTI to turn on LED 35

HAL Library work flow 4

Peripheral Initializations
including peripheral interrupt NVIC
initializations

Configure the GPIO to generate interrupt on


rising or falling edge

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback

User must define Callback


it is declared by default as
empty weak
1.1.2 Configure EXTI to turn on LED 36

HAL Library work flow 5

Peripheral Initializations
including peripheral interrupt NVIC
initializations

Configure the GPIO to generate interrupt on


rising or falling edge

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback

Usually in main.c between


/* USER CODE BEGIN */
tags
1.1.2 Configure EXTI to turn on LED 37

HAL Library work flow summary


3. create
edge
1. init NVIC
Peripheral Initializations
including peripheral interrupt NVIC
initializations
2. init GPIO
Configure the GPIO to generate interrupt on
rising or falling edge 5. HAL EXTI 4. EXTI interrupt
interrupt handler handler

HAL_EXTI0_IRQHandler EXTI0_IRQHandler

Edge detection callback


HAL_GPIO_EXTI_Callback HAL files clearing
6. HAL EXTI flags, check errors,
callback …
1.1.2 Configure EXTI to turn on LED 38

• 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
1.1.2 Configure EXTI to turn on LED 39

• 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

/* USER CODE BEGIN 4 */


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_0) {
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
} else {
__NOP();
}
}
/* USER CODE END 4 */
1.2.1 Low Power mode SLEEP
lab

STM32F42xx Technical Training 02/08/2016


1.2.1 Use SLEEP mode with EXTI 41

• 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, and which events can wake up
MCU
• Verify the correct functionality by measuring consumption
1.2.1 SLEEP Mode

GPIO’s
Core
FLASH • Core is stopped
CM4
RTC/backup reg. RAM • Peripherals are running
IWDG DMA

Clock
GP timers HSI

SPI HSE
USB LSI USART
LSE
DAC I2C
Reset
ADC

Power
regulator
1.2.1 Use SLEEP mode with EXTI 43

HAL Library work flow summary


4. create
edge
1. init NVIC
Peripheral Initializations
including peripheral interrupt NVIC
initializations
2. init GPIO
Configure the GPIO to generate interrupt on
rising or falling edge 5. EXTI interrupt
handler
3. Use WFI
HAL_EXTI0_IRQHandler EXTI0_IRQHandler
Enter into SLEEP mode(WFI)
6. HAL EXTI
interrupt handler

Edge detection callback


HAL_GPIO_EXTI_Callback HAL files clearing
7. HAL EXTI flags, check errors,
callback …
1.2.1 Use SLEEP mode with EXTI 44

• 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

To be able to reprogram
the STM32 which is in
LP mode, use
connection during
reset option
1.2.1 Use SLEEP mode with EXTI 45

• 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

/* USER CODE BEGIN 3 */


/* Infinite loop */
while (1)
{
HAL_Delay(1000);

HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI);
}
/* USER CODE END 3 */
1.2.1 Use SLEEP mode with EXTI 46

• Consumption still to high?


• Is STM32 really in SLEEP?
• Is the Systick disabled?

/* USER CODE BEGIN 3 */


/* Infinite loop */
while (1)
{
HAL_Delay(1000);
HAL_SuspendTick();
HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI);
HAL_ResumeTick();
}
/* USER CODE END 3 */

• Is this better?
1.2.2 Low Power mode STOP lab

STM32F42xx Technical Training 02/08/2016


1.2.2 Use STOP mode with EXTI 48

• 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
1.2.2 STOP Mode
• Core is stopped
GPIO’s
Core
FLASH • HSE, MSI clocks are OFF
CM4
RTC/backup reg. RAM
• SRAM and registers
IWDG DMA content is preserved
• Peripherals with HSI, LSI,
Clock
LSE clock option can be
GP timers HSE ON
HSI
SPI
• GPIO’s keep their setup
USB LSI USART
LSE
DAC I2C

ADC Reset

Power
regulator
1.2.2 Use STOP mode with EXTI 50

HAL Library work flow summary


4. create
edge
1. init NVIC
Peripheral Initializations
including peripheral interrupt NVIC
initializations
2. init GPIO
Configure the GPIO to generate interrupt on
rising or falling edge 5. EXTI interrupt
handler
3. Use WFI
Enter into STOP mode(WFI) HAL_EXTI0_IRQHandler EXTI0_IRQHandler

8. Reconfigure 6. HAL EXTI


Clock reconfigure clock after interrupt handler
wakeup

Edge detection callback


HAL_GPIO_EXTI_Callback HAL files clearing
7. HAL EXTI flags, check errors,
callback …
1.2.2 Use STOP mode with EXTI 51

• 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 reprogram
the STM32 which is in
LP mode, use
connection during
reset option
1.2.2 Use STOP mode with EXTI 52

• 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)
{
HAL_Delay(1000);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON,PWR_STOPENTRY_WFI);
SystemClock_Config();
}
/* USER CODE END 3 */
1.2.2 Use STOP mode with EXTI 53

• Or different function (for STM32F42X/43X)


/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_Delay(1000);
HAL_PWREx_EnterUnderDriveSTOPMode(PWR_LOWPOWERREGULATOR_UNDERDRIVE_ON,PWR_STOPENTRY_WFI);
SystemClock_Config();
}
/* USER CODE END 3 */
1.2.3 Low Power mode STANDBY
lab

STM32F42xx Technical Training 02/08/2016


1.2.3 Use STANDBY mode 55

• Objective
• For this lab create CubeMX project
• For testing purpose enable LED on PG14
• 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
1.2.3 STANDBY Mode
• Core and all peripherals
GPIO’s FLASH
Core are OFF, except RTC and
CM4
RTC/backup reg. RAM IWDG if enabled

IWDG DMA • HSE, HSI clocks are OFF,


LSI LSE can be ON

Clock • SRAM and registers


GP timers HSE content is lost, except
SPI RTC, and standby circuitry
HSI
USB LSI USART • GPIO’s are in high Z,
LSE
DAC I2C except Reset, RTC OUT
ADC Reset and WKUP 1,2,3
Power
regulator

Reset RTC OUT WKUP 1,2,3


1.2.3 Use STANDBY mode 57

HAL Library work flow summary


1. init NVIC
Peripheral Initializations
including peripheral interrupt NVIC
initializations
2. Standby

Enable Wake-up pin

4. create
3. Use WFI edge
Enter into STANDBY mode(WFI)

5. Wake up
Reset
STM with reset
1.2.3 Use STANDBY mode 58

• 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 reprogram
the STM32 which is in
LP mode, use
connection during
reset option
1.2.3 Use STANDBY mode 59

• 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)
{
HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_14);
HAL_Delay(2000);
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
HAL_PWR_EnterSTANDBYMode();
}
/* USER CODE END 3 */
1.2.3 Use STANDBY mode 60

• We cannot go into STANDBY again?


• Try to clear wake up flag
• __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

/* USER CODE BEGIN 2 */

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* USER CODE END 2 */
1.3.1 Data transfer over DMA lab

STM32F42xx Technical Training 02/08/2016


1.3.1 Use DMA in M2M transfer 62

• 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
1.3.1 Use DMA in M2M transfer 63

CORTEX-M4 Ethernet High Speed Dual Port Dual Port


CCM 168MHz 10/100 USB2.0 DMA1 DMA2
data RAM w/ FPU & MPU
64KB Master 5 Master 4 Master 2 Master 3
Master 1
FIFO/DMA FIFO/DMA FIFO/8 Streams FIFO/8 Streams

Dual Port
S-Bus
D-Bus

I-Bus

AHB1-APB2
AHB1
Dual Port
AHB1-APB1
AHB2

SRAM1
112KB

SRAM2
16KB

FSMC

Accelerator
I-Code FLASH

ART
D-Code
1Mbytes

Multi-AHB Bus Matrix


1.3.1 Use DMA in M2M transfer 64

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• For DMA we don’t need to configure any pins


1.3.1 Use DMA in M2M transfer 65

• In order to run on maximum frequency, setup clock system


• Details in lab 0
1.3.1 Use DMA in M2M transfer 66

• DMA configuration
• TAB>Configuration
• System>DMA
• TAB>DMA2
1. TAB >
• Button ADD
Configuration
2. System DMA

3. TAB>DMA 2

4. Add DMA
channel
1.3.1 Use DMA in M2M transfer 67

• DMA configuration
• Select MEMTOMEM
1. MEMTOMEM
DMA request
• Normal mode
• Increment source and
destination address
• FIFO setup
• Byte data width 3. Increment
• Burst size 5. Data width and addresses
• Button OK Burst

2. Normal
mode

4. FIFO setup

6. OK
1.3.1 Use DMA in M2M transfer 68

• 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
1.3.1 Use DMA in M2M transfer 69

• Start process DMA (same for TIM, ADC)


• Non blocking start process
• The end of the process must be checked by polling

1. init DMA
Peripheral
Initializations

2. Start DMA
Start Process
(HAL_DMA_Start)
3. DMA transfer data

Poll for process complete


(HAL_DMA_PollForTransfer)
4. Check if transfer is complete
1.3.1 Use DMA in M2M transfer 70

• 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_OK HAL_ERROR HAL_BUSY

DMA transfer was successfully


finished and data was
transferred to destination
without error
1.3.1 Use DMA in M2M transfer 71

• 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_OK HAL_ERROR HAL_BUSY

Error occurs during DMA


transfer you use
HAL_DMA_GetError for
details what happened
1.3.1 Use DMA in M2M transfer 72

• 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_OK HAL_ERROR HAL_BUSY

DMA transfer in progress,


user can only abort the
transfer
1.3.1 Use DMA in M2M transfer 73

• 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_t
Timeout)
1.3.1 Use DMA in M2M transfer 74

• We create two buffers


• One with source data
• Second as destination buffer

/* USER CODE BEGIN 0 */


uint8_t Buffer_Src[]={0,1,2,3,4,5,6,7,8,9};
uint8_t Buffer_Dest[10];
/* USER CODE END 0 */
1.3.1 Use DMA in M2M transfer 75

• HAL_DMA_Start start the M2M data transfer


• HAL_DMA_PollForTransfer check if the transfer ends successfully

/* USER CODE BEGIN 2 */


HAL_DMA_Start(&hdma_memtomem_dma2_stream0, (uint32_t) (Buffer_Src), (uint32_t) (Buffer_Dest), 10);
while(HAL_DMA_PollForTransfer(&hdma_memtomem_dma2_stream0, HAL_DMA_FULL_TRANSFER, 100) != HAL_OK)
{
__NOP();
}
/* USER CODE END 2 */
1.3.2 Data transfer over DMA with
interrupt lab

STM32F42xx Technical Training 02/08/2016


1.3.2 Use DMA M2M with interrupt 77

• 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
1.3.2 Use DMA M2M with interrupt 78

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• For DMA we don’t need to configure any pins


1.3.2 Use DMA M2M with interrupt 79

• In order to run on maximum frequency, setup clock system


• Details in lab 0
1.3.2 Use DMA M2M with interrupt 80

• DMA configuration
• TAB>Configuration
• System>DMA
• TAB>DMA2
1. TAB >
• Button ADD
Configuration
2. System DMA

3. TAB>DMA 2

4. Add DMA
channel
1.3.2 Use DMA M2M with interrupt 81

• DMA configuration
• Select MEMTOMEM
1. MEMTOMEM
DMA request
• Normal mode
• Increment source and
destination address
• FIFO setup
• Byte data width 3. Increment
• Burst size 5. Data width and addresses
• Button OK Burst

2. Normal
mode

4. FIFO setup

6. OK
1.3.2 Use DMA M2M with interrupt 82

• DMA configuration 1. TAB >


• System > NVIC Configuration
• Enable DMA2 Stream interrupt
• Button OK
2. System > NVIC

3. Enable DMA2
interrupts

4. OK
1.3.2 Use DMA M2M with interrupt 83

• 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
1.3.2 Use DMA M2M with interrupt 84

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 85

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC Generated by CubeMX
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 86

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback Defined by user


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 87

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations
Generated in main.c
Start process with interrupt generation at end
of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 88

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback
We recommend to use it
in main.c
process Error callback
DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 89

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

User defined functions. The


user must define functions by
process Error callback himself and put function names
DMA_XferErrorCallback
into DMA structure
1.3.2 Use DMA M2M with interrupt 90

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback
Generated in
stm32f4xx_it.c
process Error callback
DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 91

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback
Defined in
stm32f4xx_hal_dma.c
process Error callback
DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 92

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC Start DMA buffer transfer
initializations
Not blocking function
Start process with interrupt generation at end
program can continue
of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 93

HAL Library DMA with IT flow


DMA Initializations
Interrupt indicate DMA
including peripheral interrupt NVIC process is half/complete
initializations or error was detected
Start process with interrupt generation at end
of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 94

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations
Process interrupt
Start process with interrupt generation at end information
of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 95

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback
Data correctly transferred
Complete callback
function
process Error callback
DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 96

HAL Library DMA with IT flow


DMA Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

end of process callback


DMA_XferCpltCallback Error was detected
Error callback function

process Error callback


DMA_XferErrorCallback
1.3.2 Use DMA M2M with interrupt 97

HAL Library DMA with IT flow


DMA Initializations 1. DMA init
including peripheral interrupt NVIC
initializations

2. DMA transfer
Start process with interrupt generation at end
of process
start
HAL_DMA_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA2_Stream0_IRQHandler

4. HAL DMA 3. DMA transfer


5. DMA transfer management complete or error
end of process callback
DMA_XferCpltCallback was correct

process Error callback 5. Error in DMA


DMA_XferErrorCallback transfer
1.3.2 Use DMA M2M with interrupt 98

• 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

• 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)
1.3.2 Use DMA M2M with interrupt 99

• We create two buffers


• One with source data
• Second as destination buffer

/* USER CODE BEGIN 0 */


uint8_t Buffer_Src[]={0,1,2,3,4,5,6,7,8,9};
uint8_t Buffer_Dest[10];
/* USER CODE END 0 */
1.3.2 Use DMA M2M with interrupt 100

• DMA callback creation function prototype


/* USER CODE BEGIN 0 */
uint8_t Buffer_Src[]={0,1,2,3,4,5,6,7,8,9};
uint8_t Buffer_Dest[10];

void XferCpltCallback(DMA_HandleTypeDef *hdma);


/* USER CODE END 0 */

• DMA complete callback with nop where we can put breakpoint


/* USER CODE BEGIN 4 */
void XferCpltCallback(DMA_HandleTypeDef *hdma)
{
__NOP();//we reach this only if DMA transfer was correct
}
/* USER CODE END 4 */
1.3.2 Use DMA M2M with interrupt 101

• DMA Start
• Before we start the DMA with interrupt we need to set the callback into DMA structure
• Then is possible use the HAL_DMA_Start_IT to begin DMA transfer

/* USER CODE BEGIN 2 */


hdma_memtomem_dma2_stream0.XferCpltCallback=&XferCpltCallback;
HAL_DMA_Start_IT(&hdma_memtomem_dma2_stream0,(uint32_t)Buffer_Src,(uint32_t)Buffer_Dest,10);
/* USER CODE END 2 */
2.1.1 UART Poll lab

STM32F42xx Technical Training 02/08/2016


2.1.1 Simple UART communication 103

• Objective
• Learn how to setup UART in CubeMX
• 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
2.1.1 Simple UART communication 104

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• Pin selection
• We are looking for free pins where is possible to create wire loopback connection
2.1.1 Simple UART communication 105

• Pin selection
• We are looking for free pins
where is possible to create
wire loopback connection

Image from
STM32F429-Discovery
user manual
2.1.1 Simple UART communication 106

• Pin selection
• We are looking for free pins where is possible to create wire loopback connection

Image from
STM32F429
datasheet
2.1.1 Simple UART communication 107

• Hardware preparation
• We connect selected pins together by jumper, this help us to create loopback on UART

Hardware
connection
RX <-TX
TX>RX
2.1.1 Simple UART communication 108

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX UART selection


• Select USART1 in asynchronous mode
• Select PA9 and PA10 for USART1 if weren't selected
2.1.1 Simple UART communication 109

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.1.1 Simple UART communication 110

• CubeMX UART configuration


• Tab>Configuration>Connectivity>USART1
2.1.1 Simple UART communication 111

• CubeMX USART configuration check:


• BaudRate
• World length
• Parity
• Stop bits
• Data direction
• Oversampling
2.1.1 Simple UART communication 112

• CubeMX USART GPIO configuration check:


• On high baud rate set the
GPIO speed to HIGH

• TAB>Configuration>System>
>GPIO
• Set the HIGH output speed
Button OK
2.1.1 Simple UART communication 113

• 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
2.1.1 Simple UART communication 114

HAL Library init flow


Peripheral Initializations
CubeMX UART init start in
main.c file
MX_USART1_UART_Init();

Init UART1 structure

HAL_UART_Init(&huart1);

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 115

HAL Library init flow


1. We need init
Peripheral Initializations UART1

MX_USART1_UART_Init();

Init UART1 structure

HAL_UART_Init(&huart1);

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 116

HAL Library init flow


1. We need init
Peripheral Initializations UART1

MX_USART1_UART_Init();
CubeMX create for us
function which handle
Init UART1 structure UART initialization

HAL_UART_Init(&huart1);

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 117

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure

HAL_UART_Init(&huart1);

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 118

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure CubeMX fill the UART


structure with parameters
which we choose in
HAL_UART_Init(&huart1);
Configuration window

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 119

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1);

HAL_UART_MspInit callback

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 120

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1);
Function wrote
parameters from structure
HAL_UART_MspInit callback into UART1 registers

Init GPIO and NVIC for UART


2.1.1 Simple UART communication 121

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1); 4. Write to UART1 registers

HAL_UART_MspInit callback Optional callback from


HAL_UART_Init function,
be default empty weak
Init GPIO and NVIC for UART
function
2.1.1 Simple UART communication 122

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1); 4. Write to UART1 registers

HAL_UART_MspInit callback CubeMX configure here


UART1 GPIOs and
enable UART1 clock
Init GPIO and NVIC for UART
system
2.1.1 Simple UART communication 123

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1); 4. Write to UART1 registers

HAL_UART_MspInit callback 5. UART1 init callback

Init GPIO and NVIC for UART CubeMX configure here


UART1 GPIOs and
enable UART1 clock
system and NVIC
2.1.1 Simple UART communication 124

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1); 4. Write to UART1 registers

HAL_UART_MspInit callback 5. UART1 init callback

Init GPIO and NVIC for UART 6. UART1 GPIOS, NVIC and RCC init
2.1.1 Simple UART communication 125

HAL Library init flow


1. We need init
Peripheral Initializations UART1

2. Call UART1 init


MX_USART1_UART_Init(); function

Init UART1 structure 3. Store UART1 configuration


into structure

HAL_UART_Init(&huart1); 4. Write to UART1 registers

HAL_UART_MspInit callback 5. UART1 init callback

Init GPIO and NVIC for UART 6. UART1 GPIOS, NVIC and RCC init

7. Next periph init or user code


2.1.1 Simple UART communication 126

HAL Library transmit flow

Peripheral Initializations Generated by


CubeMX
Polling process Function blocks
HAL_UART_Transmit Polling with timeout

HAL_TIMEOUT HAL_OK HAL_ERROR HAL_BUSY


2.1.1 Simple UART communication 127

HAL Library transmit flow

Created by user
Peripheral Initializations

Polling process Function blocks


HAL_UART_Transmit Polling with timeout

HAL_TIMEOUT HAL_OK HAL_ERROR HAL_BUSY


2.1.1 Simple UART communication 128

HAL Library receive flow

Peripheral Initializations Generated by


CubeMX
Polling process Function blocks
HAL_UART_Receive Polling with timeout

HAL_TIMEOUT HAL_OK HAL_ERROR HAL_BUSY


2.1.1 Simple UART communication 129

HAL Library receive flow

Created by user
Peripheral Initializations

Polling process Function blocks


HAL_UART_Receive Polling with timeout

HAL_TIMEOUT HAL_OK HAL_ERROR HAL_BUSY


2.1.1 Simple UART communication 130

• 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

• 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);
2.1.1 Simple UART communication 131

• Transmit solution
• Create data structure for data

/* USER CODE BEGIN 0 */


uint8_t data[]={0,1,2,3,4,5,6,7,8,9};
/* USER CODE END 0 */

• Call transmit function from while loop

/* USER CODE BEGIN 3 */


/* Infinite loop */
while (1)
{
HAL_UART_Transmit(&huart1,data,10,1000);
}
/* USER CODE END 3 */
2.1.1 Simple UART communication 132

• Receive solution
• Create data structure for data

/* USER CODE BEGIN 0 */


uint8_t data[10];
/* USER CODE END 0 */

• Call transmit function from while loop


/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_UART_Receive(&huart1,data,10,1000);
}
/* USER CODE END 3 */
2.1.2 UART Interrupt lab

STM32F42xx Technical Training 02/08/2016


2.1.2 Use UART with interrupt 134

• 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

• Goal
• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART with interrupts
• Verify the correct functionality
2.1.2 Use UART with interrupt 135

• 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
2.1.2 Use UART with interrupt 136

• Hardware preparation
• We connect selected pins together by jumper, this help us to create loopback on UART

Hardware
loopback
2.1.2 Use UART with interrupt 137

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.1.2 Use UART with interrupt 138

• CubeMX UART configuration


• Tab>Configuration>Connectivity>USART1
2.1.2 Use UART with interrupt 139

• CubeMX UART configuration check:


• BaudRate
• World length
• Parity
• Stop bits
• Data direction
• Oversampling
2.1.2 Use UART with interrupt 140

• CubeMX USART configuration NVIC settings


• TAB>NVIC Settings
• Enable interrupts
• OK
2.1.2 Use UART with interrupt 141

• 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
2.1.2 Use UART with interrupt 142

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Receive_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 143

HAL Library UART with IT transmit flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 144

HAL Library UART with IT transmit flow


Peripheral Initializations
including peripheral interrupt NVIC Generated by CubeMX
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 145

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback Defined by user


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 146

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations Generated in main.c and
stm32f4xx_hal_msp.c
Start process with interrupt generation at end
of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 147

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback
We recommend to use it
in main.c
process Error callback
HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 148

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

Defined as __weak you can


process Error callback find name of this functions in
HAL_UART_ErrorCallback stm32f4xx_hal_uart.c
2.1.2 Use UART with interrupt 149

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback
Generated in
stm32f4xx_it.c
process Error callback
HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 150

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback
Defined in
stm32f4xx_hal_uart.c
process Error callback
HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 151

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC Send buffer over UART
initializations
Not blocking function
Start process with interrupt generation at end
program can continue
of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 152

HAL Library UART with IT receive flow


Interrupt indicate the data
Peripheral Initializations register is empty we can
including peripheral interrupt NVIC
initializations send more data or error
was detected
Start process with interrupt generation at end
of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 153

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations
Process interrupt
Start process with interrupt generation at end information
of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 154

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations
Send more data if is buffer
Start process with interrupt generation at end
not empty
of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 155

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback
If data buffer is empty use
Complete callback
function
process Error callback
HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 156

HAL Library UART with IT receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

end of process callback


HAL_UART_TxCpltCallback
Or if error was detected
use Error callback
function
process Error callback
HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 157

HAL Library UART with IT receive flow


Peripheral Initializations 1. UART1 init
including peripheral interrupt NVIC
initializations

2. UART1 send
Start process with interrupt generation at end
of process
buffer
HAL_UART_Transmit_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_UART_IRQHandler USART1_IRQHandler

4. Send more 3. UART1 Tx


data or manage register empty
end of process callback 5. Buffer sent error
HAL_UART_TxCpltCallback

process Error callback 5. Transmit error


HAL_UART_ErrorCallback
2.1.2 Use UART with interrupt 158

• 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


• HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
2.1.2 Use UART with interrupt 159

• Buffer definition
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buff[10];
/* USER CODE END 0 */

• Sending and receiving methods


/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1,rx_buff,10);
HAL_UART_Transmit_IT(&huart1,tx_buff,10);
/* USER CODE END 2 */
2.1.2 Use UART with interrupt 160

• Complete callback check


• We can put brakepoints on NOPs to watch if we send or receive complete buffer

/* USER CODE BEGIN 4 */


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
__NOP();//test if we reach this position
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)


{
__NOP();//test if we reach this position
}
/* USER CODE END 4 */
2.1.3 UART DMA lab

STM32F42xx Technical Training 02/08/2016


2.1.3 Use UART with DMA transfer 163

• 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

• Goal
• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART with DMA
• Verify the correct functionality
2.1.3 Use UART with DMA transfer 164

• 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
2.1.3 Use UART with DMA transfer 165

• Hardware preparation
• We connect selected pins together by jumper, this help us to create loopback on UART

Hardware
loopback
2.1.3 Use UART with DMA transfer 166

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.1.3 Use UART with DMA transfer 167

• CubeMX UART configuration


• Tab>Configuration>Connectivity>USART1
2.1.3 Use UART with DMA transfer 168

• CubeMX USART configuration check:


• BaudRate
• World length
• Parity
• Stop bits
• Data direction
• Oversampling
2.1.3 Use UART with DMA transfer 169

• CubeMX USART configuration DMA settings


• TAB>DMA Settings
• Button ADD
2.1.3 Use UART with DMA transfer 170

• CubeMX USART configuration DMA Tx settings


• Set USART1_TX request
• Memory to peripheral direction
• Normal mode
• Byte data width
• Increment memory address
2.1.3 Use UART with DMA transfer 171

• CubeMX USART configuration DMA Rx settings


• Button ADD
• Set USART1_RX request
• Peripheral to memory direction
• Normal mode
• Byte data width
• Increment memory address
2.1.3 Use UART with DMA transfer 172

• CubeMX USART configuration NVIC settings


• TAB>NVIC Settings
• Enable DMA2 interrupts for
USART1
• Button OK
2.1.3 Use UART with DMA transfer 173

• 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
2.1.3 Use UART with DMA transfer 174

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 175

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations Generated by CubeMX

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 176

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

Defined by user
process Error callback
HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 177

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations
Generated in main.c and
stm32f4xx_hal_msp.c
Start process with DMA end of transfer
interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 178

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback We recommend to use it
in main.c

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 179

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback
Generated in
stm32f4xx_it.c
process Error callback
HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 180

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback
Defined in
stm32f4xx_hal_dma.c and
then linked to
process Error callback
HAL_UART_ErrorCallback stm32f4xx_hal_uart.c
2.1.3 Use UART with DMA transfer 181

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations
Receive buffer over UART
with DMA
Not blocking function
Start process with DMA end of transfer program can continue
interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 182

HAL Library UART with DMA RX flow


Peripheral Initializations Interrupt indicate the DMA
including DMA stream initializations receive half/complete
buffer or DMA error was
Start process with DMA end of transfer detected
interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 183

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Process interrupt
Start process with DMA end of transfer information from DMA and
interrupt generation at end of process UART
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback

process Error callback


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 184

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback If data buffer is full HAL
use Complete callback
function
process Error callback
HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 185

HAL Library UART with DMA RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_UART_RxCpltCallback Or if DMA error was
detected use Error
callback function
process Error callback
HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 186

HAL Library UART with DMA RX flow


Peripheral Initializations 1. UART1 and
including DMA stream initializations DMA2 init

Start process with DMA end of transfer 2. UART1


interrupt generation at end of process receive buffer
HAL_UART_Receive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA_Stream_IRQ
HAL_DMA_IRQHandler
Handler
3. DMA buffer
4. Process
5. Buffer full
end of process callback interrupt
HAL_UART_RxCpltCallback received

process Error callback 5. Receive error


HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 187

• 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_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

• For receive use function


• HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
2.1.3 Use UART with DMA transfer 188

• Buffer definition
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buff[10];
/* USER CODE END 0 */

• Sending and receiving methods with DMA


/* USER CODE BEGIN 2 */
HAL_UART_Receive_DMA(&huart1,rx_buff,10);
HAL_UART_Transmit_DMA(&huart1,tx_buff,10);
/* USER CODE END 2 */
2.1.3 Use UART with DMA transfer 189

• Complete callback check


• We can put breakpoints on NOPs to watch if we receive complete buffer

/* USER CODE BEGIN 4 */


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
__NOP();//check if we receive all data
}
/* USER CODE END 4 */
2.2.1 SPI Poll lab

STM32F42xx Technical Training 02/08/2016


2.2.1 Simple SPI communication 191

• Objective
• Learn how to setup SPI in CubeMX
• How to Generate Code in CubeMX and use HAL functions

• Goal
• Configure SPI in CubeMX and Generate Code
• Learn how to send and receive data over SPI without interrupts
• Verify the correct functionality
2.2.1 Simple SPI communication 192

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• Pin selection
• We are looking for free pins where is possible to create wire loopback connection
2.2.1 Simple SPI communication 193

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX SPI selection


• Select SPI1 Full-Duplex Master
• Select PA5, PA6, PA7 for SPI1 if weren't selected
2.2.1 Simple SPI communication 194

• Hardware preparation
• Connect PA6 and PA7 together with jumper

Hardware
loopback
2.2.1 Simple SPI communication 195

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.2.1 Simple SPI communication 196

• CubeMX SPI configuration


• Tab>Configuration>Connectivity>SPI1
• Check the settings
• Button OK

• The CLK frequency


with core on 180MHz
is now 45MHz
• For this clock use
HIGH GPIO speed
2.2.1 Simple SPI communication 197

• CubeMX SPI – GPIO configuration


• The SPI CLK frequency
with core on 180MHz
is now 45MHz
• For this clock use
HIGH GPIO speed

• Tab>Configuration>System>
>GPIO
• Tab>SPI1
• Seth High output speed
• Button OK
2.2.1 Simple SPI communication 198

• 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
2.2.1 Simple SPI communication 199

HAL Library transmit receive flow

Peripheral Initializations

Polling process Function blocks


HAL_SPI_TransmitReceive Polling with timeout

HAL_TIMEOUT HAL_OK HAL_ERROR HAL_BUSY


2.2.1 Simple SPI communication 200

• 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 transmit and receive use function


• HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t
*pRxData, uint16_t Size, uint32_t Timeout)
2.2.1 Simple SPI communication 201

• Transmit receive solution


• Create data structure for data
/* USER CODE BEGIN PV */
uint8_t tx_buffer[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buffer[10];
/* USER CODE END PV */
• Call transmit receive function
/* USER CODE BEGIN 2 */
HAL_SPI_TransmitReceive(&hspi1,tx_buffer,rx_buffer,10,100);
/* USER CODE END 2 */
2.2.2 SPI Interrupt lab

STM32F42xx Technical Training 02/08/2016


2.2.2 Use SPI with interrupt 203

• Objective
• Learn how to setup SPI with interrupts in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple loopback example with interrupts

• Goal
• Configure SPI in CubeMX and Generate Code
• Learn how to send and receive data over SPI with interrupts
• Verify the correct functionality
2.2.2 Use SPI with interrupt 204

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX SPI selection


• Select SPI1 Full-Duplex Master
• Select PA5, PA6, PA7 for SPI1 if weren't selected
2.2.2 Use SPI with interrupt 205

• Hardware preparation
• Connect PA6 and PA7 together with jumper

Hardware
loopback
2.2.2 Use SPI with interrupt 206

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.2.2 Use SPI with interrupt 207

• CubeMX SPI configuration


• Tab>Configuration>Connectivity>SPI1
• Check the settings
• Button OK
2.2.2 Use SPI with interrupt 208

• CubeMX SPI configuration


• TAB>NVIC Settings
• Enable SPI interrupt
• Button OK
2.2.2 Use SPI with interrupt 209

• 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
2.2.2 Use SPI with interrupt 210

HAL Library SPI with IT transmit receive flow


Peripheral Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_SPI_TransmitReceive_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_SPI_IRQHandler SPI1_IRQHandler

end of process callback


HAL_SPI_TxRxCpltCallback

process Error callback


HAL_SPI_ErrorCallback
2.2.2 Use SPI with interrupt 211

• 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_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t
*pRxData, uint16_t Size)
2.2.2 Use SPI with interrupt 212

• Buffer definition
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buff[10];
/* USER CODE END 0 */

• Sending and receiving methods


/* USER CODE BEGIN 2 */
HAL_SPI_TransmitReceive_IT(&hspi1,tx_buff,rx_buff,10);
/* USER CODE END 2 */
2.2.2 Use SPI with interrupt 213

• Complete callback check


• We can put breakpoints on NOPs to watch if we send or receive complete buffer

/* USER CODE BEGIN 4 */


void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
__NOP();
}
/* USER CODE END 4 */
2.2.3 SPI DMA lab

STM32F42xx Technical Training 02/08/2016


2.2.3 Use SPI with DMA transfer 215

• Objective
• Learn how to setup SPI with DMA in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple loopback example with DMA

• Goal
• Configure SPI in CubeMX and Generate Code
• Learn how to send and receive data over SPI with DMA
• Verify the correct functionality
2.2.3 Use SPI with DMA transfer 216

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX SPI selection


• Select SPI1 Full-Duplex Master
• Select PA5, PA6, PA7 for SPI1 if weren't selected
2.2.3 Use SPI with DMA transfer 217

• Hardware preparation
• Connect PA6 and PA7 together with jumper

Hardware
loopback
2.2.3 Use SPI with DMA transfer 218

• In order to run on maximum frequency, setup clock system


• Details in lab 0
2.2.3 Use SPI with DMA transfer 219

• CubeMX SPI configuration


• Tab>Configuration>Connectivity>SPI1
• Check the settings
• Button OK
2.2.3 Use SPI with DMA transfer 220

• CubeMX SPI configuration DMA settings


• TAB>DMA Settings
• Button ADD
• SPI1_RX
• Memory increment
• Button ADD
• SPI1_Tx
• Memory increment
• Button OK
2.2.3 Use SPI with DMA transfer 221

• CubeMX SPI configuration NVIC settings


• TAB>NVIC Settings
• Enable DMA2 interrupts for
SPI1
• Button OK
2.2.3 Use SPI with DMA transfer 222

• 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
2.2.3 Use SPI with DMA transfer 223

HAL Library SPI with DMA TX RX flow


Peripheral Initializations
including DMA stream initializations

Start process with DMA end of transfer


interrupt generation at end of process
HAL_SPI_TransmitReceive_DMA

HAL_OK HAL_ERROR HAL_ERROR


DMA2_Stream0_IRQ
HAL_DMA_IRQHandler
Handler

end of process callback


HAL_SPI_TxRxCpltCallback

process Error callback


HAL_SPI_ErrorCallback
2.2.3 Use SPI with DMA transfer 224

• 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_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t
*pRxData, uint16_t Size)
2.2.3 Use SPI with DMA transfer 225

• Buffer definition
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buff[10];
/* USER CODE END 0 */

• Sending and receiving methods


/* USER CODE BEGIN 2 */
HAL_SPI_TransmitReceive_DMA(&hspi1,tx_buff,rx_buff,10);
/* USER CODE END 2 */
2.2.3 Use SPI with DMA transfer 226

• Complete callback check


• We can put breakpoints on NOPs to watch if we send or receive complete buffer

/* USER CODE BEGIN 4 */


void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
__NOP();
}
/* USER CODE END 4 */
2.3.1 I2C Poll lab

STM32F42xx Technical Training 02/08/2016


2.3.1 Simple I2C communication 228

• Objective
• Learn how to setup i2C in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Work in pairs, one will create I2C transmitter and second I2C receiver

• Goal
• Configure I2C in CubeMX and Generate Code
• Learn how to send and receive data over I2C without interrupts
• Verify the correct functionality
2.3.1 Simple I2C communication 229

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX SPI selection


• Select I2C3 Periphery
• Select PA8, PC9 for I2C3 if weren't selected
2.3.1 Simple I2C communication 230

• Hardware preparation
• Connect together PA8 and PC9 pins from both boards, and GND

Hardware connection
PA8 <-> PA8
PC9<->PC9
2.3.1 Simple I2C communication 231

• CubeMX I2C configuration


• Tab>Configuration>Connectivity>I2C3
• Check the settings
• Define Slave address
to 20
• Button OK
2.3.1 Simple I2C communication 232

• CubeMX I2C configuration


• Tab>GPIO Settings
• Check Pull-UP
• Check GPIO speed

Check if the Pull-UP resistor are


necessary in case you are using
external one
Check the GPIO speed
This two parameters can limit
I2C maximum speed
2.3.1 Simple I2C communication 233

• 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
2.3.1 Simple I2C communication 234

• Buffer definition
/* USER CODE BEGIN PV */
/* Private variables
uint8_t data_tx[10]={1,2,3,4,5,6,7,8,9,0};
uint8_t data_rx[10];
/* USER CODE END PV */

• Master simple send and receive functions

/* USER CODE BEGIN 2 */


HAL_Delay(100);
HAL_I2C_Master_Transmit(&hi2c3,20,data_tx,10,1000);
HAL_Delay(2);
HAL_I2C_Master_Receive(&hi2c3,20,data_rx,10,1000);
/* USER CODE END 2 */
2.3.1 Simple I2C communication 235

• Slave simple send and receive functions


/* USER CODE BEGIN 2 */
status1=HAL_I2C_Slave_Receive(&hi2c3,data_rx,10,10000);
status2=HAL_I2C_Slave_Transmit(&hi2c3,data_rx,10,10000);
/* USER CODE END 2 */

• During this example the SLAVE must begin the code execution first to be
prepared on MASTER Receive/Send requests
2.3.2 I2C Interrupt lab

STM32F42xx Technical Training 02/08/2016


2.3.2 Use I2C with interrupt 237

• Objective
• Learn how to setup I2C with interrupts in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple loopback example with interrupts

• Goal
• Configure I2C in CubeMX and Generate Code
• Learn how to send and receive data over I2C with interrupts
• Verify the correct functionality
2.3.2 Use I2C with interrupt 238

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX SPI selection


• Select I2C3 Periphery
• Select PA8, PC9 for I2C3 if weren't selected
2.3.2 Use I2C with interrupt 239

• Hardware preparation
• Connect together PA8 and PC9 pins from both boards, and GND

Hardware connection
PA8 <-> PA8
PC9<->PC9
2.3.2 Use I2C with interrupt 240

• CubeMX I2C configuration


• Tab>Configuration>Connectivity>I2C3
• Check the settings
• Define Slave address
to 20
• Button OK
2.3.2 Use I2C with interrupt 241

• CubeMX I2C configuration


• Tab>GPIO Settings
• Check Pull-UP
• Check GPIO speed

Check if the Pull-UP resistor are


necessary in case you are using
external one
Check the GPIO speed
This two parameters can limit
I2C maximum speed
2.3.2 Use I2C with interrupt 242

• CubeMX I2C configuration


• Tab>NVIC settings
• Enable interrupts
• Button OK
2.3.2 Use I2C with interrupt 243

• 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
2.3.2 Use I2C with interrupt 244

• Buffer definition
/* USER CODE BEGIN PV */
/* Private variables
uint8_t data_tx[10]={1,2,3,4,5,6,7,8,9,0};
uint8_t data_rx[10];
/* USER CODE END PV */

• Master simple send and receive functions with interrupts


/* USER CODE BEGIN 2 */
HAL_I2C_Master_Transmit_IT(&hi2c3,20,data_tx,10);
HAL_Delay(2);
HAL_I2C_Master_Receive_IT(&hi2c3,20,data_rx,10);
/* USER CODE END 2 */
• Master callbacks
/* USER CODE BEGIN 4 */
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){}
/* USER CODE END 4 */
2.3.2 Use I2C with interrupt 245

• Slave receive
/* USER CODE BEGIN 2 */
HAL_I2C_Slave_Receive_IT(&hi2c3,data_rx,10);
/* USER CODE END 2 */

• Slave transmit in callback


/* USER CODE BEGIN 4 */
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){
}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){


HAL_I2C_Slave_Transmit_IT(&hi2c3,data_rx,10);
}
/* USER CODE END 4 */
2.3.3 I2C DMA lab

STM32F42xx Technical Training 02/08/2016


2.3.3 Use I2C with DMA transfer 247

• 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

• Goal
• Configure UART in CubeMX and Generate Code
• Learn how to send and receive data over UART with DMA
• Verify the correct functionality
3.1.1 Use RTC Alarm lab

STM32F42xx Technical Training 02/08/2016


3.1.1 Use RTC and Alarm with interrupt 249

• Objective
• Learn how to setup RTC with interrupt in CubeMX
• 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
3.1.1 Use RTC and Alarm with interrupt 250

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• Set Internal Alarm on Alarm A or Alarm B


• Set GPIO to toggle with LED as alarm indication
3.1.1 Use RTC and Alarm with interrupt 251

• In order to run on maximum frequency, setup clock system


• Details in lab 0
3.1.1 Use RTC and Alarm with interrupt 252

• RTC Configuration
• TAB>Configuration
• Control > RTC
1. TAB > Configuration
• Set parameters which you want

2. RTC

3. Check configuration
3.1.1 Use RTC and Alarm with interrupt 253

• RTC Configuration NVIC


1. TAB > NVIC
• TAB>NVIC Setup
• Enable Alarm interrupt
• Button OK

2. Enable RTC Alarm

3. Button OK
3.1.1 Use RTC and Alarm with interrupt 254

• 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
3.1.1 Use RTC and Alarm with interrupt 255

• 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
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_t
Format)

• RTC alarm indication will be done by LED


• HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
3.1.1 Use RTC and Alarm with interrupt 256

• RTC enable
/* Initialize all configured peripherals */
HAL_PWR_EnableBkUpAccess();//enable PWR backup domain access (RTC,BKReg)
__HAL_RCC_RTC_ENABLE();//Enable RTC. not created by cube because the RTC can run.
MX_GPIO_Init();
MX_RTC_Init();

• In MX_RTC_Init we set first Alarm to 1s

/**Enable the Alarm A


*/
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 1;
sAlarm.AlarmTime.SubSeconds = 0;
3.1.1 Use RTC and Alarm with interrupt 257

• RTC enable
/* USER CODE BEGIN 4 */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc){
RTC_AlarmTypeDef sAlarm;
HAL_RTC_GetAlarm(hrtc,&sAlarm,RTC_ALARM_A,FORMAT_BIN);
if(sAlarm.AlarmTime.Seconds>58){
sAlarm.AlarmTime.Seconds=0;
}else{
sAlarm.AlarmTime.Seconds=sAlarm.AlarmTime.Seconds+1;
}
while(HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, FORMAT_BIN)!=HAL_OK){}
HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);
}
/* USER CODE END 4 */
3.1.1 Use RTC and Alarm with interrupt 258

• Advanced task
• The counting stops after 1minute
• Modify the project to create alarm every 1s for infinite time
/**Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 1;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS|RTC_ALARMMASK_MINUTES;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BCD);

• We only need to modify the Alarm mask to ignore Days, Hours and Minutes
3.2.1 TIM with interrupt lab

STM32F42xx Technical Training 02/08/2016


3.2.1 Use TIM with interrupt 260

• Objective
• Learn how to setup TIM with Interrupt in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Indicate TIM interrupt with LED toggle

• Goal
• Configure TIM in CubeMX and Generate Code
• Learn how start timer and handle interrupt
• Verify the correct functionality
3.2.1 Use TIM with interrupt 261

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX TIM selection


• Select TIM clock source Internal clock
• Enable GPIO for LED PG14
3.2.1 Use TIM with interrupt 262

• CubeMX TIM configuration


• Tab>Configuration>Control>TIM1
• Check the settings
3.2.1 Use TIM with interrupt 263

• CubeMX TIM configuration


• Tab>Parameter Settings
• Prescaler to 18000
• Counter period to 10000
• Together with 180MHz TIMER1
clock we get period 1Hz
3.2.1 Use TIM with interrupt 264

• CubeMX TIM configuration


• Tab>NVIC Settings
• Enable TIM1 Update interrupt
• Button OK
3.2.1 Use TIM with interrupt 265

• 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
3.2.1 Use TIM with interrupt 266

HAL Library TIM with IT flow


TIM Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_TIM_Base_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_TIM_IRQHandler TIM1_UP_TIM10_IRQHandler

process callback
HAL_TIM_PeriodElapsedCallback

process Error callback


HAL_TIM_ErrorCallback
3.2.1 Use TIM with interrupt 267

• 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 TIM start use function


• HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)

• TIM callback
• void TIM1_UP_TIM10_IRQHandler(void)

• GPIO LED toggle


• HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
3.2.1 Use TIM with interrupt 268

• Solution
• TIM start
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim1);
/* USER CODE END 2 */

• Callback handling
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);
}
/* USER CODE END 4 */
3.2.2 TIM with PWM output lab

STM32F42xx Technical Training 02/08/2016


3.2.2 Use TIM with PWM output 270

• Objective
• Learn how to setup TIM with PWM out in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Indicate TIM PWM on LED

• Goal
• Configure TIM in CubeMX and Generate Code
• Learn how start timer and set PWM out
• Verify the correct functionality with LED
3.2.2 Use TIM with PWM output 271

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX TIM selection


• Select TIM clock source - Internal clock
• Set Channel1 to PWM generation
3.2.2 Use TIM with PWM output 272

• CubeMX TIM configuration


• TAB>Configuration
>Control>TIM1
• TAB>Parameter settings
• Prescaler to 18000
• Counter period to 10000
• Together with 180MHz TIMER1
clock we get period 1Hz
• PWM pulse to 5000 this give
us 1Hz blinking frequency
3.2.2 Use TIM with PWM output 273

• 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
3.2.2 Use TIM with PWM output 274

• Start process TIM with PWM(same for DMA, ADC)


• Non blocking start process

Peripheral
Initializations

Start Process
HAL_TIM_PWM_Start
3.2.2 Use TIM with PWM output 275

• 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 TIM start use function


• HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)

• GPIO LED toggle


• We wire the Channel1 PE9 with LED PG14
3.2.2 Use TIM with PWM output 276

• Solution
• TIM PWM start
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
/* USER CODE END 2 */
• TIM1 Channel 1 and LED connection
3.2.3 TIM with DMA lab

STM32F42xx Technical Training 02/08/2016


3.2.3 Use TIM with DMA transfer 278

• Objective
• Learn how to setup TIM with DMA in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Indicate TIM DMA transfer with LED toggle

• Goal
• Configure TIM in CubeMX and Generate Code
• Learn how start timer and setup DMA
• Verify the correct functionality with DMA transfer into GPIO register
3.2.3 Use TIM with DMA transfer 279

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX TIM selection


• Select TIM clock source Internal clock
• Enable GPIO for LED PG14
3.2.3 Use TIM with DMA transfer 280

• CubeMX TIM configuration


• Tab>Configuration>Control>TIM1
• Check the settings
3.2.3 Use TIM with DMA transfer 281

• CubeMX TIM configuration


• Tab>Parameter Settings
• Prescaler to 18000
• Counter period to 10000
• Together with 180MHz TIMER1
clock we get period 1Hz
3.2.3 Use TIM with DMA transfer 282

• CubeMX TIM configuration


• TAB>DMA Settings
• Button ADD
• Select TIM1_UP DMA request
• Memory to peripheral direction
• Set Memory increment
• Circular mode
• Half word data width
• Button OK
3.2.3 Use TIM with DMA transfer 283

• 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
3.2.3 Use TIM with DMA transfer 284

HAL Library TIM with DMA flow

Peripheral Initializations
including DMA stream initializations

Start process with DMA


HAL_DMA_Start

HAL_OK HAL_ERROR HAL_ERROR

Start process with TIM


HAL_TIM_Base_Start

HAL_OK HAL_ERROR HAL_ERROR


3.2.3 Use TIM with DMA transfer 285

• 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 TIM start use function


• HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t
Length)

• TIM1 trigger DMA transfer


• __HAL_TIM_ENABLE_DMA

• DMA start function


• HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t
DstAddress, uint32_t DataLength)

• GPIO LED register address


• (uint32_t)(&GPIOG->ODR)
3.2.3 Use TIM with DMA transfer 286

• Variable data definition


/* USER CODE BEGIN PV */
uint16_t data[]={GPIO_PIN_14,0x0000};
/* USER CODE END PV */

• DMA and TIM start


/* USER CODE BEGIN 2 */
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_UPDATE);
HAL_DMA_Start(&hdma_tim1_up,(uint32_t)data,(uint32_t)&GPIOG->ODR,2);
HAL_TIM_Base_Start(&htim1);
/* USER CODE END 2 */
3.2.4 TIM as counter lab

STM32F42xx Technical Training 02/08/2016


3.2.4 Use TIM as pulse counter 288

• Objective
• Learn how to setup TIM as counter in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Indicate TIM count 5 button press with LED toggle

• Goal
• Configure TIM as counter in CubeMX and Generate Code
• Learn how start timer and handle interrupt
• Verify the correct functionality with LED toggle after 5 button press
3.2.4 Use TIM as pulse counter 289

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX TIM selection


• Select TIM clock source ETR2
• Enable GPIO for LED PG14
3.2.4 Use TIM as pulse counter 290

• Hard ware setting


• Connect Button PA0 and ETR pin PE7 with wire together
3.2.4 Use TIM as pulse counter 291

• CubeMX TIM configuration


• Tab>Configuration>Control>TIM1
• Check the settings
3.2.4 Use TIM as pulse counter 292

• CubeMX TIM configuration


• Tab>Parameter Settings
• Counter set to 5, 5 button press
• Clock set the ETR pin filter and
edge reaction
3.2.4 Use TIM as pulse counter 293

• CubeMX TIM configuration


• Tab>NVIC Settings
• Enable TIM1 Update interrupt
• Button OK
3.2.4 Use TIM as pulse counter 294

• 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
3.2.4 Use TIM as pulse counter 295

HAL Library TIM with IT flow


TIM Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_TIM_Base_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_TIM_IRQHandler TIM1_UP_TIM10_IRQHandler

process callback
HAL_TIM_PeriodElapsedCallback

process Error callback


HAL_TIM_ErrorCallback
3.2.4 Use TIM as pulse counter 296

• 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 TIM start use function


• HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)

• TIM callback
• void TIM1_UP_TIM10_IRQHandler(void)

• GPIO LED toggle


• HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
3.2.4 Use TIM as pulse counter 297

• Solution
• TIM start
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim1);
/* USER CODE END 2 */

• Callback handling
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);
}
/* USER CODE END 4 */
3.3.1 WWDG lab

STM32F42xx Technical Training 02/08/2016


3.3.1 Use WWDG 299

• Objective
• Learn how to setup WWDG in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple application to test WWDG

• Goal
• Configure WDGin in CubeMX and Generate Code
• Learn how to start WWDG
• WWDG indication via LED
3.3.1 Use WWDG 300

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX WWDG selection


• Select WWDG
• Configure PG14 for LED indication
3.3.1 Use WWDG 301

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX WWDG selection


• Select WWDG
• Configure PG14 for LED indication
3.3.1 Use WWDG 302

• In order to run on maximum frequency, setup clock system


• Details in lab 0
3.3.1 Use WWDG 303

• CubeMX ADC configuration


• TAB>Configuration>System>
>WWDG>Parameter Settings
• Set prescaller to 8
• WWDG window to 80
• And free running counter to 127
• Button OK
3.3.1 Use WWDG 304

• 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
3.3.1 Use WWDG 305

• How calculate the window APB1freq=45MHz, prescaller 8

twwdg min  f APB1* 4096*NWWDG_PRESCALLER*(N REFRESH  NWINDOW ) 


1
( 6
)* 4096*8*( 127-80 )  34.2ms
45*10
1
twwdg max  ( 6
) * 4096 * 8 * (127 - 63)  46.6ms
45 *10

We refresh the
WWDG to 127

In our case 80

Fixed 63
3.3.1 Use WWDG 306

• Solution
• WWDG Start

/* USER CODE BEGIN 2 */


HAL_WWDG_Start(&hwwdg);
/* USER CODE END 2 */

• WWDG refresh
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
//30ms or 50ms is outside the WWDG window, 40ms fits inside the window
HAL_Delay(40);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_WWDG_Refresh(&hwwdg, 127);
}
/* USER CODE END 3 */
3.3.2 IWDG lab

STM32F42xx Technical Training 02/08/2016


3.3.2 Use IWDG 308

• Objective
• Learn how to setup IWDG in CubeMX
• How to Generate Code in CubeMX and use HAL functions
• Create simple application to test IWDG

• Goal
• Configure IWDG in CubeMX and Generate Code
• Learn how to start IWDG
• IWDG indication via LED
3.3.2 Use IWDG 309

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX IWDG selection


• Select IWDG
• Configure PG14 for LED indication
3.3.2 Use IWDG 310

• In order to run on maximum frequency, setup clock system


• Details in lab 0
3.3.2 Use IWDG 311

• CubeMX ADC configuration


• TAB>Configuration>System>
>IWDG>Parameter Settings
• Set prescaller to 4
• Max value to 50
• Button OK
3.3.2 Use IWDG 312

• 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
3.3.2 Use IWDG 313

• IWDG refresh interval


1 1
t iwdg  * PIWDG _ PRESCALLER * N IWDG _ COUNTERVAL  ( 3
) * 4 * 50  6.25ms
f LSI 32 *10
3.3.2 Use IWDG 314

• Solution
• IWDG Start

/* USER CODE BEGIN 2 */


HAL_IWDG_Start(&hiwdg);
/* USER CODE END 2 */

• IWDG refresh
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_Delay(7);//try delay 6ms and 7ms
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,GPIO_PIN_SET);
HAL_IWDG_Refresh(&hiwdg);
}
/* USER CODE END 3 */
3.3.2 Use IWDG 315

• Hardware IWDG
• Remove IWDG start from project

/* USER CODE BEGIN 2 */


/* USER CODE END 2 */

• Use ST-Link utility and enable IWDG Hardware start


3.3.2 Use IWDG 316

• Hardware IWDG
• Start ST-Link utility
• Menu>Target>Option bytes
or CTRL+B
• Uncheck the WDG_WS
• Button APPLY
• Now the IWDG is automatically started after reset

• !!! DO NOT FORGET disable


IWDG automatic start after
you end this example
4.1.1 DAC wave generator lab

STM32F42xx Technical Training 02/08/2016


4.1.1 Use DAC as wave generator 318

• Objective
• Learn how to setup DAC as wave generator in CubeMX
• How to Generate Code in CubeMX and use HAL functions

• Goal
• Configure DAC as wave generator in CubeMX and Generate Code
• Learn how start it in project
4.1.1 Use DAC as wave generator 319

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX DAC selection


• Select DAC OUT2
4.1.1 Use DAC as wave generator 320

• CubeMX DAC configuration


• TAB>Configuration>Analog>DAC>Parametr Settings
• Enable
Output buffer
• Button OK
4.1.1 Use DAC as wave generator 321

• 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
4.1.1 Use DAC as wave generator 322

• Start process DAC generation (same for DMA, ADC)


• Non blocking start process

Peripheral
Initializations

Start Process
HAL_DAC_Start

Set value
HAL_DAC_SetValue
4.1.1 Use DAC as wave generator 323

• 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
• and /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags

• For DAC start use function


• HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)

• DAC set DAC value


• HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment,
uint32_t Data)
4.1.1 Use DAC as wave generator 324

• Solution
• DAC setup and start
/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
/* USER CODE END 2 */

• Create the wave


/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
if(value_dac>4095)
{
value_dac=0;
}
HAL_Delay(1);

}
/* USER CODE END 3 */
4.2.1 ADC Poll lab

STM32F42xx Technical Training 02/08/2016


4.2.1 Use ADC in polling mode 326

• Objective
• Use the DAC part from previous lab
• Learn how to setup ADC in CubeMX
• How to Generate Code in CubeMX and use HAL functions

• Goal
• Configure ADC in poll in CubeMX and Generate Code
• Learn how to start ADC and measure the DAC
• Verify the measured wave in STMStudio
(http://www.st.com/web/en/catalog/tools/PF251373 require JAVA)
4.2.1 Use ADC in polling mode 327

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX DAC selection


• Select DAC OUT2
• Select ADC IN13
4.2.1 Use ADC in polling mode 328

• Hardware connection
• Connect DAC out2 PA5 and ADC1 IN13 PC3 together
4.2.1 Use ADC in polling mode 329

• CubeMX ADC configuration


• TAB>Configuration>Analog>ADC1>Parametr Settings
• Set ADC1
• Set sampling time
for CH13
• Button OK

• DAC from previous


example
4.2.1 Use ADC in polling mode 330

• 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
4.2.1 Use ADC in polling mode 331

• Start process ADC(same for DMA, DAC, TIM)


• Non blocking start process

Peripheral
Initializations

Start Process
HAL_DAC_Start

Peripheral
Initializations

Start Process
HAL_DAC_Start
4.2.1 Use ADC in polling mode 332

• 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
• and /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags

• For DAC start use function


• HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
• HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
• HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)

• DAC functions
• HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
• HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment,
uint32_t Data)
4.2.1 Use ADC in polling mode 333

• Solution
• Variables
/* USER CODE BEGIN PV */
uint32_t value_adc;
uint32_t value_dac=0;
/* USER CODE END PV */

• DAC setup and start


/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
/* USER CODE END 2 */
4.2.1 Use ADC in polling mode 334

• Solution
• Main loop with DAC set and ADC set
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,10);
value_adc=HAL_ADC_GetValue(&hadc1);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
value_dac++;
if(value_dac>4095){
value_dac=0;
}
HAL_Delay(1);
}
/* USER CODE END 3 */
4.2.1 Use ADC in polling mode 335

• Test the functionality


• We need the address of variable value_adc
• This can be found usually in debug mode in watch, my address is
0x2000005C (depends on compilator and optimizations)

• Start the STMStudio


• Set the ST Link SWD
• Right click into
Display variable
settings
• Select NEW
4.2.1 Use ADC in polling mode 336

• STM studio settings


• Set value_adc address
my 0x2000005C
• Set 16bit unsigned val
• Right click on this line
• Select Send To
VarViewer1
4.2.1 Use ADC in polling mode 337

• STM studio settings


• Viewers settings is on bottom
• Set the correct upper value
to 4096(12bit)
4.2.1 Use ADC in polling mode 338

• STM studio settings


• Now press
green play button
• And you will see
content of
value_adc
4.2.2 ADC Interrupt lab

STM32F42xx Technical Training 02/08/2016


4.2.2 Use ADC with interrupt 340

• Objective
• Use the DAC part from previous lab
• Learn how to setup ADC with interrupt in CubeMX
• How to Generate Code in CubeMX and use HAL functions

• Goal
• Configure ADC in interrupt in CubeMX and Generate Code
• Learn how to start ADC and measure the DAC
• Verify the measured wave in STMStudio
(http://www.st.com/web/en/catalog/tools/PF251373 require JAVA)
4.2.2 Use ADC with interrupt 341

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX DAC selection


• Select DAC OUT2
• Select ADC IN13
4.2.2 Use ADC with interrupt 342

• CubeMX ADC configuration


• TAB>Configuration>Analog>ADC1>Parametr Settings
• Set ADC1
• Set sampling time
for CH13
• Button OK

• DAC from previous


example
4.2.2 Use ADC with interrupt 343

• CubeMX ADC configuration


• TAB>NVIC settings
• Enable ADC1 interrupt
• Button OK
4.2.2 Use ADC with interrupt 344

• CubeMX NVIC configuration


• Because we want use the
Systick for delay in interrupt
The ADC interrupt priority must
be changed
• TAB>Configuration>System>NVIC
• Change ADC1
preemption priority
to 1
4.2.2 Use ADC with interrupt 345

• 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
4.2.2 Use ADC with interrupt 346

HAL Library ADC with IT flow


ADCInitializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_ADC_Start_IT

HAL_OK HAL_ERROR HAL_BUSY HAL_ADC_IRQHandler ADC_IRQHandler

process callback
HAL_ADC_ConvCpltCallback

process Error callback


HAL_ADC_ErrorCallback
4.2.2 Use ADC with interrupt 347

• 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
• and /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags

• For DAC start use function


• HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc, uint32_t Channel)
• HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)

• ADC complete callback function


• HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

• DAC functions
• HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
• HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment,
uint32_t Data)
4.2.2 Use ADC with interrupt 348

• Solution
• Variables
/* USER CODE BEGIN PV */
uint32_t value_adc;
uint32_t value_dac=0;
/* USER CODE END PV */

• DAC setup and start ADC/DAC


/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
HAL_ADC_Start_IT(&hadc1);
/* USER CODE END 2 */
4.2.2 Use ADC with interrupt 349

• Solution
• ADC complete callback routine
/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
value_adc=HAL_ADC_GetValue(&hadc1);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
value_dac++;
if(value_dac>4095){
value_dac=0;
}
HAL_Delay(1);
HAL_ADC_Start_IT(&hadc1);
}
/* USER CODE END 4 */
4.2.2 Use ADC with interrupt 350

• STM studio settings


• Check functionality
again with
STMstudio
4.2.3 ADC with DMA lab

STM32F42xx Technical Training 02/08/2016


4.2.3 Use ADC with DMA 352

• Objective
• Use the DAC part from previous lab
• Learn how to setup ADC with DMA in CubeMX
• How to Generate Code in CubeMX and use HAL functions

• Goal
• Configure ADC in DMA in CubeMX and Generate Code
• Learn how to start ADC and measure the DAC
• Verify the measured wave in STMStudio
(http://www.st.com/web/en/catalog/tools/PF251373 require JAVA)
4.2.3 Use ADC with DMA 353

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• CubeMX DAC selection


• Select DAC OUT2
• Select ADC IN13
4.2.3 Use ADC with DMA 354

• CubeMX ADC configuration


• TAB>Configuration>Analog>ADC1>Parameter Settings
• Set ADC1
• Set sampling time
for CH13
• DMA Continuous requests
• Button OK

• DAC from previous


example
4.2.3 Use ADC with DMA 355

• CubeMX ADC configuration


• TAB>DMA Settings
• Button ADD
• DMA request ADC1
• Peripheral to memory direction
• Circular mode
• Memory increment
• Half word data width
• Button OK
4.2.3 Use ADC with DMA 356

• 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
4.2.3 Use ADC with DMA 357

HAL Library ADC with DMA flow


ADCInitializations
including peripheral interrupt NVIC
initializations

Start process with interrupt generation at end


of process
HAL_ADC_Start_DMA

HAL_OK HAL_ERROR HAL_BUSY HAL_DMA_IRQHandler DMA1_StreamX_IRQHandler

process callback
HAL_ADC_ConvCpltCallback

process Error callback


HAL_ADC_ErrorCallback
4.2.3 Use ADC with DMA 358

• 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
• and /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags

• For DAC start use function


• HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)

• DAC functions
• HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
• HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment,
uint32_t Data)
4.2.3 Use ADC with DMA 359

• Solution
• Variables
/* USER CODE BEGIN PV */
uint32_t value_adc;
uint32_t value_dac=0;
/* USER CODE END PV */

• DAC setup and start ADC/DAC


/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&value_adc,1);
/* USER CODE END 2 */
4.2.3 Use ADC with DMA 360

• Solution
• ADC main routine
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
value_dac++;
if(value_dac>4095){
value_dac=0;
}
HAL_Delay(5);
HAL_ADC_Start(&hadc1);
HAL_Delay(5);
}
/* USER CODE END 3 */
4.2.3 Use ADC with DMA 361

• STM studio settings


• Check functionality
again with
STMstudio
5.1 BSP SDRAM lab

STM32F42xx Technical Training 02/08/2016


5.1 Use BSP for SDRAM initialization 363

• Objective
• Learn how import BSP into project
• Which part need to by configured in GUI
• Try to write data into SDRAM and read it

• Goal
• Successfully import BSP into your project
• Learn which part you need to import
• How to setup the project
5.1 Use BSP for SDRAM initialization 364

• Create project in CubeMX


• Menu > File > New Project
• Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx

• We need only blank project with clock initialization


• We only set the RCC and configure the core to maximum speed
5.1 Use BSP for SDRAM initialization 365

• 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
5.1 Use BSP for SDRAM initialization 366

• Now we have bank project


• For SDRAM we need to include more parts

Our project

BSP package

Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components

ili9341.c lis3dsh.c stmpe811.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 367

BSP SDRAM organization

Our project

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components

ili9341.c lis3dsh.c stmpe811.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 368

BSP SDRAM organization


1. include
Our project stm32f4xx_discovery_sdram.h

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components 2. include
stm32f4xx_discovery.h
ili9341.c lis3dsh.c stmpe811.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 369

BSP SDRAM organization

Our project

BSP package 3. include stm32f4xx_hal.h

Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components 4. stm32f4xx_hal.h include all


peripherals which are used in
ili9341.c lis3dsh.c stmpe811.c stm32f4xx_discovery.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 370

BSP SDRAM organization

Our project

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components 5. stm32f4xx_hal.h include all


peripherals which are used in
ili9341.c lis3dsh.c stmpe811.c stm32f4xx_discovery_sdram.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 371

BSP SDRAM organization

Our project 6. HAL_SDRAM_Init


is function from
7. HAL_SDRAM_Init stm32f4xx_discovery_sdram.c
BSP package
call functions from
stm32f4xx_II_fmc.c Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_sdram.c stm32f4xx_discovery_io.c

Components
8. stm32f4xx_II_fmc.c use
ili9341.c lis3dsh.c stmpe811.c functions from
stm32f4xx_hal_sdram.c

HAL API
stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c stm32f4xx_hal_sdram.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.1 Use BSP for SDRAM initialization 372

Source files Include files


user_code.h
user_code.c

User Code
stm32f4xx_it.c stm32f4xx_it.h

Stm32f4xx_discovery.c Stm32f4xx_discovery.h
BSP Drivers Stm32ffxx_discovery_sdram.h
Stm32ffxx_discovery_sdram.c

stm32f4xx_hal.h
stm32f4xx_hal_ppp.c
stm32f4xx_hal_ppp_ex.c
HAL Drivers stm32f4xx_hal_conf.h

stm32f4xx_hal.c
stm32f4xx_hal_ppp.h
stm32f4xx_hal_ppp_ex.h

stm32f4xx_hal_def.h
5.1 Use BSP for SDRAM initialization 373

• The copy part


• In our project in Drivers folder create folder BSP

• Now go into CubeMX repository …\STM32Cube_FW_F4_V1.3.0\Drivers\BSP\


• And copy Components and STM32F429I-Discovery into BSP folder

Copy folders into BSP


5.1 Use BSP for SDRAM initialization 374

• Now we need to add this files also in project


• Create BSP folder in project
• Right click on project in Workplace>ADD>Group
• Name it BSP
• Now right click on BSP>ADD>Files
• From Drivers\BSP\STM32F429I-Discovery\
add stm32f429i_discovery.c
and stm32f429i_discovery_sdram.c
5.1 Use BSP for SDRAM initialization 375

• The stm32f429i_discovery.c contains functions


for all components on discovery kit
(LCD, GYRO,…)
• Then we also need add into project HAL library
which handle their interface (I2C, SPI, … )
• Right click on STM32F4xx_HAL_Drive>ADD
from \Drivers\STM32F4xx_HAL_Driver\Src
• stm32f4xx_hal_i2c.c
• stm32f4xx_hal_spi.c
• stm32f4xx_hal_sdram.c
• stm32f4xx_II_fmc.c
5.1 Use BSP for SDRAM initialization 376

• Now add the include paths for this new files


• Right click on project>Options>Category C/C++Compiler>Preprocesor
• Into Defined symbols add USE_STM32F429I_DISCO
• This allow use BSP functions
• Into additional includes add
$PROJ_DIR$\..\..\Drivers\BSP\STM32F429I-Discovery
• Button OK
• Button OK close project options
5.1 Use BSP for SDRAM initialization 377

• Now last thing is allow to include


new HAL files which we added
• Open stm32f4xx_hal_conf.h in ..\Inc\
• Uncomment files which we added
• HAL_SDRAM_MODULE_ENABLED
• HAL_I2C_MODULE_ENABLED
• HAL_SPI_MODULE_ENABLED
5.1 Use BSP for SDRAM initialization 378

• Into main.c now we add include of stm32f429i_discovery_sdram.h


/* USER CODE BEGIN Includes */
#include "stm32f429i_discovery_sdram.h"
/* USER CODE END Includes */
• Now we can use the SDRAM init functions from BSP
/* USER CODE BEGIN 2 */
BSP_SDRAM_Init();
/* USER CODE END 2 */

• Now you can try to write into SDRAM area


In stm32f429i_discovery_sdram.h you can find where is the SDRAM
memory and how is their size
• SDRAM_DEVICE_ADDR ((uint32_t)0xD0000000)
• SDRAM_DEVICE_SIZE ((uint32_t)0x800000) /* SDRAM device size in MBytes */
5.1 Use BSP for SDRAM initialization 379

• SDRAM test
/* USER CODE BEGIN PV */
volatile uint32_t value;
/* USER CODE END PV */

/* USER CODE BEGIN 2 */


BSP_SDRAM_Init();
*((uint32_t*)SDRAM_DEVICE_ADDR)=0x12345678;
value=*((uint32_t*)SDRAM_DEVICE_ADDR);
/* USER CODE END 2 */
5.2 BSP LCD lab

STM32F42xx Technical Training 02/08/2016


5.2 Use BSP for LCD init and writing 381

• Objective
• Learn how import BSP LCD into project
• Because the LCD use the SDRAM we use project from lab 25
• Which part need to by configured in GUI
• Try to write text on LCD

• Goal
• Successfully import BSP LCD into your project
• Learn which part you need to import
• How to setup the project
5.2 Use BSP for LCD init and writing 382

BSP LCD organization

Our project

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_lcd.c stm32f4xx_discovery_sdram.c

Components Utilities

ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_II_fmc.c stm32f4xx_hal_i2c.c
5.2 Use BSP for LCD init and writing 383

BSP LCD organization


1. include
Our project stm32f4xx_discovery_lcd.h

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_lcd.c stm32f4xx_discovery_sdram.c

Components 2. stm32f4xx_discovery_lcd.h
Utilities
use the discovery BSP and
ili9341.c lis3dsh.c stmpe811.c SDRAM Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_II_fmc.c stm32f4xx_hal_i2c.c
5.2 Use BSP for LCD init and writing 384

BSP LCD organization

Our project

3. stm32f4xx_discovery_lcd.h
BSP package
use driver ili9341.c which is
TFT LCD controller Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_lcd.c stm32f4xx_discovery_sdram.c

Components Utilities
4. stm32f4xx_discovery_lcd.h
use LCTD controller from HAL
ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_II_fmc.c stm32f4xx_hal_i2c.c
5.2 Use BSP for LCD init and writing 385

BSP LCD organization

Our project

BSP package
Discovery drivers

stm32f4xx_discovery.c stm32f4xx_discovery_lcd.c stm32f4xx_discovery_sdram.c

Components Utilities
2. stm32f4xx_discovery_lcd.h
ili9341.c lis3dsh.c stmpe811.c Fonts
use fonts from utility folder

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_II_fmc.c stm32f4xx_hal_i2c.c
5.2 Use BSP for LCD init and writing 386

• We use the project from BSP SDRAM


because the LCD also use the SDRAM
• We need copy the Fonts from Utilities
folder in CubeMX repository
5.2 Use BSP for LCD init and writing 387

• The copy part


• In our project in Drivers folder create folder Utilities

• Now go into CubeMX repository …\STM32Cube_FW_F4_V1.3.0\Utilities\


• And copy Fonts into Utilities folder

Copy folders into


Utilities
5.2 Use BSP for LCD init and writing 388

• We add the driver for LCD from HAL


• Right click on STM32F4xx_HAL_Drive>ADD
from \Drivers\STM32F4xx_HAL_Driver\Src
• stm32f4xx_hal_ltdc.c
• Stm32f4xx_hal_dma2d.c
5.2 Use BSP for LCD init and writing 389

• We add the driver for BSP LDC


• Right click on BSP>ADD from \Drivers\BSP\STM32F429I-Discovery\
• stm32f429i_discovery_lcd.c

• Right click on BSP>ADD from \Drivers\BSP\Components\ili9341\


• ili9341.c
5.2 Use BSP for LCD init and writing 390

• Now last thing is allow to include


new HAL files which we added
• Open stm32f4xx_hal_conf.h in ..\Inc\
• Uncomment files which we added
• HAL_DMA2D_MODULE_ENABLED
• HAL_LTDC_MODULE_ENABLED
5.2 Use BSP for LCD init and writing 391

• Into main.c now we modify include from


stm32f429i_discovery_sdram.h to stm32f429i_discovery_lcd.h
/* USER CODE BEGIN Includes */
#include "stm32f429i_discovery_lcd.h"
/* USER CODE END Includes */

• And remove the BSP_SDRAM_Init()


/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
5.2 Use BSP for LCD init and writing 392

• Simple LCD demonstration


/* USER CODE BEGIN 2 */
BSP_LCD_Init();//init LCD
//set the layer buffer address into SDRAM
BSP_LCD_LayerDefaultInit(1, SDRAM_DEVICE_ADDR);
BSP_LCD_SelectLayer(1);//select on which layer we write
BSP_LCD_DisplayOn();//turn on LCD
BSP_LCD_Clear(LCD_COLOR_BLUE);//clear the LCD on blue color
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);//set text background color
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);//set text color
//write text
BSP_LCD_DisplayStringAtLine(2,"Cube STM32");
BSP_LCD_DisplayStringAtLine(3,"BSP");
BSP_LCD_DisplayStringAtLine(4,"LCD DEMO");
/* USER CODE END 2 */
5.3 BSP EEPROM lab

STM32F42xx Technical Training 02/08/2016


5.3 Use BSP to access EEPROM 394

• Objective
• Learn how import BSP EEPROM into project
• We use the project from lab 26
• Which part need to by configured in GUI
• Try to write text into EEPROM and read it
• Read text from EEPROM and display it on LCD

• Goal
• Successfully import BSP EEPROM drivers into your project
• Learn which part you need to import
• How to setup the project
5.3 Use BSP to access EEPROM 395

BSP EEPROM organization

Our project

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_eeprom.c
stm32f4xx_discovery.c

Components Utilities

ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.3 Use BSP to access EEPROM 396

BSP EEPROM organization


1. Use functions from
Our project
stm32f4xx_discovery_eeprom.c

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_eeprom.c
stm32f4xx_discovery.c

Components Utilities

ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.3 Use BSP to access EEPROM 397

BSP SDRAM organization


2. stm32f4xx_discovery_eeprom.c
call functions from Our project
stm32f4xx_discovery_io.c

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_eeprom.c
stm32f4xx_discovery.c

3. stm32f4xx_discovery_io.c call
Components functions from Utilities

ili9341.c lis3dsh.c stmpe811.c


stm32f4xx_discovery.c
pin init and write/read functions Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.3 Use BSP to access EEPROM 398

BSP EEPROM organization

Our project

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_eeprom.c
stm32f4xx_discovery.c

Components 4. stm32f4xx_discovery.c Utilities


Use stm32f4xx_hal_i2c to
ili9341.c lis3dsh.c stmpe811.c Fonts
commutate with eeprom memory

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.3 Use BSP to access EEPROM 399

• We use the project from BSP LCD lab 26


because we want to display the memory
content on LCD
5.3 Use BSP to access EEPROM 400

• We add the driver for BSP LDC


• Right click on BSP>ADD from \Drivers\BSP\STM32F429I-Discovery\
• stm32f429i_discovery_eeprom.c
• stm32f429i_discovery_io.c
5.3 Use BSP to access EEPROM 401

• Add the define of EEPROM into project options


• Right click on project>Options>Category C/C++Compiler>Preprocesor
• Into Defined symbols add EE_M24LR64
• This allow use EEPROM functions
• Button OK close project options
5.3 Use BSP to access EEPROM 402

• Use the ATM7-M24LR-A board with M24LR memory


and connect it into STM32F429i-Discovery kit
5.3 Use BSP to access EEPROM 403

• Into main.c now modify include


/* USER CODE BEGIN Includes */
#include "stm32f429i_discovery_lcd.h"
#include "stm32f429i_discovery_io.h"
#include "stm32f429i_discovery_eeprom.h"
#include <string.h>
/* USER CODE END Includes */

• Define variables
/* USER CODE BEGIN PV */
uint8_t text_to_write[]="test text";//write to eeprom
uint8_t text_to_read[200];//read from eeprom
uint32_t address=0;//address in eeprom
uint16_t read_num=1;//number of bytes which we want to read from
eeprom
/* USER CODE END PV */
5.3 Use BSP to access EEPROM 404

• Into stm32f4xx_hal_it.c add global variable for i2c handle


/* USER CODE BEGIN 0 */
extern I2C_HandleTypeDef I2cHandle;
/* USER CODE END 0 */

• and define handler functions for I2C DMA


/* USER CODE BEGIN 1 */
void DMA1_Stream4_IRQHandler()
{
HAL_DMA_IRQHandler(I2cHandle.hdmatx);
}

void DMA1_Stream2_IRQHandler()
{
HAL_DMA_IRQHandler(I2cHandle.hdmarx);
}
/* USER CODE END 1 */
5.3 Use BSP to access EEPROM 405

• Into main.c add


/* USER CODE BEGIN 2 */
/*LCD init*/
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(1, SDRAM_DEVICE_ADDR);
BSP_LCD_SelectLayer(1);
BSP_LCD_DisplayOn();
BSP_LCD_Clear(LCD_COLOR_BLUE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);

/*EEPROM init*/
BSP_EEPROM_Init();
/*Write text into EEPROM*/
BSP_EEPROM_WriteBuffer(text_to_write,0,(strlen(text_to_write)+1));
/*Read text from EEPROM*/
do{
BSP_EEPROM_ReadBuffer((uint8_t*)&(text_to_read[address]),address,(uint16_t*)&read_num);
}while(text_to_read[address++]!=0x0);
/*Display text*/
BSP_LCD_DisplayStringAtLine(2,text_to_read);
/* USER CODE END 2 */
5.4 BSP GYRO lab

STM32F42xx Technical Training 02/08/2016


5.4 Use BSP to access GYROSCOPE 407

• Objective
• Learn how import BSP GYROSCOPE into project
• We use the project from lab 26
• Which part need to by configured in GUI
• Read data from GYROSCOPE and display it on LCD

• Goal
• Successfully import BSP GYROSCOPE drivers into your project
• Learn which part you need to import
• How to setup the project
5.4 Use BSP to access GYROSCOPE 408

BSP GYRO organization

Our project

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_gyroscope.c
stm32f4xx_discovery.c

Components Utilities

ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.4 Use BSP to access GYROSCOPE 409

BSP GYRO organization


1. Use functions from
Our project
stm32f4xx_discovery_eeprom.c

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_gyroscope.c
stm32f4xx_discovery.c

Components Utilities

ili9341.c lis3dsh.c stmpe811.c Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.4 Use BSP to access GYROSCOPE 410

BSP GYRO organization


2. stm32f4xx_discovery_gyroscope.c
call functions from Our project
stm32f4xx_discovery_io.c
BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_gyroscope.c
stm32f4xx_discovery.c

3. stm32f4xx_discovery_io.c call
Components functions from Utilities

ili9341.c lis3dsh.c stmpe811.c


stm32f4xx_discovery.c
pin init and read functions Fonts

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.4 Use BSP to access GYROSCOPE 411

BSP GYRO organization

Our project

BSP package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_gyroscope.c
stm32f4xx_discovery.c

Components 4. stm32f4xx_discovery.c Utilities


Use stm32f4xx_hal_spi to
ili9341.c lis3dsh.c stmpe811.c Fonts
commutate with gyroscope

HAL API
stm32f4xx_hal_sdram.c stm32f4xx_hal_spi.c
stm32f4xx_II_fmc.c
stm32f4xx_hal_ltdc.c stm32f4xx_hal.c
stm32f4xx_hal_i2c.c
5.4 Use BSP to access GYROSCOPE 412

• We use the project from BSP LCD lab 26


because we want to display gyro values
on LCD
5.4 Use BSP to access GYROSCOPE 413

• We add the driver for BSP LDC


• Right click on BSP>ADD from \Drivers\BSP\STM32F429I-Discovery\
• stm32f429i_discovery_gyroscope.c
• stm32f429i_discovery_io.c

• Right click on BSP>ADD from \Drivers\BSP\Components\


• l3gd20.c
5.4 Use BSP to access GYROSCOPE 414

• Into main.c now modify include


/* USER CODE BEGIN Includes */
#include "stm32f429i_discovery_lcd.h"
#include "stm32f429i_discovery_gyroscope.h"
#include "stm32f429i_discovery_io.h"
#include <stdio.h>
/* USER CODE END Includes */

• Define variables
/* USER CODE BEGIN PV */
float valxyz[3];//gyroscope values
uint8_t buffer[200];//text buffer
/* USER CODE END PV */
5.4 Use BSP to access GYROSCOPE 415

• Into main.c add


/* USER CODE BEGIN 2 */
/*LCD init*/
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(1, SDRAM_DEVICE_ADDR);
BSP_LCD_SelectLayer(1);
BSP_LCD_DisplayOn();
BSP_LCD_Clear(LCD_COLOR_BLUE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
/*Gyroscope init*/
BSP_GYRO_Init();
/* USER CODE END 2 */
5.4 Use BSP to access GYROSCOPE 416

• Into main.c add


/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
/*Get Gyrospope value*/
BSP_GYRO_GetXYZ(valxyz);
/*Display X*/
sprintf(buffer,"x:%f",valxyz[0]);
BSP_LCD_DisplayStringAtLine(2,buffer);
/*Display Y*/
sprintf(buffer,"y:%f",valxyz[1]);
BSP_LCD_DisplayStringAtLine(3,buffer);
/*Display Z*/
sprintf(buffer,"z:%f",valxyz[2]);
BSP_LCD_DisplayStringAtLine(4,buffer);
/*Delay*/
HAL_Delay(1000);
}
/* USER CODE END 3 */
Appendix A CubeMX install

STM32F42xx Technical Training 02/08/2016


A CubeMX install 418

• CubeMX tool
• http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1533/PF259242?s_searchtype=
partnumber

• The CubeMX tool need the java


• Please check if you have actual java on your pc, for sure 32bit and 64bit version

• Optionally you can download the Cube packages for STM32 device if you
don’t want to download them throe CubeMX
• STM32CubeL0
• STM32CubeL1
• STM32CubeF0
• STM32CubeF2
• STM32CubeF3
• STM32CubeF4
A CubeMX install 419

• Install the CubeMX


• After installation run CubeMX
• In case you download the package from web we need to find the place where
they need to be stored
• MENU>Help>Updater Settings…
• You will see where is the repository folder
• Default is C:/User/Acc_name/STM32Cube/Repository/

• You need to download STM32 packages into this folder


• Or CubeMX automatically download them into this folder
A CubeMX install 420

• The comparison of the CubeMX repository settings and structure in this folder

• In case you want to download this files


automatically use in CubeMX
• MENU>Help>Install New Libraries
• Select libraries which you want
• Force download with button Install Now
A CubeMX install 421

• For the code generation the CubeMX use the package from the Repository
folder
• The CubeMX can generate the code for some GUI
• Keil
• IAR
• Atollic

• For the debugging is necessity to have the ST-Link drivers


• STSW-LINK003 driver for Win XP/Vista/7
• STSW-LINK006 driver for Win 8

• For driver installation you will need the Admin rights on your PC
Appendix B Documents

STM32F42xx Technical Training 02/08/2016


B CubeMX documentation 423

• CubeMX user manual UM1718


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/user_manual/DM00104712.pdf

• CubeMX release note RN0094


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/user_manual/DM00104712.pdf

• CubeMX technical note TN0072


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/technical_note/CD00214439.pdf
B STM32F4 documentation 424

• STM32F429ZI web page


• http://www.st.com/web/en/catalog/mmc/FM141/SC1169/SS1577/LN1806/PF255419#

• STM32F429 Datasheet
• http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00071990.pdf

• STM32F429 Reference Manual


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/reference_manual/DM00031020.pdf

• STM32F429 Programming manual


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/programming_manual/DM00046982.pdf
STM32F429i-Discovery
B documentation
425

• STM32F429i-Discovery page
• http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1848/PF259090?s_searchtyp
e=keyword

• STM32F429i-Discovery user manual with discovery schematics


• http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/user_manual/DM00093903.pdf
www.st.com/stm32
STM32F42xx Technical Training 02/08/2016

You might also like