
Hands-On RTOS with Microcontrollers
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
- Strengthen your embedded programming skills for real-world applications
- Explore advanced RTOS techniques to unlock innovative embedded solutions
- All formats include a free PDF and an invitation to the Embedded System Professionals community
Book DescriptionThis updated edition of Hands-On RTOS with Microcontrollers is packed with cutting-edge content to help you expand your skills and stay ahead of the curve with embedded systems development. Written by senior engineers with decades of experience in cybersecurity, operating systems (OSs), and embedded systems, it covers the role of real-time OSs in today's time-critical applications and FreeRTOS with its key capabilities and APIs. You'll find a detailed overview of system design (memory management), project design (MCU, IDE, and RTOS APIs), and hands-on system use as well as the system platform, dev-boards with an MCU and a debug probe, and development tools (IDE, build system, and source-code debugging). This second edition teaches you how to implement over 20 real-world embedded applications with the latest FreeRTOS features and how to optimize your code with dynamic analysis. The chapters include example programs on GitHub with detailed instructions. You'll create and install your own FreeRTOS system on the dev-board and set up an IDE project with debugging tools. With dozens of reference manuals listed, you'll always have ample resources for system development. By the end of this book, you'll have the hands-on skills to design, build, and optimize embedded applications using FreeRTOS, dev-boards, and modern debugging tools.What you will learn - Understand RTOS use cases, and decide when (and when not) to use real-time OS
- Utilize the FreeRTOS scheduler to create, start, and monitor task states
- Improve task signaling and communication using queues, semaphores, and mutexes
- Streamline task data transfer with queues and notifications
- Upgrade peripheral communication via UART, USB, and DMA by using drivers and ISRs
- Enhance interface architecture with a command queue for optimized system control
- Maximize FreeRTOS memory management with trade-off insights
Who this book is forThis book is for systems programmers, embedded systems engineers, and software developers who want to learn about real-time operating systems (RTOS) and how to use FreeRTOS in their embedded system design. A basic understanding of the C programming language, embedded systems, and microcontrollers is assumed. The book also includes hardware tutorials for systems programmers.
All prices
More details
Content
- Cover
- Title Page
- Contributors
- Table of Contents
- Preface
- Your Book Comes with Exclusive Perks - Here's How to Unlock Them
- Part 1: Introduction to RTOSes, the Dev-Board, and Tools
- Chapter 1: Introducing Real-Time Systems
- What is "real-time" anyway?
- Hardware and software roles in real-time systems
- Defining RTOSes
- Practical real-time systems
- Choosing an RTOS
- Summary
- Chapter 2: Introducing the Development Board
- Dev-board overview
- Understanding the dev-board's components
- Understanding the hardware documentation
- Summary
- Chapter 3: Introducing the Development-Tools
- Using the book's instructions and example-programs
- The IDE and the example-programs
- The debugging tools: J-Link, Ozone and SystemView
- Summary
- Chapter 4: Understanding Super-Loops
- Introducing super-loops
- Improving the super-loop with interrupts
- Adding a timer to the super-loop
- Summary
- Chapter 5: Implementing the Super-Loop
- The super-loop example-program
- Learning about system APIs and hardware features
- How to develop the super-loop program
- Summary
- Part 2: The FreeRTOS Scheduler, Tasks, and Primary APIs
- Chapter 6: Understanding RTOS Tasks
- The FreeRTOS scheduler and tasks: a conceptual view
- Choosing FreeRTOS as our RTOS
- Hands-on with FreeRTOS's scheduler and tasks
- Summary
- Chapter 7: Running the FreeRTOS Scheduler
- Creating tasks and starting the scheduler
- Deleting tasks
- Trying out the code
- Understanding the scheduler
- Task implementation and troubleshooting
- Summary
- Chapter 8: Protecting Data and Synchronizing Tasks
- Running the chapter's example-programs
- Semaphores
- Priority inversion (how not to use semaphores)
- Mutexes
- Race conditions
- Software timers
- Summary
- Further reading
- Chapter 9: Intertask Communication
- Understanding RTOS queues
- Passing data through queues by value
- Passing data through queues by reference
- Task notifications
- Summary
- Further reading
- Part 3: Using FreeRTOS to Build Systems
- Chapter 10: Drivers and ISRs
- Introducing the UART
- Creating a polled UART driver
- FreeRTOS-tasks vs. ISRs
- Creating ISR-based drivers
- Summary
- Further reading
- Chapter 11: More Efficient Drivers and ISRs
- Creating a basic DMA-based driver
- Creating an enhanced DMA-based driver
- Choosing a driver model
- Using third-party libraries and drivers
- Summary
- Further reading
- Chapter 12: Sharing Hardware Peripherals among Tasks
- Understanding how messages are sent over USB
- Sending messages using a simple USB API
- Creating a VCP driver, using a stream-buffer
- Enhancing the VCP driver, for use by multiple tasks
- Summary
- Further reading
- Chapter 13: Creating Loose Coupling with Queues
- Creating a command queue
- Reusing a queue definition for a new target
- Understanding queues as interfaces
- Summary
- Further reading
- Part 4: System Design and Troubleshooting
- Chapter 14: FreeRTOS Memory Management
- Understanding memory allocation
- Static and dynamic allocation of FreeRTOS primitives
- FreeRTOS heap implementations
- Replacing malloc and free
- Monitoring for stack and heap problems
- Using a memory protection unit (MPU)
- Summary
- Further reading
- Chapter 15: Multi-Processor and Multi-Core Systems
- Introducing multi-core and multi-processor systems
- Exploring multi-core systems
- Exploring multi-processor systems
- Exploring inter-processor communication
- Choosing between multi-core and multi-processor systems
- Summary
- Further reading
- Chapter 16: Troubleshooting Tips and Next Steps
- General FreeRTOS debugging-techniques
- Troubleshooting for the debugging tools
- Next steps
- Part 5: Reference Information and FreeRTOS Installation
- Appendix A: Tools Quick-Reference
- STM32CubeIDE
- Ozone
- SystemView
- Appendix B: Reference Information
- Chapters 2 and 3: Introducing the dev-board and tools
- Chapter 4: Understanding super-loops
- Chapter 5: Implementing the super-loop
- Chapters 6 and 7: RTOS tasks and the FreeRTOS scheduler
- Chapter 10: Drivers and ISRs
- Chapter 11: More efficient drivers and ISRs
- Chapter 12: Sharing hardware peripherals among tasks
- Chapter 14: FreeRTOS memory management
- Appendix C: FreeRTOS installation
- Appendix C: Creating FreeRTOS Projects, and Installing FreeRTOS
- Creating IDE-projects for use in learning FreeRTOS
- Copying and renaming projects (cloning)
- Installing FreeRTOS and SystemView
- Packt Page
- Other Books You May Enjoy
- Index
1
Introducing Real-Time Systems
Real-time systems come in a wide variety of implementations and use cases. This book focuses on how to use a real-time operating system (RTOS) to create real-time applications on a microcontroller unit (MCU).
In this chapter, we'll start with an overview of what an RTOS is and get an idea of the wide range of systems that can have real-time requirements. From there, we'll look at some of the different ways of achieving real-time performance, along with an overview of the types of systems (such as hardware, firmware, and software) that may be used. We'll wrap up by discussing how to choose an RTOS, when it is advisable to use an RTOS in an MCU application, and when it might not be necessary at all.
For readers who are not familiar with MCUs, tutorial information on MCUs can be found in Chapter 2, Introducing the Development Board.
In a nutshell, we will cover the following topics in this chapter:
- What is "real-time" anyway?
- Hardware and software roles in real-time systems
- Defining RTOSes
- Practical real-time systems
- Choosing an RTOS
What is "real-time" anyway?
Real-time systems are systems that have timing requirements. A real-time system is considered to fail when it doesn't meet a timing requirement. How a timing failure is defined (and the consequences of a failed system) can vary widely. It is extremely important to realize that real-time requirements can vary widely, both in the speed of the timing requirement and the severity of consequences if the required real-time deadlines are not met.
Every real-time system is constrained in its ability to respond to events and/or signals. A system's ability to respond to events and/or signals is referred to as the system's bandwidth. For example, when designing a signal acquisition chain, the system's bandwidth for each filter stage must be wide enough to pass through the desired signal in order to maintain the integrity of the signal. If the system's bandwidth for the filters is not properly designed, the system will fail to provide the desired real-time response to certain input signals.
The system's hardware and software both have limited bandwidth. All software executes on hardware that has limited resources. For example, the clock frequency of an MCU limits how many instructions it can execute in a given period of time. The rate of instruction execution is also limited by the MCU architecture, which determines how many clock cycles are required to execute each instruction. The ability of the software to respond to events in real time is affected by the algorithms used, the operating system (OS) used (if any), and the effective use of the OS's features, such as multitasking and timing features.
The limitations of the hardware and software make real-time design both challenging and fun. Challenging, because the designer often has to look into the nitty-gritty of the MCU architecture, including the hardware peripheral registers and the assembly code instructions. But fun, from the satisfaction of understanding the intricacies of how the hardware works, and from being able to write software that takes advantage of it.
It is both a science and an art to balance these limitations and to understand the trade-offs between hardware and software in order to get to the end goal of a working real-time system.
The range of timing requirements
To illustrate the range of timing requirements that can be encountered, let's consider a few different systems that acquire readings from analog-to-digital converters (ADCs).
Let's first look at a lower-bandwidth control system. The control system is part of a soldering iron, and the control system maintains the tip's temperature (as seen in Figure 1.1). The parts of the system we're concerned with are the MCU, ADC, sensor, and heater.
The MCU is the brain of the control system. It is responsible for the following:
- Taking readings from a temperature sensor via the ADC
- Running a closed-loop control algorithm (to maintain a constant temperature at the soldering iron tip)
- Adjusting the output of the heater as needed
These can be seen in the following figure:
Figure 1.1: MCU-controlled soldering iron
A typical soldering iron would have pretty low bandwidth needs. For example, since the temperature of the tip doesn't change very quickly, the MCU may only need to acquire 50 ADC samples per second (50 Hz). The control algorithm responsible for adjusting the heater (to maintain a constant temperature) may run at an even slower pace, perhaps 5 times per second (5 Hz).
While we are here, let's take a quick look at the timing requirements of the ADC. Doing so will give us a feel for some of the time constraints we deal with when designing real-time systems. Figure 1.2 shows a common interface between an MCU and an ADC:
Figure 1.2: MCU with external ADC
The ADC asserts a signal to inform the MCU that the ADC has completed a conversion of the analog signal and that a digital value is ready for the MCU to read. The MCU must read the converted digital value, using the communication channel, before the next conversion takes place, or the result will be overwritten. The MCU has up to 20 ms to transfer the data from the ADC to internal memory before a new reading needs to be taken (as seen in Figure 1.3 below). The MCU also needs to be running the control algorithm to calculate the updated values for the heater output at 5 Hz (200 ms). Both of these cases (although not particularly fast) are examples of real-time requirements.
Figure 1.3: Free-running 50-Hz ADC
Now that we have looked at a lower-bandwidth control system, let's consider that other control systems may require much higher bandwidth. For example, a network analyzer or oscilloscope may require reading an ADC at a rate of tens of GHz! The raw ADC readings will likely be converted into the frequency domain and graphically displayed on a high-resolution front panel dozens of times a second. To function properly, such a system requires significant data-processing bandwidth, and it must adhere to extremely tight timing requirements.
Somewhere in the middle of the possible bandwidth requirements, you'll find systems such as closed-loop motion controllers, which will typically need to execute their proportional-integral-derivative (PID) control loops between hundreds of Hz to tens of kHz in order to provide stability in a fast-moving system. So, how fast is real-time? Well, as you can see from the ADC examples alone, it depends on the requirements.
In some of the previous cases, such as the oscilloscope or soldering iron, failure to meet a timing requirement results in poor performance or incorrect data being reported. In the case of the soldering iron, this might be poor temperature control (which could cause damage to components). For the oscilloscope, missing deadlines could cause erroneous readings, which is a failure. Too many inaccurate readings will cause the device to be viewed as unreliable, and sales will decline-all because a real-time requirement wasn't being met consistently.
In other systems, such as the flight control of an unmanned aerial vehicle (UAV) or motion control in industrial process control, failing to run the control algorithm in a timely manner could result in something more physically catastrophic, such as a crash. In this case, the consequences are potentially life-threatening.
Thankfully, there are steps that can be taken to avoid all of these failure scenarios.
The ways of guaranteeing real-time behavior
One of the easiest ways to ensure that a system does what it is meant to do is to make sure it is as simple as possible while still meeting the requirements (some call this the KISS principle-Keep It Simple, Stupid!). This means resisting the urge to over-complicate a simple task. If a toaster is meant to toast a slice of bread, don't put a display on it and make it tell you the weather too; just have it turn on a heating element for the right amount of time. This simple task has been accomplished for years without requiring any code or programmable devices whatsoever.
As programmers, when we encounter a problem, we tend to reach for the nearest MCU and start coding. However, some functions of a product are best handled without code at all (this is especially pertinent if a product has electro-mechanical components). A car window doesn't really need an MCU with a polling loop to run, turning on motors through drivers and watching sensors for feedback to shut them off. This task can actually be handled by a few mechanical switches and diodes. If a feedback-reporting mechanism is required for a given system-such as an error that needs to be asserted in the case of a stuck window-then there may be no choice but to use a more complex solution. However, our...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.