【ACM32G103RCT6开发板测评】+ I2C之EEPROM读写测试
[复制链接]
测试下使用ACM32G103RCT6开发板的硬件I2C来读写AT24C02。
一、I2c
1.1、主要特性
>I2C 主设备功能;
>I2C 从设备功能:
- 可编程的 I2C 从设备地址;
- 可编程的 NACK/ACK 回复;
>速率支持 100K/400K/1M;
>支持 7bit 设备地址,支持多个从设备地址;
>支持从机拉时钟功能;
>支持 DMA 收发数据;
>支持主机仲裁模式;
1.2、结构框图
二、程序部分
2.1、i2c端口初始化部分
在acm3213_hal_msp.c
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
/* For Example */
GPIO_InitTypeDef GPIO_Handle;
/* I2C1 */
if (hi2c->Instance == I2C1)
{
/* Enable Clock */
__HAL_RCC_I2C1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* I2C1 SDA PB7 */
/* I2C1 SCL PB6 */
GPIO_Handle.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Drive = GPIO_DRIVE_LEVEL0;
GPIO_Handle.Alternate = GPIO_FUNCTION_5;
HAL_GPIO_Init(GPIOB, &GPIO_Handle);
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(I2C1_IRQn);
/* Enable External Interrupt */
NVIC_EnableIRQ(I2C1_IRQn);
// printfS("I2C PINs selected:\r\n");
// printfS("I2C SCL:PB6\r\n");
// printfS("I2C SDA:PB7\r\n\r\n");
}
/* I2C2 */
else if (hi2c->Instance == I2C2)
{
__HAL_RCC_I2C2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* I2C2 SCL PB10 */
/* I2C2 SDA PB11 */
GPIO_Handle.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Drive = GPIO_DRIVE_LEVEL0;
GPIO_Handle.Alternate = GPIO_FUNCTION_5;
HAL_GPIO_Init(GPIOB, &GPIO_Handle);
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(I2C2_IRQn);
/* Enable External Interrupt */
NVIC_EnableIRQ(I2C2_IRQn);
// printfS("I2C PINs selected:\r\n");
// printfS("I2C SCL:PB10\r\n");
// printfS("I2C SDA:PB11\r\n\r\n");
}
}
2.2、eeprom.c
#include "main.h"
static uint8_t IC_AT24_Writepage(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
return HAL_I2C_Mem_Write(hi2c, DevAddress, MemAddress, IC_AT24_MEMADD_SIZE, pData, Size,Timeout);
}
/************************************************************************
* function : IC_AT24_WriteBytes
* Description: IC_AT24 write bytes
* input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for I2C module
* DevAddress : Target device address
* MemAddress : MemAddress Internal memory address
* MemAddSize : MemAddSize Size of internal memory address
* pData : Pointer to data buffer
* Size : Amount of data to be sent
* return:
************************************************************************/
void IC_AT24_WriteBytes(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint32_t len;
len = (AT24_PAGE_SIZE - MemAddress % AT24_PAGE_SIZE);
if(len)
{
if(len > Size) len = Size;
IC_AT24_Writepage(hi2c, DevAddress, MemAddress, pData, len,Timeout);
Size -= len;
MemAddress += len;
pData += len;
}
while(Size > AT24_PAGE_SIZE)
{
IC_AT24_Writepage(hi2c, DevAddress, MemAddress, pData, AT24_PAGE_SIZE,Timeout);
Size -= AT24_PAGE_SIZE;
MemAddress += AT24_PAGE_SIZE;
pData += AT24_PAGE_SIZE;
}
if(Size)
{
IC_AT24_Writepage(hi2c, DevAddress, MemAddress, pData, Size,Timeout);
}
}
/************************************************************************
* function : IC_AT24_ReadBytes
* Description: IC_AT24 read bytes
* input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for I2C module
* DevAddress : Target device address
* MemAddress : MemAddress Internal memory address
* MemAddSize : MemAddSize Size of internal memory address
* pData : Pointer to data buffer
* Size : Amount of data to be sent
* return:
************************************************************************/
void IC_AT24_ReadBytes(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
HAL_I2C_Mem_Read(hi2c, DevAddress, MemAddress, IC_AT24_MEMADD_SIZE, pData, Size,Timeout);
}
#define AT24_HANDLE I2C_Handle
void eeprom_test(void)
{
uint8_t temp[255];
uint8_t i;
for(i=0;i<255;i++)
{
temp[i]=i;
}
printf("eeprom write data\r\n");
IC_AT24_WriteBytes(&AT24_HANDLE, AT24_DEV_ADDRESS, 0x02, temp, sizeof(temp),1000);
printf("eeprom txdat:\r\n");
for(i=0;i<255;i++)
{
printf(" %2x",temp[i]);
}
printf("\r\n");
memset(temp,0,sizeof(temp));
printf("eeprom read data\r\n");
IC_AT24_ReadBytes(&AT24_HANDLE, AT24_DEV_ADDRESS, 0x02, temp, sizeof(temp),1000);
printf("eeprom rxdat:\r\n");
for(i=0;i<255;i++)
{
printf(" %2x",temp[i]);
}
printf("\r\n");
}
2.3、eeprom.h
#ifndef __EEPROM_H
#define __EEPROM_H
#define AT24C02
//#define AT24LC256
#ifdef AT24C02
#define IC_AT24_MEMADD_SIZE I2C_MEMADD_SIZE_8BIT
#define AT24_PAGE_SIZE 8
#elif defined AT24LC256
#define IC_AT24_MEMADD_SIZE I2C_MEMADD_SIZE_16BIT
#define AT24_PAGE_SIZE 64
#endif
#define AT24_DEV_ADDRESS 0xA0
/************************************************************************
* function : IC_AT24_WriteBytes
* Description: IC_AT24 write bytes
* input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for I2C module
* DevAddress : Target device address
* MemAddress : MemAddress Internal memory address
* MemAddSize : MemAddSize Size of internal memory address
* pData : Pointer to data buffer
* Size : Amount of data to be sent
* return:
************************************************************************/
void IC_AT24_WriteBytes(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
/************************************************************************
* function : IC_AT24_ReadBytes
* Description: IC_AT24 read bytes
* input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for I2C module
* DevAddress : Target device address
* MemAddress : MemAddress Internal memory address
* MemAddSize : MemAddSize Size of internal memory address
* pData : Pointer to data buffer
* Size : Amount of data to be sent
* return:
************************************************************************/
void IC_AT24_ReadBytes(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
void eeprom_test(void);
#endif
2.4、main.c
#include "main.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
usart_init(115200);
printf("\r\n\r\n====== ACM32G103 MCU is runing ======\r\nSysCoreClk: %dHz, HCLK:%dHz\r\nPCLK1:%dHz, PCLK2:%dHz\r\n\r\n",g_SystemCoreClock, HAL_RCC_GetHCLKFreq(),HAL_RCC_GetPCLK1Freq(),HAL_RCC_GetPCLK2Freq());
init_led();
init_tim2();
init_key();
I2C_Init();
//adc_test();
eeprom_test();
while(1)
{
//HAL_Delay(500);
//led1_tog();
//printf("https://bbs.eeworld.com.cn/\r\n");
}
}
三、程序运行
下载程序到开发板后,复位开发板,串口输出读写的数据
|