Explore
Module 1 · Part 1 of 12 · Free preview

Part 1

Chapter 1: The Day the LED Wouldn’t Die--–- “I once watched a $50,000 medical device fail because a junior engineer used malloc(). The patient was not harmed — this time. But the engineer was fired before lunch.”This is not the opening line of a thriller novel. It is a true story from my first year as an embedded systems consultant. The device was a patient-controlled analgesia pump — the kind that delivers pain medication in a hospital. The bug? A memory allocation that failed after the pump had been running for three days. The heap fragmented. malloc() returned NULL. The pump froze. The patient pressed the button. Nothing happened. A nurse pressed the reset button. The pump rebooted. The patient waited.That engineer knew C. He knew pointers. He knew structs. He did not know the embedded mindset. And that is why this book exists.The Desktop Delusion: Why Your Computer Lies to YouBefore we write a single line of code that runs on a microcontroller, we need to burn away everything your CS degree taught you about programming. Not because that knowledge is wrong — it is beautifully right for desktop applications — but because it is dangerously wrong for embedded systems.On your laptop, when you write:cCopyDownloadchar *buffer = malloc(1024);

if (buffer == NULL) { printf(“Out of memory\n”); return -1; }What happens? The operating system sighs, looks at its virtual memory pool, finds 1024 bytes somewhere in its gigabytes of RAM, and hands it to you. If it cannot, it returns NULL, you print a friendly error, and the user closes Chrome and tries again.On an embedded system, the same code does something very different. First, there is no operating system to sigh. There is no virtual memory. There is no swap file. There is no “close Chrome.” There is you, the microcontroller, and the 64 kilobytes of RAM that were soldered onto the board at the factory. When malloc() returns NULL on an embedded system, you cannot print an error because the serial port may not be initialized.

You cannot try again because the condition will not improve. You cannot ask the user to close something because there is no user interface. The system hangs. The watchdog timer expires. The microcontroller resets. The pump stops.This is the first and most important lesson of embedded systems programming: The rules are different here.The Five Constraints: Your New Best Friends (And Worst Enemies)Every embedded system — from a 0.50microcontrollerinadisposablerazortoa0.50 microcontroller in a disposable razor to a 0.50microcontrollerinadisposablerazortoa50,000 medical pump — lives under five fundamental constraints. Master these, and you will ship products.

Keep reading — free

Sign in with your SamEnrico account to read the whole of lesson 1.

Sign in