99a-esp8266_flash_rw_operation_cn_v1.0.pdf
spi_flash_erase_sector(priv_param_start_sec + PRIV_PARAM_SAVE);
spi_flash_write((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
spi_flash_read((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param_test, sizeof(struct light_saved_param));
2a-esp8266-sdk_getting_started_guide_cn.pdf
小编用的是8Mbit的ESP8266模块,直插DIP排针板载天线即ESP8266-01模块
附user_main.c
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2016
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "ets_sys.h"
#include "osapi.h"
#include "driver/uart.h"
#include "user_tcpclient.h"
#include "user_interface.h"
#include "user_httpwebserver.h"
#define PRIV_PARAM_SAVE 0
#define FLASH_TEST
uint32 priv_param_start_sec;
#ifdef FLASH_TEST
/******************************************************************************
* spi flash test,uart printf"
* before0,0,0,0
* write20,1,2,3,ifengzai,kangshihuang
* erase0,0,0,0"
*
*******************************************************************************/
struct light_saved_param {
uint32 pwm_period;
uint32 pwm_duty[5];
char rout_ssid[64];
char rout_pwd[32];
};
void ICACHE_FLASH_ATTR
user_spi_flash_test(void)
{
uint16 i=60000;
struct light_saved_param light_param,light_param_test;
light_param.pwm_period = 20;
uint32 light_init_target[5]={1,2,3,4,5};
//读flash
//while(i--);i=60000;
spi_flash_read((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param_test, sizeof(struct light_saved_param));
os_printf("\r\nbefore%d,%d,%d,%d\r\n\r\n",light_param_test.pwm_period,light_param_test.pwm_duty[0],
light_param_test.pwm_duty[1],light_param_test.pwm_duty[2]);
//while(i--);i=60000;
//写flash
os_memcpy(light_param.pwm_duty,light_init_target,sizeof(light_param.pwm_duty));
os_memcpy(light_param.rout_ssid,"ifengzai",sizeof("ifengzai"));
os_memcpy(light_param.rout_pwd,"kangshihuang",sizeof("kangshihuang"));
spi_flash_erase_sector(priv_param_start_sec + PRIV_PARAM_SAVE);
spi_flash_write((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
//延时,读
//while(i--);i=60000;
os_memset(&light_param_test, 0, sizeof(struct light_saved_param));
spi_flash_read((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param_test, sizeof(struct light_saved_param));
os_printf("\r\nwrite%d,%d,%d,%d,%s,%s\r\n\r\n",light_param_test.pwm_period,light_param_test.pwm_duty[0],
light_param_test.pwm_duty[1],light_param_test.pwm_duty[2],light_param_test.rout_ssid,
light_param_test.rout_pwd);
//while(i--);i=60000;
//清零 ,写
os_memset(&light_param, 0, sizeof(struct light_saved_param));
spi_flash_erase_sector(priv_param_start_sec + PRIV_PARAM_SAVE);
spi_flash_write((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
//延时,读
//while(i--);i=60000;
os_memset(&light_param_test, 0, sizeof(struct light_saved_param));
spi_flash_read((priv_param_start_sec + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param_test, sizeof(struct light_saved_param));
os_printf("\r\nerase%d,%d,%d,%d\r\n\r\n",light_param_test.pwm_period,light_param_test.pwm_duty[0],
light_param_test.pwm_duty[1],light_param_test.pwm_duty[2]);
}
#endif
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don't know which sector is free in user's application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
priv_param_start_sec = 0x3C;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
priv_param_start_sec = 0x7C;
break;
case FLASH_SIZE_16M_MAP_512_512:
rf_cal_sec = 512 - 5;
priv_param_start_sec = 0x7C;
break;
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
priv_param_start_sec = 0xFC;
break;
case FLASH_SIZE_32M_MAP_512_512:
rf_cal_sec = 1024 - 5;
priv_param_start_sec = 0x7C;
break;
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
priv_param_start_sec = 0xFC;
break;
case FLASH_SIZE_64M_MAP_1024_1024:
rf_cal_sec = 2048 - 5;
priv_param_start_sec = 0xFC;
break;
case FLASH_SIZE_128M_MAP_1024_1024:
rf_cal_sec = 4096 - 5;
priv_param_start_sec = 0xFC;
break;
default:
rf_cal_sec = 0;
priv_param_start_sec = 0;
break;
}
return rf_cal_sec;
}
void ICACHE_FLASH_ATTR
user_rf_pre_init(void)
{
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
uart_init(BIT_RATE_115200, BIT_RATE_115200);
os_printf("SDK version:%s\n", system_get_sdk_version());
os_printf("hello_ai\n");
#ifdef FLASH_TEST
user_spi_flash_test();
#endif
//这里是wifi初始化,ap+station模式,连接路由器,连接tcp服务器
//tcpuser_init();
//这里是web http协议,网页配置的函数
//user_httpwebserver_init();
}
打印结果