ARM® Cortex® M4 Cookbook Over 50 hands-on recipes that will help you develop amazing real-time applications using GPIO, RS232, ADC, DAC, timers, audio codecs, graphics LCD, and a touch screen Dr. Mark Fisher BIRMINGHAM - MUMBAI ARM® Cortex® M4 Cookbook Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2016 Production reference: 1020316 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78217-650-3 www.packtpub.com Table of Contents Preface v Chapter 1: A Practical Introduction to ARM® CORTEX® 1 Introduction 1 Installing uVision5 2 Linking an evaluation board 4 Running an example program 8 Writing a simple program 9 Understanding the simple use of GPIO 16 Estimating microcontroller performance 21 Chapter 2: C Language Programming 25 Introduction 25 Configuring the hardware abstraction layer 26 Writing a C program to blink each LED in turn 31 Writing a function 37 Writing to the console window 42 Writing to the GLCD 52 Creating a game application – Stage 1 55 Creating a game application – Stage 2 61 Debugging your code using print statements 68 Using the debugger 71 Chapter 3: Programming I/O 75 Introduction 75 Performing arithmetic operations 76 Illustrating machine storage classes 83 Configuring GPIO ports 85 Configuring UART ports 95 Handling interrupts 101 Using timers to create a digital clock 105 i Table of Contents Chapter 4: Assembly Language Programming 109 Introduction 109 Writing Cortex-M4 assembly language 111 Passing parameters between C and the assembly language 116 Handling interrupts in assembly language 121 Implementing a jump table 127 Debugging assembly language 131 Chapter 5: Data Conversion 135 Introduction 135 Setting up the ADC 137 Configuring general-purpose timers 144 Using timers to trigger conversions 150 Setting up the DAC 153 Generating a sine wave 157 Chapter 6: Multimedia Support 163 Introduction 163 Setting the RTE for the I2C Peripheral Bus 164 How to use the LCD touchscreen 167 Writing a driver for the audio codec 171 How to use the audio codec 179 How to use the camera 184 Designing bitmapped graphics 189 Ideas for games using sound and graphics 194 Chapter 7: Real-Time Signal Processing 195 Introduction 195 Configuring the audio codec 196 How to play prerecorded audio 205 Designing a low-pass digital filter 208 How to make an audio tone control 215 Chapter 8: Real-Time Embedded Systems 219 Introduction 219 Multithreaded programs using event flags 222 Multithreaded programs using mailboxes 229 Why ensuring mutual exclusion is important when accessing shared resources 232 Why we must use a mutex to access the GLCD 237 How to write a multithreaded Pong game 241 Debugging programs that use CMSIS-RTOS 244 ii Table of Contents Chapter 9: Embedded Toolchain 249 Introduction 249 Installing GNU ARM Eclipse 250 Programming the MCBSTM32F400 evaluation board 255 How to use the STM32CubeMX Framework (API) 260 How to port uVision projects to GNU ARM Eclipse 269 Index 273 iii Preface This book begins with an introduction to the ARM Cortex family and covers its basic concepts. We cover the installation of the ARM uVision Integrated Development Environment and topics, such as target devices, evaluation boards, code configuration, and GPIO. You will learn about the core programming topics that deal with structures, functions, pointers, and debugging in this book. You will also learn about various advanced aspects, such as data conversion, multimedia support, real-time signal processing, and real-time embedded systems. You will also get accustomed with creating game applications, programming I/O, and configuring GPIO and UART ports. By the end of this book, you will be able to successfully create robust and scalable ARM Cortex-based applications. What this book covers Chapter 1, A Practical Introduction to ARM® Cortex®, shows you how to compile, download, and run simple programs on an evaluation board. Chapter 2, C Language Programming, introduces you to writing programs in C, a high-level language that was developed in the 1970s and is popular among embedded-system developers. Chapter 3, Programming I/O, investigates some of the functions that configure I/O devices, and you will gain an understanding of what is involved in writing I/O interfaces for other targets. Chapter 4, Assembly Language Programming, explains how to write functions in assembly language. Assembly language is a low-level programming language that is specific to particular computer architecture. Therefore, unlike programs written high-level languages, programs written in assembly language cannot be easily ported to other hardware architectures. Chapter 5, Data Conversion, introduces approaches to data conversion, namely analog to digital conversion and vice versa. This chapter also covers the principal features used by microcontrollers for data conversion. v Preface Chapter 6, Multimedia Support, discusses support for various multimedia peripherals, which are discrete components connected to the microcontroller by a bus. Support for an LCD touchscreen, audio codec, and camera peripherals is a very attractive feature of the STM32F4xxx microcontroller, and selecting an evaluation board that includes these peripherals, although more expensive, will be covered in this chapter. Chapter 7, Real-Time Signal Processing, introduces you to Digital Signal Processing (DSP) and reviews the ARM Cortex M4 instruction set support for DSP applications. This chapter will walk through a DMA application using the codec, followed by designing a low-pass filter. Chapter 8, Real-Time Embedded Systems, shows you how to write a multithreaded program using flags for communication and ensuring mutual exclusion when accessing shared resources. Chapter 9, Embedded Toolchain, teaches you how to install the GNU ARM Eclipse toolchain for the Windows Operating System and to build and run a simple Blinky program on the MCBSTM32F400 evaluation board. This chapter will also show you how to use the STM32CubeMX Framework (API) and how to port projects to GNU ARM Eclipse. What you need for this book You require the Keil Development Board MCBSTM32F400 (v1.1) and ARM ULINK-ME for this book. Who this book is for This book is aimed at those with an interest in designing and programming embedded systems. These could include electrical engineers or computer programmers who want to get started with microcontroller applications using the ARM Cortex M4 architecture in a short time frame. This book's recipes can also be used to support students learning embedded programming for the first time. Basic knowledge of programming using a high-level language is essential but those familiar with other high-level languages such as Python or Java should not have too much difficulty picking up the basics of embedded C programming. Sections In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also). To give clear instructions on how to complete a recipe, we use these sections as follows: vi Preface Getting ready This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe. How to do it… This section contains the steps required to follow the recipe. How it works… This section usually consists of a detailed explanation of what happened in the previous section. There's more… This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe. See also This section provides helpful links to other useful information for the recipe. Conventions In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Copy the function named SystemClock_Config() from the example." A block of code is set as follows: #ifdef __RTX extern uint32_t os_time; uint32_t HAL_GetTick(void) { return os_time; } #endif vii
Description: