masteringstm32 - carmine noviello · masteringstm32 astep-by-stepguidetothemostcompletearmcortex-m...

19

Upload: buikhanh

Post on 29-Aug-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …
Page 2: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

Mastering STM32A step-by-step guide to the most complete ARM Cortex-Mplatform, using a free and powerful developmentenvironment based on Eclipse and GCC

Carmine Noviello

This book is for sale at http://leanpub.com/mastering-stm32

This version was published on 2018-01-03

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishingprocess. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools andmany iterations to get reader feedback, pivot until you have the right book and build traction onceyou do.

© 2015 - 2018 Carmine Noviello

Page 3: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

Tweet This Book!Please help Carmine Noviello by spreading the word about this book on Twitter!

The suggested hashtag for this book is #MasteringSTM32.

Find out what other people are saying about the book by clicking on this link to search for thishashtag on Twitter:

#MasteringSTM32

Page 4: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iWhy Did I Write the Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iWho Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iiHow to Integrate This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iiiHow Is the Book Organized? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viiErrata and Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viiiBook Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viiiHow to Help the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixCopyright Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixCredits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x

I Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1. Introduction to STM32 MCU Portfolio . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.1 Introduction to ARM Based Processors . . . . . . . . . . . . . . . . . . . . . . . . 2

1.1.1 Cortex and Cortex-M Based Processors . . . . . . . . . . . . . . . . . . . . 41.1.1.1 Core Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.1.1.2 Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.1.1.3 Bit-Banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.1.1.4 Thumb-2 and Memory Alignment . . . . . . . . . . . . . . . . . 121.1.1.5 Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.1.1.6 Interrupts and Exceptions Handling . . . . . . . . . . . . . . . . 151.1.1.7 SysTimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.1.1.8 Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.1.1.9 CMSIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191.1.1.10 Effective Implementation of Cortex-M Features in the STM32

Portfolio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201.2 Introduction to STM32 Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . 21

1.2.1 Advantages of the STM32 Portfolio…. . . . . . . . . . . . . . . . . . . . . . 22

Page 5: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

1.2.2 ….And Its Drawbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231.3 A Quick Look at the STM32 Subfamilies . . . . . . . . . . . . . . . . . . . . . . . 24

1.3.1 F0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261.3.2 F1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271.3.3 F2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.3.4 F3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301.3.5 F4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321.3.6 F7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331.3.7 H7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351.3.8 L0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361.3.9 L1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371.3.10 L4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381.3.11 L4+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401.3.12 W and J STM32 MCUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411.3.13 How to Select the Right MCU for You? . . . . . . . . . . . . . . . . . . . . 42

1.4 The Nucleo Development Board . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

2. Setting-Up the Tool-Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512.1 Why Choose Eclipse/GCC as Tool-Chain for STM32 . . . . . . . . . . . . . . . . 52

2.1.1 Two Words About Eclipse… . . . . . . . . . . . . . . . . . . . . . . . . . . 532.1.2 … and GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

2.2 Windows - Installing the Tool-Chain . . . . . . . . . . . . . . . . . . . . . . . . . 542.2.1 Windows - Eclipse Installation . . . . . . . . . . . . . . . . . . . . . . . . . 552.2.2 Windows - Eclipse Plug-Ins Installation . . . . . . . . . . . . . . . . . . . . 572.2.3 Windows - GCC ARM Embedded Installation . . . . . . . . . . . . . . . . 632.2.4 Windows – Build Tools Installation . . . . . . . . . . . . . . . . . . . . . . 642.2.5 Windows – OpenOCD Installation . . . . . . . . . . . . . . . . . . . . . . 642.2.6 Windows – ST Tools and Drivers Installation . . . . . . . . . . . . . . . . . 65

2.2.6.1 Windows – ST-LINK Firmware Upgrade . . . . . . . . . . . . . . 652.3 Linux - Installing the Tool-Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

2.3.1 Linux - Install i386 Run-Time Libraries on a 64-bit Ubuntu . . . . . . . . . 672.3.2 Linux - Java Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672.3.3 Linux - Eclipse Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 682.3.4 Linux - Eclipse Plug-Ins Installation . . . . . . . . . . . . . . . . . . . . . . 692.3.5 Linux - GCC ARM Embedded Installation . . . . . . . . . . . . . . . . . . 752.3.6 Linux - Nucleo Drivers Installation . . . . . . . . . . . . . . . . . . . . . . 75

2.3.6.1 Linux – ST-LINK Firmware Upgrade . . . . . . . . . . . . . . . . 752.3.7 Linux – OpenOCD Installation . . . . . . . . . . . . . . . . . . . . . . . . 762.3.8 Linux - ST Tools Installation . . . . . . . . . . . . . . . . . . . . . . . . . . 78

2.4 Mac - Installing the Tool-Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802.4.1 Mac - Eclipse Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812.4.2 Mac - Eclipse Plug-Ins Installation . . . . . . . . . . . . . . . . . . . . . . . 832.4.3 Mac - GCC ARM Embedded Installation . . . . . . . . . . . . . . . . . . . 88

Page 6: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

2.4.4 Mac - Nucleo Drivers Installation . . . . . . . . . . . . . . . . . . . . . . . 892.4.4.1 Mac – ST-LINK Firmware Upgrade . . . . . . . . . . . . . . . . . 89

2.4.5 Mac – OpenOCD Installation . . . . . . . . . . . . . . . . . . . . . . . . . 902.4.6 Mac - ST Tools Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

3. Hello, Nucleo! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953.1 Get in Touch With the Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . 953.2 Create a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993.3 Connecting the Nucleo to the PC . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063.4 Flashing the Nucleo using STM32CubeProgrammer . . . . . . . . . . . . . . . . . 1073.5 Understanding the Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . 108

4. STM32CubeMX Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1114.1 Introduction to CubeMX Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

4.1.1 Pinout View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1154.1.1.1 Chip View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1154.1.1.2 IP Tree Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

4.1.2 Clock View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194.1.3 Configuration View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1204.1.4 Power Consumption Calculator View . . . . . . . . . . . . . . . . . . . . . 121

4.2 Project Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1224.2.1 Generate C Project with CubeMX . . . . . . . . . . . . . . . . . . . . . . . 122

4.2.1.1 Understanding Generated Code . . . . . . . . . . . . . . . . . . . 1244.2.2 Create Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1264.2.3 Importing Generated Files Into the Eclipse Project Manually . . . . . . . . 1294.2.4 Importing Files GeneratedWith CubeMX Into the Eclipse Project Automat-

ically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1344.3 Understanding Generated Application Code . . . . . . . . . . . . . . . . . . . . . 135

4.3.1 Add Something Useful to the Firmware . . . . . . . . . . . . . . . . . . . . 1404.4 Downloading Book Source Code Examples . . . . . . . . . . . . . . . . . . . . . 141

5. Introduction to Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1455.1 Getting Started With OpenOCD . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

5.1.1 Launching OpenOCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1465.1.1.1 Launching OpenOCD on Windows . . . . . . . . . . . . . . . . . 1475.1.1.2 Launching OpenOCD on Linux and MacOS X. . . . . . . . . . . 148

5.1.2 Connecting to the OpenOCD Telnet Console . . . . . . . . . . . . . . . . . 1505.1.3 Configuring Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1515.1.4 Debugging in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

5.2 ARM Semihosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1635.2.1 Enable Semihosting on a New Project . . . . . . . . . . . . . . . . . . . . . 163

5.2.1.1 Using Semihosting With C Standard Library . . . . . . . . . . . . 1665.2.2 Enable Semihosting on an Existing Project . . . . . . . . . . . . . . . . . . 169

Page 7: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

5.2.3 Semihosting Drawbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1705.2.4 Understanding How Semihosting Works . . . . . . . . . . . . . . . . . . . 170

II Diving into the HAL . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

6. GPIO Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1766.1 STM32 Peripherals Mapping and HAL Handlers . . . . . . . . . . . . . . . . . . . 1766.2 GPIOs Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

6.2.1 GPIO Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1836.2.2 GPIO Alternate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1856.2.3 Understanding GPIO Speed . . . . . . . . . . . . . . . . . . . . . . . . . . 186

6.3 Driving a GPIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1906.4 De-initialize a GPIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

7. Interrupts Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1927.1 NVIC Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

7.1.1 Vector Table in STM32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1937.2 Enabling Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

7.2.1 External Lines and NVIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1977.2.2 Enabling Interrupts With CubeMX . . . . . . . . . . . . . . . . . . . . . . 201

7.3 Interrupt Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2037.4 Interrupt Priority Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

7.4.1 Cortex-M0/0+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2077.4.2 Cortex-M3/4/7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2127.4.3 Setting Interrupt Priority in CubeMX . . . . . . . . . . . . . . . . . . . . . 218

7.5 Interrupt Re-Entrancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2197.6 Mask All Interrupts at Once or an a Priority Basis . . . . . . . . . . . . . . . . . . 220

8. Universal Asynchronous Serial Communications . . . . . . . . . . . . . . . . . . . . . 2248.1 Introduction to UARTs and USARTs . . . . . . . . . . . . . . . . . . . . . . . . . 2248.2 UART Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

8.2.1 UART Configuration Using CubeMX . . . . . . . . . . . . . . . . . . . . . 2358.3 UART Communication in Polling Mode . . . . . . . . . . . . . . . . . . . . . . . 236

8.3.1 Installing a Serial Console in Windows . . . . . . . . . . . . . . . . . . . . 2408.3.2 Installing a Serial Console in Linux and MacOS X . . . . . . . . . . . . . . 243

8.4 UART Communication in Interrupt Mode . . . . . . . . . . . . . . . . . . . . . . 2448.4.1 UART Related Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

8.5 Error Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2528.6 I/O Retargeting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

9. DMA Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2589.1 Introduction to DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

Page 8: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

9.1.1 The Need of a DMA and the Role of the Internal Buses . . . . . . . . . . . 2599.1.2 The DMA Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

9.1.2.1 The DMA Implementation in F0/F1/F3/L1 MCUs . . . . . . . . . 2639.1.2.2 The DMA Implementation in F2/F4/F7 MCUs . . . . . . . . . . . 2679.1.2.3 The DMA Implementation in L0/L4 MCUs . . . . . . . . . . . . . 270

9.2 HAL_DMA Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2719.2.1 DMA_HandleTypeDef in F0/F1/F3/L0/L1/L4 HALs . . . . . . . . . . . . . . . 2719.2.2 DMA_HandleTypeDef in F2/F4/F7 HALs . . . . . . . . . . . . . . . . . . . . . 2749.2.3 DMA_HandleTypeDef in L0/L4 HALs . . . . . . . . . . . . . . . . . . . . . . 2779.2.4 How to Perform Transfers in Polling Mode . . . . . . . . . . . . . . . . . . 2779.2.5 How to Perform Transfers in Interrupt Mode . . . . . . . . . . . . . . . . . 2809.2.6 How to Perform Peripheral-To-Peripheral Transfers . . . . . . . . . . . . . 2829.2.7 Using the HAL_UART Module With DMA Mode Transfers . . . . . . . . . . . 2839.2.8 Miscellaneous Functions From HAL_DMA and HAL_DMA_Ex Modules . . . . . . 286

9.3 Using CubeMX to Configure DMA Requests . . . . . . . . . . . . . . . . . . . . 2879.4 Correct Memory Allocation of DMA Buffers . . . . . . . . . . . . . . . . . . . . 2889.5 A Case Study: The DMA Memory-To-Memory Transfer Performance Analysis . . 289

10. Clock Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29410.1 Clock Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

10.1.1 Overview of the STM32 Clock Tree . . . . . . . . . . . . . . . . . . . . . . 29610.1.1.1 The Multispeed Internal RC Oscillator in STM32L Families . . . . 300

10.1.2 Configuring Clock Tree Using CubeMX . . . . . . . . . . . . . . . . . . . . 30110.1.3 Clock Source Options in Nucleo Boards . . . . . . . . . . . . . . . . . . . . 303

10.1.3.1 OSC Clock Supply . . . . . . . . . . . . . . . . . . . . . . . . . . 30310.1.3.2 OSC 32kHz Clock Supply . . . . . . . . . . . . . . . . . . . . . . 304

10.2 Overview of the HAL_RCC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . 30510.2.1 Compute the Clock Frequency at Run-Time . . . . . . . . . . . . . . . . . 30710.2.2 Enabling the Master Clock Output . . . . . . . . . . . . . . . . . . . . . . . 30810.2.3 Enabling the Clock Security System . . . . . . . . . . . . . . . . . . . . . . 308

10.3 HSI Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

11. Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31111.1 Introduction to Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

11.1.1 Timer Categories in an STM32 MCU . . . . . . . . . . . . . . . . . . . . . 31211.1.2 Effective Availability of Timers in the STM32 Portfolio . . . . . . . . . . . 314

11.2 Basic Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31611.2.1 Using Timers in Interrupt Mode . . . . . . . . . . . . . . . . . . . . . . . . 319

11.2.1.1 Time Base Generation in Advanced Timers . . . . . . . . . . . . 32211.2.2 Using Timers in Polling Mode . . . . . . . . . . . . . . . . . . . . . . . . . 32211.2.3 Using Timers in DMA Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 32311.2.4 Stopping a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32511.2.5 Using CubeMX to Configure a Basic Timer . . . . . . . . . . . . . . . . . . 325

Page 9: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

11.3 General Purpose Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32611.3.1 Time Base Generator With External Clock Sources . . . . . . . . . . . . . . 326

11.3.1.1 External Clock Mode 2 . . . . . . . . . . . . . . . . . . . . . . . 32811.3.1.2 External Clock Mode 1 . . . . . . . . . . . . . . . . . . . . . . . 33211.3.1.3 Using CubeMX to Configure the Source Clock of a General

Purpose Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33711.3.2 Master/Slave Synchronization Modes . . . . . . . . . . . . . . . . . . . . . 338

11.3.2.1 Enable Trigger-Related Interrupts . . . . . . . . . . . . . . . . . . 34311.3.2.2 Using CubeMX to Configure the Master/Slave Synchronization . 343

11.3.3 Generate Timer-Related Events by Software . . . . . . . . . . . . . . . . . 34411.3.4 Counting Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34611.3.5 Input Capture Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347

11.3.5.1 Using CubeMX to Configure the Input Capture Mode . . . . . . . 35411.3.6 Output Compare Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

11.3.6.1 Using CubeMX to Configure the Output Compare Mode . . . . . 36011.3.7 Pulse-Width Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

11.3.7.1 Generating a Sinusoidal Wave Using PWM . . . . . . . . . . . . 36411.3.7.2 Using CubeMX to Configure the PWM Mode . . . . . . . . . . . 369

11.3.8 One Pulse Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37011.3.8.1 Using CubeMX to Configure the OPM Mode . . . . . . . . . . . 372

11.3.9 Encoder Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37311.3.9.1 Using CubeMX to Configure the Encoder Mode . . . . . . . . . . 378

11.3.10 Other Features Available in General Purpose and Advanced Timers . . . . . 37911.3.10.1 Hall Sensor Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 37911.3.10.2 Combined Three-Phase PWM Mode and Other Motor-Control

Related Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 38011.3.10.3 Break Input and Locking of Timer Registers . . . . . . . . . . . . 38011.3.10.4 Preloading of Auto-Reload Register . . . . . . . . . . . . . . . . . 380

11.3.11 Debugging and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38111.4 SysTick Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382

11.4.1 Use Another Timer as System Timebase Source . . . . . . . . . . . . . . . 38311.5 A Case Study: How to Precisely Measure Microseconds With STM32 MCUs . . . 384

12. Analog-To-Digital Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39012.1 Introduction to SAR ADC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39012.2 HAL_ADC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

12.2.1 Conversion Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39812.2.1.1 Single-Channel, Single Conversion Mode . . . . . . . . . . . . . 39812.2.1.2 Scan Single Conversion Mode . . . . . . . . . . . . . . . . . . . . 39812.2.1.3 Single-Channel, Continuous Conversion Mode . . . . . . . . . . 39912.2.1.4 Scan Continuous Conversion Mode . . . . . . . . . . . . . . . . . 39912.2.1.5 Injected Conversion Mode . . . . . . . . . . . . . . . . . . . . . . 40012.2.1.6 Dual Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

Page 10: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

12.2.2 Channel Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40112.2.3 ADC Resolution and Conversion Speed . . . . . . . . . . . . . . . . . . . . 40212.2.4 A/D Conversions in Polling Mode . . . . . . . . . . . . . . . . . . . . . . . 40212.2.5 A/D Conversions in Interrupt Mode . . . . . . . . . . . . . . . . . . . . . . 40612.2.6 A/D Conversions in DMA Mode . . . . . . . . . . . . . . . . . . . . . . . . 407

12.2.6.1 Convert Multiple Times the Same Channel in DMA Mode . . . . 41112.2.6.2 Multiple and not Continuous Conversions in DMA Mode . . . . . 41112.2.6.3 Continuous Conversions in DMA Mode . . . . . . . . . . . . . . 411

12.2.7 Errors Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41112.2.8 Timer-Driven Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . 41212.2.9 Conversions Driven by External Events . . . . . . . . . . . . . . . . . . . . 41512.2.10 ADC Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

12.3 Using CubeMX to Configure ADC Peripheral . . . . . . . . . . . . . . . . . . . . 416

13. Digital-To-Analog Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41913.1 Introduction to the DAC Peripheral . . . . . . . . . . . . . . . . . . . . . . . . . 41913.2 HAL_DAC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422

13.2.1 Driving the DAC Manually . . . . . . . . . . . . . . . . . . . . . . . . . . 42313.2.2 Driving the DAC in DMA Mode Using a Timer . . . . . . . . . . . . . . . 42513.2.3 Triangular Wave Generation . . . . . . . . . . . . . . . . . . . . . . . . . . 42913.2.4 Noise Wave Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

14. I²C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43214.1 Introduction to the I²C specification . . . . . . . . . . . . . . . . . . . . . . . . . 432

14.1.1 The I²C Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43414.1.1.1 START and STOP Condition . . . . . . . . . . . . . . . . . . . . 43514.1.1.2 Byte Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43514.1.1.3 Address Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43514.1.1.4 Acknowledge (ACK) and Not Acknowledge (NACK) . . . . . . . 43614.1.1.5 Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43714.1.1.6 Combined Transactions . . . . . . . . . . . . . . . . . . . . . . . 43714.1.1.7 Clock Stretching . . . . . . . . . . . . . . . . . . . . . . . . . . . 438

14.1.2 Availability of I²C Peripherals in STM32 MCUs . . . . . . . . . . . . . . . . 43914.2 HAL_I2C Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

14.2.1 Using the I²C Peripheral in Master Mode . . . . . . . . . . . . . . . . . . . 44314.2.1.1 I/O MEM Operations . . . . . . . . . . . . . . . . . . . . . . . . 45114.2.1.2 Combined Transactions . . . . . . . . . . . . . . . . . . . . . . . 45314.2.1.3 A Note About the Clock Configuration in STM32F0/L0/L4 families 455

14.2.2 Using the I²C Peripheral in Slave Mode . . . . . . . . . . . . . . . . . . . . 45514.3 Using CubeMX to Configure the I²C Peripheral . . . . . . . . . . . . . . . . . . . 461

15. SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46315.1 Introduction to the SPI Specification . . . . . . . . . . . . . . . . . . . . . . . . . 463

Page 11: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

15.1.1 Clock Polarity and Phase . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46615.1.2 Slave Select Signal Management . . . . . . . . . . . . . . . . . . . . . . . . 46715.1.3 SPI TI Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46815.1.4 Availability of SPI Peripherals in STM32 MCUs . . . . . . . . . . . . . . . 469

15.2 HAL_SPI Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47015.2.1 Exchanging Messages Using SPI Peripheral . . . . . . . . . . . . . . . . . . 47215.2.2 Maximum Transmission Frequency Reachable using the CubeHAL . . . . . 474

15.3 Using CubeMX to Configure SPI Peripheral . . . . . . . . . . . . . . . . . . . . . 474

16. Cyclic Redundancy Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47516.1 Introduction to CRC Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

16.1.1 CRC Calculation in STM32F1/F2/F4/L1 MCUs . . . . . . . . . . . . . . . . 47816.1.2 CRC Peripheral in STM32F0/F3/F7/L0/L4 MCUs . . . . . . . . . . . . . . . 480

16.2 HAL_CRC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481

17. IWDG and WWDG Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48517.1 The Independent Watchdog Timer . . . . . . . . . . . . . . . . . . . . . . . . . . 485

17.1.1 Using the CubeHAL to Program IWDG Timer . . . . . . . . . . . . . . . . 48617.2 The System Window Watchdog Timer . . . . . . . . . . . . . . . . . . . . . . . . 487

17.2.1 Using the CubeHAL to Program WWDG Timer . . . . . . . . . . . . . . . 48917.3 Detecting a System Reset Caused by a Watchdog Timer . . . . . . . . . . . . . . 49117.4 Freezing Watchdog Timers During a Debug Session . . . . . . . . . . . . . . . . 49117.5 Selecting the Right Watchdog Timer for Your Application . . . . . . . . . . . . . 491

18. Real-Time Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49318.1 Introduction to the RTC Peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . 49318.2 HAL_RTC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495

18.2.1 Setting and Retrieving the Current Date/Time . . . . . . . . . . . . . . . . 49618.2.1.1 Correct Way to Read Date/Time Values . . . . . . . . . . . . . . 498

18.2.2 Configuring Alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50018.2.3 Periodic Wakeup Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50218.2.4 Timestamp Generation and Tamper Detection . . . . . . . . . . . . . . . . 50318.2.5 RTC Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

18.2.5.1 RTC Coarse Calibration . . . . . . . . . . . . . . . . . . . . . . . 50418.2.5.2 RTC Smooth Calibration . . . . . . . . . . . . . . . . . . . . . . 50518.2.5.3 Reference Clock Detection . . . . . . . . . . . . . . . . . . . . . 506

18.3 Using the Backup SRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

III Advanced topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

19. Power Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50919.1 Power Management in Cortex-M Based MCUs . . . . . . . . . . . . . . . . . . . 509

Page 12: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

19.2 How Cortex-M MCUs Handle Run and Sleep Modes . . . . . . . . . . . . . . . . 51019.2.1 Entering/exiting sleep modes . . . . . . . . . . . . . . . . . . . . . . . . . 513

19.2.1.1 Sleep-On-Exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51519.2.2 Sleep Modes in Cortex-M Based MCUs . . . . . . . . . . . . . . . . . . . . 516

19.3 Power Management in STM32F Microcontrollers . . . . . . . . . . . . . . . . . . 51619.3.1 Power Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51719.3.2 Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

19.3.2.1 Run Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51819.3.2.1.1 Dynamic Voltage Scaling in STM32F4/F7 MCUs . . . . 51919.3.2.1.2 Over/Under-Drive Mode in STM32F4/F7 MCUs . . . . . 520

19.3.2.2 Sleep Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52019.3.2.3 Stop Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52119.3.2.4 Standby Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52219.3.2.5 Low-Power Modes Example . . . . . . . . . . . . . . . . . . . . . 522

19.3.3 An Important Warning for STM32F1 Microcontrollers . . . . . . . . . . . . 52719.4 Power Management in STM32L Microcontrollers . . . . . . . . . . . . . . . . . . 528

19.4.1 Power Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52819.4.2 Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

19.4.2.1 Run Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53019.4.2.2 Sleep Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532

19.4.2.2.1 Batch Acquisition Mode . . . . . . . . . . . . . . . . . 53319.4.2.3 Stop Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53319.4.2.4 Standby Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53419.4.2.5 Shutdown Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

19.4.3 Power Modes Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53619.4.4 Low-Power Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536

19.4.4.1 LPUART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53619.4.4.2 LPTIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

19.5 Power Supply Supervisors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53719.6 Debugging in Low-Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . 53819.7 Using the CubeMX Power Consumption Calculator . . . . . . . . . . . . . . . . . 53819.8 A Case Study: Using Watchdog Timers With Low-Power Modes . . . . . . . . . . 540

20. Memory layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54120.1 The STM32 Memory Layout Model . . . . . . . . . . . . . . . . . . . . . . . . . 541

20.1.1 Understanding Compilation and Linking Processes . . . . . . . . . . . . . . 54320.2 The Really Minimal STM32 Application . . . . . . . . . . . . . . . . . . . . . . . 546

20.2.1 ELF Binary File Inspection . . . . . . . . . . . . . . . . . . . . . . . . . . . 55020.2.2 .data and .bss Sections Initialization . . . . . . . . . . . . . . . . . . . . . 552

20.2.2.1 A Word About the COMMON Section . . . . . . . . . . . . . . . . . 55920.2.3 .rodata Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56020.2.4 Stack and Heap Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56220.2.5 Checking the Size of Heap and Stack at Compile-Time . . . . . . . . . . . . 565

Page 13: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

20.2.6 Differences With the Tool-Chain Script Files . . . . . . . . . . . . . . . . . 56620.3 How to Use the CCM Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568

20.3.1 Relocating the vector table in CCM Memory . . . . . . . . . . . . . . . . . 57120.4 How to Use the MPU in Cortex-M0+/3/4/7 Based STM32 MCUs . . . . . . . . . . 574

20.4.1 Programming the MPU With the CubeHAL . . . . . . . . . . . . . . . . . 578

21. Flash Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58221.1 Introduction to STM32 Flash Memory . . . . . . . . . . . . . . . . . . . . . . . . 58221.2 The HAL_FLASH Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586

21.2.1 Flash Memory Unlocking . . . . . . . . . . . . . . . . . . . . . . . . . . . 58621.2.2 Flash Memory Erasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58621.2.3 Flash Memory Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 58821.2.4 Flash Read Access During Programming and Erasing . . . . . . . . . . . . 589

21.3 Option Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58921.3.1 Flash Memory Read Protection . . . . . . . . . . . . . . . . . . . . . . . . 591

21.4 Optional OTP and True-EEPROM Memories . . . . . . . . . . . . . . . . . . . . 59321.5 Flash Read Latency and the ART™ Accelerator . . . . . . . . . . . . . . . . . . . 594

21.5.1 The Role of the TCM Memories in STM32F7 MCUs . . . . . . . . . . . . . 59721.5.1.1 How to Access Flash Memory Through the TCM Interface . . . . 60321.5.1.2 Using CubeMX to Configure Flash Memory Interface . . . . . . . 604

22. Booting Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60622.1 The Cortex-M Unified Memory Layout and the Booting Process . . . . . . . . . . 606

22.1.1 Software Physical Remap . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60722.1.2 Vector Table Relocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60822.1.3 Running the Firmware From SRAM Using the GNU MCU Eclipse Toolchain 610

22.2 Integrated Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61122.2.1 Starting the Bootloader From the On-Board Firmware . . . . . . . . . . . . 61322.2.2 The Booting Sequence in the GNU MCU Eclipse Tool-chain . . . . . . . . . 614

22.3 Developing a Custom Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . 61722.3.1 Vector Table Relocation in STM32F0 Microcontrollers . . . . . . . . . . . . 62822.3.2 How to Use the flasher.py Tool . . . . . . . . . . . . . . . . . . . . . . . 631

23. Running FreeRTOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63323.1 Understanding the Concepts Underlying an RTOS . . . . . . . . . . . . . . . . . 63423.2 Introduction to FreeRTOS and CMSIS-RTOS Wrapper . . . . . . . . . . . . . . . 640

23.2.1 The FreeRTOS Source Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . 64123.2.1.1 How to Import FreeRTOS Manually . . . . . . . . . . . . . . . . 64223.2.1.2 How to Import FreeRTOS Using CubeMX and CubeMXImporter 64323.2.1.3 How to Enable FPU Support in Cortex-M4F and Cortex-M7 Cores 645

23.3 Thread Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64523.3.1 Thread States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64823.3.2 Thread Priorities and Scheduling Policies . . . . . . . . . . . . . . . . . . . 649

Page 14: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

23.3.3 Voluntary Release of the Control . . . . . . . . . . . . . . . . . . . . . . . 65323.3.4 The idle Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653

23.4 Memory Allocation and Management . . . . . . . . . . . . . . . . . . . . . . . . 65423.4.1 Dynamic Memory Allocation Model . . . . . . . . . . . . . . . . . . . . . 655

23.4.1.1 heap_1.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65623.4.1.2 heap_2.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65723.4.1.3 heap_3.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65723.4.1.4 heap_4.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65723.4.1.5 heap_5.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65823.4.1.6 How to Use malloc() and Related C Functions With FreeRTOS . 65823.4.1.7 FreeRTOS Heap Definition . . . . . . . . . . . . . . . . . . . . . 659

23.4.2 Static Memory Allocation Model . . . . . . . . . . . . . . . . . . . . . . . 65923.4.2.1 idle Thread Allocation With Static Memory Allocation Model . . 660

23.4.3 Memory Pools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66023.4.4 Stack Overflow Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . 662

23.5 Synchronization Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66423.5.1 Message Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66423.5.2 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66823.5.3 Thread Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671

23.6 Resources Management and Mutual Exclusion . . . . . . . . . . . . . . . . . . . 67223.6.1 Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672

23.6.1.1 The Priority Inversion Problem . . . . . . . . . . . . . . . . . . . 67323.6.1.2 Recursive Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . 674

23.6.2 Critical Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67523.6.3 Interrupt Management With an RTOS . . . . . . . . . . . . . . . . . . . . . 676

23.6.3.1 FreeRTOS API and Interrupt Priorities . . . . . . . . . . . . . . . 67723.7 Software Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678

23.7.1 How FreeRTOS Manages Timers . . . . . . . . . . . . . . . . . . . . . . . . 67923.8 A Case Study: Low-Power Management With an RTOS . . . . . . . . . . . . . . 680

23.8.1 The idle Thread Hook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68023.8.2 The Tickless Mode in FreeRTOS . . . . . . . . . . . . . . . . . . . . . . . . 682

23.8.2.1 A Schema for the tickless Mode . . . . . . . . . . . . . . . . . . . 68423.8.2.2 A Custom tickless Mode Policy . . . . . . . . . . . . . . . . . . . 687

23.9 Debugging Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69523.9.1 configASSERT() Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69523.9.2 Run-Time Statistics and Thread State Information . . . . . . . . . . . . . . 696

23.10 Alternatives to FreeRTOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69923.10.1 ChibiOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70023.10.2 Contiki OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70023.10.3 OpenRTOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

24. Advanced Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70224.1 Understanding Cortex-M Fault-Related Exceptions . . . . . . . . . . . . . . . . . 702

Page 15: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

24.1.1 The Cortex-M Exception Entrance Sequence and the ARMCalling Conven-tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70424.1.1.1 How the GNU MCU Eclipse Tool-chain Handles Fault-Related

Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70924.1.1.2 How to Interpret the Content of the LR Register on Exception

Entrance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71124.1.2 Fault Exceptions and Faults Analysis . . . . . . . . . . . . . . . . . . . . . 712

24.1.2.1 Memory Management Exception . . . . . . . . . . . . . . . . . . 71224.1.2.2 Bus Fault Exception . . . . . . . . . . . . . . . . . . . . . . . . . 71324.1.2.3 Usage Fault Exception . . . . . . . . . . . . . . . . . . . . . . . . 71424.1.2.4 Hard Fault Exception . . . . . . . . . . . . . . . . . . . . . . . . 71524.1.2.5 Enabling Optional Fault Handlers . . . . . . . . . . . . . . . . . . 71624.1.2.6 Fault Analysis in Cortex-M0/0+ Based Processors . . . . . . . . . 716

24.2 Eclipse Advanced Debugging Features . . . . . . . . . . . . . . . . . . . . . . . . 71724.2.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717

24.2.1.1 Memory Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . 71824.2.2 Watchpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71924.2.3 Instruction Stepping Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 72024.2.4 Keil Packs and Peripheral Registers View . . . . . . . . . . . . . . . . . . . 72124.2.5 Core Registers View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724

24.3 Debugging Aids From the CubeHAL . . . . . . . . . . . . . . . . . . . . . . . . . 72524.4 External Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

24.4.1 Using SEGGER J-Link for ST-LINK Debugger . . . . . . . . . . . . . . . . 72724.4.2 Using the ITM Interface and SWV Tracing . . . . . . . . . . . . . . . . . . 731

24.5 STM Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73224.6 Debugging two Nucleo Boards Simultaneously . . . . . . . . . . . . . . . . . . . 734

25. FAT Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73725.1 Introduction to FatFs Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737

25.1.1 Using CubeMX to Include FatFs Library in Your Projects . . . . . . . . . . 74025.1.1.1 The Generic Disk Interface API . . . . . . . . . . . . . . . . . . . 74125.1.1.2 The Implementation of a Driver to Access SD Cards in SPI Mode 742

25.1.2 Relevant FatFs Structures and Functions . . . . . . . . . . . . . . . . . . . 74325.1.2.1 Mounting a Filesystem . . . . . . . . . . . . . . . . . . . . . . . 74325.1.2.2 Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74325.1.2.3 Reading From/Writing Into a File . . . . . . . . . . . . . . . . . . 74425.1.2.4 Creating and Opening a Directory . . . . . . . . . . . . . . . . . 745

25.1.3 How to Configure the FatFs Library . . . . . . . . . . . . . . . . . . . . . . 748

26. Develop IoT Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75126.1 Solutions Offered by STM to Develop IoT Applications . . . . . . . . . . . . . . . 75226.2 The W5500 Ethernet Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

26.2.1 How to Use the W5500 Shield and the ioLibrary_Driver Module . . . . . 758

Page 16: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

26.2.1.1 Configuring the SPI Interface . . . . . . . . . . . . . . . . . . . . 76126.2.1.2 Configuring the Socket Buffers and the Network Interface . . . . 762

26.2.2 Socket APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76426.2.2.1 Handling Sockets in TCP Mode . . . . . . . . . . . . . . . . . . . 76626.2.2.2 Handling Sockets in UDP Mode . . . . . . . . . . . . . . . . . . . 766

26.2.3 I/O Retargeting to a TCP/IP Socket . . . . . . . . . . . . . . . . . . . . . . 76726.2.4 Setting up an HTTP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . 769

26.2.4.1 A Web-Based Oscilloscope . . . . . . . . . . . . . . . . . . . . . 772

27. Getting Started With a New Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78527.1 Hardware Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

27.1.1 PCB Layer Stack-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78627.1.2 MCU Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78727.1.3 Decoupling of Power-Supply Pins . . . . . . . . . . . . . . . . . . . . . . . 78827.1.4 Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79027.1.5 Filtering of RESET Pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79127.1.6 Debug Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79127.1.7 Boot Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79327.1.8 Pay attention to “pin-to-pin” Compatibility… . . . . . . . . . . . . . . . . . 79427.1.9 …And to Selecting the Right Peripherals . . . . . . . . . . . . . . . . . . . 79527.1.10 The Role of CubeMX During the Board Design Stage . . . . . . . . . . . . 79527.1.11 Board Layout Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798

27.2 Software Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79927.2.1 Generating the binary image for production . . . . . . . . . . . . . . . . . 799

Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802

A. Miscellaneous HAL functions and STM32 features . . . . . . . . . . . . . . . . . . . . . 803Force MCU reset from the firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803STM32 96-bit Unique CPU ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

B. Troubleshooting guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805GNU MCU Eclipse Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805Eclipse related issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805

Eclipse cannot locate the compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . 806Eclipse continuously breaks at every instruction during debug session . . . . . . . . 807The step-by-step debugging is really slow . . . . . . . . . . . . . . . . . . . . . . . 807The firmware works only under a debug session . . . . . . . . . . . . . . . . . . . 808

STM32 related issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808The microcontroller does not boot correctly . . . . . . . . . . . . . . . . . . . . . . 808It is Not Possibile to Flash or to Debug the MCU . . . . . . . . . . . . . . . . . . . 810

Page 17: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

C. Nucleo pin-out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812Nucleo-F446RE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

Nucleo-F411RE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814

Nucleo-F410RB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815

Nucleo-F401RE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816

Nucleo-F334R8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

Nucleo-F303RE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818

Nucleo-F302R8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819

Nucleo-F103RB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820

Nucleo-F091RC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821

Nucleo-F072RB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822

Nucleo-F070RB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823

Nucleo-F030R8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824

Nucleo-L476RG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825

Nucleo-L152RE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826

Page 18: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Nucleo-L073R8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827

Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827

Nucleo-L053R8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828Arduino compatible headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828Morpho headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828

D. STM32 packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829LFBGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829LQFP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829TFBGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830TSSOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830UFBGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830UFQFPN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831VFQFP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831WLCSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831

E. History of this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833Release 0.1 - October 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833Release 0.2 - October 28th, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833

Release 0.2.1 - October 31th, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833Release 0.2.2 - November 1st, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . 834

Release 0.3 - November 12th, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834Release 0.4 - December 4th, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834Release 0.5 - December 19th, 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834Release 0.6 - January 18th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

Release 0.6.1 - January 20th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835Release 0.6.2 - January 30th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

Release 0.7 - February 8th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835Release 0.8 - February 18th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836

Release 0.8.1 - February 23th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . 836Release 0.9 - March 27th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836

Release 0.9.1 - March 28th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837Release 0.10 - April 26th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837Release 0.11 - May 27th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

Release 0.11.1 - June 3rd, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838Release 0.11.2 - June 24th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

Release 0.12 - July 4th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838Release 0.13 - July 18th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839Release 0.14 - August 12th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839Release 0.15 - September 13th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839Release 0.16 - October 3th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840

Page 19: MasteringSTM32 - Carmine Noviello · MasteringSTM32 Astep-by-stepguidetothemostcompleteARMCortex-M platform,usingafreeandpowerfuldevelopment environmentbasedonEclipseandGCC …

CONTENTS

Release 0.17 - October 24th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Release 0.18 - November 15th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Release 0.19 - November 29th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Release 0.20 - December 28th, 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Release 0.21 - January 29th, 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Release 0.22 - May 2nd, 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Release 0.23 - July 20th, 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842Release 0.24 - December 11th, 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842Release 0.25 - January 3rd, 2018 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842