mastering-stm32-2nd-toc
mastering-stm32-2nd-toc
Carmine Noviello
This book is for sale at http://leanpub.com/mastering-stm32-2nd
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
How to Integrate This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
How Is the Book Organized? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Differences With the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
Errata and Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Book Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
How to Help the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Copyright Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
I Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1. Introduction to STM32 MCU Portfolio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1 Introduction to ARM Based Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Cortex and Cortex-M Based Processors . . . . . . . . . . . . . . . . . . . . 4
1.1.1.1 Core Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1.2 Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.1.3 Bit-Banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1.1.4 Thumb-2 and Memory Alignment . . . . . . . . . . . . . . . . 11
1.1.1.5 Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.1.1.6 Interrupts and Exceptions Handling . . . . . . . . . . . . . . . 14
1.1.1.7 SysTimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.1.1.8 Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.1.9 TrustZoneTM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.1.1.10 CMSIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1.1.11 Effective Implementation of Cortex-M Features in the
STM32 Portfolio . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.2 Introduction to STM32 Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.2.1 Advantages of the STM32 Portfolio…. . . . . . . . . . . . . . . . . . . . . . 22
CONTENTS
3. Hello, Nucleo! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.1 Create a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.2 Adding Something Useful to the Generated Code . . . . . . . . . . . . . . . . . . . . 79
3.3 Connecting the Nucleo to the PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.3.1 ST-LINK Firmware Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.4 Flashing the Nucleo using STM32CubeProgrammer . . . . . . . . . . . . . . . . . . . 86
4. STM32CubeMX Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.1 Introduction to CubeMX Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.1.1 Target Selection Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.1.1 MCU/MPU Selector . . . . . . . . . . . . . . . . . . . . . . . . 91
4.1.1.2 Board Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
CONTENTS
22.1 The Cortex-M Unified Memory Layout and the Booting Process . . . . . . . . . . . 558
22.1.1 Software Physical Remap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
22.1.2 Vector Table Relocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
22.1.3 Running the Firmware From SRAM Using the STM32CubeIDE . . . . . 562
22.2 Integrated STM32 Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
22.2.1 Starting the STM32 Bootloader from the On-Board Firmware . . . . . . 566
22.2.2 The Booting Sequence in the STM32CubeIDE Tool-chain . . . . . . . . . 567
22.3 Developing a Custom Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
22.3.1 Vector Table Relocation in STM32F0 Microcontrollers . . . . . . . . . . . 579
22.3.2 How to Use the flasher.py Tool . . . . . . . . . . . . . . . . . . . . . . . . 581
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860