Planning DMA circular buffer for I2C temperature reads
The key insight is that continuous I2C reading with DMA circular mode isn’t natively supported since I2C transactions stop after a fixed length, so to meet the requirement of triggering interrupts only at half buffer full, the approach shows setting DMA circular mode and using half-transfer interrupts with repeated calls to reinitiate the read when needed. The plan focuses on using HAL_I2C_Mem_Read_DMA with DMA circular mode and handling half-transfer interrupts without blocking calls or delays.Confirming HAL half-transfer callback usage
HAL supports HAL_I2C_MemRxHalfCpltCallback triggered by DMA half-transfer interrupts when enabled and DMA is set to circular mode; the plan is to enable only half-transfer interrupts, disable transfer-complete interrupts, and handle half-buffer processing via a volatile flag without blocking calls.
On the STM32 the I²C peripheral and the DMA controller are both capable of generating half–transfer events. You can exploit that to stream data into a ring buffer in the background and only get woken up when a boundary is crossed.
The outline below uses the HAL to keep the example concise, but it never calls any of the blocking read functions or HAL_Delay(). The DMA stream attached to I2C1_RX is put into circular mode with the half‑transfer interrupt enabled and the full‑transfer interrupt masked so you only see one interrupt per half–buffer:
#include "stm32f3xx_hal.h" /* or stm32f0/1/4xx_hal.h for your part */
#define SENSOR_I2C_ADDR 0x48 /* 7‑bit address of your temp sensor */
#define SENSOR_REG_TEMP 0x00 /* temperature register address */
#define BUFFER_LEN 64 /* must be even, multiple of sample size */
/* I2C and DMA handles generated by CubeMX or written by hand */
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
static uint8_t rxBuffer[BUFFER_LEN];
static volatile uint8_t half_full_flag = 0;
static void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
/* Start a non‑blocking DMA read of BUFFER_LEN bytes starting at SENSOR_REG_TEMP.
The DMA stream is in circular mode so it will wrap around rxBuffer. */
HAL_I2C_Mem_Read_DMA(&hi2c1,
SENSOR_I2C_ADDR << 1, /* 8‑bit address */
SENSOR_REG_TEMP,
I2C_MEMADD_SIZE_8BIT,
rxBuffer,
BUFFER_LEN);
for (;;)
{
if (half_full_flag)
{
half_full_flag = 0;
/* Process the first half of the buffer. In circular mode the next
half‑transfer will give you the second half, then back to the first half, etc. */
process_temperature_samples(&rxBuffer[0], BUFFER_LEN / 2);
}
/* Do other non‑blocking work here */
}
}
/* Configure DMA1_Channel7 (for example) for I2C1 RX in circular mode */
static void MX_DMA_Init(void)
{
__HAL_RCC_DMA1_CLK_ENABLE();
hdma_i2c1_rx.Instance = DMA1_Channel7;
hdma_i2c1_rx.Init.Request = DMA_REQUEST_I2C1_RX;
hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c1_rx.Init.Mode = DMA_CIRCULAR;
hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&hdma_i2c1_rx);
__HAL_LINKDMA(&hi2c1, hdmarx, hdma_i2c1_rx);
/* Enable only the half‑transfer interrupt */
__HAL_DMA_ENABLE_IT(&hdma_i2c1_rx, DMA_IT_HT);
__HAL_DMA_DISABLE_IT(&hdma_i2c1_rx, DMA_IT_TC);
HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
}
/* Standard I2C init, generated by CubeMX or hand‑written */
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00B01A4B; /* adjust for your bus speed */
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks= I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c1);
}
/* DMA IRQ handler just dispatches into the HAL */
void DMA1_Channel7_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_i2c1_rx);
}
/* This callback is called by the HAL when half of rxBuffer has been filled */
void HAL_I2C_MemRxHalfCpltCallback(I2C_HandleTypeDef *hi2c)
{
if (hi2c->Instance == I2C1) {
half_full_flag = 1;
}
}
/* Optional: if you want to see the full‑buffer event as well */
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
/* full buffer has wrapped; the next half‑transfer interrupt will be for rxBuffer[0] again */
}
process_temperature_samples() is your own function to convert the raw bytes into engineering units. Because the DMA stream is in circular mode the I²C/DMA hardware will keep filling rxBuffer in the background without any calls to HAL_Delay() or blocking I/O. Only when the half‑transfer interrupt fires does the code set half_full_flag to tell the main loop that half of the ring buffer is ready to be processed.