A step-by-step guide to the most complete ARM Cortex-M
platform, using a free and powerful development
environment based on Eclipse and GCC
Author(s): Carmine Noviello
Series: 0.26
Publisher: Lean Publishing
Year: 2018
Language: English
Pages: 852
Table of Contents......Page 4
Why Did I Write the Book?......Page 20
Who Is This Book For?......Page 21
How to Integrate This Book?......Page 22
How Is the Book Organized?......Page 23
About the Author......Page 26
Book Support......Page 27
Credits......Page 28
Acknowledgments......Page 29
I Introduction......Page 30
Introduction to ARM Based Processors......Page 31
Core Registers......Page 33
Memory Map......Page 36
Bit-Banding......Page 38
Thumb-2 and Memory Alignment......Page 41
Pipeline......Page 42
Interrupts and Exceptions Handling......Page 44
Power Modes......Page 46
CMSIS......Page 48
Effective Implementation of Cortex-M Features in the STM32 Portfolio......Page 49
Introduction to STM32 Microcontrollers......Page 50
Advantages of the STM32 Portfolio….......Page 51
….And Its Drawbacks......Page 52
A Quick Look at the STM32 Subfamilies......Page 53
F0......Page 56
F1......Page 57
F2......Page 58
F3......Page 60
F4......Page 62
F7......Page 63
H7......Page 64
L0......Page 65
L1......Page 66
L4......Page 68
L4+......Page 69
STM32WB......Page 71
How to Select the Right MCU for You?......Page 72
The Nucleo Development Board......Page 75
Setting-Up the Tool-Chain......Page 81
Why Choose Eclipse/GCC as Tool-Chain for STM32......Page 82
… and GCC......Page 83
Windows - Installing the Tool-Chain......Page 84
Windows - Eclipse Installation......Page 85
Windows - Eclipse Plug-Ins Installation......Page 86
Windows - GCC ARM Embedded Installation......Page 92
Windows – OpenOCD Installation......Page 93
Windows – ST-LINK Firmware Upgrade......Page 94
Linux - Installing the Tool-Chain......Page 95
Linux - Eclipse Installation......Page 96
Linux - Eclipse Plug-Ins Installation......Page 98
Linux – ST-LINK Firmware Upgrade......Page 104
Linux – OpenOCD Installation......Page 105
Linux - ST Tools Installation......Page 107
Mac - Installing the Tool-Chain......Page 109
Mac - Eclipse Installation......Page 110
Mac - Eclipse Plug-Ins Installation......Page 112
Mac - GCC ARM Embedded Installation......Page 117
Mac – ST-LINK Firmware Upgrade......Page 118
Mac – OpenOCD Installation......Page 119
Mac - ST Tools Installation......Page 121
Get in Touch With the Eclipse IDE......Page 123
Create a Project......Page 127
Connecting the Nucleo to the PC......Page 134
Flashing the Nucleo using STM32CubeProgrammer......Page 135
Understanding the Generated Code......Page 136
Introduction to CubeMX Tool......Page 139
Chip View......Page 143
IP Tree Pane......Page 145
Clock View......Page 147
Configuration View......Page 148
Power Consumption Calculator View......Page 149
Generate C Project with CubeMX......Page 150
Understanding Generated Code......Page 152
Create Eclipse Project......Page 154
Importing Generated Files Into the Eclipse Project Manually......Page 157
Importing Files Generated With CubeMX Into the Eclipse Project Automatically......Page 162
Understanding Generated Application Code......Page 163
Add Something Useful to the Firmware......Page 168
Downloading Book Source Code Examples......Page 169
Getting Started With OpenOCD......Page 173
Launching OpenOCD......Page 174
Launching OpenOCD on Windows......Page 175
Launching OpenOCD on Linux and MacOS X.......Page 176
Connecting to the OpenOCD Telnet Console......Page 178
Configuring Eclipse......Page 179
Debugging in Eclipse......Page 185
Enable Semihosting on a New Project......Page 190
Using Semihosting With C Standard Library......Page 193
Enable Semihosting on an Existing Project......Page 196
Understanding How Semihosting Works......Page 197
II Diving into the HAL......Page 202
STM32 Peripherals Mapping and HAL Handlers......Page 203
GPIOs Configuration......Page 208
GPIO Mode......Page 210
GPIO Alternate Function......Page 212
Understanding GPIO Speed......Page 213
Driving a GPIO......Page 216
De-initialize a GPIO......Page 217
NVIC Controller......Page 219
Vector Table in STM32......Page 220
Enabling Interrupts......Page 223
External Lines and NVIC......Page 224
Enabling Interrupts With CubeMX......Page 228
Interrupt Lifecycle......Page 230
Cortex-M0/0+......Page 234
Cortex-M3/4/7......Page 238
Interrupt Re-Entrancy......Page 245
Mask All Interrupts at Once or an a Priority Basis......Page 247
Introduction to UARTs and USARTs......Page 250
UART Initialization......Page 254
UART Configuration Using CubeMX......Page 261
UART Communication in Polling Mode......Page 262
Installing a Serial Console in Windows......Page 266
Installing a Serial Console in Linux and MacOS X......Page 268
UART Communication in Interrupt Mode......Page 270
UART Related Interrupts......Page 271
Error Management......Page 278
I/O Retargeting......Page 279
Introduction to DMA......Page 283
The Need of a DMA and the Role of the Internal Buses......Page 284
The DMA Controller......Page 287
The DMA Implementation in F0/F1/F3/L1 MCUs......Page 288
The DMA Implementation in F2/F4/F7 MCUs......Page 292
The DMA Implementation in L0/L4 MCUs......Page 295
DMA_HandleTypeDef in F0/F1/F3/L0/L1/L4 HALs......Page 296
DMA_HandleTypeDef in F2/F4/F7 HALs......Page 298
How to Perform Transfers in Polling Mode......Page 302
How to Perform Transfers in Interrupt Mode......Page 305
How to Perform Peripheral-To-Peripheral Transfers......Page 307
Using the HAL_UART Module With DMA Mode Transfers......Page 308
Miscellaneous Functions From HAL_DMA and HAL_DMA_Ex Modules......Page 310
Using CubeMX to Configure DMA Requests......Page 311
Correct Memory Allocation of DMA Buffers......Page 312
A Case Study: The DMA Memory-To-Memory Transfer Performance Analysis......Page 313
Clock Distribution......Page 319
Overview of the STM32 Clock Tree......Page 320
The Multispeed Internal RC Oscillator in STM32L Families......Page 324
Configuring Clock Tree Using CubeMX......Page 325
OSC Clock Supply......Page 327
OSC 32kHz Clock Supply......Page 328
Overview of the HAL_RCC Module......Page 329
Compute the Clock Frequency at Run-Time......Page 331
Enabling the Clock Security System......Page 332
HSI Calibration......Page 333
Introduction to Timers......Page 335
Timer Categories in an STM32 MCU......Page 336
Effective Availability of Timers in the STM32 Portfolio......Page 338
Basic Timers......Page 340
Using Timers in Interrupt Mode......Page 343
Time Base Generation in Advanced Timers......Page 345
Using Timers in Polling Mode......Page 346
Using Timers in DMA Mode......Page 347
Using CubeMX to Configure a Basic Timer......Page 349
Time Base Generator With External Clock Sources......Page 350
External Clock Mode 2......Page 352
External Clock Mode 1......Page 356
Using CubeMX to Configure the Source Clock of a General Purpose Timer......Page 360
Master/Slave Synchronization Modes......Page 362
Using CubeMX to Configure the Master/Slave Synchronization......Page 367
Generate Timer-Related Events by Software......Page 368
Counting Modes......Page 370
Input Capture Mode......Page 371
Output Compare Mode......Page 378
Pulse-Width Generation......Page 384
Generating a Sinusoidal Wave Using PWM......Page 387
Using CubeMX to Configure the PWM Mode......Page 392
One Pulse Mode......Page 393
Using CubeMX to Configure the OPM Mode......Page 395
Encoder Mode......Page 396
Other Features Available in General Purpose and Advanced Timers......Page 401
Combined Three-Phase PWM Mode and Other Motor-Control Related Features......Page 402
Preloading of Auto-Reload Register......Page 403
Debugging and Timers......Page 404
SysTick Timer......Page 405
Use Another Timer as System Timebase Source......Page 406
A Case Study: How to Precisely Measure Microseconds With STM32 MCUs......Page 407
Introduction to SAR ADC......Page 413
HAL_ADC Module......Page 418
Single-Channel, Single Conversion Mode......Page 420
Single-Channel, Continuous Conversion Mode......Page 421
Injected Conversion Mode......Page 422
Channel Selection......Page 423
A/D Conversions in Polling Mode......Page 425
A/D Conversions in Interrupt Mode......Page 429
A/D Conversions in DMA Mode......Page 430
Multiple and not Continuous Conversions in DMA Mode......Page 433
Errors Management......Page 434
Timer-Driven Conversions......Page 435
ADC Calibration......Page 438
Using CubeMX to Configure ADC Peripheral......Page 439
Introduction to the DAC Peripheral......Page 442
HAL_DAC Module......Page 444
Driving the DAC Manually......Page 446
Driving the DAC in DMA Mode Using a Timer......Page 448
Triangular Wave Generation......Page 451
Noise Wave Generation......Page 453
Introduction to the I²C specification......Page 454
The I²C Protocol......Page 456
Address Frame......Page 457
Data Frames......Page 458
Combined Transactions......Page 459
Availability of I²C Peripherals in STM32 MCUs......Page 460
HAL_I2C Module......Page 461
Using the I²C Peripheral in Master Mode......Page 465
I/O MEM Operations......Page 473
Combined Transactions......Page 474
Using the I²C Peripheral in Slave Mode......Page 476
Using CubeMX to Configure the I²C Peripheral......Page 482
Introduction to the SPI Specification......Page 484
Clock Polarity and Phase......Page 486
SPI TI Mode......Page 488
Availability of SPI Peripherals in STM32 MCUs......Page 489
HAL_SPI Module......Page 490
Exchanging Messages Using SPI Peripheral......Page 492
Using CubeMX to Configure SPI Peripheral......Page 494
Introduction to CRC Computing......Page 495
CRC Calculation in STM32F1/F2/F4/L1 MCUs......Page 498
CRC Peripheral in STM32F0/F3/F7/L0/L4 MCUs......Page 500
HAL_CRC Module......Page 501
The Independent Watchdog Timer......Page 505
Using the CubeHAL to Program IWDG Timer......Page 506
The System Window Watchdog Timer......Page 507
Using the CubeHAL to Program WWDG Timer......Page 509
Detecting a System Reset Caused by a Watchdog Timer......Page 510
Selecting the Right Watchdog Timer for Your Application......Page 511
Introduction to the RTC Peripheral......Page 512
HAL_RTC Module......Page 514
Setting and Retrieving the Current Date/Time......Page 515
Correct Way to Read Date/Time Values......Page 517
Configuring Alarms......Page 518
Periodic Wakeup Unit......Page 520
RTC Calibration......Page 522
RTC Smooth Calibration......Page 523
Reference Clock Detection......Page 524
Using the Backup SRAM......Page 525
III Advanced topics......Page 527
Power Management in Cortex-M Based MCUs......Page 528
How Cortex-M MCUs Handle Run and Sleep Modes......Page 529
Entering/exiting sleep modes......Page 532
Sleep Modes in Cortex-M Based MCUs......Page 534
Power Sources......Page 535
Power Modes......Page 536
Run Mode......Page 537
Dynamic Voltage Scaling in STM32F4/F7 MCUs......Page 538
Sleep Mode......Page 539
Stop Mode......Page 540
Low-Power Modes Example......Page 541
An Important Warning for STM32F1 Microcontrollers......Page 545
Power Sources......Page 547
Run Modes......Page 549
Sleep Modes......Page 551
Stop Modes......Page 552
Standby Modes......Page 553
Power Modes Transitions......Page 554
LPUART......Page 555
Power Supply Supervisors......Page 556
Using the CubeMX Power Consumption Calculator......Page 557
A Case Study: Using Watchdog Timers With Low-Power Modes......Page 558
The STM32 Memory Layout Model......Page 560
Understanding Compilation and Linking Processes......Page 562
The Really Minimal STM32 Application......Page 565
ELF Binary File Inspection......Page 569
.data and .bss Sections Initialization......Page 571
A Word About the COMMON Section......Page 577
.rodata Section......Page 579
Stack and Heap Regions......Page 580
Checking the Size of Heap and Stack at Compile-Time......Page 583
Differences With the Tool-Chain Script Files......Page 584
How to Use the CCM Memory......Page 586
Relocating the vector table in CCM Memory......Page 589
How to Use the MPU in Cortex-M0+/3/4/7 Based STM32 MCUs......Page 592
Programming the MPU With the CubeHAL......Page 596
Introduction to STM32 Flash Memory......Page 599
The HAL_FLASH Module......Page 602
Flash Memory Erasing......Page 603
Flash Memory Programming......Page 605
Option Bytes......Page 606
Flash Memory Read Protection......Page 608
Optional OTP and True-EEPROM Memories......Page 610
Flash Read Latency and the ART™ Accelerator......Page 611
The Role of the TCM Memories in STM32F7 MCUs......Page 614
How to Access Flash Memory Through the TCM Interface......Page 619
Using CubeMX to Configure Flash Memory Interface......Page 621
The Cortex-M Unified Memory Layout and the Booting Process......Page 622
Software Physical Remap......Page 623
Vector Table Relocation......Page 624
Running the Firmware From SRAM Using the GNU MCU Eclipse Toolchain......Page 626
Integrated Bootloader......Page 627
Starting the Bootloader From the On-Board Firmware......Page 629
The Booting Sequence in the GNU MCU Eclipse Tool-chain......Page 630
Developing a Custom Bootloader......Page 633
Vector Table Relocation in STM32F0 Microcontrollers......Page 643
How to Use the flasher.py Tool......Page 646
Running FreeRTOS......Page 649
Understanding the Concepts Underlying an RTOS......Page 650
Introduction to FreeRTOS and CMSIS-RTOS Wrapper......Page 656
The FreeRTOS Source Tree......Page 657
How to Import FreeRTOS Manually......Page 658
How to Import FreeRTOS Using CubeMX and CubeMXImporter......Page 659
Thread Management......Page 661
Thread States......Page 664
Thread Priorities and Scheduling Policies......Page 665
Voluntary Release of the Control......Page 668
The idle Thread......Page 669
Memory Allocation and Management......Page 670
Dynamic Memory Allocation Model......Page 671
heap_1.c......Page 672
heap_4.c......Page 673
How to Use malloc() and Related C Functions With FreeRTOS......Page 674
Static Memory Allocation Model......Page 675
Memory Pools......Page 676
Stack Overflow Detection......Page 678
Message Queues......Page 680
Semaphores......Page 684
Resources Management and Mutual Exclusion......Page 687
Mutexes......Page 688
The Priority Inversion Problem......Page 689
Critical Sections......Page 690
Interrupt Management With an RTOS......Page 691
FreeRTOS API and Interrupt Priorities......Page 692
Software Timers......Page 693
A Case Study: Low-Power Management With an RTOS......Page 695
The idle Thread Hook......Page 696
The Tickless Mode in FreeRTOS......Page 697
A Schema for the tickless Mode......Page 699
A Custom tickless Mode Policy......Page 703
configASSERT() Macro......Page 710
Run-Time Statistics and Thread State Information......Page 711
Alternatives to FreeRTOS......Page 714
Contiki OS......Page 715
OpenRTOS......Page 716
Understanding Cortex-M Fault-Related Exceptions......Page 717
The Cortex-M Exception Entrance Sequence and the ARM Calling Convention......Page 719
How the GNU MCU Eclipse Tool-chain Handles Fault-Related Exceptions......Page 723
How to Interpret the Content of the LR Register on Exception Entrance......Page 725
Fault Exceptions and Faults Analysis......Page 726
Bus Fault Exception......Page 727
Usage Fault Exception......Page 728
Hard Fault Exception......Page 729
Fault Analysis in Cortex-M0/0+ Based Processors......Page 730
Expressions......Page 731
Memory Monitors......Page 732
Watchpoints......Page 733
Instruction Stepping Mode......Page 734
Keil Packs and Peripheral Registers View......Page 735
Core Registers View......Page 738
External Debuggers......Page 739
Using SEGGER J-Link for ST-LINK Debugger......Page 741
Using the ITM Interface and SWV Tracing......Page 745
STM Studio......Page 746
Debugging two Nucleo Boards Simultaneously......Page 748
Introduction to FatFs Library......Page 751
Using CubeMX to Include FatFs Library in Your Projects......Page 754
The Generic Disk Interface API......Page 755
The Implementation of a Driver to Access SD Cards in SPI Mode......Page 756
Opening a File......Page 757
Reading From/Writing Into a File......Page 758
Creating and Opening a Directory......Page 759
How to Configure the FatFs Library......Page 762
Develop IoT Applications......Page 764
Solutions Offered by STM to Develop IoT Applications......Page 765
The W5500 Ethernet Controller......Page 767
How to Use the W5500 Shield and the ioLibrary_Driver Module......Page 771
Configuring the SPI Interface......Page 773
Configuring the Socket Buffers and the Network Interface......Page 774
Socket APIs......Page 776
Handling Sockets in TCP Mode......Page 777
Handling Sockets in UDP Mode......Page 778
I/O Retargeting to a TCP/IP Socket......Page 779
Setting up an HTTP Server......Page 781
A Web-Based Oscilloscope......Page 783
Hardware Design......Page 796
PCB Layer Stack-Up......Page 797
MCU Package......Page 798
Decoupling of Power-Supply Pins......Page 799
Clocks......Page 800
Debug Port......Page 802
Boot Mode......Page 804
Pay attention to ``pin-to-pin'' Compatibility…......Page 805
The Role of CubeMX During the Board Design Stage......Page 806
Board Layout Strategies......Page 809
Generating the binary image for production......Page 810
Appendix......Page 813
STM32 96-bit Unique CPU ID......Page 814
Eclipse related issue......Page 816
Eclipse continuously breaks at every instruction during debug session......Page 817
The step-by-step debugging is really slow......Page 818
The microcontroller does not boot correctly......Page 819
It is Not Possibile to Flash or to Debug the MCU......Page 821
C. Nucleo pin-out......Page 822
Morpho headers......Page 823
Morpho headers......Page 824
Morpho headers......Page 825
Morpho headers......Page 826
Morpho headers......Page 827
Morpho headers......Page 828
Morpho headers......Page 829
Morpho headers......Page 830
Morpho headers......Page 831
Morpho headers......Page 832
Morpho headers......Page 833
Morpho headers......Page 834
Morpho headers......Page 835
Morpho headers......Page 836
Morpho headers......Page 837
Morpho headers......Page 838
LQFP......Page 839
UFBGA......Page 840
WLCSP......Page 841
Release 0.2.1 - October 31th, 2015......Page 843
Release 0.5 - December 19th, 2015......Page 844
Release 0.7 - February 8th, 2016......Page 845
Release 0.9.1 - March 28th, 2016......Page 846
Release 0.11 - May 27th, 2016......Page 847
Release 0.14 - August 12th, 2016......Page 848
Release 0.17 - October 24th, 2016......Page 849
Release 0.21 - January 29th, 2017......Page 850
Release 0.24 - December 11th, 2017......Page 851
Release 0.26 - May 7th, 2018......Page 852