STMicroelectronics STM32 Cortex-M3 User Manual

1 Intro
Tutorial
STM32 Cortex-M3
Author El. Ing. Lukas Kohler Adviser Daniel Debrunner Date 09.09.2009 Site BFH-TI Biel Study Master of Science in Biomedical Engineering
University of Bern and University of Applied Sciences Biel

Abstract

The powerful STM32 controller from STMicroelectronics with integrated Cortex-M3 core has be­come more and more relevant in embedded applications. The key points are an excellent real-time behavior, an outstanding power efficiency and superior peripherals.
The new developed Cortex-M3 has a high aim: A powerful processor with a uncomplex program­ming model which is able to replace the available 8 and 16-bit microcontroller. Tools and libraries guarantee a short developing time and a high standardisation. A big part of written and used software can be reused.
This manual guides you through programming an STM32F103RB microcontroller. The first chap­ters give an introduction to the STM32 and show how the development toolchain is set up and used. In the main part of this documentation the peripheral is illustrated. It should lead you to start straight ahead without reading application notes or data sheets.
I
Contents
1 Introduction 1
1.1 Overview of this Documentation 1
1.2 Timeline 1
1.3 Symbols used in this Document 1
1.4 License and Author 2
2 System Description 3
2.1 Introduction 3
2.2 Electrostatic Warning 3
2.3 Cortex-M3 Core 3
2.3.1 History 3
2.3.2 Cortex 4
2.4 Processor STM32F103RB from STMicroelectronics 6
2.4.1 Features 6
2.4.2 Performance Line Block Diagram 7
2.4.3 Memory Map 8
2.4.4 Interfaces 9
2.5 Development Boards 9
2.5.1 STM32H103 from Olimex 9
2.5.2 STM32P103 from Olimex 10
2.5.3 Setup for Developing 10
2.5.4 Board Layout and Schematic 11
2.5.5 BFH Mother Board 11
3 Keil SDK 13
3.1 Introduction 13
3.2 Install µVision3 13
3.3 Install the Driver for the JTAG Adapter 13
3.4 Install the STM32 Peripheral Library 13
3.5 Setting up a new Project 14
3.5.1 Preparation 14
3.5.2 Create a new project 14
3.5.3 Configure the Processor 15
3.5.4 Renaming the Target 16
3.6 Building 16
3.7 Simulation 17
3.7.1 Window update 18
III
1 Intro
Contents
3.7.2 Serial Window 18
3.7.3 Performance Analyzer 18
3.8 Configuring the Target 19
3.9 Downloading 21
3.10 Debugging 21
3.11 Serial Terminal 21
3.12 Documenting Source Code 22
4 Peripherals 23
4.1 Getting Started 23
4.2 Configuration Wizard 23
4.3 Firmware Library (FWLib) 24
4.4 Register Access 25
4.5 General Purpose Input/Output (GPIO) 26
4.5.1 Configuration 26
4.5.2 Driver 27
4.5.3 Set Output 27
4.5.4 Read Input 27
4.6 Serial Interface (USART) 28
4.6.1 Ways of Operation: Polling versus Interrupts 28
4.6.2 Using Polling 28
4.6.3 Using Interrupts 29
4.6.4 Driver 29
4.6.5 Pinning 31
4.7 System Timer (SysTick) 32
4.7.1 Configuration 32
4.8 Timer (TIM) 33
4.8.1 Configuration 33
4.9 Pulse Width Modulation (PWM) 34
4.9.1 Configuration 34
4.10 External Interrupts (IRQ) 35
4.10.1 Configuration 35
4.10.2 Functionality 35
4.11 Watchdog 36
4.11.1 Configuration 36
4.12 Real Time Clock (RTC) 36
4.13 Analog to Digital Converter (ADC) 37
4.13.1 Conversion Modes 37
4.14 Controller Area Network (CAN) 40
4.14.1 CAN Analyzer 41
4.14.2 Pinning 41
4.15 Inter IC Bus (I2C) 41
4.15.1 Physical layer 42
IV STM32 Cortex-M3 Tutorial

Contents

1 Intro
4.15.2 Communication Protocol 42
4.15.3 Pinning 44
4.16 Serial Peripheral Interface (SPI) 45
4.16.1 Physical Layer 45
4.16.2 Communication Protocol 45
4.16.3 Pinning 46
4.17 Universal Serial Bus (USB) 48
List of Figures 50
Glossary 52
Lukas Kohler V

1 Introduction

1.1 Overview of this Documentation

Introduction (Chapter 1)
This chapter shows how this documentation is structured and meant to be read as well as some project organisation.
System Description (Chapter 2)
Here one can read all about the core Cortex-M3 and it’s implementation from STMicroelec­tronics into the STM32F103 processor. The development boards from Olimex as well as the BFH mother board are presented and the interfaces shown.
Keil SDK (Chapter 3)
This chapter shows how to set up a new project with the Keil Software Development Kit
µVision3 and how to compile, simulate, download and debug software.
Peripherals (Chapter 4)
The biggest part of this documentation is showing how the peripherals are controlled. Ex­ample programs from using simple inputs and outputs to complex interrupts are provided. This example based chapter shows you the way of using peripherals with simple examples.

1.2 Timeline

Start: March 1, 2009
Project deadline: August 30, 2009
Presentation (BFH Biel): December 14, 2009

1.3 Symbols used in this Document

Warning: This warning should be taken seriously. Ignoring warnings could cause problems or destroy devices.
Sample Code: Shows that a sample code exists in the directory CD:/code/ . b
Filename: hudi.c Function: void doSomeThing()
!
1
1 Intro

1.4 License and Author

The whole Cortex-M3 Tutorial including Source Code and Documenta­tion by Lukas Kohler is licensed under a Creative Commons Attribution-
Noncommercial-Share Alike 3.0 United States License.
Written 2009 by Lukas Kohler k lukas.kohler@gmail.com
H 078 817 77 77
1 Introduction
2 STM32 Cortex-M3 Tutorial

2 System Description

2.1 Introduction

This chapter describes the technical way of the development boards. Often abbreviations are used, they are described in the glossary (Part 4.17). The microcontroller used on the different board is always the STM32F103RB form ST with the Cortex-M3 core from ARM. The first section introduces this core, the second the microcontroller STM32F103RB and the following the development boards used in this document.
This chapter describes only the processor used in this document and shows the hardware setup. The development of the software itself is described in Chapter 3.

2.2 Electrostatic Warning

Make sure that you are connected to ground before you touch any elec­tronic circuit! Electrostatic discharges may destroy any electronic circuit.
!

2.3 Cortex-M3 Core

The target market of the Cortex-M3 core is the field that has been served by 8- or 16bit controllers. In this market cheap but powerful controllers with little power consumption are needed.

2.3.1 History

For the last decade the ARM7 and ARM9 microcontrollers have been used as general purpose microcontrollers. Today exist a few hundred microcontrollers with the ARM core.
Now STMicroelectronics have launched the STM32, their first microcontroller based on the new ARM Cortex-M3 core. This device sets new standards in performance and cost, as well as being capable of low power operation and hard real-time control.
Currently the M3 core is licensed by 22 companies like Actel Corporation, Broadcom Corporation, Energy Micro, Luminary Micro, NXP, STMicroelectronics, Texas Instruments, Toshiba and Zilog1.
1
Licensed M3 cores: arm.com
3
1 Intro
2 System Description

2.3.2 Cortex

The new generation of processors comes as complete processor core that provides a standard CPU and system architecture.
On the ARM7 systems every manufacturer had to add the basic peripherals like interrupt con­troller, systick timer and sleep modes. This has now been built into the Cortex-M3 core. The Cortex-M3 provides a standardized microcontroller core which goes beyond the classical CPU to provide the entire heart of a microcontroller (including the interrupt system, 24-bit SysTick timer, debug system and memory map).
The Cortex family comes in three main profiles: The A profile for high end applications (applications processors for complex OS and user applications), R for real time and M for cost-sensitive and mi­crocontroller applications.
The STM32 is based on the Cortex-M3 profile, which is specifically designed for high system performance combined with low power consumption. It has a low enough cost to challenge tra­ditional 8 and 16-bit microcontrollers.
The Cortex-M3 is a Harvard architecture (Code and Databus are separated) and has multiple buses that allow it to perform operations in parallel. The architecture inside the Cortex-M3 is a ARMv7­M RISC processor.
With only 33’000 logic cells needed to syntetisize the M3 core it is the smallest core of the ARM family. This reduces the size of the chip and therefore cheap manufacturing processes with 0.35 µm can be used.
Interrupts
One of the key components of the Cortex-M3 core is the Nested Vector Interrupt Controller (NVIC). The NVIC provides a standard interrupt structure for all Cortex based microcontrollers and excep­tional interrupt handling. The NVIC provides dedicated interrupt vectors for up to 240 peripheral sources where each interrupt source can be individually prioritized. The NVIC provide extremely fast interrupt handling. The time taken from receiving an interrupt to reaching the first line of code in your interrupt service routine is just twelve cycles.
All interrupts can be prioritized on three levels: pre-empting, sub-priority and hardware-priority. The first two can be configured via the software and the priority can be set on 4 bits.
The interrupt service routines (ISR) can be written fully in C, therefore no Assembler is needed any more.
Instruction Set
While the ARM7 and ARM9 CPUs have two instruction sets (the 32-bit ARM and the 16-bit Thumb), the Cortex family is designed to support only the 16-bit Thumb-2 instruction set. The result is a simpler assembling of the core since there is not longer need for switching between the two instruction sets. The Thumb-2 instruction set is a rich instruction set that is designed as a target for C/C++ compilers.
4 STM32 Cortex-M3 Tutorial
2.3 Cortex-M3 Core
1 Intro
Cortex CPU
The heart of the Cortex core is a 32-bit RISC CPU. This CPU has a simplified version of the ARM7/9 programmer’s model, but a richer instruction set with good integer maths support, bet­ter bit manipulation and ’harder’ real-time performance has been added.
Pipeline
The Cortex CPU can execute most instructions in a single cycle. Like the ARM7 and ARM9 CPUs this is achieved with a three stage pipeline. Whilst one instruction is being executed, the next is being decoded and a third is being fetched from memory.
Multiplication
A 32-bit multiplication can be done within one cycle since the Cortex-M3 offers a hardware mul­tiplication unit.
Producer
The IP core form ARM can be implemented by different manufacturers. In this document only the STM32F103RB from ST is described. The Core exists as well for VHDL implementation as M1 ARM 32-bit Cortex-M3 CPU only core without peripherals. The peripherals are implemented by the manufacturers.
Power Consumption
The STM32 is a low power as well as high performance microcontroller . It can run from a 2V supply and at 72MHz with everything switched on it consumes just 36mA. In combination with the Cortex low power modes the STM32 has a standby power consumption of only 2µA.
Startup-Code
For the Cortex-M3 is no longer assembler startup code is needed, everything can be coded in C.
Debug-Interface
The debug interface has been changed to a scalable interface with advantages according to the ARM7/9 family. The Cortex-M3 has a Debug Access Port (DAP) which can be used in the JTAG or in the Serial Wire Debug Mode with only two lines (clock and data).
ARM7 processors have only a very limited amount of two Hardware-Breakpoints. In the Cortex-M3 this has been updated to 8 breakpoints. Beside the breakpoints, Fault Conditions and Exceptions can be set. Faults and Exceptions can be supervised without the need of using breakpoints.
The debug interface works as well in the Sleep-mode.
Lukas Kohler 5
1 Intro
2 System Description

2.4 Processor STM32F103RB from STMicroelectronics

2.4.1 Features

The STM32F103RB from STMicroelectronics is a processor with a ARM 32-bit Cortex-M3 CPU and peripherals. STMicroelectronics is reshaping the 16/32-bit MCU market with they’r STM32 family.
The key benefits of the STM32F103RB are:
Leading performance, real time behavior
A lot of peripherals
Outstanding power efficiency
In production since 2007 7→ proved design
70 part numbers across 4 product lines
SW Libraries are free available from www.st.com/mcu
Core: CPU clock internal up to 72Mhz, single-cycle multiplication and hardware division
Memory: 128KBytes flash, 20KByte ram and memory controller
LCD parallel interface
Low power states (sleep, stop and standby)
7 DMA channels
RTC
WDT
PWM
3x Timers and SysTick
Communication interface:
2x SPI
2x I2C
3x I2S
3x USART
USB 2.0
CAN
Up to 51 GPIO (multiplexed with peripherals, 5V tolerable [but not for analog inputs!])
2x ADC 12 bit
Operating voltage 2.0-3.6V
Temperature: -40C +85C
Debugging: JTAG and serial wire interface
A lot of information, data sheets and examples can be found at
http://www.st.com/mcu/familiesdocs-110.html.
6 STM32 Cortex-M3 Tutorial
2.4 Processor STM32F103RB from STMicroelectronics
1 Intro
Figure 1. STM32F103xx performance line block diagram
USBDP/CA N_TX
PA[15: 0]
EXTI
WWDG
12bit ADC1
16AF
JTDI
JTCK/ SWCLK
JTMS/SWDIO
NJTRSTTRST
JTDO
NRST
V
DD
= 2 to 3.6V
80AF
PB[15: 0]
PC[15: 0]
AHB2
MOSI,MISO,SCK,NSS
SRAM
2x(8x16bit)
WAKEUP
GPIOA
GPIOB
GPIOC
F
max
: 72 M
Hz
V
SS
SCL,SDA
I2C2
V
REF+
GP DMA
TIM2
TIM3
XTAL OSC
4-16 MHz
XTAL 32 kHz
OSC_IN OSC_OUT
OSC32_OUT
OSC32_IN
PLL &
APB1 : F
max
=24 / 36 MHz
PCLK1
HCLK
CLOCK MANA GT
PCLK2
as AF
as AF
Flash 128 KB
VOLT. REG.
3.3V TO 1.8V
POWER
Backu p i nterf ace
as AF
TIM 4
BusM atrix
64 bit
Interface
20 KB
RTC
RC 8 MHz
Cortex-M3 CPU
Ibus
Dbus
pbu s
obl
flash
SRAM 512B
Trace
Cont rolle r
USART1
USART2
SPI2
bxCAN
7 ch annels
Back up
reg
4 Chann els
TIM1
3 com pl. Chann els
SCL,SDA ,SMBA
I2C1
as AF
RX,TX, CTS, RTS,
USART3
Temp sens or
V
REF-
PD[15: 0]
GPIOD
PE[15: 0]
GPIOE
AHB:F
max
=48/72 MHz
ETR and BKIN
4 Chann els
4 Chann els
4 Chann els
FCLK
RC 40 kHz
Stand by
IWDG
@VBAT
POR / PDR
SUPPLY
@VDDA
VDDA VSSA
@VDDA
V
BAT
RX,TX, CTS, RTS, Smart Card as AF
RX,TX, CTS, RTS, CK, SmartCard as AF
APB2 : F
max
=48 / 72 MHz
NVIC
SPI1
MOSI,MISO,
SCK,NSS as AF
12bit ADC2
IF
IFIF
int erface
@VDDA
SUPERVISION
PVD
Rst
Int
@VDD
AHB2
APB2
APB 1
AWU
TAMPER-RTC
@VDD
USB 2.0 FS
USBDM/CAN_RX
System
ai14390d
TRACECLK TRACED[0:3]
as AS
SW/JTAG
TPIU
Trace/trig
CK, SmartCard as AF

2.4.2 Performance Line Block Diagram

The block diagram in Figure 2.1 shows how the components and the core are connected together.
Lukas Kohler 7
Figure 2.1: STM32F103xx Performance Line Block Diagram
1 Intro

2.4.3 Memory Map

The memory map is shown in Figure 9.
Figure 9. Memory map
reserved
0x4000 0000
0x4000 0400
0x4000 0800
0x4000 0C00
0x4000 2800
0x4000 2C00
0x4000 3000
0x4000 3400
0x4000 3800
0x4000 3C00
0x4000 4400
0x4000 4800
0x4000 4C00
0x4001 0C00
0x4001 1000
0x4001 1400
0x4001 1800
0x4002 1400
APB memory space
DMA
0x4002 1000
TIM2
Reserved
0x4001 0800
0x4001 1C00
0x4001 2400
0x4001 2800
0x4001 2C00
0x4001 3000
0x4001 3400
0x4001 3800
TIM3
TIM4
reserved
RTC
WWDG
IWDG
reserved
SPI2
USART2
USART3
AFIO
Port A
Port C
Port D
reserve d
ADC1
reserved
USART1
reserved
0x4002 0400
0x4002 0000
0x4001 3C00
0x4000 5400
0x4000 5800
reserved
ADC2
TIM1
SPI1
reserved
I2C1
BKP
0x4000 6000
0x4000 5C00
Port E
PWR
Port B
I2C2
reserved
bxCAN
EXTI
reserved
RCC
reserved
Flash In terface
reserved
reserved
reserved
0x4000 6400
0x4000 6800
0x4000 6C00
0x4000 7000
0x4000 7400
0x4001 0000
0x4001 0400
0x4002 2000
0x4002 2400
0x4002 3000
0x4002 3400
0x6000 0000
0xE010 0000
reserved
0xFFFF FFFF
USB Regi sters
CRC
0
1
2
3
4
5
6
7
0x2000 0000
0x4000 0000
0x6000 0000
0x8000 0000
0xA000 0000
0xC000 0000
0xE000 0000
0xFFFF FFFF
0x0000 0000
Peripherals
SRAM
Flash memory
reserved
reserved
0x0800 0000
0x0801 FFFF
0x1FFF F000
0x1FFF FFFF
System memory
Option Bytes
0x1FFF F800
0x1FFF F80F
Cortex- M3 Internal
Perip heral s
0xE010 0000
ai14394f
shared 512 byte
USB/CAN SRAM
Aliased to Flash or system memory depending on BOOT pins
0x0000 0000
The memory map shows how the memory in the STM32F103RB is configured. This needs to be known for programming the processor.
Flash Memory: 0x0800 0000 - 0x0801 FFFF
System Memory: 0x1FFF F000 - 0x1FFF F800
8 STM32 Cortex-M3 Tutorial
Figure 2.2: STM32F103xx memory map
2 System Description

2.5 Development Boards

1 Intro

2.4.4 Interfaces

To be able to communicate with other devices we need interfaces:
JTAG - Debug Interface:
The JTAG interface allows downloading the compiled code into the microcontroller and debugging the code in real time. To be able to do this operation you have to use an JTAG adapter to the USB or parallel port to the computer.
USB - Universal Serial Bus:
Using a library one can communicate with an USB master. If you power the device over USB, no wallplug or external power supply is needed.
RS232, CAN, SPI I2C, I2S:
The serial interfaces can be used to communicate with any kind of devices or can be used
as debug/status interface.
2.5 Development Boards
We use the Olimex boards with our own BFH mother board extension. On both of these boards the ARM Cortex M3 STM32F103RB produced by ST Microelectronics is present.
Both boards offer a LED on port PC12 (if LED E is bridged) and a button on PA0 (wake-up).
Figure 2.3: Development Board STM32H103 from
Olimex
Figure 2.4: Development Board STM32P103 from
Olimex

2.5.1 STM32H103 from Olimex

The STM32H103 is the minimal version of the Olimex STM32 board. It contains only a STM32F103RB processor, USB connector with power adapter and LEDs. The STM32H103 can be connected via the extension headers to the BFH mother board to connect to other interfaces. See Chapter 2.5.5 for more details.
Lukas Kohler 9
1 Intro
2 System Description

2.5.2 STM32P103 from Olimex

The STM32P103 development board from Olimex is the extended version of the STM32H103. The board includes:
USB connector with power supply
CAN driver and connector
RS232 driver and connector
JTAG with ARM 2x10 pin layout
Reset button, user button, power and user LED
SD/MMC card connector
Voltage regulator 3.3V from USB or extension power jack connector (maximum power con-
sumption 0.25W at full speed)
8MHz and 32,768kHz oscillator (external clock and backup clock for RTC)
UEXT connector that allows to connect to other modules
Prototyping area
Reset and wakeup circuit
Dimensions: 100x90mm

2.5.3 Setup for Developing

In order to be able to develop software for the STM32F103RB , Keil µVision3 has to be installed on a PC. If this is not yet the case install it according to Chapter 3. µVision3 is the tool used for writing, compiling and testing software. The compiled source is then downloaded via a JTAG adapter to the development board. Therefore the driver of the JTAG adapter has to be installed too.
If the requirements above are fulfilled you can connect a USB cable from the PC to the develop­ment board STM32P103 or STM32H103. This connection is used for the power supply.
Connect the JTAG adapter to the development board STM32P103 or STM32H103 and connect a USB cable from the JTAG adapter to the PC. This is used for downloading the software into the processor and for debugging with the Keil toolchain (see Figure 2.5).
JTAG Adaptor
The JTAG Adaptor is needed for downloading the software into the processor and for debugging with the Keil toolchain. According to Keil, different adapters can be used, such as:
uLink from Keil
jLink from Segger
J-Link from IAR
I used the J-Link from IAR.
10 STM32 Cortex-M3 Tutorial
Loading...
+ 42 hidden pages