Logout succeed
Logout succeed. See you again!

ARM-Based Microcontroller Multitasking Projects: Using the FreeRTOS Multitasking Kernel PDF
Preview ARM-Based Microcontroller Multitasking Projects: Using the FreeRTOS Multitasking Kernel
ARM-Based Microcontroller Multitasking Projects Using the FreeRTOS Multitasking Kernel Dogan IbrahIm Newnes is an imprint of Elsevier The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, United Kingdom 50 Hampshire Street, 5th Floor, Cambridge, MA 02139, United States Copyright © 2020 Elsevier Ltd. All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the publisher. Details on how to seek permission, further information about the Publisher’s permissions policies and our arrangements with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found at our website: www. elsevier.com/permissions. This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may be noted herein). Notices Knowledge and best practice in this field are constantly changing. As new research and experience broaden our understanding, changes in research methods, professional practices, or medical treatment may become necessary. Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any informa- tion, methods, compounds, or experiments described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility. To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability for any injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions, or ideas contained in the material herein. Library of Congress Cataloging-in-Publication Data A catalog record for this book is available from the Library of Congress British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library ISBN: 978-0-12-821227-1 For information on all Newnes publications visit our website at https://www.elsevier.com/books-and-journals Publisher: Mara Conner Acquisitions Editor: Tim Pitts Editorial Project Manager: Rafael G. Trombaco Production Project Manager: Nirmala Arumugam Designer: Matthew Limbert Typeset by Thomson Digital About the author Prof. Dr. Dogan Ibrahim has a BSc degree Prof. Ibrahim is the author of over 80 tech- in electronic engineering, an MSc degree in nical books and over 200 technical articles automatic control engineering, and a PhD on microcontrollers, microprocessors, and degree in digital signal processing. Dogan related fields. He is a Chartered electrical has worked in many industrial organiza- engineer and a Fellow of the Institution of tions before he returned to academic life. Engineering Technology. xi Preface A microcontroller is a single-chip micro- maintain a program developed using a high- processor system which contains data and level programming language. program memory, serial and parallel I/O, There are many different types of micro- timers, external and internal interrupts, controllers available from many manufactur- all integrated into a single chip that can ers. Most manufacturers offer development be purchased for as little as $2.00. About kits (or development boards), which are 40% of microcontroller applications are in invaluable tools during the early stages office automation, such as PCs, laser print- of project development. In this book the ers, fax machines, intelligent telephones, Clicker 2 for STM32 development board is and so forth. About one-third of microcon- used. This board is based on the STM32F407 trollers are found in consumer electronic type ARM Cortex-M4 processor that can goods. Products like CD and DVD play- operate at up to 168MHz. The highly popu- ers, hi-fi equipment, video games, wash- lar mikroC Pro for ARM compiler and IDE is ing machines, cookers, and so on fall into used for software development. this category. The communications market, The topic of this book is FreeRTOS kernel automotive market, and the military share and multitasking. Multitasking has become the rest of the application areas. one of the important topics in microcon- Microcontrollers have traditionally been troller-based systems, namely in automation programmed using the assembly language applications. As the complexity of the proj- of the target processor. Although the assem- ects grow, more functionality is demanded bly language is fast, it has the disadvantages from the projects and such projects require that it is difficult to develop and maintain the use of several inter-related tasks running large projects using the assembly language. on the same processor and sharing the CPU in Additionally, microcontrollers from differ- order to implement the required operations. ent manufacturers have different assembly As a result of this, the importance of mul- language instruction sets which makes it titasking operation in microcontroller-based very time consuming for the programmers applications has been growing steadily over to learn new assembly languages every time the last several years and many complex a different microcontroller is to be used. The automation projects nowadays make use of assembly code developed for one type of some form of a multitasking kernel. In this microcontroller cannot be ported to another book the FreeRTOS multitasking kernel is type of microcontroller. Nowadays micro- used in the projects. FreeRTOS is a market controllers are programmed using high-level leading real time multitasking kernel with languages, such as C, C++, Pascal, or Basic. millions of deployments in all market sec- Perhaps the biggest advantage of using a tors. FreeRTOS is free of charge and is fully high-level language is that the developed documented and supported. It is available code can easily be ported to other types of to run on many hardware and software plat- microcontrollers. Additionally, it is easier to forms, including the ARM processors and xiii xiv Preface the mikroC Pro for ARM compiler and IDE. This book is written for students, for prac- FreeRTOS is so popular that in 2018 it was ticing engineers, and for hobbyists interested downloaded every 175 seconds (www.fre- in developing multitasking microcontroller- ertos.org) and it came top in class in every based real time projects using the ARM fam- EETimes Embedded Market Survey since ily of microcontrollers. Attempt has been 2011. made to include as many projects as pos- This book is project based and its main sible, limited only by the size of the book. aim has been to teach the basic features Although the Clicker 2 for STM32 micro- and API functions of the FreeRTOS kernel. controller development board and the Many fully tested projects are given in the STM32F407 type ARM processor are used book using FreeRTOS in multitasking appli- in this book, readers should find it easy to cations. Each project is described fully and use other types of development boards and in detail, and the complete program listings other types of ARM processors. are given for each project. Readers should be FreeRTOS is documented and sup- able to use the projects as they are, or modify ported fully by its developers. Interested them to suit to their own needs. The follow- readers can obtain detailed information on ing sub-headings are used while describing FreeRTOS features and API functions from each project: the following Internet-based sources: • Description of the project Mastering the FreeRTOS Real Time Kernel: • Aim of the project A Hands-On Tutorial Guide, by Richard • Background (if applicable) Barry, web site: • Block diagram https://www.freertos.org/wp-content/ • Circuit diagram uploads/2018/07/161204_Mastering_ • Program listing the_FreeRTOS_Real_Time_Kernel-A_ • Suggestions for future work (if Hands-On_Tutorial_Guide.pdf applicable) or The operation of some of the complex projects have been described using the The FreeRTOS Reference Manual: API Project Description Language (PDL), which Functions and Configuration Options, web makes it easy to understand the programs site: before they are developed. https://www.freertos.org/ Knowledge of the C-programming lan- wp-content/uploads/2018/07/ guage will be useful to the readers. Also, FreeRTOS_Reference_Manual_V10.0.0.pdf familiarity with at least one microcontroller or development board (preferably with an ARM processor) will be an advantage. The FreeRTOS web site: knowledge of assembly language program- www.freertos.org ming is not required because all the proj- ects in the book are based on using the C Dogan Ibrahim language. London, 2020 Acknowledgments Some figures and text in this book has been Figures 3.3, 3.6–3.8, 3.10, 4.1–4.9, 9.2, 9.4, taken from a variety of sources. 9.9, 9.12, 9.40, 9.42, 9.44, 9.45, 11.1, 12.4, 12.8, 13.1, 13.9, 14.18, 14.28, 18.8, 18.13, 18.16, 19.1, The FreeRTOS API function details are tak- 22.1–22.6, 22.14, 22.15, 22.17, 22.18, 22.20, en from the following documents and web 22.24–22.27 sites. The author would like to thank to Mr. Richard Barry and Mr. Dirk Didascalou for The following figures and tables are copy- giving permission to use material from the right of STMicroelectronics and are taken following sources in this book: from the online document “RM 0090 Refer- ence Manual, Rev. 18, 2019” of STMicroelec- 1. Mastering the FreeRTOS Real Time tronics. The author is grateful to Mr. Michael Kernel: A Hands-On Tutorial Guide, by Markowitz of STMicroelectronics for giving Richard Barry, web site: permission to use these figures in this book: https://www.freertos.org/wp-content/ uploads/2018/07/161204_Mastering_ Figures 2.1–2.3, 2.5, 2.14, 3.2, 3.4, 18.1–18.7, the_FreeRTOS_Real_Time_Kernel-A_ Tables 18.1–18.5 Hands-On_Tutorial_Guide.pdf The following figures are the copyright of 2. The FreeRTOS Reference Manual, web the corresponding sources and are taken site:https://www.freertos.org/wp- with their permissions. The author would content/uploads/2018/07/FreeRTOS_ like to thank the copyright holders for giv- Reference_Manual_V10.0.0.pdf ing permission to use these figures in this book: 3. https://www.freertos.org/documentation Figure 3.1 - Pololu Robotics & Electronics, The following figures are copyright of mik- pololu.com roElektronika and are taken from the mik- Figure 3.5 - From SparkFun Electronics (pho- roElektronika web site (www.mikroe.com), tos taken by Juan Peña) The author would like to thank to Mr Nebojsa Matic (CEO) for giving permission to use Figure 3.9 – Texas Instruments (www.ti.com) these figures in this book: xv C H A P T E R 1 Microcomputer systems 1.1 Overview The term microcomputer is used to describe a digital processing system that includes a minimum of a microprocessor, program memory, data memory, and input-output (I/O). Some microcomputer systems include additional components such as timers, counters, ana- log-to-digital converters, and so on. Thus, a microcomputer system can be anything from a large computer having hard disks, optical disks, SSD drives, printers, and plotters to a single chip embedded controller. In this book we are going to consider only the type of microcomputers that consists of a single silicon chip. Such microcomputer systems are also called microcontrollers and they are used in many household goods such as microwave ovens, TV remote control units, cook- ers, hi-fi equipment, CD players, personal computers, fridges, games consoles, etc. There are a large variety of microcontrollers available in the market place, ranging from 8-bits to 32-bits or even 64-bits. In this book we shall be looking at the programming and system design using a member of the 32-bit STM32 family of microcontrollers, manufactured by the STMicroelectronics. As we shall be seeing in the next chapter, STM32 family is based on the highly popular ARM processor architecture. In this chapter we shall be looking at the features of the microcontroller systems and describe their basic building blocks. 1.2 Microcontroller systems A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that the device can be used in control applications. Another term used for microcontrollers is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control. A microprocessor differs from a microcontroller (or a microcomputer) in many ways. The main difference is that a microprocessor requires several other components for its opera- tion, such as program memory and data memory, input-output devices, clock circuit, inter- rupt circuits, etc. A microcontroller, on the other hand, has all the support chips incorporated inside the same chip. All microprocessors and microcontrollers operate on a set of instruc- tions (or the user program) stored in their program memories. A microcontroller fetches the ARM-Based Microcontroller Multitasking Projects. http://dx.doi.org/10.1016/B978-0-12-821227-1.00001-3 Copyright © 2020 Elsevier Ltd. All rights reserved. 1 2 1. Microcomputer systems instructions from its program memory one by one, decodes these instructions, and then car- ries out the required operations. Microprocessors and microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast and results in less memory usage, it has several major disadvantages. An assembly program consists of mnemonics and it is difficult to learn and maintain a program written using the assembly language. Also, microcontrollers manufactured by different firms have different assembly languages and the user is required to learn a new language every time a new microcontroller is to be used for a project. Microcontrollers can also be programmed using a high-level lan- guage, such as BASIC, PASCAL, C, etc. High-level languages offer many advantages: it is much easier to learn a high-level language than an assembler language. Also, very large and complex programs can easily be developed using a high-level language. It is much easier to maintain a program written using a high-level language. Programs written using a high-level language can easily be ported to run on different microcontrollers with simple or no modi- fications to the source codes. In this book we shall be learning the programming of STM32 family of microcontrollers using the popular mikroC Pro for ARM language and the IDE (inte- grated development environment), developed by mikroElektronika (www.mikroe.com). In most microcontroller-based applications, a microcontroller is used for a single task, such as controlling a motor, controlling the temperature, humidity or the pressure, turning an LED ON or OFF, responding to external switches, and so on. In this book we are interested in using a microcontroller in real time as well as in multitasking applications. A real-time application differs from a normal application as it requires a very quick response from a microcontroller. For example, in a position control application it may be required to stop a motor as soon as an input condition changes. Or, in digital signal processing applications the processor is required to respond to input signal changes very quickly. In multitasking applications, there could be several interrelated tasks running on a single microcontroller such that the CPU time and the microcontroller resources are shared between these tasks. A scheduler runs in the background and it makes sure that the tasks run correctly and share the CPU as required by the applica- tion. In this book, the highly popular FreeRTOS real-time multitasking kernel is used in the projects. The book describes the operation of the FreeRTOS and gives example projects to show how the various functions supported by the FreeRTOS kernel can be used in practical applications. Many tested and working projects are given in later chapters of the book. In general, a single chip is all that is required to have a running microcontroller system. In prac- tical applications, however, several additional external components may be required to allow a microcomputer to be interfaced with its environment. The resulting system is then usually called a microcontroller development kit (or microcontroller development board). Microcontroller development kits simplify the tasks of developing programs greatly, since the required project is developed using the hardware kit that has already been tested by the manufacturers. Basically, a microcontroller executes a user program which is loaded in its program mem- ory. Under the control of this program, data is received from external devices (inputs), manip- ulated, and then sent to external devices (outputs). For example, in a microcontroller-based oven temperature control system, the temperature is read by the microcomputer using an external temperature sensor. The microcomputer then operates a heater or a fan to control and keep the temperature at the required set value. Fig. 1.1 shows the block diagram of our simple oven temperature control system. 1.2 Microcontroller systems 3 FIGURE 1.1 Microcontroller-based oven temperature control system. The system shown in Fig. 1.1 is a very simplified temperature control system where the microcontroller software runs in an endless loop and its only task is to control the tempera- ture of the oven. In such a simple system the set point temperature is usually preset in the software and cannot be changed while the program is running. This is an example of a sys- tem having only one task. In a more sophisticated system we may have a keypad to set the temperature externally while the program is running, and additionally an LCD to display the current temperature and the set point temperature. Fig. 1.2 shows the block diagram of such FIGURE 1.2 Temperature control system with a keypad and LCD.