Difference between revisions of "STM32"

From ElectroDragon
Jump to navigation Jump to search
(MDK-ARM Setup by STD LIB)
(MDK-ARM Setup by STD LIB)
Line 46: Line 46:
| User main and customized interrupt ||  
| User main and customized interrupt ||  
* .\user\main.c;  
* .\user\main.c;  
* (replaced) .\user\system_stm32l1xx.c;
* .\user\stm32l1xx_it.c;  
* .\user\stm32l1xx_it.c;  
* .\user\main.h;  
* .\user\main.h;  
* (replaced) .\user\stm32l1xx_conf.h;  
* .\user\stm32l1xx_conf.h;  
* .\user\stm32l1xx_it.h  
* .\user\stm32l1xx_it.h  
|| -
|| -
Line 59: Line 59:
| Device system & Startup (replace by Device module)  
| Device system & Startup (replace by Device module)  
|| ||  
* (replaced) .\user\system_stm32l1xx.c;
* '''CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h; (conflict and still need)'''
* '''CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h; (conflict and still need)'''
* CMSIS\Device\ST\STM32L1xx\system_stm32l1xx.h  
* CMSIS\Device\ST\STM32L1xx\system_stm32l1xx.h  

Revision as of 07:03, 6 June 2019

STM32 Toolchain

Hardware Interface

Stm32 serial wiring 02.jpg


  • In arduino IDE install arduino SAM board first ("for arduino zero"), so toolchain "arm-none-eabi-g++ " installed
  • Git clone into arduino\hardware folder
Github Support hardward Support software
STM32 Arduino F1, F3, F4
Arduino_Core_STM32 F0~F7, L0~L4
  • Hardware programmer use ST-LINK, install driver, you can find it here
  • Connect with our STM32F103C8T6 board, upload blink sketch onto PB12 pin.
  • Support arduino library - here

MDK-ARM Setup by STM32CubeMx

  • Install MDK-ARM lastest version
  • Install software pack for platform, for example STM32F030xxx
  • Install STM32Cube
  • Follow the following animation, open to watch

Code add this two lines for blinking IO in user code 1 area:



  • Default frequency is 8Mhz
  • Choose to output hex compilied file.
  • In STM32Cube, only copy necessary files, otherwise will be very big project folder


File Struture: Manage Components

Std build stm32 projects.png
File Parts src inc
STD LIB .\user\lib\STM32L1xx_StdPeriph_Driver\src .\user\lib\STM32L1xx_StdPeriph_Driver\inc add all
User main and customized interrupt
  • .\user\main.c;
  • .\user\stm32l1xx_it.c;
  • .\user\main.h;
  • .\user\stm32l1xx_conf.h;
  • .\user\stm32l1xx_it.h
User libraries .\user\hardware .\user\hardware -
Device system & Startup (replace by Device module)
  • (replaced) .\user\system_stm32l1xx.c;
  • CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h; (conflict and still need)
  • CMSIS\Device\ST\STM32L1xx\system_stm32l1xx.h
\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm\ *.s
CMSIS Core (replace by module CMSIS Core)
  • .CMSIS\CM3\CoreSupport\core_cm3.c
  • C:\Keil_v5\ARM\PACK\ARM\CMSIS\5.5.1\CMSIS\Core\Include\core_cm3.h

Preprocesser Symbol


  • STM32L1XX_MD

Common Error

STM32 Linux

Debugger OpenOCD

  • Help page.
  • Use either ST-LINK or OpenOCD, can work with ST-LINK2
  • For example OpenOCD, run command as follow to install
  1. install openocs: sudo apt-get install openocd
  2. setup up for usbdevice: sudo vi /etc/udev/rules.d/99-stlink.rules
  3. add conent: ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="0666"
  4. reload setup: sudo udevadm control --reload-rules
  5. open board: openocd -f /usr/share/openocd/scripts/board/stm32f3discovery.cfg
  6. or use: /usr/share/openocd/scripts# openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
  • If well connected, result will show:

Open On-Chip Debugger 0.7.0 (2013-05-15-17:44) Licensed under GNU GPL v2 For bug reports, read

srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : This adapter doesn't support configurable speed
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : Target voltage: 2.915198
Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoint
  • Run another terminal and telnet to the board: telnet localhost 4444
  • halt it into debug mode: reset halt or: reset init
  • Reflash bin into board:
flash write_image erase maple_mini_boot.bin 0x08000000
flash write_image erase /media/sf_VM_XP_share/LED.hex
  • Finall reset it to start up again: reset run


  • Install: sudo apt-get install gcc-arm-none-eabi

Firmware libopencm3 and test code

  • git clone the firmware and try stm32f103 demo blink code, the firmware works like arduino
git clone https://github.com/libopencm3/libopencm3-examples.git
cd libopencm3-examples
git submodule init // 
git submodule update
  • enter into board folder like: cd libopencm3-examples/examples/stm32/f3/stm32f3-discovery/miniblink
  • for STM32F1x board, enter into /libopencm3-examples/examples/stm32/f1/stm32-h103//miniblink/
  • firmware support openocd, can run make flash directory
  • Or manually telnet to openocd: telnet localhost 4444
reset halt
flash write_image erase miniblink.elf

Uploading Code

Boot Mode Setup


  • 1)用户闪存 = 芯片内置的Flash。
  • 2)SRAM = 芯片内置的RAM区,就是内存啦。
  • 3)系统存储器 = 芯片内部一块特定的区域,芯片出厂时在这个区域预置了一段Bootloader,就是通常说的ISP程序。这个区域的内容在芯片出厂后没有人能够修改或擦除,即它是一个ROM区。


BOOT 1 BOOT 0 Note Note 2
x 0 从用户闪存启动,这是正常的工作模式。
0 1 从系统存储器启动,这种模式启动的程序功能由厂家设置。 Enter into Serial, CAN or DFU uploading mode
1 1 从内置SRAM启动,这种模式可以用于调试。

Main Flash memory 是STM32内置的Flash,一般我们使用JTAG或者SWD模式下载程序时,就是下载到这个里面,重启后也直接从这启动程序。 System memory 从系统存储器启动,这种模式启动的程序功能是由厂家设置的。一般来说,这种启动方式用的比较少。系统存储器是芯片内部一块特定的区域,STM32在出厂时,由ST在这个区域内部预置了一段BootLoader,也就是我们常说的ISP程序,这是一块ROM,出厂后无法修改。一般来说,我们选用这种启动模式时,是为了从串口下载程序,因为在厂家提供的BootLoader中,提供了串口下载程序的固件,可以通过这个BootLoader将程序下载到系统的Flash中。但是这个下载方式需要以下步骤:

  • Step1:将BOOT0设置为1,BOOT1设置为0,然后按下复位键,这样才能从系统存储器启动BootLoader
  • Step2:最后在BootLoader的帮助下,通过串口下载程序到Flash中
  • Step3:程序下载完成后,又有需要将BOOT0设置为GND,手动复位,这样,STM32才可以从Flash中启动

Embedded Memory 内置SRAM,既然是SRAM,自然也就没有程序存储的能力了,这个模式一般用于程序调试。假如我只修改了代码中一个小小的地方,然后就需要重新擦除整个Flash,比较的费时,可以考虑从这个模式启动代码(也就是STM32的内存中),用于快速的程序调试,等程序调试完成后,再将程序下载到Flash中。
要注意的是,一般不使用内置SRAM启动(BOOT1=1 BOOT0=1),因为SRAM掉电后数据就丢失。多数情况下SRAM只是在调试时使用,也可以做其他一些用途。如做故障的局部诊断,写一段小程序加载到SRAM中诊断板上的其他电路,或用此方法读写板上的Flash或EEPROM等。还可以通过这种方法解除内部Flash的读写保护,当然解除读写保护的同时Flash的内容也被自动清除,以防止恶意的软件拷贝。


  • STM32F042 don't have BOOT1
  • Switch BOOT0 from GND to 3.3V to enter into DFU mode


ST-LINK + ST Utility


Jlink, JTAG

Embedded bootloader

The embedded bootloader mode is used to reprogram the Flash memory using one of the following serial interfaces:

  • USART1 (PA9/PA10)
  • USART3 (PB10/11 and PC10/11)
  • CAN2 (PB5/13)
  • USB OTG FS (PA11/12) in Device mode (DFU: device firmware upgrade).


  • Listed supported device in ST documents AN3156


  • Flash loader demostrator from official ST


Development Schematic





Application Schematic



  • Boot0 connect via 10K to GND for normal mode, to 3.3V for upload mode.

STM32 F103 Features

Core: ARM 32-bit Cortex ™ -M3 CPU

  • Up to 72MHz operating frequency
  • Single cycle multiplication and hardware division


  • from 64K or 128K bytes of Flash program memory
  • up to 20K bytes of SRAM

Clock, reset and power management

  • 2.0 to 3.6 V supply and I / O pins
  • Power on / off reset (POR / PDR), programmable voltage monitoring (PVD)
  • 4 ~ 16MHz crystal oscillator
  • Built-in factory-calibrated 8MHz RC oscillator
  • Built-in calibrated 40kHz RC oscillator
  • PLL that generates the CPU clock
  • 32 kHz RTC oscillator with calibration function

Low power consumption

  • Sleep, Standby and Standby modes
  • V BAT supplies power to the RTC and the back-up registers

2 of 12-bit analog-to-digital converters, 1us conversion time (up to 16)

  • Conversion range: 0 to 3.6V
  • Double sampling and hold function
  • Temperature sensor


  • 7-channel DMA controller
  • Peripherals supported: timers, ADC, SPI, I 2 C and USART

Up to 80 Fast I / O ports

  • 26/37/51/80 I / O ports, all I / O ports can be mapped to 16 external interrupts; almost all ports can tolerate 5V letter number

Debug mode

  • Serial single-wire debugging (SWD) and JTAG interfaces

Up to 7 timers

  • Three 16-bit timers, up to four for each timer / Input Capture / Output Compare / PWM or Pulse Count channels and incremental encoder inputs
  • One 16-bit with dead zone control and emergency brake for motors
  • Control the PWM Advanced Control Timer
  • Two Watchdog Timers (Standalone and Windowed)
  • System time timer: 24-bit self-decrementing counter

Up to 9 communication interfaces

  • Up to 2 I 2 C interfaces (Supports SMBus / PMBus)
  • Up to 3 USART interfaces (supports ISO7816 interface,

IN, IrDA interface and modem control)

  •  Up to 2 SPI interfaces (18 Mbit / s)
  • CAN interface (2.0B active)
  • USB 2.0 full speed interface

CRC computing unit, 96-bit chip unique code


  • FSMC = flexible static memory controller
  • USB - PA11 - USB_DM, PA12 - USB_DP
  • USART1 - PA9 - U1TX, PA10-U1RX
  • USART2 - PA2, PA3 - USART2_RX
  • UART4
  • UART5
  • I2C1 - PB6, PB7
  • I2C2 - PB10, PB11
  • SPI1 - PA4 - PA7
  • SPI2 - PB12 - PB15
  • SD: PD2 - SDIO_CMD, PC12 - SDIO_CK, PC8 - SDIO_D0, PC9 - SDIO_D1, PC10 - SDIO_D2, PC11 - SDIO_D3, PA8 - SDIO_SW ?


2018-07-23 000209.png
Type Core Memory
STM32F405 168Mhz M4 32bit 1MB Flash
STM32F401 84 MHz 512 Kb Flash
STM32F103 72MHz 64K or 128K bytes, 20K bytes of SRAM

List 1

Type Price RMB Speed Package Category DFU
STM32F030F4P6 2.15 48 TSSOP-20 F0 -
STM32F030C8T6 3.35 48 QFP-48 F0 -
STM32F04x - - - F0 Y
STM32F103C8T6 4.7 72 LQFP48 F1 -
STM32F429IGT6 34 180 LQFP176 F4 Y

List Interface

Type Serial DMA IO 4
STM32F030F4P6 6x Y 5V Compatiable

List Specs

Type SRAM Flash 3 4
STM32F030F4P6 32K 256K


Main Mark Secondary Suffix-1 Suffix-2 Suffix-3 Suffix-4
STM32 F 103, 3=enhanced R, T=36 pin,C=48 pin,R=64 pin,V=100 pin,Z = 144 pin C, 4=16K,6=32K,8=64K,B=128K,C=256K,D=384K,E=512K T, H=BGA, T=LQFP, U=VFQFPN, Y=WLCSP64 6, 6=-40~85 degree,7=-40~105 degree