介绍使用硬件IIC接口读写AT24C02,STM32自带硬件IIC,比较好用,没必要千篇一律的使用模拟IIC。作为一个IIC的使用例子,可以适当修改用于其他IIC接口设备通信控制。
一、测试环境
STM32F407+CubeMx(6.1.1)+MDK(5.34)+AT24C02
IIC Pin:PB8 PB9
CubeMx配置如下:
二、I2C驱动代码
CubeMx生成i2c.c i2c.h。读写使用HAL库的IIC操作接口:
/*** @brief Write an amount of data in blocking mode to a specific memory address* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains* the configuration information for the specified I2C.* @param DevAddress Target device address: The device 7 bits address value* in datasheet must be shifted to the left before calling the interface* @param MemAddress Internal memory address* @param MemAddSize Size of internal memory address* @param pData Pointer to data buffer* @param Size Amount of data to be sent* @param Timeout Timeout duration* @retval HAL status*/
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);/*** @brief Read an amount of data in blocking mode from a specific memory address* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains* the configuration information for the specified I2C.* @param DevAddress Target device address: The device 7 bits address value* in datasheet must be shifted to the left before calling the interface* @param MemAddress Internal memory address* @param MemAddSize Size of internal memory address* @param pData Pointer to data buffer* @param Size Amount of data to be sent* @param Timeout Timeout duration* @retval HAL status*/
HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
三、AT24C02驱动代码
头文件:at24c02.h
代码里边写了一个测试用例的宏开关,测试阶段打开AT24CXX_TEST_ENABLE,配置需要测试的起始地址AT24CXX_TEST_ADDR和测试读写长度AT24CXX_TEST_SIZE,调用AT24C02_Test()进行测试,返回值表示读写出错的数据个数。
例如:
OS_msgPrintf("AT24C02_Test err = %d\r\n",AT24C02_Test());
打印:AT24C02_Test err = 0
表示指定测试地址和长度读写成功。
/******************************************************************************** File Name : at24c02.h* Description : this code is used for at24c02 application* Author : JackWang* Date : 2019-05-07******************************************************************************
*/#ifndef __AT24C02_H
#define __AT24C02_H#ifdef __cplusplusextern "C" {
#endif/*! -------------------------------------------------------------------------- */
/*! Include headers */
#include <stdint.h>#define AT24CXX_TEST_ENABLE 0/*! -------------------------------------------------------------------------- */
/*! Public functions prototype */
int AT24C02_write(uint8_t addr, uint8_t* dataPtr, uint16_t dataSize);
int AT24C02_read (uint8_t addr, uint8_t* dataPtr, uint16_t dataSize);#if (1 == AT24CXX_TEST_ENABLE)
/*! -------------------------------------------------------------------------- */
/*! Public test functions prototype */
#define AT24CXX_TEST_ADDR 39
#define AT24CXX_TEST_BUFF_SIZE 255
#define AT24CXX_TEST_SIZE 90int AT24C02_Test(void);
#endif#ifdef __cplusplus
}
#endif#endif
/*! end of the file */
实现文件:at24c02.c
读写采用了扇区Page读写方式,可以给定任意有效地址和有效长度进行操作,方便应用层调用。
移植代码时注意头文件sysdelay.h换成自己的延时函数文件。主要是实现delayms。
/******************************************************************************** File Name : at24c02.c* Description : this code is used for at24c02 application* Author : JackWang* Date : 2019-05-07******************************************************************************
*//*! -------------------------------------------------------------------------- */
/*! Include headers */
#include "at24c02.h"
#include "sysdelay.h"
#include "i2c.h"/*! -------------------------------------------------------------------------- */
/*! Private macros define */
#define AT24CXX_Write_ADDR 0xA0
#define AT24CXX_Read_ADDR 0xA1
#define AT24CXX_MAX_SIZE 256
#define AT24CXX_PAGE_SIZE 8
#define AT24CXX_PAGE_TOTAL (AT24CXX_MAX_SIZE/AT24CXX_PAGE_SIZE)/*! -------------------------------------------------------------------------- */
/*! Public functions list */
int AT24C02_write(uint8_t addr, uint8_t* dataPtr, uint16_t dataSize);
int AT24C02_read (uint8_t addr, uint8_t* dataPtr, uint16_t dataSize);/*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int
AT24C02_write(uint8_t addr, uint8_t* dataPtr, uint16_t dataSize)
{if (0 == dataSize) { return -1; }int res = HAL_OK;int selectPage_idx = addr % AT24CXX_PAGE_SIZE;int selectPage_rest = AT24CXX_PAGE_SIZE - selectPage_idx;if (dataSize <= selectPage_rest) {res = HAL_I2C_Mem_Write(&hi2c1, AT24CXX_Write_ADDR, addr,I2C_MEMADD_SIZE_8BIT, dataPtr,dataSize,0xFF);if (HAL_OK != res) { return -1; }sysDelay_ms(10);} else {/*! 1 write selectPage rest*/res = HAL_I2C_Mem_Write(&hi2c1, AT24CXX_Write_ADDR, addr,I2C_MEMADD_SIZE_8BIT, dataPtr,selectPage_rest,0xFF);if (HAL_OK != res) { return -1; }addr += selectPage_rest;dataSize -= selectPage_rest;dataPtr += selectPage_rest;sysDelay_ms(5);/*! 2 write nextPage full */int fullPage = dataSize/AT24CXX_PAGE_SIZE;for (int iPage = 0; iPage < fullPage; ++iPage) {res = HAL_I2C_Mem_Write(&hi2c1, AT24CXX_Write_ADDR, addr,I2C_MEMADD_SIZE_8BIT, dataPtr,AT24CXX_PAGE_SIZE,0xFF);if (HAL_OK != res) { return -1; }sysDelay_ms(5);addr += AT24CXX_PAGE_SIZE;dataSize -= AT24CXX_PAGE_SIZE;dataPtr += AT24CXX_PAGE_SIZE;}/*! 3 write rest */if (0 != dataSize) {res = HAL_I2C_Mem_Write(&hi2c1, AT24CXX_Write_ADDR, addr,I2C_MEMADD_SIZE_8BIT, dataPtr,dataSize,0xFF);if (HAL_OK != res) { return -1; }sysDelay_ms(5);}}return 0;
}/*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int
AT24C02_read(uint8_t addr, uint8_t* dataPtr, uint16_t dataSize)
{int res = HAL_I2C_Mem_Read(&hi2c1,AT24CXX_Read_ADDR,addr,I2C_MEMADD_SIZE_8BIT,dataPtr,dataSize,0xFF);if (HAL_OK != res) { return -1; }return 0;
}#if (1 == AT24CXX_TEST_ENABLE)
/*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#include <string.h>
static uint8_t AT24CXX_testBuff[AT24CXX_TEST_BUFF_SIZE];int
AT24C02_Test(void)
{ for (int idx = 0; idx < AT24CXX_TEST_BUFF_SIZE; ++idx) {AT24CXX_testBuff[idx] = idx;}AT24C02_write(AT24CXX_TEST_ADDR, AT24CXX_testBuff+AT24CXX_TEST_ADDR, AT24CXX_TEST_SIZE);memset(AT24CXX_testBuff, 0, AT24CXX_TEST_BUFF_SIZE);AT24C02_read(AT24CXX_TEST_ADDR, AT24CXX_testBuff, AT24CXX_TEST_SIZE); int err = 0;for (int idx = 0; idx < AT24CXX_TEST_SIZE; ++idx) {if ((AT24CXX_TEST_ADDR+idx) != AT24CXX_testBuff[idx]) err++;}return err;}
#endif
总体上代码还算简洁,供大家参考使用。