请选择 进入手机版 | 继续访问电脑版
查看: 323|回复: 0

[分享] 【MCXC444分享】eeprom读写测试

[复制链接]
  • TA的每日心情
    开心
    5 天前
  • 签到天数: 1384 天

    连续签到: 1 天

    [LV.10]以坛为家III

    135

    主题

    2943

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7958
    最后登录
    2025-2-14
    发表于 2024-11-14 22:59:40 | 显示全部楼层 |阅读模式
    本帖最后由 TLLED 于 2024-11-14 22:59 编辑



    测试I2C读写EEPROM存储器AT24C02。

    一、硬件部分

    1.1、测试使用扩展板EEPROM部分电路
    001.png


    1.2、连接到开发板的对应的接口
    002.png


    1.3、硬件连接图
    eeprom.jpg

    二、程序部分

    2.1、配置I2C1端口
    PTE0和PTE1对应的接口是I2C1,在软件中配置I2C1接口。
    003.png

    2.2、at24cxx.c
    1. #include "pin_mux.h"
    2. #include "fsl_debug_console.h"
    3. #include "fsl_i2c.h"
    4. #include "i2c/bsp_i2c.h"
    5. #include "i2c/at24cxx.h"
    6. #include "systick/systick.h"

    7. #define I2C_PAGE_SIZE       8
    8. #define I2C_AT24_BASEADDR                           I2C1
    9. #define I2C_AT24_CLOCK_FREQ                         CLOCK_GetFreq(I2C1_CLK_SRC)

    10. const uint8_t i2c_at24cxx_address = (0xA0>>1);

    11. at24cxx_config_t atconfig        = {0};
    12. at24cxx_handle_t at24Handle    = {0};

    13. status_t AT24_ReadReg(at24cxx_handle_t *handle, uint8_t reg, uint8_t *val, uint32_t bytesNumber)
    14. {
    15.     assert(handle != NULL);
    16.     assert(val != NULL);

    17.     if ((handle->I2C_ReceiveFunc) == NULL)
    18.     {
    19.         return kStatus_Fail;
    20.     }
    21.     else
    22.     {
    23.         return handle->I2C_ReceiveFunc(handle->slaveAddress, reg, 1, val, bytesNumber);
    24.     }
    25. }

    26. status_t AT24_WriteReg(at24cxx_handle_t *handle, uint8_t addr, uint8_t *val, uint32_t len)
    27. {
    28.     assert(handle != NULL);

    29.     if ((handle->I2C_SendFunc) == NULL)
    30.     {
    31.         return kStatus_Fail;
    32.     }
    33.     else
    34.     {
    35.         return handle->I2C_SendFunc(handle->slaveAddress, addr, 1, val, len);
    36.     }
    37. }
    38. status_t AT24_Init(at24cxx_handle_t *at24_handle, at24cxx_config_t *config)
    39. {
    40.         assert(at24_handle != NULL);
    41.         assert(config != NULL);
    42.         assert(config->I2C_SendFunc != NULL);
    43.         assert(config->I2C_ReceiveFunc != NULL);

    44.         uint8_t tmp[1] = {0};

    45.         /* Initialize the I2C access function. */
    46.         at24_handle->I2C_SendFunc    = config->I2C_SendFunc;
    47.         at24_handle->I2C_ReceiveFunc = config->I2C_ReceiveFunc;
    48.         /* Set Slave Address. */
    49.         at24_handle->slaveAddress = config->slaveAddress;
    50.         
    51.         return kStatus_Success;
    52. }

    53. status_t i2c_at24cxx_send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *txBuff, uint32_t txBuffSize)
    54. {
    55.         return I2C_Send(I2C_AT24_BASEADDR, deviceAddress, subAddress, subaddressSize, txBuff, txBuffSize);
    56. }


    57. status_t i2c_at24cxx_receive(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint32_t rxBuffSize)
    58. {
    59.     return I2C_Receive(I2C_AT24_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
    60. }


    61. int at24cxx_read(uint32_t addr, uint8_t *buf, uint32_t len)
    62. {
    63.         return AT24_ReadReg(&at24Handle, addr, buf, len);
    64. }

    65. int at24cxx_write_page(uint32_t addr, uint8_t *buf, uint32_t len)
    66. {
    67.         return AT24_WriteReg(&at24Handle, addr, buf, len);
    68. }

    69. void at24cxx_write(uint32_t write_address, uint8_t *p_buffer, uint32_t number_of_byte)
    70. {
    71.         uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;

    72.         address = write_address % I2C_PAGE_SIZE;
    73.         count = I2C_PAGE_SIZE - address;
    74.         number_of_page =  number_of_byte / I2C_PAGE_SIZE;
    75.         number_of_single = number_of_byte % I2C_PAGE_SIZE;
    76.         
    77.         /* if write_address is I2C_PAGE_SIZE aligned */
    78.         if(0 == address)
    79.         {
    80.                 while(number_of_page--)
    81.                 {
    82.                         at24cxx_write_page(write_address, p_buffer, I2C_PAGE_SIZE);
    83.                         SysTick_Delay_ms(10);
    84.                         write_address +=  I2C_PAGE_SIZE;
    85.                         p_buffer += I2C_PAGE_SIZE;
    86.                 }
    87.                 if(0 != number_of_single)
    88.                 {
    89.                         at24cxx_write_page(write_address, p_buffer, number_of_single);
    90.                         SysTick_Delay_ms(10);
    91.                 }
    92.         }
    93.         else
    94.         {
    95.                 /* if write_address is not I2C_PAGE_SIZE aligned */
    96.                 if(number_of_byte < count)
    97.                 {
    98.                         at24cxx_write_page(write_address, p_buffer, number_of_byte);
    99.                         SysTick_Delay_ms(10);
    100.                 }
    101.                 else
    102.                 {
    103.                         number_of_byte -= count;
    104.                         number_of_page =  number_of_byte / I2C_PAGE_SIZE;
    105.                         number_of_single = number_of_byte % I2C_PAGE_SIZE;
    106.                         if(0 != count)
    107.                         {
    108.                                 at24cxx_write_page(write_address, p_buffer, count);
    109.                                 SysTick_Delay_ms(10);
    110.                                 write_address += count;
    111.                                 p_buffer += count;
    112.                         }
    113.                         /* write page */
    114.                         while(number_of_page--)
    115.                         {
    116.                                 at24cxx_write_page(write_address, p_buffer, I2C_PAGE_SIZE);
    117.                                 SysTick_Delay_ms(10);
    118.                                 write_address +=  I2C_PAGE_SIZE;
    119.                                 p_buffer += I2C_PAGE_SIZE;
    120.                         }
    121.                         /* write single */
    122.                         if(0 != number_of_single)
    123.                         {
    124.                                 at24cxx_write_page(write_address, p_buffer, number_of_single);
    125.                                 SysTick_Delay_ms(10);
    126.                         }
    127.                 }
    128.         }
    129. }

    130. void test_at24cxx(void)
    131. {
    132.         uint16_t i=0;
    133.         uint8_t addr=0;
    134.         uint8_t rxbuf[256];
    135.         uint8_t txbuf[256];
    136.         
    137.         PRINTF("\r\neeprom test!\r\n");
    138.         
    139.         for(i=0;i<256;i++)
    140.         {
    141.                 txbuf[i]=i;
    142.         }
    143.         //write
    144.         at24cxx_write(0x00, txbuf,256);
    145.         
    146.         PRINTF("\r\n write! txbuf:\r\n");
    147.         for(i=0;i<256;i++)
    148.         {
    149.                 PRINTF("%d ",txbuf[i]);
    150.         }
    151.         PRINTF("\r\n");
    152.         
    153.         SysTick_Delay_ms(10);

    154.         //read
    155.         if(at24cxx_read(0x00, rxbuf, 256)!= kStatus_Success)
    156.         {
    157.                 PRINTF("\r\n read failed!\r\n");
    158.         }
    159.         else
    160.         {
    161.                 PRINTF("\r\nread ok! rxbuf:\r\n");
    162.                 for(i=0;i<256;i++)
    163.                 {
    164.                         PRINTF("%d ",rxbuf[i]);
    165.                 }
    166.                 PRINTF("\r\n");
    167.         }
    168. }

    169. int init_at24cxx(void)
    170. {
    171.         uint8_t i=0;
    172.         status_t result;
    173.         
    174.         BOARD_I2C1_ReleaseBus();
    175.         init_i2c1_pins();

    176.         I2C_Init(I2C_AT24_BASEADDR, I2C_AT24_CLOCK_FREQ);
    177.         
    178.         /* Configure the I2C function */
    179.         atconfig.I2C_SendFunc    = i2c_at24cxx_send;
    180.         atconfig.I2C_ReceiveFunc = i2c_at24cxx_receive;
    181.         atconfig.slaveAddress    = i2c_at24cxx_address;
    182.         
    183.         /* Initialize sensor devices */
    184.         result = AT24_Init(&at24Handle, &atconfig);
    185.         return result;
    186. }
    复制代码


    2.3、at24cxx.h
    1. #ifndef __24CXX_H__
    2. #define __24CXX_H__

    3. #include <stdint.h>
    4. #include "fsl_common.h"

    5. /*! @brief Define I2C access function. */
    6. typedef status_t (*I2C_SendFunc_t)(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *txBuff, uint32_t txBuffSize);
    7. typedef status_t (*I2C_ReceiveFunc_t)(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint32_t rxBuffSize);

    8. typedef struct _at24cxx_handle
    9. {
    10.     /* Pointer to the user-defined I2C Send Data function. */
    11.     I2C_SendFunc_t I2C_SendFunc;
    12.     /* Pointer to the user-defined I2C Receive Data function. */
    13.     I2C_ReceiveFunc_t I2C_ReceiveFunc;
    14.     /* The I2C slave address . */
    15.     uint8_t slaveAddress;
    16. } at24cxx_handle_t;

    17. /*! @brief at24cxx configure structure.*/
    18. typedef struct _at24cxx_config
    19. {
    20.     /* Pointer to the user-defined I2C Send Data function. */
    21.     I2C_SendFunc_t I2C_SendFunc;
    22.     /* Pointer to the user-defined I2C Receive Data function. */
    23.     I2C_ReceiveFunc_t I2C_ReceiveFunc;
    24.     /* The I2C slave address. */
    25.     uint8_t slaveAddress;
    26. } at24cxx_config_t;


    27. //API funtctions
    28. int init_at24cxx(void);
    29. void test_at24cxx(void);
    30. int at24cxx_read(uint32_t addr, uint8_t *buf, uint32_t len);
    31. void at24cxx_write(uint32_t write_address, uint8_t *p_buffer, uint32_t number_of_byte);
    32. #endif

    复制代码


    2.4、main.c
    1. #include "pin_mux.h"
    2. #include "clock_config.h"
    3. #include "board.h"
    4. #include "fsl_debug_console.h"
    5. #include "fsl_gpio.h"
    6. #include "slcd/slcd.h"
    7. #include "led/led.h"
    8. #include "systick/systick.h"
    9. #include "slcd_engine.h"
    10. #include "adc/adc.h"
    11. #include "i2c/eeprom.h"
    12. #include "i2c/fxls8974.h"
    13. #include "i2c/at24cxx.h"
    14. #include "fsl_uart.h"
    15. #include "stdio.h"

    16. void init_board(void)
    17. {
    18.         
    19.         SysTick_Init();
    20.         BOARD_InitPins();
    21.         BOARD_BootClockRUN();
    22.         BOARD_InitDebugConsole();
    23.         init_at24cxx();
    24. }

    25. int main(void)
    26. {
    27.         uint32_t m_adv=0;
    28.         float vl= 0.0f;
    29.         
    30.         init_board();
    31.         while (1)
    32.         {
    33.                 test_at24cxx();
    34.                 SysTick_Delay_ms(1000);
    35.         }
    36. }
    复制代码

    三、运行结果

    下载程序,串口打印读写测试数据
    004.png
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-2-19 22:11 , Processed in 0.110031 second(s), 19 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表