Professional Documents
Culture Documents
STM32F4 Labs
STM32F4 Labs
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
• 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
• 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
HIGH HIGH
LOW LOW
HIGH HIGH
1.1.1 Configure GPIO for LED toggling 19
• 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
• 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
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
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
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
Peripheral Initializations
including peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler EXTI0_IRQHandler
Peripheral Initializations
including peripheral interrupt NVIC
initializations
HAL_EXTI0_IRQHandler EXTI0_IRQHandler
HAL_EXTI0_IRQHandler EXTI0_IRQHandler
• 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
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
HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI);
}
/* USER CODE END 3 */
1.2.1 Use SLEEP mode with EXTI 46
• Is this better?
1.2.2 Low Power mode STOP lab
• 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
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
• 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
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
To be able to reprogram
the STM32 which is in
LP mode, use
connection during
reset option
1.2.3 Use STANDBY mode 59
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* USER CODE END 2 */
1.3.1 Data transfer over DMA lab
• 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
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
• 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
1. init DMA
Peripheral
Initializations
2. Start DMA
Start Process
(HAL_DMA_Start)
3. DMA transfer data
• 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
• 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
• 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
• 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
• 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
3. Enable DMA2
interrupts
4. OK
1.3.2 Use DMA M2M with interrupt 83
2. DMA transfer
Start process with interrupt generation at end
of process
start
HAL_DMA_Start_IT
• 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
• 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
• 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
• TAB>Configuration>System>
>GPIO
• Set the HIGH output speed
Button OK
2.1.1 Simple UART communication 113
HAL_UART_Init(&huart1);
HAL_UART_MspInit callback
MX_USART1_UART_Init();
HAL_UART_Init(&huart1);
HAL_UART_MspInit callback
MX_USART1_UART_Init();
CubeMX create for us
function which handle
Init UART1 structure UART initialization
HAL_UART_Init(&huart1);
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
HAL_UART_MspInit callback
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
HAL_UART_MspInit callback
HAL_UART_Init(&huart1);
Function wrote
parameters from structure
HAL_UART_MspInit callback into UART1 registers
Init GPIO and NVIC for UART 6. UART1 GPIOS, NVIC and RCC init
2.1.1 Simple UART communication 125
Init GPIO and NVIC for UART 6. UART1 GPIOS, NVIC and RCC init
Created by user
Peripheral Initializations
Created by user
Peripheral Initializations
• Transmit solution
• Create data structure for data
• Receive solution
• Create data structure for data
• 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
• 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
2. UART1 send
Start process with interrupt generation at end
of process
buffer
HAL_UART_Transmit_IT
• 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 */
• 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
• 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
Defined by user
process Error callback
HAL_UART_ErrorCallback
2.1.3 Use UART with DMA transfer 177
Process interrupt
Start process with DMA end of transfer information from DMA and
interrupt generation at end of process UART
HAL_UART_Receive_DMA
• 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 */
• 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
• Pin selection
• We are looking for free pins where is possible to create wire loopback connection
2.2.1 Simple SPI communication 193
• Hardware preparation
• Connect PA6 and PA7 together with jumper
Hardware
loopback
2.2.1 Simple SPI communication 195
• Tab>Configuration>System>
>GPIO
• Tab>SPI1
• Seth High output speed
• Button OK
2.2.1 Simple SPI communication 198
Peripheral Initializations
• 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
• Hardware preparation
• Connect PA6 and PA7 together with jumper
Hardware
loopback
2.2.2 Use SPI with interrupt 206
• 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 */
• 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
• Hardware preparation
• Connect PA6 and PA7 together with jumper
Hardware
loopback
2.2.3 Use SPI with DMA transfer 218
• 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 */
• 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
• 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
• 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 */
• 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
• 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
• 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
• 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 */
• Slave receive
/* USER CODE BEGIN 2 */
HAL_I2C_Slave_Receive_IT(&hi2c3,data_rx,10);
/* USER CODE END 2 */
• 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
• 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
• 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
3. Button OK
3.1.1 Use RTC and Alarm with interrupt 254
• 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 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();
• 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
• 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
process callback
HAL_TIM_PeriodElapsedCallback
• TIM callback
• void TIM1_UP_TIM10_IRQHandler(void)
• 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
• 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
Peripheral
Initializations
Start Process
HAL_TIM_PWM_Start
3.2.2 Use TIM with PWM output 275
• 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
• 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
Peripheral Initializations
including DMA stream initializations
• 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
process callback
HAL_TIM_PeriodElapsedCallback
• TIM callback
• void TIM1_UP_TIM10_IRQHandler(void)
• 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
• 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
We refresh the
WWDG to 127
In our case 80
Fixed 63
3.3.1 Use WWDG 306
• Solution
• WWDG Start
• 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
• 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
• Solution
• IWDG Start
• 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
• 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
• 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
Peripheral
Initializations
Start Process
HAL_DAC_Start
Set value
HAL_DAC_SetValue
4.1.1 Use DAC as wave generator 323
• Solution
• DAC setup and start
/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
/* USER CODE END 2 */
}
/* USER CODE END 3 */
4.2.1 ADC Poll lab
• 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
• Hardware connection
• Connect DAC out2 PA5 and ADC1 IN13 PC3 together
4.2.1 Use ADC in polling mode 329
Peripheral
Initializations
Start Process
HAL_DAC_Start
Peripheral
Initializations
Start Process
HAL_DAC_Start
4.2.1 Use ADC in polling mode 332
• 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 */
• 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
• 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
process callback
HAL_ADC_ConvCpltCallback
• 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 */
• 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
• 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
process callback
HAL_ADC_ConvCpltCallback
• 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 */
• 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
• 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
Our project
BSP package
Discovery drivers
Components
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
Our project
BSP package
Discovery drivers
Components
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 package
Discovery drivers
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
Our project
Discovery drivers
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
Our project
BSP package
Discovery drivers
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
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
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
• SDRAM test
/* USER CODE BEGIN PV */
volatile uint32_t value;
/* USER CODE END PV */
• 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
Our project
BSP package
Discovery drivers
Components Utilities
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 package
Discovery drivers
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
Our project
3. stm32f4xx_discovery_lcd.h
BSP package
use driver ili9341.c which is
TFT LCD controller Discovery drivers
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
Our project
BSP package
Discovery drivers
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
• 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
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
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 package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_eeprom.c
stm32f4xx_discovery.c
Components Utilities
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 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
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
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
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
• 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
void DMA1_Stream2_IRQHandler()
{
HAL_DMA_IRQHandler(I2cHandle.hdmarx);
}
/* USER CODE END 1 */
5.3 Use BSP to access EEPROM 405
/*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
• 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
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
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 package
Discovery drivers stm32f4xx_discovery_lcd.c
stm32f4xx_discovery_sdram.c
stm32f4xx_discovery_io.c stm32f4xx_discovery_gyroscope.c
stm32f4xx_discovery.c
Components Utilities
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
3. stm32f4xx_discovery_io.c call
Components functions from Utilities
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
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
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
• 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
• CubeMX tool
• http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1533/PF259242?s_searchtype=
partnumber
• 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
• The comparison of the CubeMX repository settings and structure in this folder
• 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 driver installation you will need the Admin rights on your PC
Appendix B Documents
• STM32F429 Datasheet
• http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00071990.pdf
• STM32F429i-Discovery page
• http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1848/PF259090?s_searchtyp
e=keyword