Recently, I designed a board that uses I2C, so I decided to test whether the hardware I2C is as bad as some people say.

#include "at24c64.h"
#include "i2c.h"
#define AT24CXX_ADDR_READ 0xA1
#define AT24CXX_ADDR_WRITE 0xA0
#define PAGE_SIZE 32
/** * @brief Read multiple bytes of data continuously from any address of AT24C64 * @param addr —— Address to read data (0-65535) * @param dat —— Address to store read data * @retval Success —— HAL_OK*/
uint8_t At24cxx_Read_Amount_Byte(uint16_t addr, uint8_t* recv_buf, uint16_t size){
return HAL_I2C_Mem_Read(&hi2c2, AT24CXX_ADDR_READ, addr, I2C_MEMADD_SIZE_16BIT, recv_buf, size, 0xFFFFFFFF);
}
/** * @brief Write multiple bytes of data continuously to any address of AT24C64 * @param addr —— Address to write data (0-65535) * @param dat —— Address to store written data * @retval Success —— HAL_OK*/
uint8_t At24cxx_Write_Amount_Byte(uint16_t addr, uint8_t* dat, uint16_t size){
uint8_t i = 0;
uint16_t cnt = 0; // Count of written bytes
/* For the starting address, there are two cases to consider */
if(0 == addr % PAGE_SIZE ) {
/* The starting address is exactly the beginning of a page */
/* For the number of bytes to be written, there are two cases to consider */
if(size <= PAGE_SIZE) {
// If the number of bytes to be written does not exceed one page, write directly
return HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, dat, size, 0xFFFFFFFF);
}
else {
// If the number of bytes to be written exceeds one page, write the entire page in a loop
for(i = 0;i < size/PAGE_SIZE; i++) {
HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, &dat[cnt], PAGE_SIZE, 0xFFFFFFFF);
HAL_Delay(3);
addr += PAGE_SIZE;
cnt += PAGE_SIZE;
}
// Write the remaining bytes
return HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, &dat[cnt], size - cnt, 0xFFFFFFFF);
}
}
else {
/* The starting address is offset from the beginning of a page */
/* For the number of bytes to be written, there are two cases to consider */
if(size <= (PAGE_SIZE - addr%PAGE_SIZE)) {
/* Can write all within this page */
return HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, dat, size, 0xFFFFFFFF);
}
else {
/* Cannot finish writing in this page */
// First, finish writing this page
cnt += PAGE_SIZE - addr%PAGE_SIZE;
HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, dat, cnt, 0xFFFFFFFF);
addr += cnt;
HAL_Delay(3);
// Loop to write full page data
for(i = 0;i < (size - cnt)/PAGE_SIZE; i++) {
HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, &dat[cnt], PAGE_SIZE, 0xFFFFFFFF);
HAL_Delay(3);
addr += PAGE_SIZE;
cnt += PAGE_SIZE;
}
// Write the remaining bytes
return HAL_I2C_Mem_Write(&hi2c2, AT24CXX_ADDR_WRITE, addr, I2C_MEMADD_SIZE_16BIT, &dat[cnt], size - cnt, 0xFFFFFFFF);
} }
}
-
Super valuable content: The most comprehensive I2C tutorial, absolutely worth saving! (A lengthy article, recommended for collection)
-
Understanding the I2C bus
-End-
“If useful, please share”
Copyright statement: This article is reproduced from the internet, and the copyright belongs to the original author. If there is any infringement, please contact us for removal!
Click below“Hardware Engineer”to follow, choose“Pin/Star the public account”
Technical electronic content delivered to you first-hand
👇

-
What is inside an IGBT module?
-
An engineer’s hardcore microcontroller programming philosophy
-
Several interesting circuits
-
Replacing STM32 with Huada HC32, these details must be seen!
-
USB external power supply and lithium battery automatic switching circuit design, have you learned these essentials?
-
Common peripheral circuit design, hardware circuit design references and precautions
