Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

ARM® Cortex® M4 Cookbook
ARM® Cortex® M4 Cookbook
ARM® Cortex® M4 Cookbook
Ebook604 pages3 hours

ARM® Cortex® M4 Cookbook

Rating: 4.5 out of 5 stars

4.5/5

()

Read preview

About this ebook

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

About This Book

- This book focuses on programming embedded systems using a practical approach
- Examples show how to use bitmapped graphics and manipulate digital audio to produce amazing games and other multimedia applications
- The recipes in this book are written using ARM’s MDK Microcontroller Development Kit which is the most comprehensive and accessible development solution

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. The 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.

What You Will Learn

- Use ARM’s uVision MDK to configure the microcontroller run time environment (RTE), create projects and compile download and run simple programs on an evaluation board.
- Use and extend device family packs to configure I/O peripherals.
- Develop multimedia applications using the touchscreen and audio codec beep generator.
- Configure the codec to stream digital audio and design digital filters to create amazing audio effects.
- Write multi-threaded programs using ARM’s real time operating system (RTOS).
- Write critical sections of code in assembly language and integrate these with functions written in C.
- Fix problems using ARM’s debugging tool to set breakpoints and examine variables.
- Port uVision projects to other open source development environments.

In Detail

Embedded microcontrollers are at the core of many everyday electronic devices. Electronic automotive systems rely on these devices for engine management, anti-lock brakes, in car entertainment, automatic transmission, active suspension, satellite navigation, etc. The so-called internet of things drives the market for such technology, so much so that embedded cores now represent 90% of all processor’s sold. The ARM Cortex-M4 is one of the most powerful microcontrollers on the market and includes a floating point unit (FPU) which enables it to address applications.
The ARM Cortex-M4 Microcontroller Cookbook provides a practical introduction to programming an embedded microcontroller architecture. This book attempts to address this through a series of recipes that develop embedded applications targeting the ARM-Cortex M4 device family. The recipes in this book have all been tested using the Keil MCBSTM32F400 board. This board includes a small graphic LCD touchscreen (320x240 pixels) that can be used to create a variety of 2D gaming applications. These motivate a younger audience and are used throughout the book to illustrate particular hardware peripherals and software concepts.
C language is used predominantly throughout but one chapter is devoted to recipes involving assembly language. Programs are mostly written using ARM’s free microcontroller development kit (MDK) but for those looking for open source development environments the book also shows how to configure the ARM-GNU toolchain. Some of the recipes described in the book are the basis for laboratories and assignments undertaken by undergraduates.

Style and approach

The ARM Cortex-M4 Cookbook is a practical guide full of hands-on recipes. It follows a step-by-step approach that allows you to find, utilize and learn ARM concepts quickly.
LanguageEnglish
Release dateMar 16, 2016
ISBN9781782176510
ARM® Cortex® M4 Cookbook

Related to ARM® Cortex® M4 Cookbook

Related ebooks

Hardware For You

View More

Related articles

Reviews for ARM® Cortex® M4 Cookbook

Rating: 4.4 out of 5 stars
4.5/5

5 ratings1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    more day to day application to understand complex concepts in this book would be useful.

Book preview

ARM® Cortex® M4 Cookbook - Fisher Dr. Mark

Table of Contents

ARM® Cortex® M4 Cookbook

Credits

About the Author

About the Reviewer

www.PacktPub.com

eBooks, discount offers, and more

Why Subscribe?

Preface

What this book covers

What you need for this book

Who this book is for

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Conventions

Reader feedback

Customer support

Downloading the example code

Downloading the color images of this book

Errata

Piracy

Questions

1. A Practical Introduction to ARM® CORTEX®

Introduction

Installing uVision5

How to do it…

How it works…

Linking an evaluation board

How to do it…

How it works…

There's more…

Running an example program

How to do it…

How it works…

Writing a simple program

How to do it…

How it works…

There's more…

Understanding the simple use of GPIO

How to do it…

How it works…

There's more…

Estimating microcontroller performance

How to do it…

There's more…

See also

2. C Language Programming

Introduction

Configuring the hardware abstraction layer

How to do it…

How it works…

Writing a C program to blink each LED in turn

Getting ready…

How to do it…

How it works…

There's more…

Writing a function

How to do it

How it works…

There's more…

Writing to the console window

Getting ready

How to do it…

How it works…

Writing to the GLCD

Getting ready

How to do it…

How it works…

Creating a game application – Stage 1

How to do it…

How it works…

Creating a game application – Stage 2

There's more…

How it works…

Debugging your code using print statements

How to do it…

How it works…

Using the debugger

How to do it…

See also

3. Programming I/O

Introduction

Performing arithmetic operations

How to do it…

How it works…

Illustrating machine storage classes

How to do it…

How it works…

Configuring GPIO ports

How to do it…

How it works…

There's more…

Configuring UART ports

How to do it…

How it works…

Handling interrupts

Getting ready

How to do it…

How it works…

There's more…

Using timers to create a digital clock

How to do it…

How it works…

4. Assembly Language Programming

Introduction

Writing Cortex-M4 assembly language

Getting ready

How to do it…

How it works…

There's more…

See also

Passing parameters between C and the assembly language

Getting ready

How to do it…

How it works…

There's more…

See also

Handling interrupts in assembly language

Getting ready

How to do it…

How it works…

There's more…

Implementing a jump table

Getting ready

How to do it…

How it works…

Debugging assembly language

How to do it…

How it works…

There's more…

5. Data Conversion

Introduction

Setting up the ADC

How to do it…

How it works…

There's more…

Configuring general-purpose timers

How to do it…

How it works…

Using timers to trigger conversions

How to do it…

How it works…

There's more…

Setting up the DAC

How to do it…

How it works…

There's more…

Generating a sine wave

Getting ready

How to do it…

How it works…

6. Multimedia Support

Introduction

Setting the RTE for the I2C Peripheral Bus

How to do it…

How it works…

How to use the LCD touchscreen

How to do it…

How it works…

Writing a driver for the audio codec

How to do it…

How it works…

How to use the audio codec

How to do it…

How it works…

How to use the camera

How to do it…

How it works…

There's more…

Designing bitmapped graphics

How to do it…

How it works…

There's more…

Ideas for games using sound and graphics

7. Real-Time Signal Processing

Introduction

Configuring the audio codec

How to do it…

How it works…

There's more…

How to play prerecorded audio

Getting ready

How to do it…

How it works…

Designing a low-pass digital filter

Getting ready

How to do it…

How it works…

How to make an audio tone control

Getting ready

How to do it…

How it works…

There's more...

8. Real-Time Embedded Systems

Introduction

Multithreaded programs using event flags

How to do it…

How it works…

Multithreaded programs using mailboxes

How to do it…

How it works…

Why ensuring mutual exclusion is important when accessing shared resources

How to do it…

How it works…

There's more…

Why we must use a mutex to access the GLCD

How to do it…

How it works…

How to write a multithreaded Pong game

How to do it…

How it works…

Debugging programs that use CMSIS-RTOS

How to do it…

How it works…

9. Embedded Toolchain

Introduction

Installing GNU ARM Eclipse

How to do it…

How it works…

Programming the MCBSTM32F400 evaluation board

How to do it…

How it works…

How to use the STM32CubeMX Framework (API)

How to do it…

How it works…

There's more…

How to port uVision projects to GNU ARM Eclipse

How to do it…

Index

ARM® Cortex® M4 Cookbook


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

Credits

Author

Dr. Mark Fisher

Reviewer

Alex Barrett

Commissioning Editor

Kunal Parikh

Acquisition Editor

Sonali Vernekar

Content Development Editor

Mayur Pawanikar

Technical Editor

Kunal Chaudhari

Copy Editors

Priyanka Ravi

Sonia Mathur

Project Coordinator

Nidhi Joshi

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite

About the Author

Dr. Mark Fisher is a chartered engineer, MIET. He started his career as an electronics apprentice with the UK Ministry of Defence. This was before he studied Electrical and Electronic Engineering at Aston University, Birmingham. After his graduation, he joined Ferranti Computer Systems, Manchester. However, he returned to academia to study Microprocessor Engineering and Digital Electronics at Manchester University (UMIST), and he then remained as a research assistant within the Department of Computation to gain a PhD in Applied Machine Learning. Currently, he is a senior lecturer at the School of Computing Sciences, University of East Anglia, and the course director of the Computer Systems Engineering Degree programme. Many of the recipes in this book were originally developed in the context of a taught module that Mark leads, which is popular among undergraduate and master's students in the school.

Mark currently researches in the fields of medical imaging and computer vision, and he is a co-author of over a hundred journal and conference papers in this area.

About the Reviewer

Alex Barrett has been heavily involved in all aspects of design, development, and manufacture of electronic systems and devices as a director of design consultants Rocolec Ltd. for over twenty years. Prior to this, he worked in the oil industry in the designing and manufacturing of remotely-operated submersible vehicles (ROVs), and manufacturing and testing television reception equipment. He enjoys traveling, and he has an interest in languages, currently focusing on learning Russian. He is also a volunteer on the Anglian Coastal committee of The Institution of Engineering and Technology.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

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.

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:

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

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Run the program by pressing RESET on the evaluation board.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ARMCortexM4Cookbook_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. A Practical Introduction to ARM® CORTEX®

In this chapter, we will cover the following topics:

Installing uVision5

Linking an evaluation board

Running an example program

Writing a simple program

Understanding the simple use of GPIO

Estimating microcontroller performance

Introduction

This chapter will show you how to compile, download, and run simple programs on an evaluation board. A software tool called a Microcontroller Development Kit (MDK), including an Integrated Development Environment (IDE), is the simplest way of achieving this. Keil (a company owned by ARM) markets an extensive range of software tools to support embedded system development. Amongst these, the MDK-ARM development kit represents an integrated software development environment, supporting devices based on the Cortex-M (and associated) cores (see http://www.keil.com/arm/mdk.asp).

Installing uVision5

A free evaluation version of the IDE known as the MDK-ARM Lite edition, running (albeit with limited functionality) under the Windows operating system, is available for download. The main limitation of the environment is that programs that generate more than 32 KB of code cannot be compiled and linked (see http://www.keil.com/demo/limits.asp). However, since most programs written by novices tend be quite small, this limitation is not a serious problem. For those who expect their executable image to exceed 32 KB, other open source compiler and IDE options are considered in Chapter 9, Embedded Toolchain.

uVision5, the latest version of the IDE is distributed as two components. An MDK core contains all the development tools, and software packs, together with Cortex Microcontroller Software Interface standard (CMSIS) and middleware libraries, which add support for target devices.

Installation involves downloading and running an executable (.exe) file. Users can download and install the latest version after first registering their contact details at http://www2.keil.com/mdk5/install/.

How to do it…

Download the latest version of the software by following the instructions provided by Keil. Device-specific libraries are not included in installations from version 5 onwards, so at the end of the installation, we must configure the IDE using the Pack Installer to choose the resources (that is target devices, boards, and examples) that we need.

Select the Boards tab, choose the MCBSTM32F400 Keil evaluation board featuring the STM32F407IGHx STMicroelectronics part, as this is the target for all the practical examples described in this cookbook.

With the Packs tab, in addition to the default installation options: CMSIS and Keil ARM Processional Middleware for ARM Cortex-M-based devices, board support for MCBSTM32F400 is also needed. Select the latest version Keil::32F4xx_DFP (2.6.0).

Select the Examples tab, and copy the board-specific example programs to a convenient local folder. Note: the example programs illustrate many useful features of the evaluation board, and are an invaluable resource.

Once we have downloaded and installed MDK-ARM uVision5, the IDE can be invoked from the Windows Taskbar. If we wish to update the installation, the pack installer can be invoked by selecting the pack installer icon on uVision5 toolbar.

We demonstrate the basic features of uVision in this chapter, but later on, we'll probably need to access the uVision user guide via the Help menu (also available at http://www2.keil.com/mdk5) to learn about the more advanced features of the IDE. A useful guide to getting started with uVision5 can be found at https://armkeil.blob.core.windows.net/product/mdk5-getting-started.pdf. An overview of uVision5 is available at http://www2.keil.com/mdk5, and this includes some video clips that describe the design philosophy, and explain how to use the Pack Installer and create a new project.

How it works…

Computer programming involves specifying a sequence of binary codes that are interpreted by the machine as instructions that together enable it to undertake some task. The instruction sets of early computers were small and easily memorized by programmers, so programs were written directly in machine code, and each instruction code word was set up on switches and written to memory. Finally, once all the instructions had been entered, the program was executed. With the development of more powerful machines and larger instruction sets, this approach became unworkable. This motivated the need to program in higher level (human understandable) languages that are translated into machine code by a special program called a compiler. Modern day programmers rarely need to interpret individual binary codes; instead, they use a text editor to enter a sequence of high-level language statements, a compiler to convert them into machine code, a linker to allow programs to reuse previously written (library) code, and a loader to write the binary codes to memory. The steps comprising edit, compile, link, load can be undertaken by running each program (editor, compiler, linker, loader) separately. However, nowadays they are usually packaged together within a wrapper called an IDE. Some IDEs are language-specific and some are

Enjoying the preview?
Page 1 of 1