CA and OS

Computer Architecture

Computer Parts

Part of ComputerDescription
Central Processing Unit (CPU)Executing instructions and performing arithmetic operations on Instructions Set Architecture (ISA) like ARM (Mobile), x86 (Traditional Desktop), RISC (Open Source). Each ISA has its own assembly language.
Random Access Memory (RAM)Main memory for storing data and instructions. Quick but volatile.
Read Only Memory (ROM)Read-only memory for storing instructions. Slow but non-volatile. Mask ROM can’t be overwritten while flash ROM can.
Hard Drive (SSD, HDD)Store data
Graphics Processing Unit (GPU)Processing repeated tasks in parallel efficiently

Types of Chips

TypeDescriptionExample
MicroprocessorCPU on chip. Does not include memory, storage, IODesktop CPU
MicrocontrollerSingle-chip computer that integrates a CPU, memory and peripheralsArduino
SoC (System on a Chip)Integrates one or more CPU, RAM, microcontrollers into one systemSmartphone, tablet, laptop

Tips

Rasberry PI contains SoC

Connectivity

BusTypeUse CaseKey Traits
SPI (Serial Peripheral Interface)Synchronous serialFast comms to flash, sensorsFull duplex, needs CS, MOSI, MISO, SCLK
I2C (Inter-Integrated Circuit)Synchronous serialLow-speed sensors, EEPROMOnly 2 wires (SDA + SCL), addressable slaves
UART (Universal Async Receiver Transmitter)Asynchronous serialSerial consoles, Bluetooth modulesSimple 2-wire (TX/RX), no clock line
CAN (Controller Area Network)Message-basedAutomotive, industrialRobust, supports priorities and multi-master
  • Memory-Mapped I/O memory address that is mapped with a peripheral to communicate.
  • Debouncing remove noise from signal (hardware and software method available)

Firmware

Firmware is the system lifecycle software program that runs on the chip. It consist of two part, bootloader and firmware. Bootloader is a small program that loads the firmware into the chip when booted.

The firmware is coded in C/C++ or assembly language and compiled into a binary machine code through compiler like gcc. The compiler will need to know the chip specification (ISA, cores, memory) to compile the binary code correctly. The machine code then is stored in ROM.

Take a bluetooth module as example. It consists CPU, RAM and ROM with firmware stored. When it is booted, the bootloader will call the CPU to load the firmware from ROM into RAM and execute the firmware line by line.

For Arduino, the C/C++ code we burn into SD card is the firmware. The bootloader in Arduino loads the firmware in SD card. It doesn’t have OS.

Embedded System

Embedded system is a system that integrates hardware and software to perform a specific task. Take rasberry pi as an example. The linux OS and bootloader is running on the SoC. It is a embedded system.

When rasberry pi is booted, the bootloader load bootcode.bin from ROM or EEPROM to initialze RAM. Then the bootcode will call start.elf in SD card to call firmware on GPU. It reads config.txt, kernel.img and .dtb on SD card.

The configuration config.txt determime which kernel image to use, which device tree .dtb to use, and the memory allocation, etc. Device tree tells the kernel about the hardware configuration like architecture, GPIO address, etc. for kernel initialization.

The kernel image must be compatible with SoC family like x32 or x64, arm or x86. The kernel image is coded in c and compiled by compiler like gcc.

A same CPU/SoC can be integrated to different type of board. The need of device tree to tell a GPIO driver in kernel where is the address of GPIO (General-Purpose Input/Output) signal pin. A driver is a piece of software in kernel that provides API for OS and controls the hardware.

PC Boot

The steps of booting a PC:

  1. Power on → CPU runs BIOS (Basic Input/Output System) / UEFI firmware. This was preinstalled on the motherboard.
  2. BIOS initialize memory and hardware.
  3. BIOS loads bootloader like GRUB into memory.
  4. Bootloader choose OS kernel.
  5. kernel loads and initialize root filesystem.
  6. kernel scans devices using ACPI (Advanced Configuration and Power Interface) and loads drivers. ACPI is like device tree.
  7. Login to user space.

Low-Level Development

ToolUse
Compiler: gcc, clangConvert .c/.cpp to .o
AssemblerConvert .asm to .o
LinkerLink .o files into final binary using linker script
Debugger: gdbGNU Debugger for C/C++
OpenOCDOpen On-Chip Debugger
FlasherWrite binary into the chip
Build System: make, cmake, npmAutomate above steps
QEMUEmulator
JLink DebuggerA very eficient piece of hardware, connects to your PC and the chip for debugging

Low-Level Analyzing

ToolUse
OscilloscopeView voltage and current to check a GPIO pin
Logic AnalyzerView digital signal over time — usually across multiple lines. Great for reverse engineering, debudding SPI, I2C, …

Some CPU have a tract unit that records every instruction executed. Debugger use symbol + memory maps to remember the address of a memory location. CPU execution is abstract using symbol tables and IDE/GDB integration.

Real Time Operating System (RTOS)

An OS kernel designed for embedded systems that need predictable timing and task scheduling.

| RTOS kernel | Who Made It | Notes | | FreeRTOS | Amazon | Most popular RTOS ever. Open-source, tiny (<10KB), easy to port. | | Zephyr | Linux Foundation | Modular, modern, built for IoT. More complex, includes full drivers, POSIX-ish API. | | CMSIS-RTOS RTX | Arm | Integrated in ARM ecosystem. | | TI-RTOS | Texas Instruments | TI-specific, optimized for their chips. Not as portable. |

Operating System

The manager of computer resources and processes.

A program is a set of instructions like an application. A process is a running instance of a program like multile instances of a browser, it has it’s own memory. A thread is a lightweight unit of execution within a process, they share the memory of a process and allows cncurrency. A task is a more general term that can refer to either a process or a thread, represent a unit of work.

Task/Threads Scheduler

Decide which runs next based on round-robin, priority, etc. Interrupts to switch tasks (context switch).

Mutexes/Semaphores

Mutex is a lock so only one thread can own it. Used to protect shared resources. A thread spinlock to acquire lock repeateadly. Semaphore is a counter that allow N threads to pass or wait.

IPC (Inter-Process Communication)

Using message queues, shared memory, socket, signal, semaphore, event flags.

Memory Management

Keep track of who owns what memory. A paging/virtual memory maps shorter virtual memory to physical memory.

ISR(Interrupt Service Routine)

Signal kernel to run an interrupt. Interrupt vector table maps an interrupt trigger to a handler function which tells what to do.

POSIX(Portable Operating System Interface)

A standard API for UNIX-like OS. It’s a contract between C and OS kernel.

Cache

Cache TypeRole
CPU cache (L1/L2/L3)Stores recently used memory
Disk cacheStores recently accessed disk blocks
Filesystem cacheBuffers file reads/writes
Web/app cacheHigh-level user-space caching
  • L1: Closest to core, fastest, smallest (~32KB)
  • L2: Shared per core or pair (~256KB-1MB)
  • L3: Shared across all cores (~2MB-64MB)

File System

FSNotes
ext4Default in Linux, simple, reliable and efficient
FAT32/exFATUbiquitous, low-overhead, compatiblity, used for flash drives and SD cards
NTFSWindows default, complex and advanced
APFS/HFS+Apple systems
LittleFS / SPIFFSFor embedded systems (flash memory!)