[demo] add esp8266 demo.

This commit is contained in:
suyoujiang1
2020-10-19 13:47:00 +08:00
parent 086bc47cf1
commit 00ddb6b430
16 changed files with 16855 additions and 1 deletions
+6
View File
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello-world)
File diff suppressed because it is too large Load Diff
+9
View File
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := flashDB_demo
include $(IDF_PATH)/make/project.mk
+22
View File
@@ -0,0 +1,22 @@
# flashDB demo Example
启动一个freertos任务运行flashDB的例程,然后倒计时1000秒后重启。运行log,请看当前目录下的ESP8266_flashDB_log.rtf 日志
# ESP8266开发环境
* Ubuntu 16.04 64位
* 用乐鑫IDF:ESP8266_RTOS_SDK(版本:ESP-IDF v3.2-252-g73bf28b-dirty),下载:https://github.com/espressif/ESP8266_RTOS_SDK.git
* 安装交叉编译工具链,下载:https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
# ESP8266开发指南
* 详细的安装开发教程,请参考:https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/get-started/index.html
# 编译和下载
* cd进入esp8266_spi_flash目录
* make all
* 使用串口线把8266开发板连接到PC
* 使能串口,执行命令:sudo chmod 777 /dev/ttyUSB*
* make flash
* make monitor
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,19 @@
#
# Component Makefile
#
COMPONENT_ADD_INCLUDEDIRS += \
inc \
../../../../inc \
../../../../port/fal/inc
COMPONENT_SRCDIRS += \
../../../../src \
../../../../samples \
../../../../port/fal/src \
porting
# COMPONENT_PRIV_INCLUDEDIRS +=
# COMPONENT_ADD_LDFLAGS += -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive
@@ -0,0 +1,56 @@
/*
* File : fal_cfg.h
* This file is part of FAL (Flash Abstraction Layer) package
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-05-17 armink the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
//#include <rtconfig.h>
//#include <board.h>
//#define FAL_DEBUG 1
#define FAL_PART_HAS_TABLE_CFG
#define NOR_FLASH_DEV_NAME "norflash0"
//#define FAL_PART_TABLE_FLASH_DEV_NAME NOR_FLASH_DEV_NAME
//#define FAL_PART_TABLE_END_OFFSET 65536
/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "fdb_kvdb1", NOR_FLASH_DEV_NAME, 0, 16*1024, 0}, \
{FAL_PART_MAGIC_WORD, "fdb_tsdb1", NOR_FLASH_DEV_NAME, 16*1024 , 16*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief configuration file
*/
#ifndef _FDB_CFG_H_
#define _FDB_CFG_H_
/* using KVDB feature */
#define FDB_USING_KVDB
#ifdef FDB_USING_KVDB
/* Auto update KV to latest default when current KVDB version number is changed. @see fdb_kvdb.ver_num */
/* #define FDB_KV_AUTO_UPDATE */
#endif
/* using TSDB (Time series database) feature */
#define FDB_USING_TSDB
/* the flash write granularity, unit: bit
* only support 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1) */
#define FDB_WRITE_GRAN 1 /* @note you must define it for a value */
/* MCU Endian Configuration, default is Little Endian Order. */
/* #define FDB_BIG_ENDIAN */
/* log print macro. default EF_PRINT macro is printf() */
/* #define FDB_PRINT(...) my_printf(__VA_ARGS__) */
/* print debug information */
#define FDB_DEBUG_ENABLE
#endif /* _FDB_CFG_H_ */
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_spi_flash.h"
#include <string.h>
#include <fal.h>
#define FLASH_ERASE_MIN_SIZE (4 * 1024)
#define LOCKER_ENABLE
#ifdef LOCKER_ENABLE
#include "FreeRTOS.h"
#include "semphr.h"
static SemaphoreHandle_t s_lock = NULL;
#define LOCK() \
do { \
xSemaphoreTake(s_lock, portMAX_DELAY); \
} while(0)
#define UNLOCK() \
do { \
xSemaphoreGive(s_lock); \
} while(0)
#else
#define LOCK()
#define UNLOCK()
#endif
static int init(void)
{
#ifdef LOCKER_ENABLE
if (s_lock == NULL) {
s_lock = xSemaphoreCreateCounting(1, 1);
assert(s_lock != NULL);
}
#endif
return 1;
}
static int read(long offset, uint8_t *buf, size_t size)
{
//Xassert(size % 4 == 0);
/* You can add your code under here. */
int32_t ret;
uint32_t addr = nor_flash0.addr + offset;
LOCK();
ret = spi_flash_read(addr, buf, size);
UNLOCK();
return ret;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
int32_t ret;
uint32_t addr = nor_flash0.addr + offset;
LOCK();
ret = spi_flash_write(addr, buf, size);
UNLOCK();
return ret;
}
static int erase(long offset, size_t size)
{
int32_t ret;
uint32_t addr = nor_flash0.addr + offset;
int32_t erase_size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1;
LOCK();
ret = spi_flash_erase_range(addr, erase_size * FLASH_ERASE_MIN_SIZE);
UNLOCK();
return ret;
}
/*
"esp8266_onchip" : Flash 设备的名字。
0x08000000: 对 Flash 操作的起始地址。
1024*1024:Flash 的总大小(1MB)。
128*1024:Flash 块/扇区大小(因为 STM32F2 各块大小不均匀,所以擦除粒度为最大块的大小:128K)。
{init, read, write, erase} :Flash 的操作函数。 如果没有 init 初始化过程,第一个操作函数位置可以置空。
8 : 设置写粒度,单位 bit, 0 表示未生效(默认值为 0 ),该成员是 fal 版本大于 0.4.0 的新增成员。各个 flash 写入粒度不尽相同,可通过该成员进行设置,以下列举几种常见 Flash 写粒度:
nor flash: 1 bit
stm32f2/f4: 8 bit
stm32f1: 32 bit
stm32l4: 64 bit
*/
//1.定义 flash 设备
const struct fal_flash_dev nor_flash0 =
{
.name = "norflash0",
.addr = 0x100000,
.len = 50*1024,
.blk_size = FLASH_ERASE_MIN_SIZE,
.ops = {init, read, write, erase},
.write_gran = 1
};
@@ -0,0 +1,2 @@
idf_component_register(SRCS "hello_world_main.c"
INCLUDE_DIRS "")
@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
@@ -0,0 +1,157 @@
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "semphr.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include <flashdb.h>
#define FDB_LOG_TAG "[main]"
static uint32_t boot_count = 0;
static time_t boot_time[10] = {0, 1, 2, 3};
/* default KV nodes */
static struct fdb_default_kv_node default_kv_table[] = {
{"username", "armink", 0}, /* string KV */
{"password", "123456", 0}, /* string KV */
{"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */
{"boot_time", &boot_time, sizeof(boot_time)}, /* int array type KV */
};
/* KVDB object */
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;
static SemaphoreHandle_t s_lock = NULL;
extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_blob_sample(fdb_kvdb_t kvdb);
extern void tsdb_sample(fdb_tsdb_t tsdb);
static void lock(fdb_db_t db)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
}
static void unlock(fdb_db_t db)
{
xSemaphoreGive(s_lock);
}
static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}
int flashdb_demo(void)
{
fdb_err_t result;
if (s_lock == NULL) {
s_lock = xSemaphoreCreateCounting(1, 1);
assert(s_lock != NULL);
}
#ifdef FDB_USING_KVDB
{ /* KVDB Sample */
struct fdb_default_kv default_kv;
default_kv.kvs = default_kv_table;
default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
/* set the lock and unlock function if you want */
fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
/* Key-Value database initialization
*
* &kvdb: database object
* "env": database name
* "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
* Please change to YOUR partition name.
* &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully.
* NULL: The user data if you need, now is empty.
*/
result = fdb_kvdb_init(&kvdb, "env", "fdb_kvdb1", &default_kv, NULL);
if (result != FDB_NO_ERR) {
return -1;
}
/* run basic KV samples */
kvdb_basic_sample(&kvdb);
/* run string KV samples */
kvdb_type_string_sample(&kvdb);
/* run blob KV samples */
kvdb_type_blob_sample(&kvdb);
}
#endif /* FDB_USING_KVDB */
#ifdef FDB_USING_TSDB
{ /* TSDB Sample */
/* set the lock and unlock function if you want */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, lock);
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
/* Time series database initialization
*
* &tsdb: database object
* "log": database name
* "fdb_tsdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
* Please change to YOUR partition name.
* get_time: The get current timestamp function.
* 128: maximum length of each log
* NULL: The user data if you need, now is empty.
*/
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", get_time, 128, NULL);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
if (result != FDB_NO_ERR) {
return -1;
}
/* run TSDB sample */
tsdb_sample(&tsdb);
}
#endif /* FDB_USING_TSDB */
return 0;
}
void app_main()
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is ESP8266 chip with %d CPU cores, WiFi, ",
chip_info.cores);
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
flashdb_demo();
for (int i = 1000; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -55,7 +55,8 @@
#else
#error not supported tool chain
#endif /* __CC_ARM */
USED static const struct fal_partition partition_table_def[] SECTION("FalPartTable") = FAL_PART_TABLE;
//USED static const struct fal_partition partition_table_def[] SECTION("FalPartTable") = FAL_PART_TABLE;
static const struct fal_partition partition_table_def[] = FAL_PART_TABLE;
static const struct fal_partition *partition_table = NULL;
#else /* FAL_PART_HAS_TABLE_CFG */