RISC-V技术william hill官网
直播中

voidpbq

8年用户 131经验值
擅长:嵌入式技术,处理器/DSP
私信 关注
[经验]

【芯昇科技RISC-V生态开发板试用体验】2. eeprom存储wifi信息


# 1 前言

本章介绍下使用芯昇的eeprom保存wifi账户和密码。

测试功能块:I2C

# 2 硬件

如图,eeprom使用的是PC0和PC1。

weizhi.png

威廉希尔官方网站
图.png

# 3 打开示例

File -- New -- Project -- Next -- 选start下的

pro.png


# 代码

建议查看下readme.txt

  1. #include "nuclei_sdk_soc.h"
  2. #include "i2c_eeprom.h"
  3. #include "bsp.h"
  4. #include "main.h"

  5. // add .h
  6. #include "string.h"
  7. #include "stdlib.h"

  8. uint8_t tx_buf[TEST_EEPROM_SIZE] = {0};
  9. uint8_t rx_buf[TEST_EEPROM_SIZE] = {0};
  10. volatile Status test_status      = FAILED;

  11. #define HEAD_ID "ID:"
  12. #define HEAD_PW "PW:"
  13. #define END_EOF "EOF"

  14. #define WIFI_ID "wifi_name"
  15. #define WIFI_PW "123456"

  16. Status Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  17. void get_id();
  18. void get_pw();

  19. int main(void)
  20. {
  21.     uint16_t i = 0;

  22.     log_init();
  23.     log_info("this is a I2C EEPROM demorn");
  24.     /* Initialize the I2C EEPROM driver ----------------------------------------*/
  25.     I2C_EE_Init();

  26.     /* Fill the buffer to send */
  27.     for (i = 0; i < TEST_EEPROM_SIZE; i++)
  28.     {
  29.         tx_buf[i] = 0;
  30. //        log_info("tx_buf[%d]= %dn",i,tx_buf[i]);
  31.     }
  32.     log_info("Write to I2C EEPROMrn");
  33.     /* First write in the memory followed by a read of the written data --------*/
  34.     /* Write to I2C EEPROM from TEST_EEPROM_ADDR */
  35. //    I2C_EE_WriteBuffer(tx_buf, TEST_EEPROM_ADDR, TEST_EEPROM_SIZE);

  36.     e2_set_wifi(WIFI_ID,WIFI_PW);

  37.     log_info("nRead from I2C EEPROMrn");
  38.     /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
  39.     I2C_EE_ReadBuffer(rx_buf, TEST_EEPROM_ADDR, TEST_EEPROM_SIZE);

  40.     // print rx value ---- eeprom value
  41. //    print_eeprom();

  42.     get_id();
  43.    
  44.     while (1)
  45.     {
  46.     }
  47. }

  48. void get_id()
  49. {
  50.         char * temp_id_start;
  51.         char * temp_id_eof;
  52.         temp_id_start = strstr(rx_buf,HEAD_ID);
  53.         if(temp_id_start == NULL)
  54.                 log_info("not find idn");
  55.         else
  56.         {
  57. //                printf("find id = %sn",temp_id_start+strlen(HEAD_ID));
  58.                 temp_id_eof = strstr(rx_buf,END_EOF);
  59.                 if(temp_id_eof == NULL)
  60.                         log_info("not find pw eofn");
  61.                 else
  62.                 {
  63.                         temp_id_start = temp_id_start + strlen(HEAD_ID);
  64.                         int len_id = temp_id_eof - temp_id_start ;
  65.                         printf("nfind id len = %dn",len_id);
  66.                         char * temp_id_value = (char *) malloc(len_id +1);
  67.                         memset(temp_id_value,0x00,len_id+1);
  68.                         strncpy(temp_id_value,temp_id_start ,len_id);
  69.                         printf("find id  = %sn",temp_id_value);

  70.                         // get pw
  71.                         get_pw(temp_id_eof + strlen(END_EOF));

  72.                         free(temp_id_value);
  73.                 }
  74.         }
  75.         return;
  76. }

  77. void get_pw(char * str)
  78. {
  79.         char * temp_pw_start;
  80.         char * temp_pw_eof;
  81.         temp_pw_start = strstr(str,HEAD_PW);
  82.         if(temp_pw_start == NULL)
  83.                 log_info("not find pwn");
  84.         else
  85.         {
  86. //                printf("find pw = %sn",temp_pw_start+strlen(HEAD_PW));
  87.                 temp_pw_eof = strstr(str,END_EOF);
  88.                 if(temp_pw_eof == NULL)
  89.                         log_info("not find pw eofn");
  90.                 else
  91.                 {
  92.                         temp_pw_start = temp_pw_start + strlen(HEAD_PW);
  93.                         int len_pw = temp_pw_eof - temp_pw_start ;
  94.                         printf("find pw len = %dn",len_pw);
  95.                         char * temp_pw_value = (char *) malloc(len_pw +1);
  96.                         memset(temp_pw_value,0x00,len_pw+1);
  97.                         strncpy(temp_pw_value,temp_pw_start ,len_pw);
  98.                         printf("find pw  = %sn",temp_pw_value);
  99.                         free(temp_pw_value);
  100.                 }
  101.         }
  102.         return;
  103. }

  104. void print_eeprom()
  105. {
  106.     for (int i = 0; i < TEST_EEPROM_SIZE; i++)
  107.     {
  108.         log_info("tx_buf[%d]= %02xn",i,rx_buf[i]);
  109.     }
  110.     log_info("n");
  111. }

  112. void e2_set_wifi(char* id, char* pw)
  113. {
  114.         char* char_id_pw;
  115.         int len_id = strlen(id);
  116.         int len_pw = strlen(pw);
  117.         int len_all = len_id + len_pw + strlen(HEAD_ID) + strlen(HEAD_PW) + 2*strlen(END_EOF)+1;
  118.         log_info("len_id = %dn",len_all);

  119.         char_id_pw =(char*)malloc(len_all);

  120.         memset(char_id_pw,0x00,len_all);
  121.         strcat(char_id_pw,HEAD_ID);
  122.         strcat(char_id_pw,id);
  123.         strcat(char_id_pw,END_EOF);
  124.         strcat(char_id_pw,HEAD_PW);
  125.         strcat(char_id_pw,pw);
  126.         strcat(char_id_pw,END_EOF);
  127.         log_info("char_id_pw = %sn",char_id_pw);

  128.         I2C_EE_WriteBuffer(char_id_pw, TEST_EEPROM_ADDR, len_all);
  129.         free(char_id_pw);

  130.         return;
  131. }

  132. Status Buffercmp(uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength)
  133. {
  134.     while (BufferLength--)
  135.     {
  136.         if (*pBuffer != *pBuffer1)
  137.         {
  138.             return FAILED;
  139.         }

  140.         pBuffer++;
  141.         pBuffer1++;
  142.     }

  143.     return PASSED;
  144. }

readme.txt介绍项目

  1. 1、功能说明

  2.     此例程展示了通过I2C模块与外部EEPRON的通信,分别采用查询、中断、DMA方式进行EEPROM的读写。   

  3. 2、使用环境

  4.         /* 软件开发环境:当前工程使用的软件工具名称及版本号 */
  5.     IDE工具:NucleiStudio IDE for C/C++ 2022-01
  6.        
  7.         /* 开发板 */
  8.         CM32M433R-START

  9. 3、使用说明
  10.        
  11.     1、时钟源:HSE+PLL
  12.     2、主时钟:144MHz
  13.     3、I2C3 配置:
  14.             SCL   -->  PC0
  15.             SDA   -->  PC1

  16.             ADDR:0xA0(7bit)
  17.             CLOCK:400KHz
  18.             
  19.     4、USART1配置:
  20.             TX  -->  PD0
  21.             波特率:115200
  22.             数据位:8bit
  23.             停止位:1bit
  24.             无校验

  25.     5、测试步骤与现象
  26.         a,将六类接口功能扩展板和CM32M433R-START进行连接
  27.         b,编译下载代码复位运行
  28.         c,从串口看打印信息,验证结果
  29.                 d,修改i2c_eeprom.h中的PROCESS_MODE宏值,0代表查询方式进行I2C通信,1代表中断方式进行I2C通信,2代表DMA方式进行I2C通信。
  30.                 e,重新编译下载代码,复位运行查看结果。

  31. 4、注意事项
  32.     1,此处使用的EEPROM是AT24C02,32个page,每个page 8byte
  33.     2,读写数据时若长度大于一个page,则器件地址会自动回卷
  34.     3,SCL,SDA有1K的外部上拉电阻
  35.        

# log

如图,先打印存储的wifi信息,然后再取出eeprom的数据,从中分离出来wifi信息。

log.png
至此成功将wifi账户和密码存入eeprom。
可以测试将e2_set_wifi注销掉,然后打开print_eeprom(),验证下掉电是否保留。

# 小结


本开发板不具备wifi和bt功能,但是由于它的uart,i2c等接口较多,适合做中控或者uart模拟器,用于和多个设备进行通信。


后续再测试uart功能。



更多回帖

发帖
×
20
完善资料,
赚取积分