21
Introduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld Scientific, Inc. [email protected]

Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

  • Upload
    leque

  • View
    267

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Introduction to Embedded Programming with ARM Cortex-M3

Alan Xiao, Ph.D

Handheld Scientific, Inc.

[email protected]

Page 2: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Why ARM • ARM is a 32-bit RISC instruction set architecture

developed by ARM Holdings.

• Over 640 processor licenses sold to more than 200 companies

• Over 17 billion ARM based chips shipped to date

• ARM chips are used in 99% of the world’s cellphones, tablets and other mobile devices.

• Microsoft announced support of ARM in Windows 8.

• ARM Techcon conference held every year in Santa Clara

Copyright 2011 (c), Alan Xiao 2

Page 3: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Latest ARM Family ARM Family ARM Arch ARM-Core

ARM9TDMI ARMv4T ARM9TDMI, ARM920T, ARM922T, ARM940T

ARM9E ARMv5TE ARM946E-S, ARM966E-S, RM968E-S, ARM966HS

ARMv5TEJ ARM926EJ-S

XScale ARMv5TE XScale, Bulverde, Monahans

ARM11 ARMv6 ARM1136J(F)-S

ARMv6T2 ARM1156T2(F)-S

ARMv6ZK ARM1176JZ(F)-S

ARMv6K ARM11-MPCore

Cortex-A ARMv7-A Cortex-A8, Cortex-A9, Cortex-A15

Cortex-R ARMv7-R

Cortex-M ARMv6-M Cortex-M0, Cortex-M1

ARMv7-M Cortex-M3

ARMv7-ME Cortex-M4 Copyright 2011 (c), Alan Xiao 3

Page 4: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

ARM Cortex A Family

ARM Core Features Application Devices

Cortex-A8 600MHz – 1G Smartphones, tablets, set-top boxes, etc.

Apple A4 (iPhone, iPad), Freescale iMX51, Qualcomm Snapdragon, TI OMAP3

Cortex-A9 Multi-core Apple A5, TI OMap4

Cortex-A15 1-2GHz Multicore Competing with x86 (Intel/AMD)

A15 devices will be available Q4/2012 or early 2013.

The Cortex-M Family is primarily for rich OS platforms such as smartphones and tablets

Copyright 2011 (c), Alan Xiao 4

Page 5: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

ARM Cortex-M Family

ARM Core Features Application Devices

Cortex-M0 Lowest power, smallest ARM MCU. Clock to 24MHz.

Low-end micro-controller

NXP LPC11xx

Cortex-M1 Developed Specifically for FPGA implementation

In FPGA Soft Core for major FPGA: Altera, Xilinx, Actel.

Cortex-M3 High Performance MCU. Up to 120MHz

Mid to high-end micro-controller

NXP LPC17xx, ST Micro STM32

Cortex-M4 DSP Instructions. Memory Protection Unit. Up to 150MHz

High-end Computation-intensive embedded applications

NXP LPC43xx (dual core M4/M0)

The Cortex-M Family is primarily for low-power micro-controller applications.

Copyright 2011 (c), Alan Xiao 5

Page 6: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

ARM Cortex-M3 Micro-controllers

• They are micro-controllers

– Micro-controllers: have everything in them (flash, ram, peripherals, etc.)

– Micro-processors: usually need supporting chips (memory, peripheral bridges, etc.)

• From many different companies (NXP/Phillips, TI, Atmel, too many to list. See list in the tool).

• Use the same core so they are essentially the same in the heart: learn one, use any.

Copyright 2011 (c), Alan Xiao 6

Page 7: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Cortex-M3 and Operating System • What operating systems can they run?

– No Linux/WinCE/Android (since those normally require MMU).

– Can run: uC/OSII (Micrium), FreeRTOS (freeRTOS.org), eCOSPro (eCosCentric), CMS (CMX Systems), etc.

• Carefully consider whether the application needs an OS: – Event driven model, SysTick, timer, watchdogs and

DMA have powerful OS-like capability – OS adds cost and complexity. Hard to debug. – If the app really needs an OS, consider ARM9/ARM11.

Copyright 2011 (c), Alan Xiao 7

Page 8: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Which Cortex-M3? • STM32 series from STMicroelectronics

• Why? – Wide range of devices from low-cost (<$2) value

line to very powerful STM32F2 (150MHz), all pin and software compatible

– Software library to ease software development pain (huge advantage)

– Good selection of development boards

– Excellent documentation and code examples

– Supported by all major development tool chains (IAR, Keil, TrueStudio, etc.)

Copyright 2011 (c), Alan Xiao 8

Page 9: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

The (big) STM32 Family

Copyright 2011 (c), Alan Xiao 9

Page 10: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

STM32 Portfolio

Copyright 2011 (c), Alan Xiao 10

Page 11: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Which Development Tool? • Software:

– Tier 1: IAR, Keil

– Tier 2: TrueStudio, Hitex

– Tier 3: Eclipse, notepad + command line

• In circuit debuggers/programmers:

– Segger JLink

– Keil ULink2

– ST Link

– OpenJTAG, etc. Copyright 2011 (c), Alan Xiao 11

Page 12: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

IAR Embedded Workbench

• Trial version (either size limited or time limited) is readily available.

• For not only ARM but many other chips (8051, TI MSP430, Atmel AVR/AVR32, PIC, etc.)

• Supports most ARM chips

• Supports most in-circuit debuggers

• Fast compilation time

• Flexibility for advanced users

Copyright 2011 (c), Alan Xiao 12

Page 13: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

STM32 Discovery Kit • Value line STM32F100RB MCU,

128KB Flash, 8KB RAM in 64-pin LQFP. 24MHz Clock

• On-board ST-Link In-circuit debugger/programmer

• Two LEDs, one user push button and one reset button

• 32K Real-time clock crystal • Extension header for all 64 IOs • Powered by USB • Very low cost ($12). Perfect for

starters.

Copyright 2011 (c), Alan Xiao 13

Page 14: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

STM3210C-EVAL Development Board

• STM32F107VCT connectivity line MCU, 256K Flash, 64K RAM, 72MHz clock.

• 3.2” TFT LCD with touch screen.

• JTAG and Debug Wire connector

• No ICD but can use the ICD in Discovery kit

• Relatively costly ($300). Several similar boards from other companies for a lot less (half)

Copyright 2011 (c), Alan Xiao 14

Page 15: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

STM3210C-EVAL Development Board

• Connectivity features: – USB-OTG full speed. USB MicroAB connector.

– IEEE 802.3 compliant Ethernet

– 2 CAN’s (Controller Area Network)

– 5 USARTs, 3 SPIs, 2 IICs.

– I2S (audio), IrDA, LIN (Local Interconnect network)

• Other features: – Smart card

– Motor control

Copyright 2011 (c), Alan Xiao 15

Page 16: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Device Peripheral Library • Embedded programming is all about initializing

and controlling the peripherals (IOs, USARTs, SPIs, etc.)

• 32-bit cortex is a lot more powerful and therefore more complex to setup and use

• The Device Peripheral Library is to shield the complexity from the users.

• Provides initialization and other major functions

• Down sides: more typing, larger code size and bugs (rare).

Copyright 2011 (c), Alan Xiao 16

Page 17: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

General Purpose Input/Output (GPIO) • Each IO pin can be configured as:

– Input Analog – Input Floating – Input pull down – Input pull up – Output open drain – Output pull-push – Alternate function open drain – Alternate function pull-push

• Need to specify the max speed of IO, for the purpose of saving energy.

• Need to turn on the clock of the peripheral

Copyright 2011 (c), Alan Xiao 17

Page 18: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Example 1: General Purpose IO

• Use Peripheral Device Library

• Polling model (versus interrupt/event model)

• Blinking LED. See code

Copyright 2011 (c), Alan Xiao 18

Page 19: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

Example 2: USART (Serial Port)

• USART/Serial was the most widely used low speed communication protocol before USB

• Two signal levels: TTL and RS-232

• Full duplex with speed up to a few hundreds K bps.

• Have to specify: – Baud rate

– Protocol (starting bit, stop bit, parity)

• Use Interrupt. See code.

Copyright 2011 (c), Alan Xiao 19

Page 20: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

USART (con’t) 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 20

Page 21: Introduction to Embedded Programming with ARM …files.meetup.com/1952191/Cortex_Programming_01_Intro.pdfIntroduction to Embedded Programming with ARM Cortex-M3 Alan Xiao, Ph.D Handheld

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 21