Programming - Minimum C program

Created:2018-01-20  Last modified:2018-02-08


  1. Introduction

    The example in this note is generated by STM32CubeMX, but it is cut by removing the unnessary header files. Under the STM32CubeMX toolchain/IDE, select the Makefile option, and only copy the necessary library files.

    * The makefile generated by STM32CubeV1.0 version 4.23.0 has some mistakes, it has duplicated .c file in the src_dir.

    The example in this note is just toggle the nucleo-64 on board user led. It does not involve any interrupt.

    Download the source code

    The files contains a Makefile, a include directory and a source directory. Host OS is ubuntu 16.04LTS.

  2. File organization

    1. include/core_cm3.h: define the Cortex-M3 core, NIVC, debug and core components.
    2. include/cmsis_gcc.h: define __asm assembly instructions that can be realized by gcc.
    3. include/stm32f10x.h: define stm32f10x series peripheral registers and data structures for accessing them.
    4. include/system_stm32f10x.h: define the function signature for system clock configuration.
    5. Src/main.c: the main program.
    6. Src/system_stm32f10x.c: implement the functions defined in system_stm32f10x.h
    7. Src/startup_stm32f103xb.s: define the reset handler, which is the entry point after reset. (The entry point for the device is the reset handler, not the main function. This is true for all MCU, includes the atmega328p).
    8. Src/STM32F103RBTx_FLASH.ld: a linker script that used by the linker to link the program at correct position. For example, it declares a memory section called flash that starts from 0x08000000 and has 128K length. Moreover, it puts the .isr_vector, .text (code) into the section.

    * The core_cm3.h and stm32f10x.h are slightly different from the source file provided by stm32Cube, it excludes those unnecessary header files.

    The most necessary files are marked with green background.