Difference between revisions of "STM32F103 Microcontroller"

From ScienceZero
Jump to: navigation, search
(Registers)
(Pinout)
Line 108: Line 108:
  
 
== Pinout ==
 
== Pinout ==
Pin   Function1  Function2  Function3  Function4
+
{| class="wikitable" border="1"
-------------------------------------------------------
+
|+ '''Port A Pin Functions'''
  PA0 - WKUP       USART2_CTS ADC0       TIM2_CH1_ETR
+
! Pin
  PA1 - USART2_RTS ADC1       TIM2_CH2
+
! Function 1
  PA2 - USART2_TX   ADC2       TIM2_CH3
+
! Function 2
  PA3 - USART2_RX   ADC3       TIM2_CH4
+
! Function 3
  PA4 - SPI1_NSS   USART2_CK   ADC4
+
! Function 4
  PA5 - SPI1_SCK   ADC5
+
|-
  PA6 - SPI1_MISO   ADC6       TIM3_CH1
+
| PA0
  PA7 - SPI1_MOSI   ADC7       TIM3_CH2
+
| WKUP
  PA8 - USART1_CK   TIM1_CH1   MCO
+
| USART2_CTS
  PA9 - USART1_TX   TIM1_CH2
+
| ADC0
PA10 - USART1_RX   TIM1_CH3
+
| TIM2_CH1_ETR
PA11 - USART1_CTS CANRX       TIM1_CH4   USBDM
+
|-
PA12 - USART1_RTS CANTX       TIM1_ETR   USBDP
+
| PA1
PA13 - JTMS       SWDIO
+
| USART2_RTS
PA14 - JTCK       SWCLK
+
| ADC1
PA15 - JTDI       TIM2_CH1   SPI1_NSS
+
| TIM2_CH2
+
|
  PB0 - ADC8       TIM3_CH3
+
|-
  PB1 - ADC9       TIM3_CH4
+
| PA2
  PB2 - BOOT1
+
| USART2_TX
  PB3 - JTDO       TIM2_CH2   SPI1_SCK  
+
| ADC2
  PB4 - JNTRST     TIM3_CH1   SPI1_MISO
+
| TIM2_CH3
  PB5 - I2C1_SMBAI TIM3_CH2   SPI1_MOSI
+
|
  PB6 - I2C1_SCL   TIM4_CH1   USART1_TX
+
|-
  PB7 - I2C1_SDA   TIM4_CH2   USART1_RX
+
| PA3
  PB8 - TIM4_CH3   I2C1_SCL   CANRX
+
| USART2_RX
  PB9 - TIM4_CH4   I2C1_SDA   CANTX
+
| ADC3
PB10 - I2C2_SCL   USART3_TX   TIM2_CH3
+
| TIM2_CH4
PB11 - I2C2_SDA   USART3_RX   TIM2_CH4
+
|
PB12 - SPI2_NSS   I2C2_SMBAI USART3_CK TIM1_BKIN
+
|-
PB13 - SPI2_SCK   USART3_CTS TIM1_CH1N
+
| PA4
PB14 - SPI2_MISO   USART3_RTS TIM1_CH2N
+
| SPI1_NSS
PB15 - SPI2_MOSI   TIM1_CH3N
+
| USART2_CK
+
| ADC4
  PC0 - ADC10  
+
|
  PC1 - ADC11  
+
|-
  PC2 - ADC12  
+
| PA5
  PC3 - ADC13  
+
| SPI1_SCK
  PC4 - ADC14  
+
| ADC5
  PC5 - ADC15  
+
|
  PC6 - TIM3_CH1  
+
|
  PC7 - TIM3_CH2  
+
|-
  PC8 - TIM3_CH3  
+
| PA6
  PC9 - TIM3_CH4  
+
| SPI1_MISO
PC10 - USART3_TX  
+
| ADC6
PC11 - USART3_RX  
+
| TIM3_CH1
PC12 - USART3_CK  
+
|
PC13 - TAMPER-RTC  
+
|-
PC14 - OSC32_IN  
+
| PA7
PC15 - OSC32_OUT  
+
| SPI1_MOSI
+
| ADC7
  PD0 - OSC_IN  
+
| TIM3_CH2
  PD1 - OSC_OUT  
+
|
  PD2 - TIM3_ETR
+
|-
 +
| PA8
 +
| USART1_CK
 +
| TIM1_CH1
 +
| MCO
 +
|
 +
|-
 +
| PA9
 +
| USART1_TX
 +
| TIM1_CH2
 +
|
 +
|
 +
|-
 +
| PA10
 +
| USART1_RX
 +
| TIM1_CH3
 +
|
 +
|
 +
|-
 +
| PA11
 +
| USART1_CTS
 +
| CANRX
 +
| TIM1_CH4
 +
| USBDM
 +
|-
 +
| PA12
 +
| USART1_RTS
 +
| CANTX
 +
| TIM1_ETR
 +
| USBDP
 +
|-
 +
| PA13
 +
| JTMS
 +
| SWDIO
 +
|
 +
|
 +
|-
 +
| PA14
 +
| JTCK
 +
| SWCLK
 +
|
 +
|
 +
|-
 +
| PA15
 +
| JTDI
 +
| TIM2_CH1
 +
| SPI1_NSS
 +
|
 +
|}
 +
 
 +
 
 +
{| class="wikitable" border="1"
 +
|+ '''Port B Pin Functions'''
 +
! Pin
 +
! Function 1
 +
! Function 2
 +
! Function 3
 +
! Function 4
 +
|-
 +
| PB0
 +
| ADC8
 +
| TIM3_CH3
 +
|
 +
|
 +
|-
 +
| PB1
 +
| ADC9
 +
| IM3_CH4
 +
|
 +
|
 +
|-
 +
| PB2
 +
| BOOT1
 +
|
 +
|
 +
|
 +
|-
 +
| PB3
 +
| JTDO
 +
| TIM2_CH2
 +
| SPI1_SCK
 +
|
 +
|-
 +
| PB4
 +
| JNTRST
 +
| TIM3_CH1
 +
| SPI1_MISO
 +
|
 +
|-
 +
| PB5
 +
| I2C1_SMBAI
 +
| TIM3_CH2
 +
| SPI1_MOSI
 +
|
 +
|-
 +
| PB6
 +
| I2C1_SCL
 +
| TIM4_CH1
 +
| USART1_TX
 +
|
 +
|-
 +
| PB7
 +
| I2C1_SDA
 +
| TIM4_CH2
 +
| USART1_RX
 +
|
 +
|-
 +
| PB8
 +
| TIM4_CH3
 +
| I2C1_SCL
 +
| CANRX
 +
|
 +
|-
 +
| PB9
 +
| TIM4_CH4
 +
| I2C1_SDA
 +
| CANTX
 +
|
 +
|-
 +
| PB10
 +
| I2C2_SCL
 +
| USART3_TX
 +
| TIM2_CH3
 +
|
 +
|-
 +
| PB11
 +
| I2C2_SDA
 +
| USART3_RX
 +
| TIM2_CH4
 +
|
 +
|-
 +
| PB12
 +
| SPI2_NSS
 +
| I2C2_SMBAI
 +
| USART3_CK
 +
| TIM1_BKIN
 +
|-
 +
| PB13
 +
| SPI2_SCK
 +
| USART3_CTS
 +
| TIM1_CH1N
 +
|
 +
|-
 +
| PB14
 +
| SPI2_MISO
 +
| USART3_RTS
 +
| TIM1_CH2N
 +
|
 +
|-
 +
| PB15
 +
| SPI2_MOSI
 +
| TIM1_CH3N
 +
|
 +
|
 +
|}
 +
 
 +
 
 +
{| class="wikitable" border="1"
 +
|+ '''Port C Pin Functions'''
 +
! Pin
 +
! Function 1
 +
|-
 +
| PC0
 +
| ADC10
 +
|-
 +
| PC0
 +
| ADC10
 +
|-
 +
| PC1
 +
| ADC11
 +
|-
 +
| PC2
 +
| ADC12
 +
|-
 +
| PC3
 +
| ADC13
 +
|-
 +
| PC4
 +
| ADC14
 +
|-
 +
| PC5
 +
| ADC15
 +
|-
 +
| PC6
 +
| TIM3_CH1
 +
|-
 +
| PC7
 +
| TIM3_CH2
 +
|-
 +
| PC8
 +
| TIM3_CH3
 +
|-
 +
| PC9
 +
| TIM3_CH4
 +
|-
 +
| PC10
 +
| USART3_TX
 +
|-
 +
| PC11
 +
| USART3_RX
 +
|-
 +
| PC12
 +
| USART3_CK
 +
|-
 +
| PC13
 +
| TAMPER-RTC
 +
|-
 +
| PC14
 +
| OSC32_IN
 +
|-
 +
| PC15
 +
| OSC32_OUT
 +
|}
 +
 
 +
 
 +
{| class="wikitable" border="1"
 +
|+ '''Port D Pin Functions'''
 +
! Pin
 +
! Function 1
 +
|-
 +
| PD0
 +
| OSC_IN
 +
|-
 +
| PD1
 +
| OSC_OUT
 +
|-
 +
| PD2
 +
| TIM3_ETR
 +
|}
  
 
==Programming==
 
==Programming==

Revision as of 13:08, 25 October 2010

STM32F103RET6 block diagram

Key features of the STM32F103RET6 microcontroller

Documents and files

Power saving levels

  1. Sleep mode (Cortex-M3 core stopped, peripherals kept running)
  2. Stop mode (all clocks are stopped)
  3. Standby mode (1.8V domain powered-off)

Clocks

STM32F103RET6 clock tree
HSI (8 MHz internal RC oscillator)
HSE (8 MHz external crystal oscillator)
LSI (40 kHz internal RC - watchdog, RTCCLK)
LSE (32768 Hz external crystal (RTCCLK))

PLLCLK  - Output from PLL, 8-64 MHz in 4 MHz steps, 72-128 MHz in 8 MHz steps
SYSCLK  - PLLCLK, HSI or HSE
AHBCLK  - SYSCLK / AHB prescaler (this clock has no name in the datasheet, except AHB peripheral clock)
PCLK1   - AHBCLK / APB1 prescaler - APB1 bus (max 36 MHz)
PCLK2   - AHBCLK / APB2 prescaler - APB2 bus (max 72 MHz)
USBCLK  - PLLCLK / USB prescaler
I2SCLK  - SYSCLK
SDIOCLK - AHBCLK
FSMCCLK - AHBCLK
FCLK    - AHBCLK
ADCCLK  - AHBCLK / (APB2 prescaler * ADC prescaler)

The MCO pin (PA8) can output one of SYSCLK, HSI, HSE, PLLCLK / 2
When the HSI is the PLL clock input, maximum system clock frequency is 64 MHz.
The SDIO AHB interface is clocked with a fixed frequency equal to AHBCLK / 2.
If the APB prescaler is 1, timers run at PCLK else at 2 * PCLK

Flash memory

  • 0-24 MHz - zero wait state
  • 24-48 MHz - one wait state
  • 48-72 MHz - two wait states

The prefetch buffer must be switched on/off only when SYSCLOCK is lower than 24 MHz.

Pins with special functions

PA9  ----------> ICL3232
PA10 <--|100R|-- ICL3232
PB2  - Boot1 22 kOhm to +3.3 V or Gnd
PC14 - 32 kHz crystal
PC15 - 32 kHz crystal
PD0  - 8 MHz crystal
PD1  - 8 MHz crystal

The JTAG pins are in input PU/PD after reset:
 PA15: JTDI in PU
 PA14: JTCK in PD
 PA13: JTMS in PU
 PB4: NJTRST in PU

Peripherials

64 kB SRAM, 512 kB FLASH
3 x 12 bit A/D converters
2 x 12 bit D/A converters
5 x USART
2 x Advanced-control timers (TIM1 and TIM8) can each be seen as a three-phase PWM multiplexed on 6 channels.
4 x General purpose timer TIM2, TIM3, TIM4 and TIM5 - 16-bit auto-reload up/down counter, a 16-bit prescaler and feature 4 independent channels each for input capture/output compare, PWM or onepulse mode output. This gives up to 16 input captures / output compares / PWMs 
2 x Basic timer TIM6 and TIM7 - These timers are mainly used for DAC trigger generation. They can also be used as a generic 16-bit time base.
3 x SPI
2 x I2S (multiplexed on SPI pins)
2 × I2C
1 x SD card interface 1/4/8 bit
1 x USB full speed
1 x CAN network interface
1 x CRC (cyclic redundancy check) calculation unit
1 x 24 bit systick timer
1 x RTC (real-time clock) and backup registers
2 x DMA controller, 5 + 7 channels
1 x Temperature sensor

A/D converters

  • ADC1_IN17 is connected to VREFINT 1.2 V (1.16 V - 1.24 V)
  • ADC1_IN16 is connected to the Temperature sensor
  • ADC2_IN16 and ADC2_IN17 are connected to VSS
  • ADC3_IN9, ADC3_IN14, ADC3_IN15, ADC3_IN16 and ADC3_IN17 are connected to VSS

The recommended sampling time for the internal channels is 17.1 μs. The TSVREFE bit must be set to enable both internal channels.

Timers

SYSTICK Independent timer within the ARM core
TIMER2 to TIMER5 16 bit general purpose timers

Flash uploader

Command line parameters for STMFlashLoader.exe:

   -c  --pn 3 --br 115200 --db 8 --sb 1 --to 1000 -i "STM32_High-density_512K" -e --all -d --fn "#H" --v

--pn 3 indicates com port: COM3

Important things to remember

  • Registers need a clock to latch the data, so enable all the the upstream clocks before trying to configure a peripheral
  • Pins with special functions

Pinout

Port A Pin Functions
Pin Function 1 Function 2 Function 3 Function 4
PA0 WKUP USART2_CTS ADC0 TIM2_CH1_ETR
PA1 USART2_RTS ADC1 TIM2_CH2
PA2 USART2_TX ADC2 TIM2_CH3
PA3 USART2_RX ADC3 TIM2_CH4
PA4 SPI1_NSS USART2_CK ADC4
PA5 SPI1_SCK ADC5
PA6 SPI1_MISO ADC6 TIM3_CH1
PA7 SPI1_MOSI ADC7 TIM3_CH2
PA8 USART1_CK TIM1_CH1 MCO
PA9 USART1_TX TIM1_CH2
PA10 USART1_RX TIM1_CH3
PA11 USART1_CTS CANRX TIM1_CH4 USBDM
PA12 USART1_RTS CANTX TIM1_ETR USBDP
PA13 JTMS SWDIO
PA14 JTCK SWCLK
PA15 JTDI TIM2_CH1 SPI1_NSS


Port B Pin Functions
Pin Function 1 Function 2 Function 3 Function 4
PB0 ADC8 TIM3_CH3
PB1 ADC9 IM3_CH4
PB2 BOOT1
PB3 JTDO TIM2_CH2 SPI1_SCK
PB4 JNTRST TIM3_CH1 SPI1_MISO
PB5 I2C1_SMBAI TIM3_CH2 SPI1_MOSI
PB6 I2C1_SCL TIM4_CH1 USART1_TX
PB7 I2C1_SDA TIM4_CH2 USART1_RX
PB8 TIM4_CH3 I2C1_SCL CANRX
PB9 TIM4_CH4 I2C1_SDA CANTX
PB10 I2C2_SCL USART3_TX TIM2_CH3
PB11 I2C2_SDA USART3_RX TIM2_CH4
PB12 SPI2_NSS I2C2_SMBAI USART3_CK TIM1_BKIN
PB13 SPI2_SCK USART3_CTS TIM1_CH1N
PB14 SPI2_MISO USART3_RTS TIM1_CH2N
PB15 SPI2_MOSI TIM1_CH3N


Port C Pin Functions
Pin Function 1
PC0 ADC10
PC0 ADC10
PC1 ADC11
PC2 ADC12
PC3 ADC13
PC4 ADC14
PC5 ADC15
PC6 TIM3_CH1
PC7 TIM3_CH2
PC8 TIM3_CH3
PC9 TIM3_CH4
PC10 USART3_TX
PC11 USART3_RX
PC12 USART3_CK
PC13 TAMPER-RTC
PC14 OSC32_IN
PC15 OSC32_OUT


Port D Pin Functions
Pin Function 1
PD0 OSC_IN
PD1 OSC_OUT
PD2 TIM3_ETR

Programming

Registers

  • note [y:x] refers to all bits in a register from bit x to bit y (inclusive)
Register Name Description
GPIOx_ODR Output Data Register sets the pins bit-state to that of the bits in this register, but only those pins configured in output mode

x denotes the port number, A to G, however, the stamp module only provides ports A to C

GPIOx_IDR Input Data Register reads the pins bit-state to that of the logic level applied to the pins

x denotes the port number, A to G, however, the stamp module only provides ports A to C

GPIOx_BSRR Port bit set/reset register sets or resets(un-sets) particular pins while leaving all other pins in their current state

if set and reset are both set, set has priority
x denotes the port number, A to G, however, the stamp module only provides ports A to C

[31:16] [15:0]
bits that are 1 clear the pins(0-15) bits that are 1 set the pins(0-15)
GPIOx_CRL Port configuration register low sets pin modes, function, speed, and direction for pins 0-7

x denotes the port number, A to G, however, the stamp module only provides ports A to C

[30:31] [29:28] [27:26] [25:24] [23:22] [21:20] [19:18] [17:16] [15:14] [13:12] [11:10] [9:8] [7:6] [5:4] [3:2] [1:0]
Pin7 Function Pin7 Mode Pin6 Function Pin6 Mode Pin5 Function Pin5 Mode Pin4 Function Pin4 Mode Pin3 Function Pin3 Mode Pin2 Function Pin2 Mode Pin1 Function Pin1 Mode Pin0 Function Pin0 Mode


Modes
Bits Description
00 Input mode (reset state)
01 Output mode, max speed 10 MHz
10 Output mode, max speed 2 MHz
11 Output mode, max speed 50 MHz


Input Functions
Bits Description
00 Analog mode
01 Floating input (reset state)
10 Input with pull-up / pull-down
11 Reserved (do not use)


Output Functions
Bits Description
00 General purpose output push-pull
01 General purpose output Open-drain
10 Alternate function output Push-pull
11 Alternate function output Open-drain
GPIOx_CRH Port configuration register high sets pin modes, function, speed, and direction for pins 8-15

x denotes the port number, A to G, however, the stamp module only provides ports A to C

[30:31] [29:28] [27:26] [25:24] [23:22] [21:20] [19:18] [17:16] [15:14] [13:12] [11:10] [9:8] [7:6] [5:4] [3:2] [1:0]
Pin15 Function Pin15 Mode Pin14 Function Pin14 Mode Pin13 Function Pin13 Mode Pin12 Function Pin12 Mode Pin11 Function Pin11 Mode Pin10 Function Pin10 Mode Pin9 Function Pin9 Mode Pin8 Function Pin8 Mode


Modes
Bits Description
00 Input mode (reset state)
01 Output mode, max speed 10 MHz
10 Output mode, max speed 2 MHz
11 Output mode, max speed 50 MHz


Input Functions
Bits Description
00 Analog mode
01 Floating input (reset state)
10 Input with pull-up / pull-down
11 Reserved (do not use)


Output Functions
Bits Description
00 General purpose output push-pull
01 General purpose output Open-drain
10 Alternate function output Push-pull
11 Alternate function output Open-drain