You are on page 1of 22

C STM32 helloworld

STM32
OpenOCD STM32 Flash

C
CPU FLASH
0 RAM
RAM

memory map
FLASH
RAM

0x20001000
0x08000000
(0x00000000)

0x20000000

FLASH RAM

->PC
The Definitive Guide To ARM Cortex M3
Cortex M3 32

0x0000,0000 MSP

0x0000,0004 PC LSB 1

ARM CortexM3 ST ST

ST Reference Manual STM32


Flash BOOT0=0
Flash 0x08000000 Cortex M3 Flash
MSP PC STM32 Flash
0x08000000 0x00000000
image

MSP

PC LSB 1

__asm__(".word 0x20001000");
__asm__(".word main");
main()
{

MSP 0x200010000x20001000-0x20000000=0x1000
4k GNU tool chain
PC LSB 1 main C PC
main
tool chain tool chain

helloworld

STM32 GPIO GPIO


ST
D GPIO 9 LED
LED(DS4)

1. D GPIO
2. D GPIO 9(PD9)(General purpose output push-pull)
3. PD9 0 1 LED
ST

APB2 peripheral clock enable register(RCC_APB2ENR)

0x40021000 + 0x18
0x00000000

(IOPD EN) 1 GPIOD

Port configuration register high (GPIOx_CRH)(x=D)


0x40011400 + 0x4
0x44444444

CNF9[1:0] = 00(General purpose output push-pull)

MODE[1:0] = 01( 10MHz)

Port bit set/reset register (GPIOx_BSRR) (x=D)


0x40011400 + 0x10
0x00000000

GPIOD9 BR9 = 1
GPIOD9 BS9 = 1
happy programming!

blink.c

#define GPIOD_CRH
(*((volatile unsigned long*)(0x40011400 + 0x4)))
#define GPIOD_BSRR (*((volatile unsigned long*)(0x40011400 + 0x10)))
#define RCC_APB2ENR (*((volatile unsigned long*)(0x40021000 + 0x18)))
__asm__(".word 0x20001000");
__asm__(".word main");
main()
{
unsigned int c = 0;
RCC_APB2ENR = (1 << 5);
GPIOD_CRH = 0x44444414;
while(1) {
GPIOD_BSRR
for(c = 0;
GPIOD_BSRR
for(c = 0;
}

=
c
=
c

//IOPDEN = 1

(1 << 25); // ON
< 100000; c++);
(1 << 9);
// OFF
< 100000; c++);

simple.ld
SECTIONS {
. = 0x0;
.text : {
*(.text)
}
}

Makefile
PREFIX := arm-none-eabi.PHONY: all clean
all: blink.bin
blink.o: blink.c
$(PREFIX)gcc -mcpu=cortex-m3 -mthumb -nostartfiles -c blink.c -o blink.o
blink.out: blink.o simple.ld
$(PREFIX)ld -T simple.ld -o blink.out blink.o
blink.bin: blink.out
$(PREFIX)objcopy -j .text -O binary blink.out blink.bin
clean:
rm -f *.o *.out *.bin

blink.c for STM32


LED (
)
simple.ld . = 0x0 0x0
0x0Linker and Loader
STM32 Flash 0x08000000

Makefile GNU make Managing Projects with GNU Make


Makefile
-mcpu=cortex-m3 -mthumb -nostartfilesMakefile
The Definitive Guide To The ARM Cortex M3

makemake blink.bin

arm-none-eabi-objdump -D blink.out

blink.out:

file format elf32-littlearm

Disassembly of section .text:


00000000 <main-0x8>:
0:
20001000
4:
00000009
00000008 <main>:
8:
b480
a:
b083
c:
af00
e:
f04f 0300
12:
607b
14:
f241 0318
18:
f2c4 0302
.......

andcs
andeq

r1, r0, r0
r0, r0, r9

push
sub
add
mov.w
str
movw
movt

{r7}
sp, #12
r7, sp, #0
r3, #0
r3, [r7, #4]
r3, #4120
r3, #16386

; 0x1018
; 0x4002

0x00000000 MSP 0x20001000PC


0x00000009GNU tool chain LSB 1

.text.comment.ARM.attributes".text
.rodata.data.bss

Flash
#od -t x1 blink.bin
0000000 00 10 00 20
0000020 00 03 7b 60
0000040 1a 60 41 f2
0000060 44 42 1a 60
0000100 1a 60 4f f0
0000120 7b 60 7a 68
0000140 41 f2 10 43
0000160 00 03 7b 60

09
41
04
41
00
48
c4
03

00
f2
43
f2
03
f2
f2
e0

00
18
c4
10
7b
9f
01
7b

00
03
f2
43
60
63
03
68

80
c4
01
c4
03
c0
4f
03

b4
f2
03
f2
e0
f2
f4
f1

83
02
44
01
7b
01
00
01

b0
03
f2
03
68
03
72
03

00
4f
14
4f
03
9a
1a
7b

af
f0
42
f0
f1
42
60
60

4f
20
c4
00
01
f4
4f
7a

f0
02
f2
72
03
d9
f0
68

0000200 48 f2 9f 63 c0 f2 01 03 9a 42 f4 d9 d2 e7 00 bf
0000220

little-endian

Flash
OpenOCD+OpenJTAG OpenOCD linux
google

1. OpenOCD
#openocd -f openocd.cfg -f stm32.cfg

2. shell Flash
#telnet localhost 4444
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
> halt
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x0800002a msp: 0x200000d8
> poll
background polling: on
TAP: stm32.cpu (enabled)
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x0800002a msp: 0x200000d8
> flash protect_check 0
device id = 0x10016414
flash size = 512kbytes
successfully checked protect state
> stm32x mass_erase 0
stm32x mass erase complete
> flash write_bank 0 /tmp/blink.bin 0
not enough working area available(requested 16384, free 16336)
wrote 144 bytes from file /tmp/blink.bin to flash bank 0 at offset 0x00000000 in 0.175022s (0.803 kb/s)
> reset
JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
JTAG tap: stm32.bs tap/device found: 0x06414041 (mfg: 0x020, part: 0x6414, ver: 0x0)

3.
>reset

C helloworld v1
helloworld
LED
STM32

RAM

loader)
PC
(*.ld)

(section)

loadable)
allocatable)

arm .text .data .rodata .bss .comment .ARM.attributes


.comment .ARM.attributes

.text

.rodata
C const
.data
C
.bss

.text,.rodata.data".bss
(loadable
section)".bss(allocatable section)
(*.o)(*.out)

1.
.text, .rodata, .data, .bss
.text, .data, .bss .text .rodata
4

input sections
output sectionsinput sectionoutput section

2.

1. LMA(Load Memory Address):


2. VMA(Virtual Memory Address):

FLASH
FLASH SRAM LMA VMA
FLASH LMA SRAM VMA
LMA VMA

//stm32f103vet6.ld
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 512k
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64k
}
/* Section Definitions */
SECTIONS
{
.text :
{
KEEP(*(.isr_vector))
*(.text)
*(.rodata)
. = ALIGN(4);
_etext = .;
} > FLASH
.data : AT (_etext)
{
_data = .;
*(.data)
. = ALIGN(4);
_edata = . ;
} > SRAM
/* .bss section which is used for uninitialized data */
.bss (NOLOAD) :
{
_bss = . ;
*(.bss)
. = ALIGN(4);
_ebss = . ;
} > SRAM
}

_end = . ;


MEMORY)

SECTIONS SECTIONS
SECTIONS
{
section-command
section-command
...
}

sections-command

ENTRY

"ENTY
ENTRY

--

".
SECTIONS
(LMA)VMA)

section [address] [(type)] :


[AT(lma)] [ALIGN(section_align)]

[SUBALIGN(subsection_align)]

{
output-section-command
output-section-command

} [>region] [AT>lma_region] [:phdr :phdr ] [=fillexp]

LMA
AT
Output-section-command

file-name [(sections_name1 section_name2 ...)]

.text

*.(.text)

data.o(.text)

1
2
...

1
2
...
> 1

...
2

...
> n
...

.text
.text .isr_vector .text .rodata .isr_vector KEEP
.isr_vector isr_vector
The Definitive Guide to The ARMCortexM3" 0 MSP
32
STM32 FLASH 0x00000000
0x08000000 isr_vector MSP

CortexM3
1-15 15 CortexM3

32

0x0000,0000

MSP

0x0000,0004

PC

0x0000,0008

NMI

0x0000,000c

Hard Fault

__attribute__((section(".isr_vector")))
pfnISRVectorTable[]=
{
(pfnISR)(0x20010000),//TheinitialstackpointeristhetopofSRAM
ResetISR,//Theresethandler
NMIException,
HardFaultException
};

PC ResetISR()

.text

5 _etext, _data, _edata, _bss, _ebss

_etext
.text .text .data
.text

_data, _edata
.data

_bss, _ebss
.bss

ResetISR()
//*****************************************************************************
//
//Thefollowingareconstructscreatedbythelinker,indicatingwherethe
//the"data"and"bss"segmentsresideinmemory.Theinitializersforthe
//forthe"data"segmentresidesimmediatelyfollowingthe"text"segment.
//
//*****************************************************************************
externunsignedlong_etext;
externunsignedlong_data;
externunsignedlong_edata;
externunsignedlong_bss;
externunsignedlong_ebss;
voidResetISR(void)
{
unsignedlong*src,*dst;
//
//CopythedatasegmentinitializersfromflashtoSRAM.
//
src=&_etext;
dst=&_data;
while(dst<&_edata){
*dst++=*src++;

}
//
//Zerofillthebsssegment.
//
for(dst=&_bss;dst<&_ebss;dst++){
*dst=0;
}
//
//Calltheapplication'sentrypoint.
//
main();
}

ResetISR.bss main()

C helloworld v2
FLASH FLASH RAM
RAM RAM
RAM

1. RAM
2. RAM STM32

FLASH RAM

ARM9 bootloader bootloader


RAM NAND FLASH

FLASH ResetISR SRAM

LED
STM32 USART(Universal synchronous asynchronous receiver transmitter) USART1,
USART2, USART3 UART(Universal asynchronous receiver and transmitter) UART4, UART5

DMA
USART1 4.5Mbits/s 2.25Mbit/s

(Receive Data In, RX)(Transmit Data Out, TX)


RX
TX(GPIO)

9600bps

8 1
USART1

1. USART_CR1 UE 1 USART
2. USART_CR1 M
3. USART_CR2
4. USART_CR3 DMA
5. USART_BRR
6. USART_CR1 TE idle
7. USART_DR STM32
8. 7 TC 1TC 1

Tx / Rx baud=

f ck
16USARTDIV

fck USART2345 PCLK1USART1 PCLK2 USART1


USARTDIV
USART_BRR
USART_BRR DIV_Mantissa DIV_Fraction USARTDIV

USARTDIV =DIVMantissa DIVFraction /16

ST

USART_BRR STM32
fck 8MHz

fck=8MHz, baud = 9600, USARTDIV=52.083


DIV_Mantissa=0d52=0x34DIV_Fraction=0.083*16=0x1 USART_BRR=0x341

helloworld helloworld
USART1 0x40013800USART_SR 0x00USART_DR
0x04USART_BRR 0x08USART_CR1 0x0CUSART_CR2 0x10USART_CR3 0x14

IO
Tx GPIOA 9 Rx GPIOA 10 GPIO
Tx Rx Rx GPIO Tx
(Alternate function output Push-pull Alternate function output
Open-drain) General purpose output Alternate function output
Push-pull GPIOA_CRH
USART1GPIOA GPIO
PC
PC

STM32
AMBA
AMBA(Advanced Microcontroller Bus Architecture) ARM

1. Advanced High-performance Bus (AHB)


2. Advanced System Bus (ASB)
3. Advanced Peripheral Bus (APB)
ASB AHB AHB
ASB SoC AHB APB
AMBA DMA
APB AHB AMBA

APB AHB-to-APB APB AHB

AHB APB
AHB APB
STM32 AMBA STM32 AHB
APB ARM AMBA

STM32
STM32 AHB ARM SoC APB

APB STM32 APB1 APB2


APB1
CAN, USB, I2C1, I2C2, UART2, UART3, UART4, UART5, SPI2, SPI3, Watchdog, Timer2, Timer3, Timer4,
Timer5
APB2
GPIO_A-E, USART1, ADC1, ADC2, ADC3, Timer1, Timer8
STM32 Datesheet

STM32

STM32 72MHz 3 LED


CPU

8MHzSTM32

1. RC (HSI oscillator clock) 8MHz


2. (HSE oscillator clock)
3. (PLL clock)

1. 40kHz RC (LSI)
/
2. 32.768kHz LSE)
RC

ST


SYSCLK
PLL HCLKPCLK1PCLK2TIMxCLKTIM1CLKADCCLK
HCLK
AHB ClockAHB
PCLK1
APB1 low speed clock
PCLK2
APB2 high speed clock
TIMxCLK
Timer2, Timer3, Timer4
TIM1CLK
Timer1
ADCCLK
ADC
USBCLK
USB 48MHz
RTCCLK
RTC
IWDGCLK

MCO

HSI
PLL SYSCLK 36MHz


HSE
32.768KHz
HSE

OSC_OUT

HSE
LSE HSE

STM32 HSI, LSI)


ST

STM32
STM32

OSC_INOSC_OUT / 8MHzOSC32_IN OSC32_OUT


/ 32.768kHz 72MHz PLL
9


1. HSE
2.
3.
PLL HSE
PLL
PCLK1PCLK2 APB1 APB2
HCLK AHB
4. PLL
5. PLL
6. PLL
7. PLL
8MHz 72MHz PLL
GPIO LED PCLK2
PCLK1 8
LED

ST

ST ST Standard Peripheral
Libarary) ST ST
ST

STM32F10x Standard Peripherals Libray) ST

ST CMSISCotex Microcontroller Software Interface


StandardCMSIS ARM CortexM

CMSIS CortexM
CMSIS ST CMSIS
ARM
ST ST

ST STM32F10x Standard Peripherals Driver"CMSIS Layers

ST ST

misc
ST

PPP
1. PPP_InitTypeDef
PPP_InitTypeDef PPP_InitStructure;

PPP
2.

PPP_InitStructure.member1 = val1;
PPP_InitStructure.member2 = val2;
PPP_InitStructure.member3 = val3;

PPP_StructInit(&PPP_InitStructure);
PPP_InitStructure.member1 = val1;

3. PPP_Init() PPP

PPP_Init(PPP, &PPP_InitStructure);

4. PPP

PPP_Cmd(PPP, ENABLE);

5.
PPP_xxx(PPP, );

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_PPPx, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_PPPx, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PPPx, ENABLE);

PPP_DeInit()
PPP_InitStructure PPP_Init()


ST
ST
GNU toolchain ST toolchain 5
RealView Microcontroller Development Kit MDK-ARM
Embedded Workbench for ARM EWARM
Raisonance Integrated Development Environment RIDE7
Hitex Development Tools HiTOP
Atolic TrueSTUDIO
RIDE7 high density
startup_stm32f10x_hd.sbss

SystemInit
main

stm32f103vet6.ld
stm32f10x.h
stm32f10x_conf.h USE_STDPERIPH_DRIVER
stm32f10x.h stm32f10x_conf.h stm32f10x_conf.h
stm32f10x.h
USE_STDPERIPH_DRIVER
stm32f10x.h

.
|-- Makefile
|-- cmsis
| |-- core_cm3.c
| `-- system_stm32f10x.c
|-- driver
| |-- misc.c
| |-- stm32f10x_gpio.c
| |-- stm32f10x_rcc.c
| `-- stm32f10x_usart.c
|-- include
| |-- core_cm3.h
| |-- misc.h
| |-- stm32f10x.h
| |-- stm32f10x_conf.h
| |-- stm32f10x_gpio.h
| |-- stm32f10x_rcc.h
| |-- stm32f10x_usart.h
| `-- system_stm32f10x.h
|-- startup
| `-- startup_stm32f10x_hd.s
|-- stm32f103vet6.ld
`-- usr
|-- main.c
|-- stm32f10x_it.c
`-- stm32f10x_it.h
SystemInit() main()
SystemInit() cmsis/system_stm32f10x.c ST
clock PLL 72MHz
main()ST ST ST
ST
"STM32 EVAL Driver LED

LEDInit(), LEDOn(), LEDOff()

--

LED main()
CMSIS

Makefile
MakefileMakefile
Managing Projects with GNU Make

<...>

You might also like