You are on page 1of 18

Beginning STM32

Developing with FreeRTOS,


libopencm3 and GCC

Warren Gay
Beginning STM32: Developing with FreeRTOS, libopencm3 and GCC
Warren Gay
St. Catharines, Ontario, Canada

ISBN-13 (pbk): 978-1-4842-3623-9      ISBN-13 (electronic): 978-1-4842-3624-6


https://doi.org/10.1007/978-1-4842-3624-6
Library of Congress Control Number: 2018945101
Copyright © 2018 by Warren Gay
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
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 orders-ny@springer-sbm.
com, 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 information on translations, please email rights@apress.com or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
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.
Printed on acid-free paper
For Jackie
Table of Contents
About the Author����������������������������������������������������������������������������������������������������xix
About the Technical Reviewer��������������������������������������������������������������������������������xxi

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

Chapter 2: Software Setup�������������������������������������������������������������������������������������� 17


Directory Conventions Used�������������������������������������������������������������������������������������������������������� 17
Operating Software��������������������������������������������������������������������������������������������������������������������� 17

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 3: Power Up and Blink������������������������������������������������������������������������������� 27


Power������������������������������������������������������������������������������������������������������������������������������������������ 27
+3.3V Regulator�������������������������������������������������������������������������������������������������������������������� 29
USB Power/+5V��������������������������������������������������������������������������������������������������������������������� 30
+3.3V Supply������������������������������������������������������������������������������������������������������������������������� 30
One Power Source Rule��������������������������������������������������������������������������������������������������������� 31
Ground���������������������������������������������������������������������������������������������������������������������������������������� 32
Reset������������������������������������������������������������������������������������������������������������������������������������������� 32
Showtime������������������������������������������������������������������������������������������������������������������������������������ 32
ST-Link V2����������������������������������������������������������������������������������������������������������������������������������� 34
st-flash Utility������������������������������������������������������������������������������������������������������������������������������ 36
Read STM32�������������������������������������������������������������������������������������������������������������������������� 36
Write Image��������������������������������������������������������������������������������������������������������������������������� 37
Erase Flash���������������������������������������������������������������������������������������������������������������������������� 38
Summary������������������������������������������������������������������������������������������������������������������������������������ 38
Bibliography�������������������������������������������������������������������������������������������������������������������������������� 38

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

Chapter 7: USB Serial��������������������������������������������������������������������������������������������� 97


Blue Pill USB Issue���������������������������������������������������������������������������������������������������������������������� 97
Introduction to USB��������������������������������������������������������������������������������������������������������������������� 99
Pipes and Endpoints�������������������������������������������������������������������������������������������������������������� 99
USB Serial Device��������������������������������������������������������������������������������������������������������������������� 101
Linux USB Serial Device������������������������������������������������������������������������������������������������������ 101
MacOS USB Serial Device���������������������������������������������������������������������������������������������������� 102
Windows USB Serial Device������������������������������������������������������������������������������������������������ 103
USB GPIO����������������������������������������������������������������������������������������������������������������������������� 103
Demo Source Code������������������������������������������������������������������������������������������������������������������� 104
cdcacm_set_config()����������������������������������������������������������������������������������������������������������� 105
cdc_control_request()��������������������������������������������������������������������������������������������������������� 106
cdcacm_data_rx_cb()��������������������������������������������������������������������������������������������������������� 107
USB Task������������������������������������������������������������������������������������������������������������������������������ 108
USB Receiving��������������������������������������������������������������������������������������������������������������������� 110
USB Sending������������������������������������������������������������������������������������������������������������������������ 110

viii
Table of Contents

USB Serial Demo����������������������������������������������������������������������������������������������������������������������� 111


Summary���������������������������������������������������������������������������������������������������������������������������������� 113
Bibliography������������������������������������������������������������������������������������������������������������������������������ 114

Chapter 8: SPI Flash��������������������������������������������������������������������������������������������� 115


Introducing W25QXX����������������������������������������������������������������������������������������������������������������� 115
Serial Peripheral Interface Bus������������������������������������������������������������������������������������������������� 115
Chip Select�������������������������������������������������������������������������������������������������������������������������� 117
Wiring and Voltages������������������������������������������������������������������������������������������������������������� 117
SPI Circuit��������������������������������������������������������������������������������������������������������������������������������� 118
Hardware /NSS Control�������������������������������������������������������������������������������������������������������� 118
STM32 SPI Configuration���������������������������������������������������������������������������������������������������������� 120
SPI Clock Rate��������������������������������������������������������������������������������������������������������������������� 124
SPI Clock Modes������������������������������������������������������������������������������������������������������������������ 125
Endianess and Word Length������������������������������������������������������������������������������������������������ 127
SPI I/O��������������������������������������������������������������������������������������������������������������������������������������� 128
Read SR1����������������������������������������������������������������������������������������������������������������������������� 128
Waiting for Ready���������������������������������������������������������������������������������������������������������������� 129
Read Manufacturer ID��������������������������������������������������������������������������������������������������������� 130
Writing Flash����������������������������������������������������������������������������������������������������������������������� 131
Flash Erase�������������������������������������������������������������������������������������������������������������������������� 133
Reading Flash���������������������������������������������������������������������������������������������������������������������� 136
Demonstration�������������������������������������������������������������������������������������������������������������������������� 137
Running the Demo��������������������������������������������������������������������������������������������������������������� 139
Manufacturer ID������������������������������������������������������������������������������������������������������������������� 144
Power Down������������������������������������������������������������������������������������������������������������������������ 144
Summary����������������������������������������������������������������������������������������������������������������������������� 145
Bibliography������������������������������������������������������������������������������������������������������������������������������ 145

ix
Table of Contents

Chapter 9: Code Overlays������������������������������������������������������������������������������������� 147


The Linker Challenge���������������������������������������������������������������������������������������������������������������� 147
MEMORY Section����������������������������������������������������������������������������������������������������������������� 149
Entry������������������������������������������������������������������������������������������������������������������������������������ 151
Sections������������������������������������������������������������������������������������������������������������������������������� 151
PROVIDE������������������������������������������������������������������������������������������������������������������������������� 154
Relocation���������������������������������������������������������������������������������������������������������������������������� 154
Defining Overlays���������������������������������������������������������������������������������������������������������������������� 155
Overlay Code����������������������������������������������������������������������������������������������������������������������� 157
Overlay Stubs���������������������������������������������������������������������������������������������������������������������� 159
Overlay Manager����������������������������������������������������������������������������������������������������������������� 159
VMA and Load Addresses���������������������������������������������������������������������������������������������������� 160
Linker Symbols in Code������������������������������������������������������������������������������������������������������� 161
Overlay Manager Function��������������������������������������������������������������������������������������������������� 162
Overlay Stubs���������������������������������������������������������������������������������������������������������������������� 164
Demonstration�������������������������������������������������������������������������������������������������������������������������� 165
Extracting Overlays�������������������������������������������������������������������������������������������������������������� 166
Upload Overlays to W25Q32������������������������������������������������������������������������������������������������ 167
Overlay Demo Continued����������������������������������������������������������������������������������������������������� 171
Code Change Trap���������������������������������������������������������������������������������������������������������������� 173
Summary���������������������������������������������������������������������������������������������������������������������������������� 173
Bibliography������������������������������������������������������������������������������������������������������������������������������ 174

Chapter 10: Real-Time Clock (RTC)����������������������������������������������������������������������� 175


Demonstration Projects������������������������������������������������������������������������������������������������������������� 175
RTC Using One Interrupt������������������������������������������������������������������������������������������������������������ 175
RTC Configuration���������������������������������������������������������������������������������������������������������������� 176
Interrupt and Setup������������������������������������������������������������������������������������������������������������� 178
Interrupt Service Routine���������������������������������������������������������������������������������������������������� 179
Task Notification������������������������������������������������������������������������������������������������������������������ 181
Mutexes������������������������������������������������������������������������������������������������������������������������������� 183

x
Table of Contents

Demonstration�������������������������������������������������������������������������������������������������������������������������� 184
UART1 Connections������������������������������������������������������������������������������������������������������������� 187
Running the Demo��������������������������������������������������������������������������������������������������������������� 188
rtc_alarm_isr()�������������������������������������������������������������������������������������������������������������������������� 190
EXTI Controller��������������������������������������������������������������������������������������������������������������������� 190
Summary���������������������������������������������������������������������������������������������������������������������������������� 192

Chapter 11: I2C����������������������������������������������������������������������������������������������������� 195


The I2C Bus������������������������������������������������������������������������������������������������������������������������������� 195
Master and Slave����������������������������������������������������������������������������������������������������������������� 196
Start and Stop��������������������������������������������������������������������������������������������������������������������� 196
Data Bits������������������������������������������������������������������������������������������������������������������������������ 197
I2C Address�������������������������������������������������������������������������������������������������������������������������� 198
I2C Transactions������������������������������������������������������������������������������������������������������������������ 199
PCF8574 GPIO Extender������������������������������������������������������������������������������������������������������������ 200
I2C Circuit���������������������������������������������������������������������������������������������������������������������������������� 202
The PCF8574 INT Line��������������������������������������������������������������������������������������������������������� 203
PCF8574 Configuration������������������������������������������������������������������������������������������������������������� 203
PCF8574 GPIO Drive������������������������������������������������������������������������������������������������������������ 205
Wave Shaping���������������������������������������������������������������������������������������������������������������������� 206
Demo Circuit����������������������������������������������������������������������������������������������������������������������������� 206
EXTI Interrupt����������������������������������������������������������������������������������������������������������������������� 207
I2C Software����������������������������������������������������������������������������������������������������������������������������� 209
Testing I2C Ready���������������������������������������������������������������������������������������������������������������� 210
Start I2C������������������������������������������������������������������������������������������������������������������������������� 211
I2C Write������������������������������������������������������������������������������������������������������������������������������ 213
I2C Read������������������������������������������������������������������������������������������������������������������������������ 213
I2C Restart��������������������������������������������������������������������������������������������������������������������������� 214
Demo Program�������������������������������������������������������������������������������������������������������������������������� 215
Demo Session���������������������������������������������������������������������������������������������������������������������� 218
Summary���������������������������������������������������������������������������������������������������������������������������������� 220

xi
Table of Contents

Chapter 12: OLED�������������������������������������������������������������������������������������������������� 223


OLED Display����������������������������������������������������������������������������������������������������������������������������� 223
Configuration����������������������������������������������������������������������������������������������������������������������� 224
Display Connections������������������������������������������������������������������������������������������������������������ 226
Display Features����������������������������������������������������������������������������������������������������������������������� 226
Demo Schematic����������������������������������������������������������������������������������������������������������������������� 227
AFIO������������������������������������������������������������������������������������������������������������������������������������������� 228
Graphics������������������������������������������������������������������������������������������������������������������������������������ 230
The Pixmap�������������������������������������������������������������������������������������������������������������������������� 232
Pixmap Writing�������������������������������������������������������������������������������������������������������������������� 233
The Meter Software������������������������������������������������������������������������������������������������������������� 234
Main Module������������������������������������������������������������������������������������������������������������������������ 236
Demonstration�������������������������������������������������������������������������������������������������������������������������� 238
Summary���������������������������������������������������������������������������������������������������������������������������������� 240

Chapter 13: OLED Using DMA�������������������������������������������������������������������������������� 241


Challenges�������������������������������������������������������������������������������������������������������������������������������� 241
Circuit���������������������������������������������������������������������������������������������������������������������������������� 242
DMA Operation�������������������������������������������������������������������������������������������������������������������������� 242
DMA Execution�������������������������������������������������������������������������������������������������������������������� 242
The Demonstration�������������������������������������������������������������������������������������������������������������������� 247
Initializing DMA�������������������������������������������������������������������������������������������������������������������� 249
Launching DMA������������������������������������������������������������������������������������������������������������������� 250
OLED SPI/DMA Management Task��������������������������������������������������������������������������������������� 251
DMA ISR Routine����������������������������������������������������������������������������������������������������������������� 254
Restarting DMA Transfers���������������������������������������������������������������������������������������������������� 255
Executing the Demo������������������������������������������������������������������������������������������������������������������ 256
Further Challenges�������������������������������������������������������������������������������������������������������������� 258
Summary���������������������������������������������������������������������������������������������������������������������������������� 259

xii
Table of Contents

Chapter 14: Analog-to-Digital Conversion������������������������������������������������������������ 261


STM32F103C8T6 Resources����������������������������������������������������������������������������������������������������� 261
Demonstration�������������������������������������������������������������������������������������������������������������������������� 262
Analog Inputs PA0 and PA1������������������������������������������������������������������������������������������������� 263
ADC Peripheral Configuration���������������������������������������������������������������������������������������������� 263
Demonstration Run������������������������������������������������������������������������������������������������������������������� 266
Reading ADC������������������������������������������������������������������������������������������������������������������������ 267
Analog Voltages������������������������������������������������������������������������������������������������������������������������� 270
Summary���������������������������������������������������������������������������������������������������������������������������������� 271
Bibliography������������������������������������������������������������������������������������������������������������������������������ 272

Chapter 15: Clock Tree������������������������������������������������������������������������������������������ 273


In the Beginning������������������������������������������������������������������������������������������������������������������������ 273
RC Oscillators���������������������������������������������������������������������������������������������������������������������� 274
Crystal Oscillators��������������������������������������������������������������������������������������������������������������� 275
Oscillator Power������������������������������������������������������������������������������������������������������������������ 276
Real-time Clock������������������������������������������������������������������������������������������������������������������������� 276
Watchdog Clock������������������������������������������������������������������������������������������������������������������������ 276
System Clock (SYSCLK)������������������������������������������������������������������������������������������������������������� 277
SYSCLK and USB����������������������������������������������������������������������������������������������������������������� 279
AHB Bus������������������������������������������������������������������������������������������������������������������������������������ 280
rcc_clock_setup_in_hse_8mhz_out_72mhz()�������������������������������������������������������������������� 281
APB1 Peripherals����������������������������������������������������������������������������������������������������������������� 285
APB2 Peripherals����������������������������������������������������������������������������������������������������������������� 285
Timers���������������������������������������������������������������������������������������������������������������������������������� 285
rcc_set_mco()��������������������������������������������������������������������������������������������������������������������������� 286
HSI Demo���������������������������������������������������������������������������������������������������������������������������������� 286
HSE Demo��������������������������������������������������������������������������������������������������������������������������������� 288
PLL ÷ 2 Demo��������������������������������������������������������������������������������������������������������������������������� 289
Summary���������������������������������������������������������������������������������������������������������������������������������� 290
Bibliography������������������������������������������������������������������������������������������������������������������������������ 290

xiii
Table of Contents

Chapter 16: PWM with Timer 2����������������������������������������������������������������������������� 293


PWM Signals����������������������������������������������������������������������������������������������������������������������������� 293
Timer 2�������������������������������������������������������������������������������������������������������������������������������������� 294
PWM Loop��������������������������������������������������������������������������������������������������������������������������������� 298
Calculating Timer Prescale�������������������������������������������������������������������������������������������������� 298
30 Hz Cycle�������������������������������������������������������������������������������������������������������������������������� 299
Servo Hookup���������������������������������������������������������������������������������������������������������������������������� 300
Running the Demo�������������������������������������������������������������������������������������������������������������������� 301
PWM on PB3����������������������������������������������������������������������������������������������������������������������������� 301
Other Timers����������������������������������������������������������������������������������������������������������������������������� 302
More PWM Channels����������������������������������������������������������������������������������������������������������� 303
Summary���������������������������������������������������������������������������������������������������������������������������������� 304
Bibliography������������������������������������������������������������������������������������������������������������������������������ 304

Chapter 17: PWM Input with Timer 4������������������������������������������������������������������� 305


The Servo Signal����������������������������������������������������������������������������������������������������������������������� 305
Signal Voltage��������������������������������������������������������������������������������������������������������������������������� 306
Demo Project���������������������������������������������������������������������������������������������������������������������������� 306
GPIO Configuration�������������������������������������������������������������������������������������������������������������� 306
Timer 4 Configuration���������������������������������������������������������������������������������������������������������� 306
Task1 Loop�������������������������������������������������������������������������������������������������������������������������� 308
ISR Routine�������������������������������������������������������������������������������������������������������������������������� 309
Demonstration Run������������������������������������������������������������������������������������������������������������������� 310
Session Output�������������������������������������������������������������������������������������������������������������������� 312
Timer Inputs������������������������������������������������������������������������������������������������������������������������������ 313
Summary���������������������������������������������������������������������������������������������������������������������������������� 315

Chapter 18: CAN Bus�������������������������������������������������������������������������������������������� 317


The CAN Bus����������������������������������������������������������������������������������������������������������������������������� 317
Differential Signals�������������������������������������������������������������������������������������������������������������������� 319
Dominant/Recessive������������������������������������������������������������������������������������������������������������ 320

xiv
Table of Contents

Bus Arbitration�������������������������������������������������������������������������������������������������������������������������� 321


Synchronization������������������������������������������������������������������������������������������������������������������� 322
Message Format������������������������������������������������������������������������������������������������������������������ 323
STM32 Limitation���������������������������������������������������������������������������������������������������������������������� 324
Demonstration�������������������������������������������������������������������������������������������������������������������������� 325
Software Build��������������������������������������������������������������������������������������������������������������������� 325
UART Interface��������������������������������������������������������������������������������������������������������������������� 326
MCU Flashing����������������������������������������������������������������������������������������������������������������������� 326
Demo Bus���������������������������������������������������������������������������������������������������������������������������� 327
Session Run������������������������������������������������������������������������������������������������������������������������������ 328
CAN Messages��������������������������������������������������������������������������������������������������������������������� 330
Synchronicity����������������������������������������������������������������������������������������������������������������������� 331
Summary���������������������������������������������������������������������������������������������������������������������������������� 331
Bibliography������������������������������������������������������������������������������������������������������������������������������ 331

Chapter 19: CAN Bus Software����������������������������������������������������������������������������� 333


Initialization������������������������������������������������������������������������������������������������������������������������������� 333
can_init()����������������������������������������������������������������������������������������������������������������������������� 336
CAN Receive Filters������������������������������������������������������������������������������������������������������������� 338
CAN Receive Interrupts�������������������������������������������������������������������������������������������������������� 339
Application Receiving���������������������������������������������������������������������������������������������������������� 343
Sending CAN Messages������������������������������������������������������������������������������������������������������� 345
Summary���������������������������������������������������������������������������������������������������������������������������������� 346

Chapter 20: New Projects������������������������������������������������������������������������������������� 347


Project Creation������������������������������������������������������������������������������������������������������������������������ 347
Makefile������������������������������������������������������������������������������������������������������������������������������� 348
Included Makefiles�������������������������������������������������������������������������������������������������������������� 351
Header Dependencies��������������������������������������������������������������������������������������������������������� 351
Compile Options������������������������������������������������������������������������������������������������������������������ 351
Flashing 128k���������������������������������������������������������������������������������������������������������������������� 352

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

Chapter 21: Troubleshooting��������������������������������������������������������������������������������� 361


Gnu GDB������������������������������������������������������������������������������������������������������������������������������������ 361
GDB Server�������������������������������������������������������������������������������������������������������������������������� 361
Remote GDB������������������������������������������������������������������������������������������������������������������������ 363
GDB Text User Interface������������������������������������������������������������������������������������������������������� 366
Peripheral GPIO Trouble������������������������������������������������������������������������������������������������������������ 367
Alternate Function Fail�������������������������������������������������������������������������������������������������������������� 368
Peripheral Fail��������������������������������������������������������������������������������������������������������������������������� 369
ISR FreeRTOS Crash������������������������������������������������������������������������������������������������������������������ 369
Stack Overflow�������������������������������������������������������������������������������������������������������������������������� 370
Estimating Stack Size��������������������������������������������������������������������������������������������������������������� 371
When a Debugger Doesn’t Help������������������������������������������������������������������������������������������������ 371
Push/Pull or Open Drain������������������������������������������������������������������������������������������������������������ 372
Peripheral Defects�������������������������������������������������������������������������������������������������������������������� 372
Resources��������������������������������������������������������������������������������������������������������������������������������� 372
libopencm3�������������������������������������������������������������������������������������������������������������������������� 373
FreeRTOS Task Priorities����������������������������������������������������������������������������������������������������������� 375
Scheduling Within libopencm3�������������������������������������������������������������������������������������������� 376
Summary���������������������������������������������������������������������������������������������������������������������������������� 377

xvi
Table of Contents

Appendix A: Answers to Exercises����������������������������������������������������������������������� 379


Chapter 4���������������������������������������������������������������������������������������������������������������������������������� 379
Chapter 5���������������������������������������������������������������������������������������������������������������������������������� 381
Chapter 6���������������������������������������������������������������������������������������������������������������������������������� 382
Chapter 7���������������������������������������������������������������������������������������������������������������������������������� 383
Chapter 8���������������������������������������������������������������������������������������������������������������������������������� 384
Chapter 9���������������������������������������������������������������������������������������������������������������������������������� 385
Chapter 10�������������������������������������������������������������������������������������������������������������������������������� 386
Chapter 11�������������������������������������������������������������������������������������������������������������������������������� 386
Chapter 12�������������������������������������������������������������������������������������������������������������������������������� 387
Chapter 13�������������������������������������������������������������������������������������������������������������������������������� 388
Chapter 14�������������������������������������������������������������������������������������������������������������������������������� 389
Chapter 15�������������������������������������������������������������������������������������������������������������������������������� 390
Chapter 16�������������������������������������������������������������������������������������������������������������������������������� 390
Chapter 17�������������������������������������������������������������������������������������������������������������������������������� 391
Chapter 19�������������������������������������������������������������������������������������������������������������������������������� 391
Chapter 20�������������������������������������������������������������������������������������������������������������������������������� 392

Appendix B: STM32F103C8T6 GPIO Pins�������������������������������������������������������������� 393

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

You might also like