Applying C For The IoT With Linux 

 

 Buy from:  Amazon


 

Applying C is the book you need if you are programming for Single Board Computers (SBCs) that run Linux, or if you do any coding in C that interacts with the hardware. As there isn’t a good name for this body of knowledge, it isn’t easy to find a single source for it. This book gathers together all of these low-level, hardware-oriented and often hardware-specific ideas. As such it is a moderately advanced book. This is not to say that it is difficult, but it does presuppose that you already know how to program in C and that you know the basic idioms of C.

Starting off from the very simple task of making a program run automatically, we look at how your program works with user-mode Linux. If you are working with hardware, arithmetic cannot be ignored and separate chapters are devoted to integer, fixed-point and floating-point arithmetic. Equally, to handle I/O you need to have a good grasp of files and the pseudo file system. The dev/mem file coupled with memory-mapped files makes it possible to work with raw memory without leaving user mode. Sockets are general-purpose way of communicating over networks and similar infrastructure and here the focus is on sending data over the internet and for this we build a web client and a server.

Next we consider graphics, which you might find unusual in a book on small systems, but today even small systems have GPUs and graphics comes as standard. It is common to think of adding low-cost output devices such as 7-segment displays to IoT devices, but with low cost HDMI/DVI displays available it becomes cost effective to simply use the built-in graphics hardware.

After this we turn to the idea of multi-tasking using Pthreads. As well as looking at threads, we consider locking, using mutex and condition variables, and scheduling. Although interrupts don't exist in user-mode Linux, we can get very close using poll and threading. Now that multiple cores are a feature of even low-cost SBC, in later chapters we cover managing cores, look at C11’s atomics and introduce its memory models and barriers. Finally we take a short look at how to mix assembler with C.

Harry Fairhead has a hardware background and, having worked with microprocessors and electronics in general, for many years, he is an enthusiastic proponent of the IoT. He is the author of Fundamental C: Getting Closer To The Machine and his other recent titles include Raspberry Pi IoT in C and Micro:bit IoT in C

  • Paperback: 332 pages
  • Publisher: I/O Press (May 29, 2019)
  • Language: English
  • ISBN-10: 1871962617
  • ISBN-13: 978-1871962611
  • Product Dimensions: 7.5 x 0.8 x 9.2 inches

Errata: 

You can find the revision of the copy you have by looking at the title page.

Revision 1 (current) - No known errors.

Revision 0 - All errors listed below removed in Revision 1 which is current.

None of the errors caused any of the programs to not work as listed.

Thanks to reader Robert E Boughner for spotting and reporting the following:

page  130

float w = 0.0/0.0;;

should be 

float w = 0.0/0.0;

that is delete second semi-colon.

 

page170

        memcpy(memptr,&value,4);;

should be 

        memcpy(memptr,&value,4);

that is delete second semi-colon.

page 171

Delete the line:

  value = ~value;

inside the for loop - it doesn't do anything.

page 219

Change

HOST:example.org

 to

HOST:example.com

 In fact both example.com and example.org work as they resolve to the same IP address.

page 218

Change

connect(sockfd, (struct sockaddr *) &addr, sizeof (addr)

to

connect(sockfd, (struct sockaddr *) &addr, sizeof (addr));

i.e. add the closing parentheis and semicolon.

page 220

Change

 struct sockaddr_in addr;addr.sin_family = AF_INET;

to

 struct sockaddr_in addr;
addr.sin_family = AF_INET;

i.e. start a newline for clarity.

page 221

Change

 struct sockaddr_in addr;addr.sin_family = AF_INET;

to

 struct sockaddr_in addr;
addr.sin_family = AF_INET;

i.e. start a newline for clarity.

 

Page 229

Add to follow the program:

Notice the use of snprintf – the safe version of sprintf which is like printf but sends the output to a string.

Page 241

Third paragraph

- i.e. it needs to global.

change to

- i.e. it needs to be global.

 i.e. add "be".

Page 247

Change

In the real world it can be very difficult to work out what is happening but the principle is easy - make sure the a lock is held for the minimum time by each thread, and that on average it is unlocked for more time than it is locked.

to

In the real world it can be very difficult to work out what is happening but the principle is easy - make sure that a lock is held for the minimum time by each thread, and that on average it is unlocked for more time than it is locked.

That is change the to that.

Page 271

Change

When you first met this sort of possibility there is a tendency to think that things just got so complicated that you can’t possible write a working program.

to

When you first met this sort of possibility there is a tendency to think that things just got so complicated that you can’t possibly write a working program.

That is change possible to possibly