Beginning STM32: Developing with FreeRTOS, libopencm3, and GCC

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

See how using FreeRTOS and libopencm3 instead of the Arduino software environment will help you develop multi-tasking applications that go beyond Arduino norms. This updated version includes expanded coverage of software setup for Windows 10 and 11, additional TM32 hardware capabilities, including clear explanations of "totem pole outputs" versus "open drain outputs," and a new section on project showcasing an interrupt-driven approach for processing USART data.

Each chapter contains clear explanations of the STM32 hardware capabilities to help get you started with the device, including GPIO and several other ST Microelectronics peripherals like USB and CAN bus controller. You’ll learn how to download and set up the libopencm3 + FreeRTOS development environment, using GCC. With everything set up, you’ll leverage FreeRTOS to create tasks, queues, and mutexes. You’ll also learn to work with the I2C bus to add GPIO using the PCF8574 chip. And how to create PWM output for RC control using hardware timers.

You'll be introduced to new concepts that are necessary to master the STM32, such as how to extend code with GCC overlays using an external Winbond ​W25Q32 flash chip. Your knowledge is tested at the end of each chapter with exercises. Upon completing this book, you’ll be ready to work with any of the devices in the STM32 family. 
Beginning STM32, Second Edition provides the professional, student, or hobbyist a way to learn about ARM without costing an arm!
What You'll Learn
  • Initialize and use the libopencm3 drivers and handle interrupts
  • Use DMA to drive a SPI based OLED displaying an analog meter
  • Read PWM from an RC control using hardware timers
  • Understand STM32 capabilities, including USB controller, RTC, DMA, CAN bus, and more 
Who This Book Is For
Experienced embedded engineers, students, hobbyists and makers wishing to explore the ARM architecture, going beyond Arduino limits.

Author(s): Warren Gay
Series: Maker Innovations Series
Edition: 2
Publisher: Apress
Year: 2024

Language: English
Commentary: Publisher PDF | Published: 26 March 2024
Pages: xxiii, 527
City: Berkeley, CA
Tags: STM32; STM32F103; STM32F103C8T6; ARM; ARM Cortex; Cortex-M3; Blue Pill; Red Pill; Black Pill; STMicroelectronics; GCC; libopencm3; FreeRTOS; USB; CAN bus; Arduino

Table of Contents
About the Author
Chapter 1: Introduction
STM32F103C8T6
FreeRTOS
libopencm3
No Arduino
No IDE
Development Framework
Assumptions About You
What You Need
ST-Link V2 Programming Unit
Breadboard
DuPont (Jumper) Wires
0.1 uF Bypass Capacitors
USB TTL Serial Adapter
Power Supply
Small Stuff
Summary
Chapter 2: Software Setup
Directory Conventions Used
Operating Software
Book Software
Cloning with a github Account
Anonymous github Download
libopencm3
FreeRTOS Kernel
ARM Cross Compiler
Build the Software
ST-Link Tool
Windows User Installs
Linux Account and Password
Update the WSL
Starting Ubuntu
Updating Ubuntu
Install Dependencies
Windows Install usbipd
Windows Ubuntu Software for usbipd
STM32 ST-LINK Utility
Summary
Chapter 3: Power Up and Blink
Power
+3.3V Regulator
USB Power/+5V
+3.3V Supply
One Power Source Rule
Ground
Reset
Showtime
ST-Link V2
st-flash Utility
Read STM32
Write Image
Erase Flash
Windows STM32 ST-LINK Utility
Function Info
Function Read
Function Write
Function Erase
Summary
Bibliography
Chapter 4: GPIO
Building miniblink
Flashing miniblink
miniblink.c Source Code
GPIO API
GPIO Configuration
Input Ports
Output Ports
Push/Pull or Totem Pole Outputs
Open-Drain Outputs
GPIO_MODE_OUTPUT_*_MHZ
Ducks in a Row
GPIO Inputs
Digital Output, Push/Pull
Digital Output, Open Drain
GPIO Characteristics
Input Voltage Thresholds
Output-Voltage Thresholds
Programmed Delays
The Problem with Programmed Delay
Summary
Chapter 5: FreeRTOS
FreeRTOS Facilities
Tasking
Message Queues
Semaphores and Mutexes
Timers
Event Groups
The blinky2 Program
Build and Test blinky2
Execution
FreeRTOSConfig.h
FreeRTOS Naming Convention
FreeRTOS Macros
Summary
Chapter 6: USART
USART/UART Peripheral
Asynchronous Data
USB Serial Adapters
Hookup
Project uart
Project
Project uart2
USART API
Include Files
Clocks
Configuration
DMA
Interrupts
Input/Output/Status
Ducks in a Row
FreeRTOS
Tasks
Queues
Summary
Chapter 7: USB Serial
Blue Pill USB Issue
Introduction to USB
Pipes and Endpoints
USB Serial Device
Linux USB Serial Device
macOS USB Serial Device
Windows USB Serial Device
USB GPIO
Demo Source Code
cdcacm_set_config()
cdc_control_request()
cdcacm_data_rx_cb()
USB Task
USB Receiving
USB Sending
USB Serial Demo
Summary
Bibliography
Chapter 8: SPI Flash
Introducing W25QXX
Serial Peripheral Interface Bus
Chip Select
Wiring and Voltages
SPI Circuit
Hardware /NSS Control
STM32 SPI Configuration
SPI Clock Rate
SPI Clock Modes
Endianness and Word Length
SPI I/O
Read SR1
Waiting for Ready
Read Manufacturer ID
Writing Flash
Flash Erase
Reading Flash
Demonstration
Running the Demo
Manufacturer ID
Power Down
Summary
Bibliography
Chapter 9: Code Overlays
The Linker Challenge
MEMORY Section
Entry
Sections
PROVIDE
Relocation
Defining Overlays
Overlay Code
Overlay Stubs
Overlay Manager
VMA and Load Addresses
Linker Symbols in Code
Overlay Manager Function
Overlay Stubs
Demonstration
Extracting Overlays
Upload Overlays to W25Q32
Overlay Demo Continued
Code Change Trap
Summary
Bibliography
Chapter 10: Real-Time Clock (RTC)
Demonstration Projects
RTC Using One Interrupt
RTC Configuration
RTC Clock Source
Prescaler
Starting Counter Value
RTC Flags
Interrupt and Setup
Interrupt Service Routine
Servicing Interrupts
Task Notification
Mutexes
Demonstration
UART1 Connections
Running the Demo
rtc_alarm_isr()
EXTI Controller
Configuring EXT17
Summary
Chapter 11: I2C
The I2C Bus
Master and Slave
Start and Stop
Data Bits
I2C Address
I2C Transactions
PCF8574 GPIO Extender
I2C Circuit
The PCF8574 Line
PCF8574 Configuration
PCF8574 GPIO Drive
Wave Shaping
Demo Circuit
EXTI Interrupt
I2C Software
Testing I2C Ready
Start I2C
I2C Write
I2C Read
I2C Restart
Demo Program
Demo Session
Summary
Chapter 12: OLED
OLED Display
Configuration
Display Connections
Display Features
Demo Schematic
AFIO
Graphics
The Pixmap
Pixmap Writing
The Meter Software
meter_init()
meter_set_value()
meter_redraw()
meter_update()
Main Module
oled_command()
oled_data()
oled_reset()
oled_init()
Demonstration
Summary
Chapter 13: OLED Using DMA
Challenges
Circuit
DMA Operation
DMA Execution
DMA Request Signals
SPI1_TX Request
The Demonstration
Initializing DMA
Launching DMA
OLED SPI/DMA Management Task
DMA ISR Routine
Restarting DMA Transfers
Executing the Demo
Further Challenges
Summary
Chapter 14: Analog-to-Digital Conversion
STM32F103C8T6 Resources
Demonstration
Analog Inputs PA0 and PA1
ADC Peripheral Configuration
ADC Prescaler
ADC Modes
Sample Time
Readying the ADC
Demonstration Run
Reading ADC
Computing Temperature
Voltage Reference
Analog Voltages
Summary
Bibliography
Chapter 15: Clock Tree
In the Beginning
RC Oscillators
Crystal Oscillators
Oscillator Power
Real-Time Clock
Watchdog Clock
System Clock (SYSCLK)
SYSCLK and USB
AHB Bus
rcc_clock_setup_in_hse_8mhz_out_72mhz()
APB1 Peripherals
APB2 Peripherals
Timers
rcc_set_mco()
HSI Demo
HSE Demo
PLL ÷ 2 Demo
Summary
Bibliography
Chapter 16: PWM with Timer 2
PWM Signals
Timer 2
PWM Loop
Calculating Timer Prescale
30 Hz Cycle
Servo Hookup
Running the Demo
PWM on PB3
Other Timers
More PWM Channels
Summary
Bibliography
Chapter 17: PWM Input with Timer 4
The Servo Signal
Signal Voltage
Demo Project
GPIO Configuration
Timer 4 Configuration
task1 Loop
ISR Routine
Demonstration Run
Session Output
Timer Inputs
Summary
Chapter 18: CAN Bus
The CAN Bus
Differential Signals
Dominant/Recessive
Bus Arbitration
Synchronization
Message Format
STM32 Limitation
Demonstration
Software Build
UART Interface
MCU Flashing
Demo Bus
Session Run
CAN Messages
Synchronicity
Summary
Bibliography
Chapter 19: CAN Bus Software
Initialization
can_init()
CAN Receive Filters
CAN Receive Interrupts
Application Receiving
Processing the Message
Sending CAN Messages
Summary
Chapter 20: UART with Interrupts
Background
Interrupt Service Routines (ISR)
Demonstration
Program Design
Clock Initialization
GPIO Initialization
UART Initialization
Interrupt Service Routine
Function uart_getc()
Task main_task()
Function uart_putc()
Task tx_task()
Running the Demo
Configuration
Demonstration
Data Loss
Summary
Bibliography
Chapter 21: New Projects
Project Creation
Makefile
Macro BINARY
Macro SRCFILES
Macro LDSCRIPT
Macro DEPS
Macro CLOBBER
Included Makefiles
Header Dependencies
Compile Options
Flashing 128k
FreeRTOS
rtos/opencm3.c
rtos/heap_4.c
Required Modules
FreeRTOSConfig.h
User Libraries
Rookie Mistakes
Summary
Bibliography
Chapter 22: Troubleshooting
Gnu Debugger (GDB)
GDB Server
Remote GDB
GDB Text User Interface
Peripheral GPIO Trouble
Alternate Function Fail
Peripheral Fail
ISR FreeRTOS Crash
Stack Overflow
Estimating Stack Size
When a Debugger Doesn’t Help
Push/Pull (Totem Pole) or Open Drain
Peripheral Defects
Resources
libopencm3
FreeRTOS Task Priorities
Scheduling Within libopencm3
Summary
Appendix A: Answers to Exercises
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 19
Chapter 20
Chapter 21
Appendix B: STM32F103C8T6 GPIO Pins
Index