在线时间830 小时
UID3067179
注册时间2014-10-23
NXP金币387
TA的每日心情 | 开心 5 天前 |
---|
签到天数: 1384 天 连续签到: 1 天 [LV.10]以坛为家III
版主
  
- 积分
- 7958
- 最后登录
- 2025-2-14
|
本帖最后由 TLLED 于 2024-11-14 22:59 编辑
测试I2C读写EEPROM存储器AT24C02。
一、硬件部分
1.1、测试使用扩展板EEPROM部分电路
1.2、连接到开发板的对应的接口
1.3、硬件连接图
二、程序部分
2.1、配置I2C1端口
PTE0和PTE1对应的接口是I2C1,在软件中配置I2C1接口。
2.2、at24cxx.c
- #include "pin_mux.h"
- #include "fsl_debug_console.h"
- #include "fsl_i2c.h"
- #include "i2c/bsp_i2c.h"
- #include "i2c/at24cxx.h"
- #include "systick/systick.h"
- #define I2C_PAGE_SIZE 8
- #define I2C_AT24_BASEADDR I2C1
- #define I2C_AT24_CLOCK_FREQ CLOCK_GetFreq(I2C1_CLK_SRC)
- const uint8_t i2c_at24cxx_address = (0xA0>>1);
- at24cxx_config_t atconfig = {0};
- at24cxx_handle_t at24Handle = {0};
- status_t AT24_ReadReg(at24cxx_handle_t *handle, uint8_t reg, uint8_t *val, uint32_t bytesNumber)
- {
- assert(handle != NULL);
- assert(val != NULL);
- if ((handle->I2C_ReceiveFunc) == NULL)
- {
- return kStatus_Fail;
- }
- else
- {
- return handle->I2C_ReceiveFunc(handle->slaveAddress, reg, 1, val, bytesNumber);
- }
- }
- status_t AT24_WriteReg(at24cxx_handle_t *handle, uint8_t addr, uint8_t *val, uint32_t len)
- {
- assert(handle != NULL);
- if ((handle->I2C_SendFunc) == NULL)
- {
- return kStatus_Fail;
- }
- else
- {
- return handle->I2C_SendFunc(handle->slaveAddress, addr, 1, val, len);
- }
- }
- status_t AT24_Init(at24cxx_handle_t *at24_handle, at24cxx_config_t *config)
- {
- assert(at24_handle != NULL);
- assert(config != NULL);
- assert(config->I2C_SendFunc != NULL);
- assert(config->I2C_ReceiveFunc != NULL);
- uint8_t tmp[1] = {0};
- /* Initialize the I2C access function. */
- at24_handle->I2C_SendFunc = config->I2C_SendFunc;
- at24_handle->I2C_ReceiveFunc = config->I2C_ReceiveFunc;
- /* Set Slave Address. */
- at24_handle->slaveAddress = config->slaveAddress;
-
- return kStatus_Success;
- }
- status_t i2c_at24cxx_send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *txBuff, uint32_t txBuffSize)
- {
- return I2C_Send(I2C_AT24_BASEADDR, deviceAddress, subAddress, subaddressSize, txBuff, txBuffSize);
- }
- status_t i2c_at24cxx_receive(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint32_t rxBuffSize)
- {
- return I2C_Receive(I2C_AT24_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
- }
- int at24cxx_read(uint32_t addr, uint8_t *buf, uint32_t len)
- {
- return AT24_ReadReg(&at24Handle, addr, buf, len);
- }
- int at24cxx_write_page(uint32_t addr, uint8_t *buf, uint32_t len)
- {
- return AT24_WriteReg(&at24Handle, addr, buf, len);
- }
- void at24cxx_write(uint32_t write_address, uint8_t *p_buffer, uint32_t number_of_byte)
- {
- uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
- address = write_address % I2C_PAGE_SIZE;
- count = I2C_PAGE_SIZE - address;
- number_of_page = number_of_byte / I2C_PAGE_SIZE;
- number_of_single = number_of_byte % I2C_PAGE_SIZE;
-
- /* if write_address is I2C_PAGE_SIZE aligned */
- if(0 == address)
- {
- while(number_of_page--)
- {
- at24cxx_write_page(write_address, p_buffer, I2C_PAGE_SIZE);
- SysTick_Delay_ms(10);
- write_address += I2C_PAGE_SIZE;
- p_buffer += I2C_PAGE_SIZE;
- }
- if(0 != number_of_single)
- {
- at24cxx_write_page(write_address, p_buffer, number_of_single);
- SysTick_Delay_ms(10);
- }
- }
- else
- {
- /* if write_address is not I2C_PAGE_SIZE aligned */
- if(number_of_byte < count)
- {
- at24cxx_write_page(write_address, p_buffer, number_of_byte);
- SysTick_Delay_ms(10);
- }
- else
- {
- number_of_byte -= count;
- number_of_page = number_of_byte / I2C_PAGE_SIZE;
- number_of_single = number_of_byte % I2C_PAGE_SIZE;
- if(0 != count)
- {
- at24cxx_write_page(write_address, p_buffer, count);
- SysTick_Delay_ms(10);
- write_address += count;
- p_buffer += count;
- }
- /* write page */
- while(number_of_page--)
- {
- at24cxx_write_page(write_address, p_buffer, I2C_PAGE_SIZE);
- SysTick_Delay_ms(10);
- write_address += I2C_PAGE_SIZE;
- p_buffer += I2C_PAGE_SIZE;
- }
- /* write single */
- if(0 != number_of_single)
- {
- at24cxx_write_page(write_address, p_buffer, number_of_single);
- SysTick_Delay_ms(10);
- }
- }
- }
- }
- void test_at24cxx(void)
- {
- uint16_t i=0;
- uint8_t addr=0;
- uint8_t rxbuf[256];
- uint8_t txbuf[256];
-
- PRINTF("\r\neeprom test!\r\n");
-
- for(i=0;i<256;i++)
- {
- txbuf[i]=i;
- }
- //write
- at24cxx_write(0x00, txbuf,256);
-
- PRINTF("\r\n write! txbuf:\r\n");
- for(i=0;i<256;i++)
- {
- PRINTF("%d ",txbuf[i]);
- }
- PRINTF("\r\n");
-
- SysTick_Delay_ms(10);
- //read
- if(at24cxx_read(0x00, rxbuf, 256)!= kStatus_Success)
- {
- PRINTF("\r\n read failed!\r\n");
- }
- else
- {
- PRINTF("\r\nread ok! rxbuf:\r\n");
- for(i=0;i<256;i++)
- {
- PRINTF("%d ",rxbuf[i]);
- }
- PRINTF("\r\n");
- }
- }
- int init_at24cxx(void)
- {
- uint8_t i=0;
- status_t result;
-
- BOARD_I2C1_ReleaseBus();
- init_i2c1_pins();
- I2C_Init(I2C_AT24_BASEADDR, I2C_AT24_CLOCK_FREQ);
-
- /* Configure the I2C function */
- atconfig.I2C_SendFunc = i2c_at24cxx_send;
- atconfig.I2C_ReceiveFunc = i2c_at24cxx_receive;
- atconfig.slaveAddress = i2c_at24cxx_address;
-
- /* Initialize sensor devices */
- result = AT24_Init(&at24Handle, &atconfig);
- return result;
- }
复制代码
2.3、at24cxx.h
- #ifndef __24CXX_H__
- #define __24CXX_H__
- #include <stdint.h>
- #include "fsl_common.h"
- /*! @brief Define I2C access function. */
- typedef status_t (*I2C_SendFunc_t)(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *txBuff, uint32_t txBuffSize);
- typedef status_t (*I2C_ReceiveFunc_t)(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint32_t rxBuffSize);
- typedef struct _at24cxx_handle
- {
- /* Pointer to the user-defined I2C Send Data function. */
- I2C_SendFunc_t I2C_SendFunc;
- /* Pointer to the user-defined I2C Receive Data function. */
- I2C_ReceiveFunc_t I2C_ReceiveFunc;
- /* The I2C slave address . */
- uint8_t slaveAddress;
- } at24cxx_handle_t;
- /*! @brief at24cxx configure structure.*/
- typedef struct _at24cxx_config
- {
- /* Pointer to the user-defined I2C Send Data function. */
- I2C_SendFunc_t I2C_SendFunc;
- /* Pointer to the user-defined I2C Receive Data function. */
- I2C_ReceiveFunc_t I2C_ReceiveFunc;
- /* The I2C slave address. */
- uint8_t slaveAddress;
- } at24cxx_config_t;
- //API funtctions
- int init_at24cxx(void);
- void test_at24cxx(void);
- int at24cxx_read(uint32_t addr, uint8_t *buf, uint32_t len);
- void at24cxx_write(uint32_t write_address, uint8_t *p_buffer, uint32_t number_of_byte);
- #endif
复制代码
2.4、main.c
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "board.h"
- #include "fsl_debug_console.h"
- #include "fsl_gpio.h"
- #include "slcd/slcd.h"
- #include "led/led.h"
- #include "systick/systick.h"
- #include "slcd_engine.h"
- #include "adc/adc.h"
- #include "i2c/eeprom.h"
- #include "i2c/fxls8974.h"
- #include "i2c/at24cxx.h"
- #include "fsl_uart.h"
- #include "stdio.h"
- void init_board(void)
- {
-
- SysTick_Init();
- BOARD_InitPins();
- BOARD_BootClockRUN();
- BOARD_InitDebugConsole();
- init_at24cxx();
- }
- int main(void)
- {
- uint32_t m_adv=0;
- float vl= 0.0f;
-
- init_board();
- while (1)
- {
- test_at24cxx();
- SysTick_Delay_ms(1000);
- }
- }
复制代码
三、运行结果
下载程序,串口打印读写测试数据
|
|