Professional Documents
Culture Documents
Warren Gay
Beginning STM32: Developing with FreeRTOS, libopencm3 and GCC
Warren Gay
St. Catharines, Ontario, Canada
Chapter 1: Introduction�������������������������������������������������������������������������������������������� 1
STM32F103C8T6��������������������������������������������������������������������������������������������������������������������������� 2
FreeRTOS�������������������������������������������������������������������������������������������������������������������������������������� 5
libopencm3����������������������������������������������������������������������������������������������������������������������������������� 5
No Arduino������������������������������������������������������������������������������������������������������������������������������������ 6
No IDE������������������������������������������������������������������������������������������������������������������������������������������� 6
Development Framework�������������������������������������������������������������������������������������������������������������� 7
Assumptions About You����������������������������������������������������������������������������������������������������������������� 7
What You Need������������������������������������������������������������������������������������������������������������������������������ 8
ST-Link V2 Programming Unit�������������������������������������������������������������������������������������������������� 8
Breadboard������������������������������������������������������������������������������������������������������������������������������ 9
DuPont (Jumper) Wires���������������������������������������������������������������������������������������������������������� 10
0.1 uF Bypass Capacitors������������������������������������������������������������������������������������������������������ 11
USB TTL Serial Adapter���������������������������������������������������������������������������������������������������������� 12
Power Supply������������������������������������������������������������������������������������������������������������������������ 14
Small Stuff����������������������������������������������������������������������������������������������������������������������������� 15
Summary������������������������������������������������������������������������������������������������������������������������������������ 16
v
Table of Contents
Book Software����������������������������������������������������������������������������������������������������������������������������� 18
libopencm3���������������������������������������������������������������������������������������������������������������������������� 18
FreeRTOS������������������������������������������������������������������������������������������������������������������������������� 19
~/stm32f103c8t6/rtos/Project.mk����������������������������������������������������������������������������������������� 19
ARM Cross Compiler������������������������������������������������������������������������������������������������������������������� 20
Build the Software���������������������������������������������������������������������������������������������������������������������� 23
ST-Link Tool��������������������������������������������������������������������������������������������������������������������������������� 24
Summary������������������������������������������������������������������������������������������������������������������������������������ 25
Chapter 4: GPIO������������������������������������������������������������������������������������������������������ 39
Building miniblink����������������������������������������������������������������������������������������������������������������������� 39
Flashing miniblink����������������������������������������������������������������������������������������������������������������������� 40
miniblink.c Source Code������������������������������������������������������������������������������������������������������������� 41
GPIO API�������������������������������������������������������������������������������������������������������������������������������������� 44
GPIO Configuration���������������������������������������������������������������������������������������������������������������� 46
vi
Table of Contents
Ducks in a Row��������������������������������������������������������������������������������������������������������������������������� 51
GPIO Inputs���������������������������������������������������������������������������������������������������������������������������� 51
Digital Output, Push/Pull�������������������������������������������������������������������������������������������������������� 52
Digital Output, Open Drain����������������������������������������������������������������������������������������������������� 53
GPIO Characteristics������������������������������������������������������������������������������������������������������������������� 53
Input Voltage Thresholds������������������������������������������������������������������������������������������������������� 55
Output-Voltage Thresholds���������������������������������������������������������������������������������������������������� 55
Programmed Delays�������������������������������������������������������������������������������������������������������������������� 56
The Problem with Programmed Delay����������������������������������������������������������������������������������� 57
Summary������������������������������������������������������������������������������������������������������������������������������������ 58
Chapter 5: FreeRTOS����������������������������������������������������������������������������������������������� 59
FreeRTOS Facilities��������������������������������������������������������������������������������������������������������������������� 59
Tasking���������������������������������������������������������������������������������������������������������������������������������� 60
Message Queues������������������������������������������������������������������������������������������������������������������� 60
Semaphores and Mutexes����������������������������������������������������������������������������������������������������� 61
Timers������������������������������������������������������������������������������������������������������������������������������������ 61
Event Groups������������������������������������������������������������������������������������������������������������������������� 62
The blinky2 Program������������������������������������������������������������������������������������������������������������������� 62
Build and Test blinky2����������������������������������������������������������������������������������������������������������� 66
Execution������������������������������������������������������������������������������������������������������������������������������� 66
FreeRTOSConfig.h����������������������������������������������������������������������������������������������������������������������� 67
FreeRTOS Naming Convention���������������������������������������������������������������������������������������������������� 69
FreeRTOS Macros������������������������������������������������������������������������������������������������������������������ 70
Summary������������������������������������������������������������������������������������������������������������������������������������ 71
Chapter 6: USART���������������������������������������������������������������������������������������������������� 73
USART/UART Peripheral�������������������������������������������������������������������������������������������������������������� 73
Asynchronous Data���������������������������������������������������������������������������������������������������������������� 74
USB Serial Adapters�������������������������������������������������������������������������������������������������������������������� 74
Hookup���������������������������������������������������������������������������������������������������������������������������������� 76
Project uart��������������������������������������������������������������������������������������������������������������������������������� 77
vii
Table of Contents
Project ���������������������������������������������������������������������������������������������������������������������������������������� 81
Project uart2������������������������������������������������������������������������������������������������������������������������������� 85
USART API����������������������������������������������������������������������������������������������������������������������������������� 90
Include Files�������������������������������������������������������������������������������������������������������������������������� 92
Clocks������������������������������������������������������������������������������������������������������������������������������������ 92
Configuration������������������������������������������������������������������������������������������������������������������������� 92
DMA��������������������������������������������������������������������������������������������������������������������������������������� 93
Interrupts������������������������������������������������������������������������������������������������������������������������������� 93
Input/Output/Status��������������������������������������������������������������������������������������������������������������� 93
Ducks-in-a-Row�������������������������������������������������������������������������������������������������������������������������� 93
FreeRTOS������������������������������������������������������������������������������������������������������������������������������������ 94
Tasks������������������������������������������������������������������������������������������������������������������������������������� 94
Queues���������������������������������������������������������������������������������������������������������������������������������� 95
Summary������������������������������������������������������������������������������������������������������������������������������������ 96
viii
Table of Contents
ix
Table of Contents
x
Table of Contents
Demonstration�������������������������������������������������������������������������������������������������������������������������� 184
UART1 Connections������������������������������������������������������������������������������������������������������������� 187
Running the Demo��������������������������������������������������������������������������������������������������������������� 188
rtc_alarm_isr()�������������������������������������������������������������������������������������������������������������������������� 190
EXTI Controller��������������������������������������������������������������������������������������������������������������������� 190
Summary���������������������������������������������������������������������������������������������������������������������������������� 192
xi
Table of Contents
xii
Table of Contents
xiii
Table of Contents
xiv
Table of Contents
xv
Table of Contents
FreeRTOS���������������������������������������������������������������������������������������������������������������������������������� 353
rtos/opencm3.c������������������������������������������������������������������������������������������������������������������� 353
rtos/heap_4.c���������������������������������������������������������������������������������������������������������������������� 354
Required Modules���������������������������������������������������������������������������������������������������������������� 354
FreeRTOSConfig.h��������������������������������������������������������������������������������������������������������������������� 355
User Libraries���������������������������������������������������������������������������������������������������������������������������� 357
Rookie Mistakes������������������������������������������������������������������������������������������������������������������������ 358
Summary���������������������������������������������������������������������������������������������������������������������������������� 358
Bibliography������������������������������������������������������������������������������������������������������������������������������ 358
xvi
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 401
xvii
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.
xix
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.
xxi