Keil™, An ARM® Company

MCBSTM32 User's Guide

Using STM32_Init.c

The file STM32_Init.c is part of every example. It contains the initialization of the clocks, independent watchdog (IWDG), system timer (SysTick), realtime clock (RTC), Timers, USARTs, general purpose I/O (GPIO) and embedded Flash interface (EFI).

The Configuration Wizard makes it easy to configure these peripherals.

File STM32_Init.c provides the function void stm32_Init (void) which initializes the peripherals according the selected configuration. Call stm32_Init() at the beginning of your int main (void) function.

/*------------------------------------------------------------------------
  MAIN function
 *----------------------------------------------------------------------*/
int main (void) {

  stm32_Init ();                                  // STM32 setup

  // ... add a your code here

  while (1) {                                     // Loop forever

    // ... add your code here

  } // end while
} // end main

The following peripherals may be configured: Clock Configuration

  • Configures the settings of clock control register (RCC_CR).
  • Configures the settings of clock configuration register (RCC_CFGR).
  • Configures the External High Speed Clock (HSE) frequency.

Independent Watchdog Configuration

  • Configures the IWDG period in µs.

System Timer Configuration

  • Configures the SysTick clock source.
  • Configures the SysTick period in ms.
  • Enables the SysTick interrupt.

Realtime clock Configuration

  • Configures the RTC clock source.
  • Configures the RTC period in ms.
  • Sets the RTC time value.
  • Sets the RTC alarm time.
  • Enables the different RTC interrupts.

Timer Configuration

For each timer (TIM1..TIM4) it is possible to change the following settings:

  • Configures the timer period in ms.
  • Configures the repetition counter (only available for TIM1).
  • Selects the timers' detailed settings.
  • Enables the different timer interrupts.

Under detailed settings it is possible to set the contents of the single timer register. If detailed settings is checked than the settings for period and repetition counter are ignored.

USART Configuration

For each USART (USART1..USART3) it is possible to change the following settings:

  • Configures the Baudrate.
  • Configures the data bit length.
  • Configures stop bit length.
  • Configures parity.
  • Configures flow control.
  • Configures the pins used for receive and transmit.
  • Enables the different timer interrupts.

Tamper Configuration

For Tamper detection it is possible to change the following settings:

  • Tamper Pin enable.
  • Tamper pin active level.
  • Tamper interrupt enable.

External Interrupt/Event Configuration

For each external line (EXTI0..EXTI15) it is possible to change the following settings:

  • Interrupt enable.
  • Generate interrupt.
  • Generate event.
  • Use rising trigger for interrupt/event.
  • Use falling trigger for interrupt/event.
  • Select pin for for interrupt/event.

Alternate Function Remap Configuration

For some peripherals it is possible to remap the used pins:

  • SPI1 remapping.
  • I2C1 remapping.
  • USART1 remapping.
  • USART2 remapping.
  • USART3 remapping.
  • TIM1 remapping.
  • TIM2 remapping.
  • TIM3 remapping.
  • TIM4 remapping.
  • CAN remapping.
  • PD01 remapping.

General Purpose I/O Configuration

For each GPIO register (GPIOA..GPIOE) change the configuration of every single pin.

Embedded Flash Configuration

  • Change the settings of the Flash Access Control register (FLASH_ACR).

Note

  • It is possible to add the configuration of additional peripherals to STM32_Init.c. You just have to add a Configuration Wizard section and a dedicated initialisation function for the peripheral you want to add. You need to call the new initialization function in stm32_Init().

    See the Configuration Wizard page in the µVision User's Guide for more information about working with the Configuration Wizard.