ST AN3983 APPLICATION NOTE

AN3983
Application note
STM32F4DISCOVERY peripheral firmware examples
Introduction
This application note describes the peripheral firmware examples provided for the STM32F4DISCOVERY Kit.
These ready-to-run examples are provided to help the user get started quickly with STM32F4xx peripherals and STM32F4DISCOVERY board hardware. Preconfigured projects for EWARM, MDK-ARM, TrueSTUDIO and TASKING toolchains are provided for each example.
These examples are included in the firmware applications package available for download on www.st.com/stm32f4-discovery.
Users are advised to first read the document Getting started with software and firmware environments for the STM32F4DISCOVERY Kit (UM1467) to familiarize themselves with the STM32F4DISCOVERY Kit.
September 2011 Doc ID 022237 Rev 2 1/19
www.st.com
Contents AN3983
Contents
1 Peripheral firmware examples structure overview . . . . . . . . . . . . . . . . . 4
2 Peripheral firmware examples description . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 GPIO toggle example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 EXTI example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 SysTick example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Standby mode example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Stop mode example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Current consumption example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Flash program example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Flash write protection example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.9 IWDG (independent watchdog) example . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.9.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.9.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.10 ADC interleaved DMA mode 2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2/19 Doc ID 022237 Rev 2
AN3983 Contents
2.11 ADC DMA example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.11.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.11.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.12 MEMS accelerometer (LIS302DL) example . . . . . . . . . . . . . . . . . . . . . . . 12
2.12.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.12.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.13 RCC (reset and clock control) example . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.13.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.13.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.14 DMA Flash RAM example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.14.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.14.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.15 DAC signals generation example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.15.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.15.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.16 TIM complementary signals example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.16.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.16.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.17 TIM time base example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.17.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.17.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.18 TIM PWM input example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.18.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.18.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.19 TIM PWM output example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.19.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.19.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Doc ID 022237 Rev 2 3/19
Peripheral firmware examples structure overview AN3983

1 Peripheral firmware examples structure overview

The peripheral firmware examples are provided within the STM32F4DISCOVERY firmware applications package and are located in the \Project folder as shown in Figure 1.

Figure 1. Hardware environment

1. VX.Y.Z refer to the package version, for example, V1.0.0.
To run an example, open the project with your preferred toolchain, compile, load and run it.
Some examples may require additional hardware such as an oscilloscope. For further detail about the required hardware, refer to the readme file provided within each example.
4/19 Doc ID 022237 Rev 2
AN3983 Peripheral firmware examples description

2 Peripheral firmware examples description

2.1 GPIO toggle example

2.1.1 Purpose

This example shows how to use the GPIO port bit set/reset registers (BSRRL and BSRRH) for I/O toggling.

2.1.2 Description

In this example:
GPIOD clock is enabled.
GPIOD pins 12, 13, 14 and 15 are configured.
In a while loop, the ODR12, ODR13, ODR14 and ODR15 bits are set in the GPIOD
output data register (ODR) by setting the corresponding bits in the port bit set/reset register (BSRRL). Then the ODR12, ODR13, ODR14 and ODR15 bits are reset in the GPIOD output data register (ODR) by setting the corresponding bits in the port bit reset register (BSRRH).
A delay is inserted between setting and resetting the GPIOD ODR12, ODR13, ODR14
and ODR15 bits.
When the program is executed, the four LEDs LD3, LD4, LD5 and LD6 are turned ON then OFF in an infinite loop. The duration between the ON and OFF states corresponds to the inserted delay.

2.2 EXTI example

2.2.1 Purpose

This example shows how to configure an external interrupt line.

2.2.2 Description

In this example:
PA0 pin is configured in input floating.
PA0 is configured to be used as external interrupt source line 0 (EXTI0).
The EXTI line 0 is configured to generate an interrupt on each rising edge detected on
the PA0 pin. The interrupt is triggered every time the User push button is pressed.
In the NVIC (nested vectored interrupt controller), the EXTI line 0 interrupt priority is
configured and the interrupt is enabled.
When the program is executed and the user pushes on the User push button (EXTI0 interrupt routine), the LED LD4 connected to PD12 is toggled.
Doc ID 022237 Rev 2 5/19
Peripheral firmware examples description AN3983

2.3 SysTick example

2.3.1 Purpose

This example shows how to configure system tick timer and use it to generate a 1 ms time base.

2.3.2 Description

In this example:
The system tick timer is initialized.
The system tick timer interrupt is enabled in the NVIC.
The system tick timer/counter starts in free running mode to generate periodical
interrupts.
The system tick timer interrupt is triggered every 1 ms.
A Delay function is implemented based on the system tick timer end-of-count event.
The four LEDs LD3, LD4, LD5 and LD6 are toggled with a timing defined by the Delay function.

2.4 Standby mode example

2.4.1 Purpose

This example shows how to put the system in Standby mode and wake it up from this mode using the external reset, RTC Alarm A or WKUP pin.

2.4.2 Description

In this example:
The system tick timer is initialized.
The system tick timer interrupt is enabled in the NVIC.
The system tick timer/counter starts in free running mode to generate periodical
interrupts. The system tick timer interrupt is triggered every 250 ms. The LD4 is toggled, indicating whether the MCU is in Standby or Run mode.
The EXTI line 1 is configured to generate an interrupt on each rising/falling edge
detected on the PA1 pin. The external interrupt is generated every time the PA1 changes the level (GND or VDD).
When a falling or rising edge is detected on the EXTI line, an interrupt is
generated. In the EXTI handler routine, the RTC is configured to generate an Alarm event in 5 seconds, after which the system enters Standby mode causing the LD4 to stop toggling.
A rising edge on WKUP pin (User push button) or an external reset will wakeup
the system from Standby. If neither rising edge on WKUP pin (User push button) nor external RESET are generated within 5 seconds, the RTC Alarm A will wakeup the system.
6/19 Doc ID 022237 Rev 2
Loading...
+ 13 hidden pages