ST AN4062 APPLICATION NOTE

AN4062
Application note
STM32F0DISCOVERY peripheral firmware examples
Introduction
This application note describes the peripheral firmware examples provided for the STM32F0DISCOVERY Kit.
These ready-to-run examples are provided to help the user get started quickly with STM32F0xx peripherals and STM32F0DISCOVERY 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/stm32f0discovery.
Users are advised to first read the document Getting started with software and firmware environments for the STM32F0DISCOVERY Kit (UM1523) to familiarize themselves with the STM32F0DISCOVERY Kit.
Ta bl e 1 lists the microcontrollers and development tools concerned by this application note.

Table 1. Applicable products and tools

Type Applicable products
Microcontrollers STM32 F0 series Entry-level Cortex™-M0 microcontrollers
Development tools STM32F0DISCOVERY evaluation board and discovery kit
May 2012 Doc ID 022897 Rev 2 1/19
www.st.com
Contents AN4062
Contents
1 Peripheral firmware examples structure overview . . . . . . . . . . . . . . . . . 4
2 Clock configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 PLL_SOURCE_HSI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 PLL_SOURCE_HSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 PLL_SOURCE_HSE_BYPASS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Peripheral firmware examples description . . . . . . . . . . . . . . . . . . . . . . . 6
3.1 GPIO toggle example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 EXTI example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 SysTick example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Standby mode example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Stop mode example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6 Current consumption example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.7 Flash program example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8 IWDG (independent watchdog) example . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.9 ADC DMA example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.9.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2/19 Doc ID 022897 Rev 2
AN4062 Contents
3.9.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.10 ADC low power example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.10.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.10.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.11 DMA Flash RAM example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.11.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.11.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.12 DMA RAM DAC example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.12.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.12.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.13 DAC signal generation example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.13.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.13.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.14 TIM complementary signal example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.14.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.14.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.15 TIM time base example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.15.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.15.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.16 TIM PWM input example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.16.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.16.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.17 TIM PWM output example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.17.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.17.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.18 Timer ADC trigger example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.18.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.18.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.19 NVIC WFI mode example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.19.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.19.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.20 RCC (reset and clock control) example . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.20.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.20.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Doc ID 022897 Rev 2 3/19
Peripheral firmware examples structure overview AN4062

1 Peripheral firmware examples structure overview

The peripheral firmware examples are provided within the STM32F0DISCOVERY 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 022897 Rev 2
AN4062 Clock configuration

2 Clock configuration

The peripheral examples provided within STM32F0-Discovery Kit Firmware package are configured to run at 48 MHz, using HSI as the clock source.
However, the user can modify this configuration to use HSE (crystal or bypass mode) as the clock source, which needs some hardware modification on the discovery kit hardware.
The "system_stm32f0xx.c" file provided within each example was customized for use with the discovery kit, allowing the user to select one of the three configurations below (by un­commenting the adequate define).

2.1 PLL_SOURCE_HSI

The HSI clock signal is generated from an internal 8 MHz RC Oscillator and can be used directly as a system clock, or divided by 2 to be used as a PLL input.
The HSI RC oscillator has the advantage of providing a clock source at low cost (no external components). It also has a faster startup time than the HSE crystal oscillator. However, even with a calibration, the frequency is less accurate than an external crystal oscillator or a ceramic resonator.
Note: This is the default configuration.

2.2 PLL_SOURCE_HSE

The high speed external clock signal (HSE) can be generated from two possible clock sources:
HSE external crystal/ceramic resonator
HSE user external clock
The HSE crystal is not provided with the discovery kit. Some hardware modifications are necessary to connect this crystal.
For more details, refer to section "4.7 OSC clock" in "STM32F0 discovery kit User manual (UM1525)".

2.3 PLL_SOURCE_HSE_BYPASS

In this mode, the HSE is bypassed with an external clock (fixed at 8 MHz, coming from ST­Link circuit). It is used to clock the PLL, and the PLL is used as the system clock source.
Some hardware modifications are necessary to bypass the HSE with the clock coming from the ST-Link circuit.
For more details, refer to section "4.7 OSC clock" in "STM32F0 discovery kit User manual (UM1525)".
Doc ID 022897 Rev 2 5/19
Peripheral firmware examples description AN4062

3 Peripheral firmware examples description

3.1 GPIO toggle example

3.1.1 Purpose

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

3.1.2 Description

In this example:
GPIOC clock is enabled.
GPIOC pins 8 and 9 are configured.
In a while loop, the ODR8 and ODR9 bits are set in the GPIOC output data register
(ODR) by setting the corresponding bits in the port bit set/reset register (BSRR). Then the ODR8 and ODR9 bits are reset in the GPIOC output data register (ODR) by setting the corresponding bits in the port bit reset register (BRR).
A delay is inserted between setting and resetting the GPIOC ODR8 and ODR9 bits.
When the program is executed, the two LEDs, LED3 and LED4, are turned ON then OFF in an infinite loop. The duration between the ON and OFF states corresponds to the inserted delay.

3.2 EXTI example

3.2.1 Purpose

This example shows how to configure an external interrupt line.

3.2.2 Description

In this example:
PA0 pin is configured in input floating.
PA0 is configured to be used as an 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), LED3 and LED4 connected to PC9 and PC8 are toggled.
6/19 Doc ID 022897 Rev 2
Loading...
+ 13 hidden pages