Embedded SystemUnit 215 min read

Embedded Processors & Microcontrollers: Architecture, Types, Applications & Selection

Unit 2 of Embedded System explores the core components of embedded processors and microcontrollers, covering their architecture, classification, programming models, and real-world applications in IoT, automotive, and industrial systems. This note includes detailed comparisons, performance analysis, and case studies fro

Core Concepts: What Are Embedded Processors and Microcontrollers?

Definitions and Key Differences

An embedded processor is a specialized CPU designed to perform dedicated tasks within a larger system. It is optimized for low power consumption, real-time operation, and cost efficiency. Examples include Digital Signal Processors (DSPs) and Application-Specific Instruction Set Processors (ASIPs).

A microcontroller (MCU) is a self-contained system-on-chip (SoC) that integrates a processor core, memory (RAM/ROM/Flash), peripherals (timers, ADCs, UARTs), and often an operating system or firmware. Microcontrollers are the backbone of embedded systems due to their compact size and integrated functionality.

Key Difference:

Feature Embedded Processor Microcontroller (MCU)
Integration Standalone CPU core SoC with CPU + peripherals
Memory External (often required) On-chip (RAM/Flash)
Power Consumption Higher (general-purpose) Optimized for low power
Cost Moderate to high Low (mass-produced)
Use Case Complex tasks (DSPs) Simple to moderate tasks

How They Work: Architecture and Components

1. Embedded Processor Architecture

Embedded processors often use modified Harvard or von Neumann architectures to optimize speed and power. Key components include:

  • CPU Core: Reduced Instruction Set Computing (RISC) or Complex Instruction Set Computing (CISC) cores (e.g., ARM Cortex-M, MIPS).
  • Cache Memory: Small, fast memory (L1/L2) to reduce access latency to main memory.
  • Memory Management Unit (MMU): Optional in some MCUs for virtual memory support.
  • Interrupt Controller: Prioritizes real-time tasks (e.g., sensor data acquisition).

2. Microcontroller Architecture

A typical MCU integrates:

  • Processor Core: 8-bit (e.g., AVR ATmega328P), 16-bit (e.g., PIC18F), or 32-bit (e.g., STM32F4).
  • On-Chip Memory:
    • Flash: Non-volatile program storage (e.g., 128KB–2MB).
    • SRAM: Volatile data storage (e.g., 8KB–256KB).
    • EEPROM: Non-volatile data storage (limited writes).
  • Peripherals:
    • Timers/Counters: For PWM, delays, or input capture.
    • ADCs/DACs: Analog-to-digital conversion (e.g., 10-bit ADC in STM32).
    • Communication Interfaces: UART, SPI, I2C, CAN, USB.
    • GPIO: General-purpose input/output pins.

Types of Embedded Processors and Microcontrollers

1. Classification by Bit Architecture

Type Examples Pros Cons Use Cases
8-bit AVR ATmega328P, PIC16F877A Low cost, simple, low power Limited performance, no FPU Sensors, LEDs, basic IoT
16-bit PIC24F, dsPIC33 Balanced performance/cost Limited market support Motor control, industrial automation
32-bit ARM Cortex-M (STM32, NXP LPC), AVR32 High performance, FPU, DSP extensions Higher cost/power Automotive, robotics, advanced IoT
64-bit ARM Cortex-A (Raspberry Pi CM4) Full OS support, multimedia Overkill for simple tasks Gateways, complex embedded Linux
AVR (Atmega)PIC (Microchip)80518-bitPIC18MSP43016-bitARM Cortex-MSTM32ESP3232-bitMCUs by Bit Architecture
Classification of microcontrollers based on data bus width.

2. Classification by Application Domain

A. General-Purpose MCUs

  • Examples: Arduino Uno (ATmega328P), ESP8266 (WiFi-enabled).
  • Applications: Prototyping, hobbyist projects, simple IoT devices.
  • Why? Low cost, easy to program (Arduino IDE), and widely supported.

B. Specialized MCUs

  • Automotive: Infineon AURIX (safety-critical, CAN bus).
  • Industrial: Siemens SIMATIC (robust, high-temperature).
  • Wireless: Nordic nRF52 (Bluetooth Low Energy for wearables).
  • Audio/DSP: Texas Instruments TMS320C55x (digital audio processing).

C. Digital Signal Processors (DSPs)

  • Examples: TI C6000, Analog Devices Blackfin.
  • Key Features:
    • Hardware accelerators for multiply-accumulate (MAC) operations.
    • Optimized for real-time signal processing (e.g., audio, radar).
  • Applications: Medical imaging, 5G base stations, ultrasonic sensors.

Programming Models for Embedded Systems

1. Bare-Metal Programming

  • Definition: Directly programming the MCU without an OS, using assembly or C.
  • Pros:
    • Full control over hardware (critical for real-time systems).
    • Minimal overhead (no OS scheduling delays).
  • Cons:
    • Complex (manual memory management, interrupt handling).
    • Error-prone (no built-in debugging tools).
  • Example: Writing a PWM signal for an LED using STM32 HAL libraries.

Worked Example: Blinking an LED on STM32 (Bare-Metal)

#include "stm32f4xx.h"

void delay_ms(uint32_t ms) {
    for (uint32_t i = 0; i < ms * 1000; i++) {
        __NOP(); // No-operation (CPU stall)
    }
}

int main() {
    // Enable GPIOA clock
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

    // Set PA5 (LED) as output
    GPIOA->MODER &= ~(3 << (5*2)); // Clear bits
    GPIOA->MODER |= (1 << (5*2));  // Set as output

    while (1) {
        GPIOA->ODR ^= (1 << 5);    // Toggle LED
        delay_ms(500);
    }
}

2. Real-Time Operating Systems (RTOS)

  • Definition: Lightweight OS for embedded systems (e.g., FreeRTOS, Zephyr, VxWorks).
  • Key Features:
    • Task Scheduling: Preemptive or cooperative multitasking.
    • Interrupt Handling: Prioritized ISRs (Interrupt Service Routines).
    • Memory Management: Dynamic allocation (heap) or static (no fragmentation).
  • Pros:
    • Easier to develop complex applications.
    • Better resource utilization (e.g., idle tasks).
  • Cons:
    • Overhead (~1–5% CPU usage).
    • Licensing costs for commercial RTOS (e.g., QNX).

Worked Example: FreeRTOS Task Creation (STM32)

#include "FreeRTOS.h"
#include "task.h"

void vLEDTask(void *pvParameters) {
    while (1) {
        GPIOA->ODR ^= (1 << 5); // Toggle LED
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}

int main() {
    // Initialize hardware (as above)
    xTaskCreate(vLEDTask, "LED Task", 128, NULL, 1, NULL);
    vTaskStartScheduler();
    while (1); // Should never reach here
}

Performance Metrics and Selection Criteria

1. Key Metrics

Metric Description Units
Clock Speed Maximum frequency of the CPU core. MHz
MIPS/DMIPS Millions of Instructions Per Second (DMIPS = MIPS adjusted for architecture). MIPS
Power Consumption Active/idle power draw. mA/W
Flash Size Program memory capacity. KB/MB
SRAM Size Data memory capacity. KB
Peripheral Count Number of built-in interfaces (UARTs, ADCs, etc.). Count
Package Type Physical form factor (e.g., TSSOP, QFN). -

2. How to Select the Right MCU

Use this decision flowchart to choose an MCU for your project:

flowchart TD
    A["Start"] --> B{"Is this a real-time system?"}
    B -->|"Yes"| C{"Do you need hard deadlines?"}
    C -->|"Yes"| D["Use RTOS-capable MCU (e.g., STM32 with FreeRTOS)"]
    C -->|"No"| E["Use bare-metal or cooperative RTOS"]
    B -->|"No"| F{"Is power critical?"}
    F -->|"Yes"| G["Use ARM Cortex-M0/M0+/M4 (low power)"]
    F -->|"No"| H{"Is cost the priority?"}
    H -->|"Yes"| I["Use 8-bit AVR/PIC"]
    H -->|"No"| J{"Do you need wireless?"}
    J -->|"Yes"| K["Use ESP8266/ESP32 or Nordic nRF52"]
    J -->|"No"| L["Use 32-bit MCU (STM32, NXP LPC)"]

In the Real World

1. eSewa (Nepal) – Secure Payment Processing

  • MCU Used: STM32F4 series (for secure element and cryptographic operations).
  • How It Works:
    • The STM32 handles TLS/SSL encryption for payment data (UART/SPI communication with secure chips).
    • Real-Time Clock (RTC) ensures transaction timestamps are accurate.
    • Interrupt-driven design processes card swipes or QR codes without delays.
  • Key Idea: Interrupt handling and secure communication protocols (UART/SPI) in embedded systems.

2. Pathao (Ride-Hailing) – GPS and Driver Tracking

  • MCU Used: ESP32 (WiFi + Bluetooth Low Energy) or Nordic nRF52.
  • How It Works:
    • GPS Module (e.g., NEO-6M) connects via UART to the MCU.
    • SPI/I2C interfaces with sensors (accelerometer, gyroscope) for driver behavior analysis.
    • WiFi sends real-time location data to the cloud (MQTT protocol).
  • Key Idea: Multi-protocol communication (UART, SPI, I2C, WiFi) in IoT devices.

3. Daraz Logistics – Package Sorting Automation

  • MCU Used: Siemens SIMATIC S7-1200 (industrial PLC).
  • How It Works:
    • ADC reads weight sensors to classify packages.
    • Timers control conveyor belt speeds.
    • CAN bus coordinates with robotic arms for sorting.
  • Key Idea: Industrial-grade MCUs with ADC and CAN bus for automation.

4. NTC (Nepal Telecommunications) – Smart Meters

  • MCU Used: TI MSP430 (ultra-low power) or STM32L4.
  • How It Works:
    • ADC measures voltage/current from power lines.
    • RTC logs consumption data hourly.
    • LoRaWAN (via external module) sends data to the NTC server.
  • Key Idea: Low-power MCUs with ADC and wireless protocols for IoT.

Worked Example: Traffic Light Controller (Nepali Context)

Scenario: Design a traffic light system for a busy intersection in Kathmandu using an STM32 MCU.

Requirements

  • 3 traffic lights (red, yellow, green) per direction.
  • Timings: Green (30s), Yellow (5s), Red (25s).
  • Emergency vehicle override (button input).

Solution

flowchart TD
    A["Start"] --> B["Initialize GPIO for 6 LEDs + 1 button"]
    B --> C["Configure Timers for delays"]
    C --> D["Main Loop"]
    D --> E{"Is emergency button pressed?"}
    E -->|"Yes"| F["Flash all LEDs, reset timers"]
    F --> D
    E -->|"No"| G["Cycle through traffic light states"]
    G --> H["Green for 30s"]
    H --> I["Yellow for 5s"]
    I --> J["Red for 25s"]
    J --> D

Code Implementation (STM32 HAL)

#include "stm32f4xx_hal.h"

#define GREEN1_PIN  GPIO_PIN_5
#define YELLOW1_PIN GPIO_PIN_6
#define RED1_PIN    GPIO_PIN_7
#define EMERGENCY_PIN GPIO_PIN_8

TIM_HandleTypeDef htim2;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);

int main() {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_TIM2_Init();

    while (1) {
        // Check emergency button
        if (HAL_GPIO_ReadPin(GPIOA, EMERGENCY_PIN) == GPIO_PIN_SET) {
            // Flash all LEDs
            for (int i = 0; i < 5; i++) {
                HAL_GPIO_TogglePin(GPIOA, GREEN1_PIN);
                HAL_GPIO_TogglePin(GPIOA, YELLOW1_PIN);
                HAL_GPIO_TogglePin(GPIOA, RED1_PIN);
                HAL_Delay(200);
            }
            continue;
        }

        // Green for 30s
        HAL_GPIO_WritePin(GPIOA, GREEN1_PIN, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOA, RED1_PIN, GPIO_PIN_RESET);
        HAL_Delay(30000);

        // Yellow for 5s
        HAL_GPIO_WritePin(GPIOA, GREEN1_PIN, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA, YELLOW1_PIN, GPIO_PIN_SET);
        HAL_Delay(5000);

        // Red for 25s
        HAL_GPIO_WritePin(GPIOA, YELLOW1_PIN, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA, RED1_PIN, GPIO_PIN_SET);
        HAL_Delay(25000);
    }
}

Common Pitfalls and Best Practices

1. Power Management

  • Problem: Batteries drain quickly in IoT devices.
  • Solution:
    • Use low-power modes (e.g., STM32 sleep/wakeup via external interrupt).
    • Dynamic voltage scaling (DVS) to reduce clock speed when idle.
  • Example: ESP32 can consume <100µA in deep sleep vs. 80mA active.

2. Peripheral Configuration

  • Problem: Incorrect UART/SPI settings cause communication failures.
  • Solution:
    • Always check baud rate, data bits, parity, and stop bits for UART.
    • For SPI/I2C, verify clock speed (SCLK), CS/SS pins, and pull-up resistors.

3. Interrupt Handling

  • Problem: Missed interrupts or priority conflicts.
  • Solution:
    • Use nested vectored interrupts (NVIC in ARM Cortex).
    • Keep ISRs short (avoid blocking operations).

4. Memory Leaks

  • Problem: Dynamic memory allocation without freeing causes crashes.
  • Solution:
    • Use static memory where possible.
    • For RTOS, use heap_5.c (FreeRTOS) to track allocations.

Exam Tip

How This Unit Is Examined (PU Pattern)

  1. Theory Questions (30–40%):

    • Define and differentiate between embedded processors vs. MCUs.
    • Explain Harvard vs. von Neumann architecture in embedded systems.
    • Compare 8-bit, 16-bit, and 32-bit MCUs (pros/cons/examples).
    • Describe RTOS task states and scheduling.
  2. Short Problems (20–30%):

    • Calculate MIPS/DMIPS given clock speed and CPI.
    • Design a pinout diagram for an MCU interfacing with a sensor (e.g., DHT11 via UART).
    • Write pseudo-code for a traffic light controller.
  3. Long Problems (30–40%):

    • Case Study: Select an MCU for a given application (e.g., "Design a heart rate monitor using an MCU with ADC and Bluetooth").
    • Code Tracing: Debug a snippet of embedded C (e.g., PWM generation or UART transmission).
    • RTOS Design: Draw a task state diagram and explain scheduling for a robotics application.

Key Focus Areas for Full Marks

  • Architecture: Know the block diagram of ARM Cortex-M and STM32 peripherals.
  • Programming: Be able to write bare-metal and RTOS code snippets (LED blinking, UART communication).
  • Applications: Relate MCUs to real-world systems (e.g., eSewa, Pathao, Daraz).
  • Selection Criteria: Use the decision flowchart to justify your choice of MCU for a given scenario.

Common Mistakes to Avoid

  • Ignoring Clock Configurations: Always initialize the system clock (e.g., SystemClock_Config() in STM32 HAL).
  • Floating Pins: Unused GPIO pins should be set as inputs with pull-up/down to avoid noise.
  • Assuming Infinite Loops: In RTOS, use vTaskDelay() instead of while(1) with HAL_Delay().
  • Overlooking Peripheral Clocks: Enable clocks for GPIO, UART, etc., via RCC registers.

STM32 pinout diagram labelled**Shows GPIO, ADC, UART, and power pins for STM32F407. (Image: Rasmus Friis Kjeldsen, CC BY-SA 4.0, via Wikimedia Commons)

Based on the PU BE Computer (PU) syllabus for Embedded System (ELX320), unit 2.

Discussion

Loading…