【中移芯昇】2. eeprom存储wifi信息

1 前言

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

测试功能块:I2C

2 硬件

如图,eeprom使用的是PC0和PC1。
在这里插入图片描述

请添加图片描述

3 打开示例

File – New – Project – Next – 选start下的
在这里插入图片描述

建议查看下readme.txt

代码

main.c

#include "nuclei_sdk_soc.h"
#include "i2c_eeprom.h"
#include "bsp.h"
#include "main.h"

// add .h
#include "string.h"
#include "stdlib.h"

uint8_t tx_buf[TEST_EEPROM_SIZE] = {0};
uint8_t rx_buf[TEST_EEPROM_SIZE] = {0};
volatile Status test_status      = FAILED;

#define HEAD_ID "ID:"
#define HEAD_PW "PW:"
#define END_EOF "EOF"

#define WIFI_ID "wifi_name"
#define WIFI_PW "123456"

Status Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
void get_id();
void get_pw();

int main(void)
{
    uint16_t i = 0;

    log_init();
    log_info("this is a I2C EEPROM demo\r\n");
    /* Initialize the I2C EEPROM driver ----------------------------------------*/
    I2C_EE_Init();

    /* Fill the buffer to send */
    for (i = 0; i < TEST_EEPROM_SIZE; i++)
    {
        tx_buf[i] = 0;
//        log_info("tx_buf[%d]= %d\n",i,tx_buf[i]);
    }
    log_info("Write to I2C EEPROM\r\n");
    /* First write in the memory followed by a read of the written data --------*/
    /* Write to I2C EEPROM from TEST_EEPROM_ADDR */
//    I2C_EE_WriteBuffer(tx_buf, TEST_EEPROM_ADDR, TEST_EEPROM_SIZE);

    e2_set_wifi(WIFI_ID,WIFI_PW);

    log_info("\nRead from I2C EEPROM\r\n");
    /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
    I2C_EE_ReadBuffer(rx_buf, TEST_EEPROM_ADDR, TEST_EEPROM_SIZE);

    // print rx value ---- eeprom value
//    print_eeprom();

    get_id();
    
    while (1)
    {
    }
}

void get_id()
{
	char * temp_id_start;
	char * temp_id_eof;
	temp_id_start = strstr(rx_buf,HEAD_ID);
	if(temp_id_start == NULL)
		log_info("not find id\n");
	else
	{
//		printf("find id = %s\n",temp_id_start+strlen(HEAD_ID));
		temp_id_eof = strstr(rx_buf,END_EOF);
		if(temp_id_eof == NULL)
			log_info("not find pw eof\n");
		else
		{
			temp_id_start = temp_id_start + strlen(HEAD_ID);
			int len_id = temp_id_eof - temp_id_start ;
			printf("\nfind id len = %d\n",len_id);
			char * temp_id_value = (char *) malloc(len_id +1);
			memset(temp_id_value,0x00,len_id+1);
			strncpy(temp_id_value,temp_id_start ,len_id);
			printf("find id  = %s\n",temp_id_value);

			// get pw
			get_pw(temp_id_eof + strlen(END_EOF));

			free(temp_id_value);
		}
	}
	return;
}

void get_pw(char * str)
{
	char * temp_pw_start;
	char * temp_pw_eof;
	temp_pw_start = strstr(str,HEAD_PW);
	if(temp_pw_start == NULL)
		log_info("not find pw\n");
	else
	{
//		printf("find pw = %s\n",temp_pw_start+strlen(HEAD_PW));
		temp_pw_eof = strstr(str,END_EOF);
		if(temp_pw_eof == NULL)
			log_info("not find pw eof\n");
		else
		{
			temp_pw_start = temp_pw_start + strlen(HEAD_PW);
			int len_pw = temp_pw_eof - temp_pw_start ;
			printf("find pw len = %d\n",len_pw);
			char * temp_pw_value = (char *) malloc(len_pw +1);
			memset(temp_pw_value,0x00,len_pw+1);
			strncpy(temp_pw_value,temp_pw_start ,len_pw);
			printf("find pw  = %s\n",temp_pw_value);
			free(temp_pw_value);
		}
	}
	return;
}

void print_eeprom()
{
    for (int i = 0; i < TEST_EEPROM_SIZE; i++)
    {
        log_info("tx_buf[%d]= %02x\n",i,rx_buf[i]);
    }
    log_info("\n");
}

void e2_set_wifi(char* id, char* pw)
{
	char* char_id_pw;
	int len_id = strlen(id);
	int len_pw = strlen(pw);
	int len_all = len_id + len_pw + strlen(HEAD_ID) + strlen(HEAD_PW) + 2*strlen(END_EOF)+1;
	log_info("len_id = %d\n",len_all);

	char_id_pw =(char*)malloc(len_all);

	memset(char_id_pw,0x00,len_all);
	strcat(char_id_pw,HEAD_ID);
	strcat(char_id_pw,id);
	strcat(char_id_pw,END_EOF);
	strcat(char_id_pw,HEAD_PW);
	strcat(char_id_pw,pw);
	strcat(char_id_pw,END_EOF);
	log_info("char_id_pw = %s\n",char_id_pw);

	I2C_EE_WriteBuffer(char_id_pw, TEST_EEPROM_ADDR, len_all);
	free(char_id_pw);

	return;
}

Status Buffercmp(uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength)
{
    while (BufferLength--)
    {
        if (*pBuffer != *pBuffer1)
        {
            return FAILED;
        }

        pBuffer++;
        pBuffer1++;
    }

    return PASSED;
}
1、功能说明

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

2、使用环境

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

3、使用说明
	
    1、时钟源:HSE+PLL
    2、主时钟:144MHz
    3、I2C3 配置:
            SCL   -->  PC0
            SDA   -->  PC1

            ADDR:0xA0(7bit)
            CLOCK:400KHz
            
    4、USART1配置:
            TX  -->  PD0
            波特率:115200
            数据位:8bit
            停止位:1bit
            无校验

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

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

log

请添加图片描述

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值