(FREE PDF Sample) Programming With STM32: Getting Started With The Nucleo Board and C/C++ Donald Norris Ebooks
(FREE PDF Sample) Programming With STM32: Getting Started With The Nucleo Board and C/C++ Donald Norris Ebooks
com
https://textbookfull.com/product/programming-with-
stm32-getting-started-with-the-nucleo-board-and-c-c-donald-
norris/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/getting-started-with-advanced-c-1st-
edition-vaskaran-sarcar-sarcar/
textboxfull.com
Getting Started with the micro bit Coding and Making with
the BBC s Open Development Board 1st Edition Wolfram Donat
https://textbookfull.com/product/getting-started-with-the-micro-bit-
coding-and-making-with-the-bbc-s-open-development-board-1st-edition-
wolfram-donat/
textboxfull.com
https://textbookfull.com/product/primary-mathematics-
textbook-2b-jennifer-hoerst/
textboxfull.com
Learning C by developing games with Unity 2020 An
enjoyable and intuitive approach to getting started with C
programming and Unity Fifth Edition Harrison Ferrone
https://textbookfull.com/product/learning-c-by-developing-games-with-
unity-2020-an-enjoyable-and-intuitive-approach-to-getting-started-
with-c-programming-and-unity-fifth-edition-harrison-ferrone/
textboxfull.com
https://textbookfull.com/product/getting-started-with-phantomjs-
beltran-aries/
textboxfull.com
https://textbookfull.com/product/getting-started-with-kubernetes-2nd-
edition-baier/
textboxfull.com
https://textbookfull.com/product/make-getting-started-with-processing-
py-allison-parrish/
textboxfull.com
https://textbookfull.com/product/getting-started-with-gulp-second-
edition-travis-maynard/
textboxfull.com
Copyright © 2018 by McGraw-Hill Education. All rights reserved. Except as
permitted under the United States Copyright Act of 1976, no part of this
publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of
the publisher.
ISBN: 978-1-26-003132-4
MHID: 1-26-003132-2
The material in this eBook also appears in the print version of this title: ISBN:
978-1-26-003131-7, MHID: 1-26-003131-4.
All trademarks are trademarks of their respective owners. Rather than put a
trademark symbol after every occurrence of a trademarked name, we use names
in an editorial fashion only, and to the benefit of the trademark owner, with no
intention of infringement of the trademark. Where such designations appear in
this book, they have been printed with initial caps.
This is a copyrighted work and McGraw-Hill Education and its licensors reserve
all rights in and to the work. Use of this work is subject to these terms. Except as
permitted under the Copyright Act of 1976 and the right to store and retrieve one
copy of the work, you may not decompile, disassemble, reverse engineer,
reproduce, modify, create derivative works based upon, transmit, distribute,
disseminate, sell, publish or sublicense the work or any part of it without
McGraw-Hill Education’s prior consent. You may use the work for your own
noncommercial and personal use; any other use of the work is strictly prohibited.
Your right to use the work may be terminated if you fail to comply with these
terms.
Peter enjoys cooking, fine wine, traveling, and spending quality time with his
family. His wife is my daughter, Shauna, and their child is my two-year-old
granddaughter, Evangeline.
Peter and I have spent many hours discussing many topics ranging from ancient
Greek artifacts to the latest technologies impacting modern society. I always
look forward to those interesting and challenging discussions.
CONTENTS AT A GLANCE
3 STM32CubeMX Application
6 Interrupts
7 Timers
9 Analog-to-Digital Conversion
Index
CONTENTS
Preface
3 STM32CubeMX Application
Pinout Tab
MCU Alternative Functions
Integrated Peripheral (IP) Tree Pane
Creating an Example Project using CubeMX
The main.c Code Listing
ARM Cortex Microcontroller Software Interface Standard (CMSIS)
CubeMX-Generated C Code
Compiling and Downloading the Project
Downloading the Hex Code
Summary
6 Interrupts
Interrupts
NVIC Specifications
Interrupt Process
External Interrupts
Interrupt Demonstration
Summary
7 Timers
STM Timer Peripherals
STM Timer Configuration
Update Event Calculation
Polled or Non-interrupt Blink LED Timer Demonstration
Test Run
Interrupt-Driven Blink LED Timer Demonstration
Test Run
Multi-rate Interrupt-Driven Blink LED Timer Demonstration
Test Run
Modification to the Multi-rate Program
Test Run
Summary
9 Analog-to-Digital Conversion
ADC Functions
ADC Module with HAL
ADC Conversion Modes
Channels, Groups, and Ranks
ADC Demonstration
ADC Demonstration Software
Summary
Microcomputer vs Microcontroller
I believe at the start of this book that it must make very clear the differences
between a microcomputer and a MCU. The reason for this distinction is very
simple: STM is a company that designs and manufacturers MCUs, not
microcomputers. I think my following definition of a MCU is as good as any
that I have read:
Right away, you can see from the definition that a MCU contains a
microprocessor which is sometimes referred to as a microcomputer. There also
must be both dynamic or volatile memory as well as nonvolatile or static
memory, where the latter holds any programs or scripts necessary to run the
microprocessor. Finally, there are always peripherals added to the design that
allow for the input and output of digital signals. There are often additional
peripherals such as timers, interrupt controllers, serial data ports, and a variety of
others depending upon what requirements the MCU must meet.
I discuss all the principal MCU components below to provide you with a solid
background to understand how a MCU functions. Most of the following
explanations are based on the voluminous amount of information provided by
STM on their microcontrollers. The reason behind STM providing such a large
amount of information is to allow engineers/software developers access to all the
data they need in order to incorporate STM products into original equipment
manufacturer (OEM) designs and products. This approach is fundamentally
different than the approach taken by suppliers of maker style boards and
products, such as the Arduino, Raspberry Pi, BeagleBone, and so forth. In the
case of the latter, board documentation is geared toward how to use a board in a
project. The STM data is extremely specific describing items such as the
nanosecond timing pulses between memory chips and processor buses. This is
exactly the reason why some of the STM MCU datasheets are over 1,100 pages
in length. Fortunately, the datasheet for the primary STM MCU I will be using in
this book is only 138 pages. I will provide later in this chapter the website where
you can download the datasheet.
The boards are named Nucleo-32, Nucleo-64, and Nucleo-144 from left to
right, respectively, as shown in Figure 1-1. The number in each name represents
the number of pins present in the MCU chip. Nucleo-64 is the principal board
used in this book.
Processor
The processor used in Nucleo-32 and Nucleo-64 boards is the ARM Cortex M-4
32-bit processor. It too has a lengthy 278-page user guide available from
infocenter.arm.com/help/topic/com.arm.doc.dui0553a/DUI0553A_cortex_m4_dgug.pdf
The actual processor circuitry is part of the STM MCU because STM has
purchased intellectual property (IP) rights from the ARM Corporation in order to
integrate it into its chips. However, for all practical purposes, the ARM
processor is programmed using the tools and techniques promulgated by the
ARM Corporation to support its processor IP. This distinction is of no
consequence in our case because the software tools to be used for the book
projects have all been carefully crafted and tested to work seamlessly together by
STM. Any license issues have already been resolved without bothering the end
user.
The following list contains some of the important specifications for the
Cortex M-4 processor for interested readers:
• Full-featured ARMv7-M instruction set, optimized for embedded
applications
• Floating point unit (FPU)
• Low-power 32-bit processor
• Memory protection unit (MPU)
• Nested vector interrupt controller (NVIC)
• Trace, breakpoint, and JTAG capabilities
• Advanced Microcontroller Bus Architecture (AMBA)
• Advanced High-Performance Bus (AHB5, AHB-Lite)
There are many more features to the Cortex M-4 processor as the 278-page
user guide would suggest. I would also recommend Joseph Yiu’s book, The
Definitive Guide to ARM® Cortex®-M3 and Cortex®-M4 Processors, Third
Edition, to readers who really want to delve into this processor to a great depth.
Figure 1-2 is a block diagram of the Cortex M-4 processor showing all of the
listed items and more.
Figure 1-2 ARM Cortex M-4 block diagram.
Memory
The Cortex M-4 processor uses a Harvard architecture. This means that program
instructions are stored and retrieved from a memory separate and distinct from
the memory that holds data. The other common computer architecture is named
von Neumann in which instructions and data share a common memory. MCUs
have very limited memory and take advantage of the speed-up available by using
concurrent instruction and data access. Additionally, having separate memories
means that the processor is no longer constrained to the same sized data widths.
This means instructions can be fetched and executed in 4-byte chunks or 32 bits,
while data can be simultaneously handled with 1-byte or 8-bit chunks, thus
speeding up the overall throughput to and from the processor and memory.
Another advantage the Harvard architecture has over the von Neumann form is
that instruction prefetches can now be done in parallel with regular instruction
executions, thus further speeding up the overall system performance. Finally,
concurrent instruction and data access eliminates the need for data caches, which
are typically used in von Neumann machines. This further reduces system
complexity and power consumption.
The Nucleo-64 board used in this book is the STM MCU, model number
STM32F302R8. This chip has a 64-KB flash memory and a 16-KB static
random access memory (SRAM). Yes, those are kilobytes, not mega- or
gigabytes. You cannot expect to create any graphical programs that run in this
limited memory space. Microcontroller programs are truly a throwback to the
earliest days of computing where memory was very limited and developers had
to use every available byte to store and execute programs. Having mentioned the
scarce memory resources, you will be able to use a variety of modern-day
graphic-based programs to develop the MCU program, but they will run on a
PC. The compiled and optimized binary code will be downloaded into the MCU
from the PC.
It turns out that 64 KB is plenty of space to run fairly large programs because
the C/C++ cross-compiler used for this book’s projects produces optimize code,
eliminating all but the essential instructions needed for the program. The 16-KB
SRAM size is more than adequate for the dynamic memory requirements to
support a maximum 64-KB sized program. The actual memory is integrated onto
the MCU chip and its type is largely irrelevant for our purposes.
Peripherals
The peripherals are what make the MCU viable for its intended purposes. In my
microcontroller definition I stated, “a set of peripherals consistent with all design
requirements.” This means that microcontrollers typically have different
peripheral configurations depending upon the requirements they are designed to
meet. This is a major reason why STM manufactures such a wide variety of
MCUs. I will focus on the Nucleo-64 microcontroller I mentioned above and
will use its set of peripherals as my discussion points. I will next need to
demonstrate how the Nucleo-64 board is set up in order to fully explain how all
the peripheral features function with this board.
The first item you should note is that the board is actually made of two
sections, the top section being a ST-LINK/V2-1 programmer and the bottom
section is the STM32 MCU. The bottom section is also fitted with two sets of
connectors. One set is compatible with the Arduino shield pin configuration and
other set is the ST Morpho pinout configuration. There are two push buttons, B1
and B2, shown in the figure. B1 is multipurpose, while B2 is the reset push
button. There is also one LED indicated in the figure; however, there are several
others also which are shown in follow-on figures. The programmer section may
be separated from the microcontroller section by snapping the sparse printed
circuit board (PCB) joints, which can be seen in Figure 1-1. I would not
recommend doing so because you would lose all programming capability for the
MCU section. I suppose it was set up in this fashion so you could separate the
sections once there was a working MCU for a specific project and there was no
need for any further programming.
Figure 1-4 is a top view of the Nucleo-64 board with most of the components
clearly annotated.
Remarkably, there are relatively few components on the board. Most of the
space is taken up by connectors, push buttons, configuration jumpers, LEDs, and
the microcontroller chip. This sparse layout reflects the nature of the MCU,
which is to be an embedded device without any of the niceties needed for the
human-computer interface (HCI). HCI is possible with this board by using an
appropriate extension board that plugs into this board
Figure 1-5 shows the bottom side of the Nucleo-64 board.
Not much to see in this figure other than a bunch of 0-ohm resistors that may
be removed if it was desired to disable certain features such as the reset and user
buttons and user LED, or make hardware configuration changes to the ST-LINK
programmer.
Figure 1-6 is much more interesting as it shows the pinouts for all the
connectors on the top of the Nucleo-64 board.
Figure 1-6 Nucleo-64 pinout diagram.
There are two sets of pinouts shown in Figure 1-6, one belonging to the
Arduino and the other to the ST Morpho. Connectors CN5, CN6, CN8, and CN9
belong to the Arduino set. Connectors CN7 and CN10 belong to the Morpho set.
It is important to note that there are no one-to-one relationships between the sets.
The Morpho connectors are two double rows of 38 pins for a total of 76 pins.
The Arduino connectors are single-row connectors totaling 32 pins in all spread
across four connectors.
Arduino Connectors
Tables 1-1 to 1-4, which are from the Nucleo-64 User Guide, detail the Arduino
connectors with cross-references between Arduino pin terms and STM32 pins. I
have provided further clarifications on some of the pin functions following each
table, where I felt it was appropriate.
Table 1-1 details the CN6 connector pinout. Refer to Figure 1-4 to see the
placement of CN6 on the board.
Grounding pin 3 will reset the MCU. The STM32 pin designation NRST is
short for non-maskable reset, which means a low on this pin is always
recognized as a forcible MCU reset. Pins 2 and 4 provide 3.3 V, but I would
recommend that only a very limited amount of current be drawn from these pins
or you risk disrupting the board operations. Pin 5 provides 5 V, but again my
same caution exists for excessive current draw. VIN is where you can attach an
external power supply. The input voltage range is approximately 7 to 12 V based
on the voltage regulator ratings connected to this pin. Jumper JP5 must be
switched to accommodate the external power supply. Finally, the IOREF pin
allows Arduino shields to adjust the I/O voltage to the 3V3 for the STM32 if
those shields follow the STM specification.
Table 1-2 details the CN8 connector pinout. Refer to Figure 1-4 to see the
placement of CN8 on the board.
These eight pins are normally regular GPIO, but they may be reconfigured so
that pins 4, 6, and 7 are timer pins. In addition, pins 1 and 2 can act as the #2
USART.
Morpho Connectors
Table 1-5 is from the Nucleo-64 User Guide and details the STM Morpho
connections with references to the STM32 pins. I have provided further
clarifications on some of the pin functions following the table, where I felt it was
appropriate. I did not repeat the comments I provided for Arduino connection as
they are equally valid for the Morpho connections.
Table 1-5 CN7 and CN10 Pinouts
Table 1-5 details the CN7 and CN10 connector pinouts. Refer to Figure 1-4 to
see the placement of CN7 and CN10 on the board.
U5V is 5-V power supplied from the ST-LINK/V2-1 USB connector. The
BOOT0 input pin allows for changing how the MCU initially boots during a
power-on event. I will discuss the boot options in a later chapter. VBAT input is
often used as a battery backup for powering a real-time clock or a security key. It
is typically tied to VCC if not used for a battery backup.
The next series of MCU components that I will discuss enable the MCU to
perform its operations more efficiently and quickly than possible by relying on
software constructs.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com