Warren Gay

Beginning STM32

Developing with FreeRTOS, libopencm3 and GCC

Warren Gay

St. Catharines, Ontario, Canada

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.​apress.​com/​978-1-4842-3623-9 . For more detailed information, please visit http://​www.​apress.​com/​source-code .

ISBN 978-1-4842-3623-9

e-ISBN 978-1-4842-3624-6

https://doi.org/10.1007/978-1-4842-3624-6

Library of Congress Control Number: 2018945101

© Warren Gay 2018

Standard Apress

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, email [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For Jackie

Table of Contents

  1. Chapter 1:​ Introduction
    1. STM32F103C8T6
    2. FreeRTOS
    3. libopencm3
    4. No Arduino
    5. No IDE
    6. Development Framework
    7. Assumptions About You
    8. What You Need
      1. ST-Link V2 Programming Unit
      2. Breadboard
      3. DuPont (Jumper) Wires
      4. 0.​1 uF Bypass Capacitors
      5. USB TTL Serial Adapter
      6. Power Supply
      7. Small Stuff
    9. Summary
  2. Chapter 2:​ Software Setup
    1. Directory Conventions Used
    2. Operating Software
    3. Book Software
      1. libopencm3
      2. FreeRTOS
      3. ~/​stm32f103c8t6/​rtos/​Project.​mk
    4. ARM Cross Compiler
    5. Build the Software
    6. ST-Link Tool
    7. Summary
  3. Chapter 3:​ Power Up and Blink
    1. Power
      1. +3.​3V Regulator
      2. USB Power/​+5V
      3. +3.​3V Supply
      4. One Power Source Rule
    2. Ground
    3. Reset
    4. Showtime
    5. ST-Link V2
    6. st-flash Utility
      1. Read STM32
      2. Write Image
      3. Erase Flash
    7. Summary
    8. Bibliography
  4. Chapter 4:​ GPIO
    1. Building miniblink
    2. Flashing miniblink
    3. miniblink.​c Source Code
    4. GPIO API
      1. GPIO Configuration
    5. Ducks in a Row
      1. GPIO Inputs
      2. Digital Output, Push/​Pull
      3. Digital Output, Open Drain
    6. GPIO Characteristics
      1. Input Voltage Thresholds
      2. Output-Voltage Thresholds
    7. Programmed Delays
      1. The Problem with Programmed Delay
    8. Summary
  5. Chapter 5:​ FreeRTOS
    1. FreeRTOS Facilities
      1. Tasking
      2. Message Queues
      3. Semaphores and Mutexes
      4. Timers
      5. Event Groups
    2. The blinky2 Program
      1. Build and Test blinky2
      2. Execution
    3. FreeRTOSConfig.​h
    4. FreeRTOS Naming Convention
      1. FreeRTOS Macros
    5. Summary
  6. Chapter 6:​ USART
    1. USART/​UART Peripheral
      1. Asynchronous Data
    2. USB Serial Adapters
      1. Hookup
    3. Project uart
    4. Project
    5. Project uart2
    6. USART API
      1. Include Files
      2. Clocks
      3. Configuration
      4. DMA
      5. Interrupts
      6. Input/​Output/​Status
    7. Ducks-in-a-Row
    8. FreeRTOS
      1. Tasks
      2. Queues
    9. Summary
  7. Chapter 7:​ USB Serial
    1. Blue Pill USB Issue
    2. Introduction to USB
      1. Pipes and Endpoints
    3. USB Serial Device
      1. Linux USB Serial Device
      2. MacOS USB Serial Device
      3. Windows USB Serial Device
      4. USB GPIO
    4. Demo Source Code
      1. cdcacm_​set_​config()
      2. cdc_​control_​request()
      3. cdcacm_​data_​rx_​cb()
      4. USB Task
      5. USB Receiving
      6. USB Sending
    5. USB Serial Demo
    6. Summary
    7. Bibliography
  8. Chapter 8:​ SPI Flash
    1. Introducing W25QXX
    2. Serial Peripheral Interface Bus
      1. Chip Select
      2. Wiring and Voltages
    3. SPI Circuit
      1. Hardware /​NSS Control
    4. STM32 SPI Configuration
      1. SPI Clock Rate
      2. SPI Clock Modes
      3. Endianess and Word Length
    5. SPI I/​O
      1. Read SR1
      2. Waiting for Ready
      3. Read Manufacturer ID
      4. Writing Flash
      5. Flash Erase
      6. Reading Flash
    6. Demonstration
      1. Running the Demo
      2. Manufacturer ID
      3. Power Down
      4. Summary
    7. Bibliography
  9. Chapter 9:​ Code Overlays
    1. The Linker Challenge
      1. MEMORY Section
      2. Entry
      3. Sections
      4. PROVIDE
      5. Relocation
    2. Defining Overlays
      1. Overlay Code
      2. Overlay Stubs
      3. Overlay Manager
      4. VMA and Load Addresses
      5. Linker Symbols in Code
      6. Overlay Manager Function
      7. Overlay Stubs
    3. Demonstration
      1. Extracting Overlays
      2. Upload Overlays to W25Q32
      3. Overlay Demo Continued
      4. Code Change Trap
    4. Summary
    5. Bibliography
  10. Chapter 10:​ Real-Time Clock (RTC)
    1. Demonstration Projects
    2. RTC Using One Interrupt
      1. RTC Configuration
      2. Interrupt and Setup
      3. Interrupt Service Routine
      4. Task Notification
      5. Mutexes
    3. Demonstration
      1. UART1 Connections
      2. Running the Demo
    4. rtc_​alarm_​isr()
      1. EXTI Controller
    5. Summary
  11. Chapter 11:​ I2C
    1. The I2C Bus
      1. Master and Slave
      2. Start and Stop
      3. Data Bits
      4. I2C Address
      5. I2C Transactions
    2. PCF8574 GPIO Extender
    3. I2C Circuit
      1. The PCF8574 INT Line
    4. PCF8574 Configuration
      1. PCF8574 GPIO Drive
      2. Wave Shaping
    5. Demo Circuit
      1. EXTI Interrupt
    6. I2C Software
      1. Testing I2C Ready
      2. Start I2C
      3. I2C Write
      4. I2C Read
      5. I2C Restart
    7. Demo Program
      1. Demo Session
    8. Summary
  12. Chapter 12:​ OLED
    1. OLED Display
      1. Configuration
      2. Display Connections
    2. Display Features
    3. Demo Schematic
    4. AFIO
    5. Graphics
      1. The Pixmap
      2. Pixmap Writing
      3. The Meter Software
      4. Main Module
    6. Demonstration
    7. Summary
  13. Chapter 13:​ OLED Using DMA
    1. Challenges
      1. Circuit
    2. DMA Operation
      1. DMA Execution
    3. The Demonstration
      1. Initializing DMA
      2. Launching DMA
      3. OLED SPI/​DMA Management Task
      4. DMA ISR Routine
      5. Restarting DMA Transfers
    4. Executing the Demo
      1. Further Challenges
    5. Summary
  14. Chapter 14:​ Analog-to-Digital Conversion
    1. STM32F103C8T6 Resources
    2. Demonstration
      1. Analog Inputs PA0 and PA1
      2. ADC Peripheral Configuration
    3. Demonstration Run
      1. Reading ADC
    4. Analog Voltages
    5. Summary
    6. Bibliography
  15. Chapter 15:​ Clock Tree
    1. In the Beginning
      1. RC Oscillators
      2. Crystal Oscillators
      3. Oscillator Power
    2. Real-time Clock
    3. Watchdog Clock
    4. System Clock (SYSCLK)
      1. SYSCLK and USB
    5. AHB Bus
      1. rcc_​clock_​setup_​in_​hse_​8mhz_​out_​72mhz()
      2. APB1 Peripherals
      3. APB2 Peripherals
      4. Timers
    6. rcc_​set_​mco()
    7. HSI Demo
    8. HSE Demo
    9. PLL ÷ 2 Demo
    10. Summary
    11. Bibliography
  16. Chapter 16:​ PWM with Timer 2
    1. PWM Signals
    2. Timer 2
    3. PWM Loop
      1. Calculating Timer Prescale
      2. 30 Hz Cycle
    4. Servo Hookup
    5. Running the Demo
    6. PWM on PB3
    7. Other Timers
      1. More PWM Channels
    8. Summary
    9. Bibliography
  17. Chapter 17:​ PWM Input with Timer 4
    1. The Servo Signal
    2. Signal Voltage
    3. Demo Project
      1. GPIO Configuration
      2. Timer 4 Configuration
      3. Task1 Loop
      4. ISR Routine
    4. Demonstration Run
      1. Session Output
    5. Timer Inputs
    6. Summary
  18. Chapter 18:​ CAN Bus
    1. The CAN Bus
    2. Differential Signals
      1. Dominant/​Recessive
    3. Bus Arbitration
      1. Synchronization
      2. Message Format
    4. STM32 Limitation
    5. Demonstration
      1. Software Build
      2. UART Interface
      3. MCU Flashing
      4. Demo Bus
    6. Session Run
      1. CAN Messages
      2. Synchronicity
    7. Summary
    8. Bibliography
  19. Chapter 19:​ CAN Bus Software
    1. Initialization
      1. can_​init()
      2. CAN Receive Filters
      3. CAN Receive Interrupts
      4. Application Receiving
      5. Sending CAN Messages
    2. Summary
  20. Chapter 20:​ New Projects
    1. Project Creation
      1. Makefile
      2. Included Makefiles
      3. Header Dependencies
      4. Compile Options
      5. Flashing 128k
    2. FreeRTOS
      1. rtos/​opencm3.​c
      2. rtos/​heap_​4.​c
      3. Required Modules
    3. FreeRTOSConfig.​h
    4. User Libraries
    5. Rookie Mistakes
    6. Summary
    7. Bibliography
  21. Chapter 21:​ Troubleshooting
    1. Gnu GDB
      1. GDB Server
      2. Remote GDB
      3. GDB Text User Interface
    2. Peripheral GPIO Trouble
    3. Alternate Function Fail
    4. Peripheral Fail
    5. ISR FreeRTOS Crash
    6. Stack Overflow
    7. Estimating Stack Size
    8. When a Debugger Doesn’t Help
    9. Push/​Pull or Open Drain
    10. Peripheral Defects
    11. Resources
      1. libopencm3
    12. FreeRTOS Task Priorities
      1. Scheduling Within libopencm3
    13. Summary
  22. Appendix A:​ Answers to Exercises
    1. Chapter 4
    2. Chapter 5
    3. Chapter 6
    4. Chapter 7
    5. Chapter 8
    6. Chapter 9
    7. Chapter 10
    8. Chapter 11
    9. Chapter 12
    10. Chapter 13
    11. Chapter 14
    12. Chapter 15
    13. Chapter 16
    14. Chapter 17
    15. Chapter 19
    16. Chapter 20
  23. Appendix B:​ STM32F103C8T6 GPIO Pins
  24. Index

About the Author and About the Technical Reviewer

About the Author

Warren Gay started out in electronics at an early age, dragging discarded TVs and radios home from public school. In high school he developed a fascination for programming the IBM 1130 computer, which resulted in a career-plan change to software development. Since graduating from Ryerson Polytechnical Institute, he has enjoyed a 30-plus-year software developer career, programming mainly in C/C++. Warren has been programming Linux since 1994 as an open source contributor and professionally on various Unix platforms since 1987.

Before attending Ryerson, Warren built an Intel 8008 system from scratch before there were CP/M systems and before computers got personal. In later years, Warren earned an advanced amateur radio license (call sign VE3WWG) and worked the amateur radio satellites. A high point of his ham-radio hobby was making digital contact with the Mir space station (U2MIR) in 1991.

Warren works at Datablocks.net, an enterprise-class ad-serving software services company where he programs C++ server solutions on Linux back-end systems.

About the Technical Reviewer

Stewart Watkiss is a keen maker with a particular interest in physical computing. He earned a master’s degree in electronic engineering from the University of Hull in 1996 and a master’s degree in computer science from the Georgia Institute of Technology in 2017.

Most of his projects are based around the Raspberry Pi, which he creates alone or together with his two children. He has also created projects based around the Arduino and other platforms. Many of his projects are available on his website, www.penguintutor.com , which also provides tutorials on Linux and electronics. He is the author of the book Learn Electronics with Raspberry Pi , published by Apress.

Stewart also volunteers as a STEM Ambassador, working with local schools and educational events to enthuse children about programming and physical computing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.54.6