6#define I2C_TIMEOUT_MS 10u
7#define FLAG_DMA_DONE 0x0100u
8#define FLAG_DMA_ERR 0x0200u
10#define LOCK_TIMEOUT_MS 5u
23 static const osMutexAttr_t a = { .name =
"i2c1", .attr_bits = osMutexPrioInherit };
30 if (!
s_lock || osKernelGetState() != osKernelRunning)
return 0;
36 if (
s_lock && osKernelGetState() == osKernelRunning) (void)osMutexRelease(
s_lock);
41static int32_t
wr(
void *h, uint8_t reg,
const uint8_t *d, uint16_t len)
45 const int32_t r = HAL_I2C_Mem_Write(
s_hi2c,
s_addr8, reg, I2C_MEMADD_SIZE_8BIT,
51static int32_t
rd(
void *h, uint8_t reg, uint8_t *d, uint16_t len)
55 const int32_t r = HAL_I2C_Mem_Read(
s_hi2c,
s_addr8, reg, I2C_MEMADD_SIZE_8BIT,
63 if (osKernelGetState() == osKernelRunning) osDelay(
ms);
else HAL_Delay(
ms);
69 s_addr8 = (uint16_t)(addr7 << 1);
81 if (HAL_I2C_Mem_Read_DMA(
s_hi2c,
s_addr8, reg, I2C_MEMADD_SIZE_8BIT, buf, len) != HAL_OK) {
void imu_i2c_make_bus(sensor_bus_t *bus, I2C_HandleTypeDef *hi2c, uint8_t addr7)
void imu_i2c_error_isr(I2C_HandleTypeDef *hi2c)
void imu_i2c_rx_cplt_isr(I2C_HandleTypeDef *hi2c)
To be called from HAL_I2C_MemRxCpltCallback / HAL_I2C_ErrorCallback.
static int32_t wr(void *h, uint8_t reg, const uint8_t *d, uint16_t len)
int32_t imu_i2c_read_dma(uint8_t reg, uint8_t *buf, uint16_t len, uint32_t timeout_ms)
Starts the DMA read and blocks the calling task until completion.
static I2C_HandleTypeDef * s_hi2c
int imu_i2c_lock(void)
0 = taken (or no kernel yet), -1 = timeout
static void dly(uint32_t ms)
static osMutexId_t s_lock
void imu_i2c_unlock(void)
static osThreadId_t s_waiter
void imu_i2c_bus_init(I2C_HandleTypeDef *hi2c)
Creates the bus lock: call once before starting the tasks that use the bus.
static int32_t rd(void *h, uint8_t reg, uint8_t *d, uint16_t len)
I2C_HandleTypeDef * imu_i2c_handle(void)
STM32 HAL (STM32WB55) binding of the sensor_bus_t interface + DMA burst read of the FIFO with task no...
bus_delay_fn delay_ms
optional (used only in init/reset)
void * handle
opaque context passed to read/write