I2C_STM32F7x.c

I²C example for STM32F7xx MCUs

Author
Michael Fitzmayer
// SPDX-License-Identifier: Beerware
#include <stdint.h>
#include "STBrickLib.h"
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_i2c.h"
I2C_HandleTypeDef hi2c1;
StatusCode I2C_Receive(uint16_t u16DevAddress, uint16_t u16MemAddress, uint8_t* pu8RxBuffer, uint16_t u16Size)
{
if (HAL_OK != HAL_I2C_Mem_Read(
&hi2c1,
u16DevAddress,
u16MemAddress,
I2C_MEMADD_SIZE_16BIT,
pu8RxBuffer,
u16Size,
300))
{
return STATUS_ERROR;
}
return STATUS_OK;
}
StatusCode I2C_Transmit(uint16_t u16DevAddress, uint16_t u16MemAddress, uint8_t* pu8TxBuffer, uint16_t u16Size)
{
if (HAL_OK != HAL_I2C_Mem_Write(
&hi2c1,
u16DevAddress,
u16MemAddress,
I2C_MEMADD_SIZE_16BIT,
pu8TxBuffer,
u16Size,
300))
{
return STATUS_ERROR;
}
return STATUS_OK;
}
void I2C_WaitUntilReady(uint16_t u16DevAddress)
{
while (HAL_I2C_STATE_READY != HAL_I2C_GetState(&hi2c1));
while (HAL_TIMEOUT == HAL_I2C_IsDeviceReady(
&hi2c1,
u16DevAddress,
100,
300));
while (HAL_I2C_STATE_READY != HAL_I2C_GetState(&hi2c1));
}
A growing collection of hardware drivers designed for STM32 microcontrollers.
__weak StatusCode I2C_Receive(uint16_t u16DevAddress, uint16_t u16MemAddress, uint8_t *pu8RxBuffer, uint16_t u16Size)
Receive an amount via I²C.
Definition: STBrickLib.c:27
__weak void I2C_WaitUntilReady(uint16_t u16DevAddress)
Wait for the end of the transfer.
Definition: STBrickLib.c:68
__weak StatusCode I2C_Transmit(uint16_t u16DevAddress, uint16_t u16MemAddress, uint8_t *pu8TxBuffer, uint16_t u16Size)
Transmit an amount via I²C.
Definition: STBrickLib.c:49
StatusCode
Status codes.
Definition: STBrickLib.h:20