The Embedded New Testament

The "Holy Bible" for embedded engineers


Project maintained by theEmbeddedGeorge Hosted on GitHub Pages — Theme by mattgraham

Real-Time Communication

Real-time communication requires bounded latency and controlled jitter from sensor/actuator to controller and across the network stack. This guide focuses on techniques for determinism on microcontrollers and embedded Linux.


🧠 Concept First

Real-Time vs Fast

Concept: Real-time systems prioritize predictability over speed. Why it matters: A system that’s sometimes very fast but sometimes slow is not real-time, even if it’s faster on average. Minimal example: Compare a system with 1ms average latency but 100ms worst-case vs. a system with 5ms consistent latency. Try it: Measure both average and worst-case latency of your communication system. Takeaways: Real-time systems must guarantee worst-case performance, not just good average performance.

Latency Budgeting

Concept: You must allocate time to each component in your system to meet overall timing requirements. Why it matters: Without proper budgeting, you can’t guarantee that your system will meet real-time constraints. Minimal example: Design a 10ms control loop with sensor read, processing, communication, and actuation. Try it: Measure the latency of each component in your system and create a budget. Takeaways: Every component must fit within its allocated time budget.


Core Concepts and Theory

Real-Time Communication Fundamentals

What Makes Communication “Real-Time”? Real-time communication is not just about speed—it’s about predictability. A system is real-time if it can guarantee that responses will occur within specified time constraints, regardless of system load or external conditions.

Key Real-Time Characteristics

Real-Time vs High-Performance

Latency and Jitter Analysis

Latency Components Understanding the sources of latency is crucial for real-time system design:

End-to-End Latency Breakdown

Jitter Sources and Analysis

Why Jitter Matters

Real-Time System Classification

Hard Real-Time Systems

Soft Real-Time Systems

Firm Real-Time Systems


End-to-End Latency Budgeting

Latency Budget Philosophy

Why Budget Latency? Latency budgeting is the process of allocating time to different system components to ensure end-to-end timing requirements are met. Without proper budgeting, systems may fail to meet real-time requirements.

Budget Allocation Strategy

Budget Components

Practical Latency Budgeting Example

System Requirements Analysis Let’s consider a real-time control system with a 5ms cycle time requirement:

System Overview

Component Latency Allocation

Budget Validation


MCU Techniques for Real-Time Communication

Interrupt and DMA Optimization

Interrupt Design Philosophy In real-time systems, interrupts must be handled quickly and predictably. The goal is to minimize interrupt latency while maintaining system responsiveness.

Interrupt Optimization Strategies

DMA Integration

Memory Management for Real-Time

Task Priority and Scheduling

Priority Assignment Philosophy Task priorities must reflect the real-time requirements of different system functions. Higher priority tasks should handle more time-critical operations.

Priority Hierarchy Design

Priority Inheritance and Inversion Prevention

Scheduling Considerations


Embedded Linux Techniques

Kernel Configuration for Real-Time

Real-Time Kernel Variants Embedded Linux offers several options for real-time operation:

PREEMPT_RT Patch

Low-Latency Kernel

Standard Kernel with Optimizations

Kernel Configuration Options

CPU Isolation and Affinity

CPU Isolation Philosophy CPU isolation ensures that real-time tasks are not interrupted by other system activities, providing predictable performance.

Isolation Techniques

Affinity Management

Implementation Considerations

Real-Time Scheduling

Linux Real-Time Scheduling Linux provides several scheduling policies for real-time applications:

SCHED_FIFO (First In, First Out)

SCHED_RR (Round Robin)

SCHED_DEADLINE

Scheduling Configuration


Network Transport Choices

Protocol Selection for Real-Time

Real-Time Protocol Requirements Different protocols offer different characteristics for real-time communication:

CAN/CAN-FD

Ethernet with TSN/AVB

UDP for Real-Time

TCP for Real-Time

Protocol Configuration for Real-Time

CAN Configuration

Ethernet TSN Configuration

UDP Configuration

TCP Configuration


Queueing and Backpressure

Queue Design for Real-Time

Queue Design Philosophy Queues in real-time systems must provide predictable performance under all load conditions. The goal is to minimize latency while preventing buffer overflow.

Queue Types and Characteristics

Queue Sizing Strategy

Queue Management

Backpressure Implementation

Backpressure Philosophy Backpressure is the mechanism by which a system signals that it cannot handle more data. In real-time systems, backpressure must be handled quickly to prevent timing violations.

Backpressure Mechanisms

Backpressure Policies

Backpressure Handling


Timestamping and Synchronization

Time Synchronization Fundamentals

Why Time Synchronization Matters Real-time systems often need to correlate events across different components and interfaces. Time synchronization enables this correlation and improves system performance.

Synchronization Types

Synchronization Methods

PTP and Network Time Synchronization

PTP (Precision Time Protocol)

PTP Implementation Considerations

Alternative Synchronization Methods

Timestamp Propagation

Timestamp Management

Timestamp Applications


Measurement and Validation

Real-Time Performance Measurement

Measurement Philosophy Real-time system performance must be measured to ensure requirements are met. Measurement provides data for optimization and validation.

Measurement Techniques

Measurement Points

Performance Metrics

Validation and Compliance

Validation Requirements

Validation Methods

Compliance Documentation


Failure Modes and Mitigations

Common Failure Modes

Timing Failures

Communication Failures

System Failures

Mitigation Strategies

Timing Failure Mitigation

Communication Failure Mitigation

System Failure Mitigation


Implementation Example

Minimal UDP Low-Latency Path

Implementation Philosophy The goal is to create a communication path with minimal latency and jitter. Every optimization must be justified by performance requirements.

Socket Configuration

// Setup socket with DSCP for priority and small buffers tuned for latency
int s = socket(AF_INET, SOCK_DGRAM, 0);
int tos = 0x2e; // Expedited Forwarding DSCP 46 (example)
setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
int rxbuf = 8 * 1024, txbuf = 8 * 1024; // small, avoid buffering delays
setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rxbuf, sizeof(rxbuf));
setsockopt(s, SOL_SOCKET, SO_SNDBUF, &txbuf, sizeof(txbuf));

Configuration Analysis

Critical Loop Design

// Critical loop does minimal work; offload heavy processing to another thread
for (;;) {
  int n = recv(s, buf, sizeof(buf), 0);
  process_minimal(buf, n);
  sendto(s, reply, reply_len, 0, (struct sockaddr*)&peer, sizeof(peer));
}

Loop Optimization


🧪 Guided Labs

Lab 1: Latency Measurement and Budgeting

Objective: Measure and budget latency for a real-time communication system. Setup: Simple embedded system with sensor, processor, and actuator. Steps:

  1. Measure sensor read latency
  2. Measure processing latency
  3. Measure communication latency
  4. Measure actuation latency
  5. Create and validate latency budget Expected Outcome: Complete understanding of system timing and budget compliance.

Lab 2: Jitter Analysis and Reduction

Objective: Analyze and reduce jitter in real-time communication. Setup: System with variable load conditions. Steps:

  1. Measure baseline jitter under no load
  2. Add background tasks and measure jitter
  3. Implement priority management
  4. Optimize critical paths
  5. Measure jitter improvement Expected Outcome: Reduced jitter and improved predictability.

Lab 3: Real-Time Protocol Implementation

Objective: Implement a simple real-time communication protocol. Setup: Two embedded devices or simulation environment. Steps:

  1. Design protocol with timing guarantees
  2. Implement with priority management
  3. Add error handling and recovery
  4. Test under various load conditions
  5. Measure and validate timing compliance Expected Outcome: Working real-time protocol with measured performance.

Check Yourself

Understanding Questions

  1. Real-Time Definition: What makes a communication system “real-time”?
  2. Latency vs Jitter: How do latency and jitter differ, and why does each matter?
  3. Priority Management: Why is priority management crucial in real-time systems?
  4. Budget Allocation: How do you allocate time budgets across system components?

Application Questions

  1. System Design: How do you design a system to meet real-time requirements?
  2. Performance Optimization: What strategies can you use to reduce latency and jitter?
  3. Error Handling: How do you handle errors without violating timing constraints?
  4. Resource Management: How do you manage resources to maintain real-time performance?

Troubleshooting Questions

  1. Timing Violations: What causes real-time systems to miss deadlines?
  2. Jitter Problems: What are the most common sources of jitter in embedded systems?
  3. Priority Issues: What problems arise from improper priority management?
  4. Resource Conflicts: How do you resolve resource conflicts in real-time systems?

Advanced Concepts

Practical Applications

Real-Time Communication Checklist

Design Phase Checklist

Implementation Phase Checklist

Validation Phase Checklist

Deployment Phase Checklist

This enhanced Real-Time Communication document now provides a better balance of conceptual explanations, practical insights, and technical implementation details that embedded engineers can use to understand and implement robust real-time communication systems.