Skip to Content
Programming Embedded Systems in C and C++
book

Programming Embedded Systems in C and C++

by Michael Barr
January 1999
Beginner to intermediate
200 pages
7h 43m
English
O'Reilly Media, Inc.
Content preview from Programming Embedded Systems in C and C++
130 Chapter 9: Putting It All Together
int
Timer::waitfor()
{
if (state != Active)
{
return (-1);
}
//
// Wait for the timer to expire.
//
pMutex->take();
//
// Restart or idle the timer, depending on its type.
//
if (type == Periodic)
{
state = Active;
timerList.insert(this);
}
else
{
pMutex->release();
state = Idle;
}
return (0);
} /* waitfor() */
When the timer does eventually expire, the interrupt service routine will release
the mutex and the calling task will awake inside waitfor. In the process of wak-
ing, the mutex will already be taken for the next run of the timer. The mutex need
only be released if the timer is of type OneShot and, because of that, not automat-
ically restarted.
Printing “Hello, World!”
The other part of our example application is a task that prints the text string
“Hello, World!” to one of the serial ports at a regular interval. Again, the timer
driver is used to create the periodicity. However, this task also depends on a serial
port driver that we haven’t seen before. The guts of the serial driver will be
described in the final two sections of this chapter, but the task that uses it is
shown here. The only thing you need to know about serial ports to understand
this task is that a SerialPort is a C++ class and that the puts method is used to
print a string of characters from that port.
#include "timer.h"
#include "serial.h"
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

You might also like

Embedded Systems Object-Oriented Programming in C and C++

Embedded Systems Object-Oriented Programming in C and C++

Israel Gbati

Publisher Resources

ISBN: 1565923545Catalog PageErrata