Home Learning Paths ECU Lab Assessments Interview Preparation Arena Pricing Log In Sign Up

Lab Scope

ExerciseHardwareGoal
UART ring bufferUART1 on Cortex-M4 (or QEMU emulation)ISR-driven RX into circular buffer; task drains buffer
SPI DMASPI1 + DMA1128-byte DMA transfer to SPI flash; compare with polled
CPU vs DMA benchmarkAny Cortex-M4Measure CPU utilisation difference for 1024-byte SPI transfer

Exercise 1: UART Ring Buffer

Cuart_ringbuf.c
/* UART RX ring buffer: ISR writes, task reads — no critical section needed
   (single producer / single consumer pattern with proper memory ordering) */
#include 
#include "Std_Types.h"

#define RING_SIZE  256u  /* must be power of 2 */
#define RING_MASK  (RING_SIZE - 1u)

typedef struct {
    volatile uint8_t  buf[RING_SIZE];
    volatile uint16_t head;  /* written by ISR */
    volatile uint16_t tail;  /* written by task */
} RingBuffer_t;

static RingBuffer_t g_uart_rx;

/* ISR: called on each received byte (RXNE interrupt) */
void USART1_IRQHandler(void)
{
    uint8_t byte = (uint8_t)(USART1_DR & 0xFFu);  /* read clears RXNE flag */
    uint16_t next_head = (g_uart_rx.head + 1u) & RING_MASK;
    if (next_head != g_uart_rx.tail) {   /* not full: discard if full */
        g_uart_rx.buf[g_uart_rx.head] = byte;
        /* Write data before advancing head (memory ordering) */
        __DMB();
        g_uart_rx.head = next_head;
    }
}

/* Task: drain ring buffer */
boolean Uart_ReadByte(uint8_t *out)
{
    if (g_uart_rx.tail == g_uart_rx.head) { return FALSE; }  /* empty */
    *out = g_uart_rx.buf[g_uart_rx.tail];
    __DMB();
    g_uart_rx.tail = (g_uart_rx.tail + 1u) & RING_MASK;
    return TRUE;
}

uint16_t Uart_Available(void)
{
    return (uint16_t)((g_uart_rx.head - g_uart_rx.tail) & RING_MASK);
}

Exercise 2: SPI DMA Transfer

Cspi_dma.c
#include 
#include "Std_Types.h"

#define SPI_BUF_SIZE  128u

static uint8_t           g_spi_tx_buf[SPI_BUF_SIZE];
static uint8_t           g_spi_rx_buf[SPI_BUF_SIZE];
static volatile boolean  g_spi_dma_complete = FALSE;

/* DMA complete ISR */
void DMA1_CH3_IRQHandler(void)  /* SPI1 TX DMA channel */
{
    DMA1_SR |= DMA_IFCR_CTCIF3;   /* clear transfer complete flag */
    while (SPI1_SR & SPI_SR_BSY) {} /* wait for last bit to shift out */
    SPI1_CS_HIGH();                 /* deassert chip select */
    g_spi_dma_complete = TRUE;      /* signal task */
}

Std_ReturnType Spi_TransmitDMA(const uint8_t *data, uint8_t len)
{
    if (len > SPI_BUF_SIZE) { return E_NOT_OK; }

    /* Copy to DMA-accessible buffer (required if data is in non-DMA-accessible RAM) */
    for (uint8_t i = 0u; i < len; i++) { g_spi_tx_buf[i] = data[i]; }

    g_spi_dma_complete = FALSE;
    SPI1_CS_LOW();

    /* Configure DMA TX channel */
    DMA1_CH3->CPAR  = (uint32_t)&SPI1_DR;
    DMA1_CH3->CMAR  = (uint32_t)g_spi_tx_buf;
    DMA1_CH3->CNDTR = len;
    DMA1_CH3->CCR   = DMA_CCR_DIR | DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_EN;

    /* Enable SPI TX DMA request — peripheral drives DMA */
    SPI1_CR2 |= SPI_CR2_TXDMAEN;

    return E_OK;  /* returns immediately; DMA complete ISR signals completion */
}

Summary

The ISR-driven ring buffer pattern (lock-free single-producer/single-consumer) is the backbone of most embedded communication drivers: UART RX, CAN RX, SPI RX all use this pattern. The DMB (Data Memory Barrier) between writing the data and advancing the head pointer is critical — without it, on Cortex-M7 or multi-core systems, the reader may see the updated head but not the updated data. SPI DMA reduces the 128-byte transfer from ~1280 CPU cycles (polled) to ~10 CPU cycles (configure + trigger), freeing the CPU for other tasks during the transfer time.

🔬 Deep Dive — Core Concepts Expanded

This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.

Key principles to reinforce:

  • Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
  • Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
  • Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.

🏭 How This Topic Appears in Production Projects

  • Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
  • Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
  • Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.

⚠️ Common Mistakes and How to Avoid Them

  1. Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
  2. Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
  3. Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
  4. Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.

📊 Industry Note

Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.

← PreviousDMA FundamentalsNext →RTOS Concepts: Tasks, Scheduling & Priorities