This is a complex embedded systems question asking for an STM32 I2C interface with a temperature sensor using DMA and circular buffering. Let me break down what's required:
- Non-blocking I/O - use DMA and interrupts
- Temperature sensor read over I2C
- DMA transfer to circular buffer
- Interrupt on half-full buffer condition
- No blocking delays (no HAL_Delay)
I'll provide a comprehensive solution that includes:
- Initialization code
- DMA configuration
- I2C configuration
- Circular buffer management
- Interrupt handlers
- Example with a common sensor like LM75 or BMP180
Let me create a complete, well-documented example:
Key points to cover:
- I2C peripheral configuration
- DMA configuration for circular mode
- Buffer structure and management
- Interrupt handler
- Non-blocking read functions
- Data processing
I'll use a realistic sensor example - the LM75A temperature sensor which uses I2C and has simple register access.
#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_i2c.h"
#include "stm32f4xx_ll_dma.h"
#include <stdint.h>
#include <string.h>
// ============================================================================
// CONFIGURATION
// ============================================================================
#define BUFFER_SIZE 32 // Must be power of 2 for circular buffer
#define DMA_TRANSFER_SIZE (BUFFER_SIZE / 2) // Interrupt at half-full
#define LM75A_ADDRESS 0x48 // 7-bit I2C address
#define LM75A_TEMP_REG 0x00
// ============================================================================
// CIRCULAR BUFFER STRUCTURE
// ============================================================================
typedef struct {
uint16_t buffer[BUFFER_SIZE];
volatile uint16_t write_index;
volatile uint16_t read_index;
volatile uint32_t half_full_count;
} CircularBuffer_t;
// ============================================================================
// GLOBAL VARIABLES
// ============================================================================
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c_rx;
CircularBuffer_t temp_buffer = {0};
volatile uint8_t dma_interrupt_flag = 0;
// ============================================================================
// FUNCTION PROTOTYPES
// ============================================================================
void I2C_DMA_Init(void);
void DMA_Init(void);
uint16_t CircularBuffer_Read(CircularBuffer_t *cb);
uint8_t CircularBuffer_IsEmpty(CircularBuffer_t *cb);
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
void HAL_DMA_IRQHandler(DMA_Stream_TypeDef *DMAx_Streamy);
The structure defines a circular buffer with indices to track read/write positions and a counter for half-full interrupts. Global handles manage the I2C peripheral and DMA stream, while a flag signals when the buffer reaches half capacity. Function prototypes establish the initialization routines, buffer operations, and interrupt callbacks needed for the non-blocking temperature acquisition system. MA_I2C_Init(void);
void CircularBuffer_Init(CircularBuffer_t *buffer);
uint16_t CircularBuffer_Read(CircularBuffer_t *buffer);
uint8_t CircularBuffer_HasData(CircularBuffer_t *buffer);
float LM75A_RawToTemp(uint16_t raw_value);
void I2C_DMA_Start_Continuous_Read(void);
// ============================================================================
// I2C INITIALIZATION
// ============================================================================
void I2C_DMA_Init(void)
{
// Enable clocks
__HAL_RCC_I2C1_CLK_ENABLE();
__HAL_RCC_DMA1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// GPIO Configuration: PB8 (SCL), PB9 (SDA)
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// I2C Initialization
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000; // 100 kHz for compatibility
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
Error_Handler();
}
// Initialize DMA
DMA_I2C_Init();
// Set up the circular buffer for storing temperature readings
CircularBuffer_Init(&temp_buffer);
}
// ============================================================================
// DMA INITIALIZATION
// ============================================================================
void DMA_I2C_Init(void)
{
// Configure DMA1 Stream 0 for I2C1 RX operations
hdma_i2c_rx.Instance = DMA1_Stream0;
hdma_i2c_rx.Init.Channel = DMA_CHANNEL_1;
hdma_i2c_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2c_rx.Init.PeriphInc = DMA_PERIPH_INC_DISABLE;
hdma_i2c_rx.Init.MemInc = DMA_
MEMORY_INC_ENABLE;
hdma_i2c_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c_rx.Init.Mode = DMA_CIRCULAR;
hdma_i2c_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_i2c_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&hdma_i2c_rx);
__HAL_LINKDMA(&hi2c1, hdmarx, hdma_i2c_rx);
// Configure and enable DMA interrupts for half-transfer and completion events
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
__HAL_DMA_ENABLE_IT(&hdma_i2c_rx, DMA_IT_HT | DMA_IT_TC);
}
// ============================================================================
// CIRCULAR BUFFER IMPLEMENTATION
// ============================================================================
void CircularBuffer_Init(CircularBuffer_t *buffer)
{
memset(buffer->buffer, 0, sizeof(buffer->buffer));
buffer->write_index = 0;
buffer->read_index = 0;
buffer->half_full_count = 0;
}
uint16_t CircularBuffer_Read(CircularBuffer_t *buffer)
{
uint16_t data = buffer->buffer[buffer->read_index];
buffer->read_index = (buffer->read_index + 1) & (BUFFER_SIZE - 1);
return data;
}
uint8_t CircularBuffer_HasData(CircularBuffer_t *buffer)
{
return buffer->read_index != buffer->write_index;
}
uint16_t CircularBuffer_GetCount(CircularBuffer_t *buffer)
{
return (buffer->write_index - buffer->read_index) & (BUFFER_SIZE - 1);
}
// ============================================================================
// I2C DMA CONTINUOUS READ
// ============================================================================
void I2C_DMA_Start_Continuous_Read(void)
{
// Initiate continuous I2C reads from the temperature sensor using DMA
HAL_I2C_Mem_Read_DMA(&hi2c1,
LM75A_ADDRESS << 1,
LM75A_TEMP_REG,
I2C_MEMADD_SIZE_8BIT,
(uint8_t*)temp_buffer.buffer,
BUFFER_SIZE);
}
I'm completing the partial thought by finishing the DMA configuration with memory increment enabled, circular mode, and interrupt setup. The circular buffer functions manage read/write indices with wraparound logic. The I2C DMA read initiates continuous transfers from the LM75A sensor into the buffer. *temp = LM75A_RawToTemp(raw_value);
return 1; // Data available
}
return 0; // No data
}
// ============================================================================
// MAIN APPLICATION EXAMPLE
// ============================================================================
int main(void)
{
HAL_Init();
SystemClock_Config();
// Initialize I2C with DMA
I2C_DMA_Init();
// Start continuous DMA reads
I2C_DMA_Start_Continuous_Read();
while (1) {
float temperature;
// Non-blocking temperature read
if (Get_Temperature_Reading(&temperature)) {
// Process temperature data
// e.g., log to UART, display, store, etc.
}
// Check
The temperature retrieval function checks if data exists in the circular buffer, converts the raw sensor value to Celsius using the LM75A conversion formula, and returns success or failure. The main loop initializes the I2C peripheral with DMA support, starts continuous background reads from the sensor, then polls for new temperature measurements in a non-blocking manner.
STM32 I2C Temperature Sensor with DMA Circular Buffer
Here's a comprehensive solution using DMA circular buffering with interrupt-driven temperature sensor reads:
Complete Implementation
// temperature_sensor_dma.h
#ifndef TEMPERATURE_SENSOR_DMA_H
#define TEMPERATURE_SENSOR_DMA_H
#include "stm32f4xx_hal.h"
#include <stdint.h>
#include <stddef.h>
// ============================================================================
// CONFIGURATION
// ============================================================================
#define BUFFER_SIZE 64 // Must be power of 2
#define HALF_BUFFER_SIZE 32 // Interrupt triggers at half-full
#define I2C_ADDRESS 0x48 // LM75A default address
#define I2C_TEMP_REG 0x00 // Temperature register
#define SENSOR_READ_INTERVAL_MS 100 // How often to trigger I2C read
// ============================================================================
// CIRCULAR BUFFER STRUCTURE
// ============================================================================
typedef struct {
uint8_t buffer[BUFFER_SIZE];
volatile uint32_t write_index;
volatile uint32_t read_index;
volatile uint32_t half_full_events;
} CircularBuffer_t;
// ============================================================================
// SENSOR DATA STRUCTURE
// ============================================================================
typedef struct {
float temperature;
int32_t raw_value;
uint32_t timestamp;
uint8_t is_valid;
} TemperatureReading_t;
// ============================================================================
// FUNCTION PROTOTYPES
// ============================================================================
// Initialization
void Sensor_DMA_Init(void);
void Sensor_Start_Reading(void);
// Non-blocking operations
uint8_t Sensor_Get_Reading(TemperatureReading_t *reading);
uint32_t Sensor_Get_Buffer_Level(void);
uint8_t Sensor_Has_Data(void);
// Utility
float Convert_Raw_To_Celsius(uint16_t raw_value);
#endif // TEMPERATURE_SENSOR_DMA_H
// temperature_sensor_dma.c
#include "temperature_sensor_dma.h"
#include <string.h>
// ============================================================================
// GLOBAL VARIABLES
// ============================================================================
static I2C_HandleTypeDef hi2c1;
static DMA_HandleTypeDef hdma_i2c_rx;
static CircularBuffer_t sensor_buffer = {0};
static volatile uint32_t dma_half_full_flag = 0;
static volatile uint32_t dma_full_flag = 0;
static TIM_HandleTypeDef htim2; // Timer for periodic reads
// ============================================================================
// CIRCULAR BUFFER FUNCTIONS
// ============================================================================
static void CircularBuffer_Init(CircularBuffer_t *buffer)
{
memset(buffer, 0, sizeof(CircularBuffer_t));
}
static void CircularBuffer_Write(CircularBuffer_t *buffer,
const uint8_t *data,
uint32_t length)
{
for (uint32_t i = 0; i < length; i++) {
buffer->buffer[buffer->write_index] = data[i];
buffer->write_index = (buffer->write_index + 1) & (BUFFER_SIZE - 1);
}
}
static uint32_t CircularBuffer_Read_Bytes(CircularBuffer_t *buffer,
uint8_t *data,
uint32_t length)
{
uint32_t bytes_read = 0;
while (bytes_read < length && buffer->read_index != buffer->write_index) {
data[bytes_read] = buffer->buffer[buffer->read_index];
buffer->read_index = (buffer->read_index + 1) & (BUFFER_SIZE - 1);
bytes_read++;
}
return bytes_read;
}
static uint32_t CircularBuffer_Available(CircularBuffer_t *buffer)
{
return (buffer->write_index - buffer->read_index) & (BUFFER_SIZE - 1);
}
// ============================================================================
// GPIO CONFIGURATION
// ============================================================================
static void I2C_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIO and I2C clocks
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_ENABLE();
// PB8 = SCL, PB9 = SDA
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
// ============================================================================
// I2C INITIALIZATION
// ============================================================================
static void I2C_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
Error_Handler();
}
// Enable I2C interrupts
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_SetPriority(I2C1_ER_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
}
// ============================================================================
// DMA INITIALIZATION
// ============================================================================
static void DMA_Init(void)
{
// Enable DMA clock
__HAL_RCC_DMA1_CLK_ENABLE();
// Configure DMA for I2C receive (check datasheet for correct stream)
// STM32F4: I2C1_RX uses DMA1_Stream0, Channel_1
hdma_i2c_rx.Instance = DMA1_Stream0;
hdma_i2c_rx.Init.Channel = DMA_CHANNEL_1;
hdma_i2c_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2c_rx.Init.PeriphInc = DMA_PERIPH_INC_DISABLE;
hdma_i2c_rx.Init.MemInc = DMA_MEMORY_INC_ENABLE;
hdma_i2c_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c_rx.Init.Mode = DMA_CIRCULAR; // *** CIRCULAR MODE ***
hdma_i2c_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_i2c_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_i2c_rx) != HAL_OK) {
Error_Handler();
}
// Link DMA to I2C peripheral
__HAL_LINKDMA(&hi2c1, hdmarx, hdma_i2c_rx);
// Enable DMA interrupts for half-transfer and complete
__HAL_DMA_ENABLE_IT(&hdma_i2c_rx, DMA_IT_HT | DMA_IT_TC);
// Configure NVIC for DMA interrupt
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
}
// ============================================================================
// TIMER INITIALIZATION (for periodic I2C reads)
// ============================================================================
static void Timer_Init(void)
{
__HAL_RCC_TIM2_CLK_ENABLE();
htim2.Instance = TIM2;
htim2.Init.Prescaler = 83; // 84MHz / 84 = 1MHz
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = SENSOR_READ_INTERVAL_MS * 1000 - 1; // 100ms
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {
Error_Handler();
}
HAL_NVIC_SetPriority(TIM2_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
// ============================================================================
// DMA INTERRUPT HANDLER - HALF BUFFER FULL
// ============================================================================
void DMA1_Stream0_IRQHandler(void)
{
// Check for half-transfer interrupt (buffer is half full)
if (__HAL_DMA_GET_FLAG(&hdma_i2c_rx, DMA_FLAG_HTIF0)) {
__HAL_DMA_CLEAR_FLAG(&hdma_i2c_rx, DMA_FLAG_HTIF0);
dma_half_full_flag = 1;
sensor_buffer.half_full_events++;
// *** INTERRUPT TRIGGERED AT HALF BUFFER ***
// First half of buffer is now ready to read
// Read from: 0 to HALF_BUFFER_SIZE
}
// Check for transfer complete interrupt (circular wrap-around)
if (__HAL_DMA_GET_FLAG(&hdma_i2c_rx, DMA_FLAG_TCIF0)) {
__HAL_DMA_CLEAR_FLAG(&hdma_i2c_rx, DMA_FLAG_TCIF0);
dma_full_flag = 1;
// *** INTERRUPT TRIGGERED AT FULL BUFFER ***
// Second half of buffer is now ready to read
// Read from: HALF_BUFFER_SIZE to BUFFER_SIZE
}
}
// ============================================================================
// TIMER INTERRUPT - TRIGGER I2C READ
// ============================================================================
void TIM2_IRQHandler(void)
{
if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_UPDATE)) {
__HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE);
// Trigger non-blocking I2C read
// This will transfer to the circular DMA buffer
HAL_I2C_Mem_Read_DMA(&hi2c1,
(I2C_ADDRESS << 1),
I2C_TEMP_REG,
I2C_ADDRESSINGMODE_7BIT,
sensor_buffer.buffer,
BUFFER_SIZE);
}
}
// ============================================================================
// I2C INTERRUPT HANDLERS
// ============================================================================
void I2C1_EV_IRQHandler(void)
{
HAL_I2C_EV_IRQHandler(&hi2c1);
}
void I2C1_ER_IRQHandler(void)
{
HAL_I2C_ER_IRQHandler(&hi2c1);
}
// ============================================================================
// HAL CALLBACKS
// ============================================================================
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
// Optional: Additional processing after full DMA transfer
if (hi2c->Instance == I2C1) {
// Restart DMA if needed (depends on your use case)
}
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
if (hi2c->Instance == I2C1) {
// Stop DMA on error
HAL_I2C_Abort_IT(&hi2c1);
Error_Handler();
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) {
// Timer triggers I2C read (handled in TIM2_IRQHandler)
}
}
// ============================================================================
// PUBLIC API FUNCTIONS
// ============================================================================
void Sensor_DMA_Init(void)
{
CircularBuffer_Init(&sensor_buffer);
I2C_GPIO_Init();
I2C_Init();
DMA_Init();
Timer_Init();
}
void Sensor_Start_Reading(void)
{
// Start periodic timer to trigger I2C reads
HAL_TIM_Base_Start_IT(&htim2);
// Start initial DMA read
HAL_I2C_Mem_Read_DMA(&hi2c1,
(I2C_ADDRESS << 1),
I2C_TEMP_REG,
I2C_ADDRESSINGMODE_7BIT,
sensor_buffer.buffer,
BUFFER_SIZE);
}
uint8_t Sensor_Get_Reading(TemperatureReading_t *reading)
{
uint8_t raw_bytes[2];
uint32_t bytes_read = CircularBuffer_Read_Bytes(&sensor_buffer,
raw_bytes,
2);
if (bytes_read == 2) {
// Combine MSB and LSB (LM75A format)
uint16_t raw_value = ((uint16_t)raw_bytes[0] << 8) | raw_bytes[1];
reading->raw_value = (int16_t)(raw_value >> 8);
reading->temperature = Convert_Raw_To_Celsius(raw_value);
reading->is_valid = 1;
// Timestamp (if you have a system time available)
reading->timestamp = HAL_GetTick();
return 1; // Data available
}
reading->is_valid = 0;
return 0; // No data available
}
uint32_t Sensor_Get_Buffer_Level(void)
{
return CircularBuffer_Available(&sensor_buffer);
}
uint8_t Sensor_Has_Data(void)
{
return CircularBuffer_Available(&sensor_buffer) >= 2;
}
float Convert_Raw_To_Celsius(uint16_t raw_value)
{
// LM75A: Bits 15-7 contain temperature, bit 0.5, 0.25, 0.125
// Typical: upper 8 bits = integer, lower bits = fraction
int16_t temp_int = (int16_t)(raw_value >> 8);
int16_t temp_frac = (raw_value >> 4) & 0x0F;
// Each LSB of fraction part = 0.0625°C
float temp = (float)temp_int + ((float)temp_frac * 0.0625f);
return temp;
}
// ============================================================================
// ERROR HANDLER
// ============================================================================
void Error_Handler(void)
{
while (1) {
// Disable interrupts
__disable_irq();
// Toggle LED for debugging
// HAL_GPIO_TogglePin(ERROR_LED_GPIO, ERROR_LED_PIN);
// Hang here for debugging
}
}
Usage Example
// main.c
#include "stm32f4xx_hal.h"
#include "temperature_sensor_dma.h"
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Initialize sensor with DMA
Sensor_DMA_Init();
Sensor_Start_Reading();
// Main loop - completely non-blocking
while (1) {
TemperatureReading_t reading;
// Check if temperature data is available
if (Sensor_Get_Reading(&reading)) {
// Process temperature reading
// Example: Send over UART, store in array, etc.
if (reading.is_valid) {
// Use reading.temperature (in Celsius)
// Use reading.raw_value (raw ADC value)
// Use reading.timestamp (when read)
}
}
// Check if DMA half-full interrupt was triggered
if (dma_half_full_flag) {
// Buffer is half full - can do batch processing here
// uint32_t buffer_level = Sensor_Get_Buffer_Level();
dma_half_full_flag = 0;
}
// Other application code here - NO BLOCKING DELAYS
// Your main application logic continues uninterrupted
}
return 0;
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}
Key Features
| Feature | Implementation |
|---|
| Non-Blocking I/O | DMA + interrupts handle all I/C transfers |
| Circular Buffer | 64-byte buffer with HALF_BUFFER_SIZE interrupt |
| Half-Full Interrupt | DMA_FLAG_HTIF triggers at 32 bytes |
| No HAL_Delay() | Timer-based periodic reads instead |
| Background Operation | Sensor reads while main loop continues |
| Power Efficient | CPU not polled; woken only by interrupts |
Critical Notes
- Verify DMA Stream/Channel - Check your STM32 datasheet for correct I2C_RX DMA stream
- Sensor Address - LM75A default is 0x48 (configurable via pins)
- Data Format - LM75A returns 16-bit:
[TEMP(8 bits)] [0.5°C, 0.25°C, 0.125°C, 0, 0, 0, 0, 0]
- Buffer Size - Must be power of 2 for efficient circular indexing
- Timing - Adjust
SENSOR_READ_INTERVAL_MS based on application requirements