16#define DEVICE_TYPE_BASE 0x50U
19#define I2C_TRANSIENT_ERRORS (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_ARLO | HAL_I2C_ERROR_AF | \
20 HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_TIMEOUT)
30static bool isTransient(I2C_HandleTypeDef *hi2c, HAL_StatusTypeDef status)
32 if (status == HAL_BUSY || status == HAL_TIMEOUT) {
35 if (status == HAL_ERROR) {
36 return (HAL_I2C_GetError(hi2c) & I2C_TRANSIENT_ERRORS) != 0U;
60 for (attempt = 1U; attempt <= dev->
maxAttempts; attempt++) {
61 HAL_StatusTypeDef status;
64 status = HAL_I2C_Mem_Write(dev->
hi2c, dev->
deviceAddress, address, I2C_MEMADD_SIZE_16BIT,
67 status = HAL_I2C_Mem_Read(dev->
hi2c, dev->
deviceAddress, address, I2C_MEMADD_SIZE_16BIT,
71 if (status == HAL_OK) {
77 if (!isTransient(dev->
hi2c, status)) {
100 uint32_t start = HAL_GetTick();
101 HAL_StatusTypeDef status;
105 if (status == HAL_OK) {
133 HAL_StatusTypeDef status;
140 dev->
deviceAddress = (uint16_t)((DEVICE_TYPE_BASE | addressPins) << 1);
145 if (status != HAL_OK) {
168 if (dev == NULL || data == NULL || length == 0U ||
173 return transfer(dev,
false, memAddress, data, length);
194 if (dev == NULL || data == NULL || length == 0U ||
199 while (length > 0U) {
202 uint16_t chunk = (length < room) ? length : room;
204 memcpy(page, data, chunk);
206 if (transfer(dev,
true, memAddress, page, chunk) !=
AT24C32_OK ||
211 memAddress = (uint16_t)(memAddress + chunk);
213 length = (uint16_t)(length - chunk);
Types, constants and function prototypes of the STM32_AT24C32 driver.
#define AT24C32_MAX_ADDRESS_PINS
#define AT24C32_SIZE_BYTES
#define AT24C32_DEFAULT_MAX_ATTEMPTS
AT24C32_Status_t at24c32Init(AT24C32_t *dev, I2C_HandleTypeDef *hi2c, uint8_t addressPins, uint8_t maxAttempts)
Set up a driver handle and check that an AT24C32 answers on the bus.
AT24C32_Status_t at24c32Read(AT24C32_t *dev, uint16_t memAddress, uint8_t *data, uint16_t length)
Read bytes from the memory.
#define AT24C32_WRITE_TIMEOUT_MS
#define AT24C32_I2C_TIMEOUT_MS
#define AT24C32_PAGE_SIZE
AT24C32_Status_t at24c32Write(AT24C32_t *dev, uint16_t memAddress, const uint8_t *data, uint16_t length)
Write bytes to the memory.
HAL_StatusTypeDef lastStatus