Embedded Programming with ARM Cortex-M3 Basic Experiments...

20
Embedded Programming with ARM Cortex-M3 Basic Experiments 1 Alan Xiao, Ph.D Handheld Scientific, Inc. [email protected]

Transcript of Embedded Programming with ARM Cortex-M3 Basic Experiments...

Page 1: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Embedded Programming with ARM Cortex-M3

Basic Experiments 1

Alan Xiao, Ph.D

Handheld Scientific, Inc.

[email protected]

Page 2: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Today’s Topics

Basics (with the Discovery board): 1. General Input/Output (GPIO) 2. USART (serial) 3. SysTick 4. Unique ID 5. EXTI - External Interrupt 6. Real-time Clock/Calendar 7. Timer/PWM 8. Watchdogs (independent/window) 9. CRC (Cyclic Redundancy Check) 10. PVD (Programmable Voltage

Detector) 11. Sleep/Stop/Standby Modes

Advanced (with the STM3210C or similar board):

1. I2C/EEPROM 2. ADC 3. RS-485 4. LCD display/Touch Screen 5. SPI and Sensors 6. I2S Audio 7. Wireless (with external 315M and 2.4G modules) 8. USB Mouse and Keyboard 9. USB DFU (device firmware upgrade) 10. USB HID 11. USB Virtual COM Port 12. USB PCM Audio 13. Ethernet – Telnet 14. Ethernet – HTTP (i.e., control hardware via a web

browser!) 15. Ethernet – TFTP 16. USB Host 17. Running an OS on Cortex-M3

Copyright 2011 (c), Alan Xiao 2

Page 3: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Getting USART/Serial Port to work

• It is essential to get the USART to work. This is the conduit to the outside world. Most code examples will use the USART to display the results/messages.

• See code example on the intro session on how to get USART to work.

Copyright 2011 (c), Alan Xiao 3

Page 4: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

TTL/RS232 Level Consideration

• The USART IO in Discovery board is TTL level. To communicate with a PC: – If you would like to use the RS-232 serial port (9 or

25pin D connector) on your PC (assuming there is one), you need a TTL to RS-232 level convertor such as a UT232R-200 from FTDI or a MAX3232 chip.

– If you would like to use an USB port, you need an USB to USART bridge such as an UB232R module from FTDI.

• In either case, only 3 wires are needed: Ground, RX<->TX, TX<-> RX.

Copyright 2011 (c), Alan Xiao 4

Page 5: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Discovery and FTDI UB232R on a Bread Board

Copyright 2011 (c), Alan Xiao 5

Page 6: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

STM32 Memory Map

Copyright 2011 (c), Alan Xiao 6

• 32-bit address: 4G addressable space.

• Everything has an address: registers, Flash, SRAM, etc.

• Flash starts at 0x08000000. SRAM starts at 0x20000000. That is probably all we need to know at the moment.

Page 7: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

STM32 Boot Modes • STM32 can boot from one of the following: User Flash Memory,

System Memory and Embedded SRAM. This is controlled by two pins:

• System memory has a built-in serial boot loader for burning firmware into the chip without restoring to JTAG/SWD port.

• Booting and running program from RAM is faster. • On the Discovery, both BOOT1 and BOOT2 are fixed to 0 so it only

boots from user flash memory (although you can remove the soldered connectors to change that).

Copyright 2011 (c), Alan Xiao 7

Page 8: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

IAR Linker Memory Setting

Copyright 2011 (c), Alan Xiao 8

Page 9: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

SysTick Timer • 24-bit countdown timer. It generates an interrupt when

reaching zero and reloads value. • The Library greatly simplifies its use. Simply call the

following function: SysTick_Config(number_of_clock_cycles_in_a_period)

• Parameter can’t exceeds 2^24 (about 16M). So for a 24M clock, the longest period is 16/24=667ms.

• If you need more control, mess up with the registers directly: – Control & Status Register (enable, clock source, etc.). – Reload value – Current value

• Code example: simply print out a message in interrupt.

Copyright 2011 (c), Alan Xiao 9

Page 10: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Unique Device ID • Each STM32 device has a 96-bit (12 bytes) unique ID

(guaranteed to be unique among the device family).

• Useful for: – product serial number

– Security key for encryptions, for instance.

– To activate secure boot processer, etc.

• ID can be read as byte, half-word or word.

• Located in 0x1FFFF7E8 ~ 0x1FFFF7F3 memory location.

• See code example how to get it.

Copyright 2011 (c), Alan Xiao 10

Page 11: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Interrupt Priority • Two priorities: Preemptive Priority and Sub-priority.

Lower number has higher priority. • STM32 uses 4 bits in a register to specify the two

priority settings. To be more flexible, it allows to specify the number of bits in that 4 bits for preemptive priority (the rest is for sub-priority). – Group 0: 0 bit for preemptive priority. 4 bits for sub-priority. – Group 1: 1 bit for preemptive priority. 3 bits for sub-priority. – … – Group 4: 4 bits for preemptive priority. No bits for sub-

priority.

• Use NVIC_PriorityGroupConfig(NVIC_PriorityGroup_N) (n=0, 1, 2, 3 or 4) for the setting. Copyright 2011 (c), Alan Xiao 11

Page 12: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

External Interrupts (EXTI)

Copyright 2011 (c), Alan Xiao 12

• There are 16 EXTI lines: – PA0, PB0,…, PF0 use the same line EXTI_Line0 – PA1, PB1,…, PF1 use the same line EXTI_Line1 – PA15, PB15,…, PF15 use the same line EXTI_Line15

• Use GPIO_EXTILineConfig() to tell the MCU which pin is being used.

Page 13: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

External Interrupts (EXTI) • EXTI Line 0 – 4 have their own dedicate interrupt

source. EXTI Line 5-9 share one interrupt source, so do EXTI Line 10-15.

• Handler Names: – EXTI0_IRQHandler(); …, EXTI4_IRQHandler() – EXTI9_5_IRQHandler(); EXTI15_10_IRQHandler()

• To set up EXTI: – Setup GPIO. Turn on AFIO (alternate function) – Setup EXTI Source – Setup EXTI Line – Setup Interrupt – Populate Interrupt handler

• Much better than polling. No debouncing is necessary!

Copyright 2011 (c), Alan Xiao 13

Page 14: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

External Interrupts (EXTI) • For EXTI line 5-15, in the interrupt handler, need

to have some code to determine which line generated the interrupt. Something like this: void EXTI9_5_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line5) != RESET) { EXTI_ClearITPendingBit(EXTI_Line5); } else if (EXTI_GetITStatus(EXTI_Line6) != RESET) { EXTI_ClearITPendingBit(EXTI_Line6); } … }

Copyright 2011 (c), Alan Xiao 14

Page 15: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Power Modes

• Run – Normal operation.

• Sleep – Core stopped. Peripherals kept running

• Stop – all clocks are stopped

• Standby – all powered off except backup domain (backup registers and RTC).

Copyright 2011 (c), Alan Xiao 15

Page 16: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Power Saving Modes

Copyright 2011 (c), Alan Xiao 16

Page 17: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Power Saving Modes

• Power consumption in different power modes (very rough numbers): – Normal (run): from a few mA to 50 mA (depending

largely on the peripherals and clock speed) – Sleep: from a few mA to 30mA – Stop: 20 – 200uA – Standby: a few uA – Backup domain and RTC (on VBAT pin): 2uA.

• A good power-saving strategy is to put the MCU in stop/standby mode and wake it up periodically using RTC or external trigger.

Copyright 2011 (c), Alan Xiao 17

Page 18: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Power Saving Modes • Use the following Library calls to enter

different power modes: – PWR_EnterSTOPMode(PWR_Regulator_LowPow

er, PWR_STOPEntry_WFE);

– PWR_EnterSTANDBYMode();

• Stop mode: external interrupt can wake it up.

• Standby mode: only the wakeup pin (and reset) can wake it up.

• See code example.

Copyright 2011 (c), Alan Xiao 18

Page 19: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

Real-time Clock (RTC) • Backup domain: RTC and 42 backup registers. Their

power is provided by a dedicated pin (VBAT) which is usually connected to a battery (not in the Discovery board though).

• The backup domain is not affected by system reset (i.e., the RTC keeps running even during reset. The backup register values also survive resets).

• The RTC is driven by a dedicate clock, either internally or more often, an external 32.768K crystal.

• After reset, the access to the backup domain is prohibited until it is explicitly enabled, to prevent accidental writing.

• RTC can generate second interrupts or alarm interrupts.

Copyright 2011 (c), Alan Xiao 19

Page 20: Embedded Programming with ARM Cortex-M3 Basic Experiments 1files.meetup.com/1952191/Cortex_Programming_02_Basic_1.pdf · Embedded Programming with ARM Cortex-M3 Basic Experiments

What’s Next

Basics (with the Discovery board): 1. General Input/Output (GPIO) 2. USART (serial) 3. SysTick 4. Unique ID 5. EXTI - External Interrupt 6. Real-time Clock/Calendar 7. Timer/PWM 8. Watchdogs (independent/window) 9. CRC (Cyclic Redundancy Check) 10. PVD (Programmable Voltage

Detector) 11. Sleep/Stop/Standby Modes

Advanced (with the STM3210C or similar board):

1. I2C/EEPROM 2. ADC 3. RS-485 4. LCD display/Touch Screen 5. SPI and Sensors 6. I2S Audio 7. Wireless (with external 315M and 2.4G modules) 8. USB Mouse and Keyboard 9. USB DFU (device firmware upgrade) 10. USB HID 11. USB Virtual COM Port 12. USB PCM Audio 13. Ethernet – Telnet 14. Ethernet – HTTP (i.e., control hardware via a web

browser!) 15. Ethernet – TFTP 16. USB Host 17. Running an OS on Cortex-M3

Copyright 2011 (c), Alan Xiao 20