STM32 Arm Programming for Embedded Systems
Using C Language with STM32 Nucleo
Muhammad Ali Mazidi
Shujen Chen
Eshragh Ghaemi
Copyright 2014-2018 MazidiAll rights reserved"Regard man as a mine rich in gems of inestimable value. Education can, alone,cause it to reveal its treasures, and enable mankind to benefit therefrom." Baha'u'llah
Dedication
To the faculty, staff, and students of BIHE university for their dedication and steadfastness.Preface
Since the early 2000s, hundreds of companies have licensed the Arm CPU and the number oflicensees is growing very rapidly. While the licensee must follow the Arm CPU architecture and instructionset, they are free to implement peripherals such as I/O ports, ADCs, Timers, DACs, SPIs, I2Cs and UARTsas they please. In other words, while one can write an Assembly language program for the Arm chip, andit will run on any Arm chip, a program written for the I/O ports of an Arm chip for company A will not runon an Arm chip from company B. This is due to the fact that special function registers and their physical address locations to access the I/O ports are not standardized and every licensee implements itdifferently. We have dedicated the first volume in this series to the Arm Assembly language programmingand architecture since the Assembly language is standard and runs on any Arm chip regardless of whomakes them. Our Arm Assembly book is called "Arm Assembly Language Programming and Architecture" and is available from Amazon. See the following link:
http://www.microdigitaled.com/ARM/ARM_ASM_books.htm
For the peripheral programming of the Arm, we had no choice but to dedicate a separate volume to each vendor. This volume covers the peripheral programming of the STM32 Arm chip. Throughout thebook, we use C language to access the special function registers and program the STM32F4xx peripherals.We have provided an Assembly language programs for I/O ports in Chapter 2 for those who want toexperiment with Assembly language in accessing the I/O ports and their special function registers. TheAssembly language programs also help to see the contrast between the C and Assembly versions of thesame program in Arm.
Two approaches in programming the Arm chipsWhen you program an Arm chip, you have two choices:
1.Use the functions written by the vendor to access the peripherals. The vast majority of thevendors/companies making the Arm chip provide a proprietary device library of functionsallowing access to their peripherals. These device library functions are copyrighted andcannot be used with another vendor's Arm chip. For students and developers, the problemwith this approach is you have no control over the functions and it is very hard to customize them for your project.
2.The second approach is to access the peripheral's special function registers directly using Clanguage and create your own custom library since you have total control over each function.Much of these functions can be modified and used with another vendor if you decide tochange the Arm chip vendor. In this book, we have taken the second approach since ourprimary goal is to teach how to program the peripherals of an Arm chip. We know thisapproach is difficult and tedious, but the rewards are great.
Compilers and IDE ToolsFor programming the Arm chip, you can use any of the widely available compilers from Keil(www.keil.com), IAR (www.IAR.COM) or any other one. For this book, we have used the Keil Arm compilerIDE to write and test the programs. See our web site for the tutorials.
STM (STMicroelectronics) Arm TrainerThe STMhas manyinexpensivetrainers for the ArmSTM32F4xxseries. Among themisSTM32F446 Nucleo board. Although we used the STM32F446 board to test the programs, the programscan run on other STM32F4xx chips with small or no modifications.
Chapters Overview
In Chapter 1, we examine the C language data types for 32-bit systems. We also explore the new
ISO C99 data types since they are widely used in IDE compilers for the embedded systems.
Chapter 2 examines the simple I/O port programming and shows sample programs on how toaccess the special function registers associated with the general purpose I/O (GPIO) ports.
Chapter 3 shows the interfacing of the Arm chip with the real-world devices: LCD and keypad. Itprovides sample programs for the devices.
In Chapter 4, the interfacing and programming of serial UART ports are examined.
Chapter 5 is dedicated to the timers in Arm. It also shows how to use timers as an event counter.
The Interrupt programming of the Arm is discussed in Chapter 6.
Chapter 7 examines the ADC and DAC concepts and shows how to program them with the Armchip. It also examines the sensor interfacing and signal conditioning.
Chapter 8 covers the SPI protocol and interfacing with sample programs in Arm.
The I2C bus protocol and interfacing of an I2C based RTC is discussed in Chapter 9.
Chapter 10 explores the relay and stepper motor interfacing with Arm.
The DC motor and PWM are examined in Chapter 11.
The Graphics LCD concepts and programming are discussed in Chapter 12.
Chapter 13 examines the concept of DMA and shows how to program it.
Appendix A provides an introduction to IC chip technology and IC interfacing along with thesystem design issues and failure analysis using MTBF. See our website for this appendix.
Appendix B provides a single source for STM32F446 microcontroller alternate pin functions.
The CPU clock source is examined in Appendix C.
Online support for this book
All the programs in this book and other support materials such as PPs and tutorials are available
on our website:
http://www.MicroDigitalEd.com/Arm/STM_Arm_books.html
Many of the interfacing programs such as LCD can be tested using the STM32 Arm Nucleo orDiscovery boards connected to an LCD on a breadboard. However, many courses use a system approachto the embedded course by using an interface trainer. For this reason, we have modified the programs forthe EduPad interface trainer using STM32F446 Nucleo board. See the following for the sample programs:
http://www.MicroDigitalEd.com/Arm/STM_Arm_books.html
Where to buy STM32 Arm Evaluation kit? See our web site for STM32 evaluation kits and datasheet.
Contact us Please contact the authors if use this book for a university course.
mdebooks@yahoo.com
Arm TrademarksFrom 1 August 2017, Arm has a new look and feel. The business has a new corporate logo and itis now using the Arm word in sentence case instead of the ARM word in uppercase in text. We ask all ofArms customers, partners, licensees and any other third parties to use the Arm word in sentence case intext in all relevant materials. The only exception to this rule will be when using the ARM word in anycircumstances, where all of the surrounding words also appear in uppercase, e.g. headings.
https://www.arm.com/company/policies/trademarks
Table of Contents
Answer to Review Questions21
Answer to Review Questions55
Answers to Review Questions79
Answer to Review Questions119
Answer to Review Questions157
Answers to Review Questions232
Answers to Review Questions256
Answers to Review Questions290
Answers to Review Questions305
Answers to Review Questions324
Answers to Review Questions336
Answers to Review Questions357
Chapter 1: C for Embedded Systems
In reading this book we assume you already have some understanding of how to program in Clanguage. In this chapter, we will examine some important concepts widely used in embedded systemdesign that you may not be familiar with due to the fact that many generic C programming books do notcover them. In section 1.1, we examine the C data types for 32-bit systems. The bit-wise operators arecovered in section 1.2.