博文

目前显示的是标签为“DMA”的博文

How to use DMA in circular mode on STM32?

图片
 Using DMA in circular mode on STM32 basically means: “Keep filling the same buffer in RAM over and over, wrapping around when you reach the end.” This is perfect for continuous ADC sampling , UART RX streams , audio , etc. Here’s how to do it, in practical steps. 1. Basic idea Peripheral data register → DMA → RAM buffer. DMA writes: buf[0] … buf[N-1] then automatically wraps back to buf[0] and continues. Your code never restarts the DMA; it just reads from the buffer while DMA keeps filling it. 2. Create the buffer in RAM Example for an ADC (16-bit samples): # define ADC_BUF_LEN 256 uint16_t adc_buf[ADC_BUF_LEN]; Example for UART RX (8-bit): # define UART_RX_BUF_LEN 128 uint8_t uart_rx_buf[UART_RX_BUF_LEN]; Make sure the buffer is global or static so it stays allocated. 3. Configure DMA in circular mode (HAL example) Let’s say you’re using STM32Cube HAL. 3.1. In CubeMX / code, set: Direction : Peripheral → Memory Mode : DMA_...

How to optimize MCU interrupt response time?

图片
 Optimizing MCU interrupt response time is crucial for real-time performance and accurate signal processing. Below are practical, layered strategies to reduce latency and improve the efficiency of your interrupt handling:  1. Minimize Interrupt Latency (Time Until ISR Starts)  What causes latency? Instruction execution delay Stack saving (context switch) Nested interrupts disabled Peripheral delay (e.g., slow flag clearing)  Optimization Techniques: Tip Description Enable fast interrupt mode (if supported) Some MCUs (e.g. ARM Cortex-M3+) support tail-chaining or fast ISR entry Use high-priority interrupts Assign highest priority to critical ISRs Avoid global interrupt disable ( __disable_irq() ) Keep interrupts globally enabled as much as possible Use vectorized interrupt handling Avoid shared interrupt vectors; direct vector → faster Place ISR in RAM (if allowed) Running code from RAM can be faster than Flash on some MCUs  2. Write Ef...