Brief Summary
This video explains how code controls hardware through a mechanism called memory-mapped IO. It demonstrates this concept by controlling an LED, a trimmer, a receipt printer, and a laptop using direct memory writes. The video details the process of identifying memory addresses and writing values to them to manipulate hardware functions, emphasizing that this principle applies universally across different devices and systems.
- Memory-mapped IO allows software to interact with hardware by reading from and writing to specific memory addresses.
- The process involves finding the correct memory addresses in device data sheets and using direct memory write operations to control hardware functions.
- This mechanism is fundamental to embedded systems and is also used in larger systems like laptops, despite operating system restrictions.
Intro
The video introduces the concept of how code controls hardware, specifically addressing the question of how abstract code can manipulate physical devices. The presenter, an embedded systems engineer, contrasts his work with that of web developers, noting the difference in understanding the underlying mechanisms of hardware control. The core idea is that a simple mechanism, memory mapped IO, allows code to control various devices. The presenter aims to demonstrate this by controlling an LED, a trimmer, a printer, and even a laptop using a single line of code principle.
Led
The video demonstrates memory mapped IO using an Arduino and a basic "Blinky" program. Initially, the standard Arduino code is used to blink an LED. The presenter then replaces the high-level Arduino functions with direct memory writes to achieve the same result. This involves identifying the memory addresses for the LED pin in the microcontroller's data sheet and using C code with pointers to write values to those addresses. The volatile keyword is used to prevent the compiler from optimizing away these memory writes. The LED blinks as before, but now controlled directly via memory writes, illustrating the fundamental principle of memory mapped IO.
Trimmer
The video extends the demonstration of memory mapped IO to a trimmer controlled by an STM32 microcontroller. The initial code uses high-level driver libraries to manage the motor and button input. The presenter replaces these functions with direct memory writes to control the motor. This involves configuring pins, setting up a timer for the motor signal, and reading the button input by writing to and reading from specific memory addresses. The addresses and values are obtained from the STM32's data sheet. The trimmer successfully turns on and off via button press, confirming that memory mapped IO is also applicable to more complex devices.
Receipt printer
The video further illustrates memory mapped IO by controlling a thermal receipt printer connected to a Nordic microcontroller board via serial communication. Instead of using a printer library, the presenter directly writes to memory to send commands and text to the printer. This involves configuring the serial communication through memory writes and creating an array of bytes representing the text and commands to be printed. A loop sends each byte to the printer one at a time. The printer successfully prints the desired text, demonstrating that memory mapped IO can control devices with more complex communication protocols.
How it works
The video explains the inner workings of memory mapped IO by comparing it to writing to regular memory (RAM) in a microcontroller. When writing to RAM, the CPU fetches and decodes instructions to store a value into a specific memory cell. With memory mapped IO, the process is similar, but an address decoder block directs the memory write to a specific hardware component instead of RAM. This decoder acts like a hard-coded if-else statement, mapping certain memory addresses to IO devices. When the correct address is accessed, the decoder enables the corresponding hardware (e.g., GPIO), which then controls the physical pin or device. This mechanism allows IO to be treated like regular memory, simplifying code and promoting portability.
Reflection
The presenter reflects on the significance of memory mapped IO, describing it as a fundamental theorem of embedded systems that connects software and hardware. Despite the existence of other mechanisms like interrupts and DMA, memory mapped IO remains the most common and fundamental way for code to interact with hardware. The presenter finds a sense of calm in knowing that, ultimately, controlling hardware involves simply flipping bits in memory, regardless of the complexity.
Laptop
The video extends the concept of memory mapped IO to a laptop, demonstrating that even high-level systems use this mechanism to control hardware. To change the screen brightness, the presenter bypasses the graphical user interface and attempts to write directly to memory. Due to operating system restrictions, the Linux kernel must be recompiled with disabled memory access restrictions. By writing specific values to a particular memory address, the screen brightness can be adjusted. This illustrates that memory mapped IO is a universal principle, even in complex systems like laptops, reinforcing the idea that hardware is fundamentally controlled through memory manipulation.

