[sync] sync from internal repo

* use nuttx libc, disable system libc
* use tlsf as default
* update lhal flash driver
* add example readme
* add flash ini for new flash tool
* add fw header for new flash tool
This commit is contained in:
jzlv 2023-01-17 20:54:15 +08:00
parent 89592fc9a3
commit 356f258e83
554 changed files with 79150 additions and 46596 deletions

View file

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.15)
include(proj.conf)
find_package(bouffalo_sdk REQUIRED HINTS $ENV{BL_SDK_BASE})
sdk_add_include_directories(.)
file(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/kv_test/*.c")
target_sources(app PRIVATE ${sources})
sdk_add_link_options(-ucmd_kv_test)
sdk_set_main_file(main.c)
project(littlefs)

View file

@ -0,0 +1,127 @@
/*
* FreeRTOS Kernel V10.2.1
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, 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.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/
#include "stdio.h"
#ifdef BL702
#define configMTIME_BASE_ADDRESS (0x02000000UL + 0xBFF8UL)
#define configMTIMECMP_BASE_ADDRESS (0x02000000UL + 0x4000UL)
#else
#define configMTIME_BASE_ADDRESS (0xE0000000UL + 0xBFF8UL)
#define configMTIMECMP_BASE_ADDRESS (0xE0000000UL + 0x4000UL)
#endif
#define configSUPPORT_STATIC_ALLOCATION 1
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ ((uint32_t)(1 * 1000 * 1000))
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES (32)
#define configMINIMAL_STACK_SIZE ((unsigned short)128) /* Only needs to be this high as some demo tasks also use this constant. In production only the idle task would use this. */
#define configTOTAL_HEAP_SIZE ((size_t)100 * 1024)
#define configMAX_TASK_NAME_LEN (16)
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
#define configUSE_TICKLESS_IDLE 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES (2)
/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1)
#define configTIMER_QUEUE_LENGTH 4
#define configTIMER_TASK_STACK_DEPTH (1024)
/* Task priorities. Allow these to be overridden. */
#ifndef uartPRIMARY_PRIORITY
#define uartPRIMARY_PRIORITY (configMAX_PRIORITIES - 3)
#endif
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xSemaphoreGetMutexHolder 1
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
void vApplicationMallocFailedHook(void);
void vAssertCalled(void);
#define configASSERT(x) \
if ((x) == 0) { \
printf("file [%s]\r\n", __FILE__); \
printf("func [%s]\r\n", __FUNCTION__); \
printf("line [%d]\r\n", __LINE__); \
printf("%s\r\n", (const char *)(#x)); \
vAssertCalled(); \
}
#if (configUSE_TICKLESS_IDLE != 0)
void vApplicationSleep(uint32_t xExpectedIdleTime);
#define portSUPPRESS_TICKS_AND_SLEEP(xExpectedIdleTime) vApplicationSleep(xExpectedIdleTime)
#endif
// #define portUSING_MPU_WRAPPERS
#endif /* FREERTOS_CONFIG_H */

View file

@ -0,0 +1,13 @@
SDK_DEMO_PATH ?= .
BL_SDK_BASE ?= $(SDK_DEMO_PATH)/../..
export BL_SDK_BASE
CHIP ?= bl616
BOARD ?= bl616dk
CROSS_COMPILE ?= riscv64-unknown-elf-
# add custom cmake definition
#cmake_definition+=-Dxxx=sss
include $(BL_SDK_BASE)/project.build

View file

@ -0,0 +1,44 @@
# littlefs
## Support CHIP
| CHIP | Remark |
|:----------------:|:------:|
|BL602/BL604 | |
|BL702/BL704/BL706 | |
|BL616/BL618 | |
|BL808 | |
## Compile
- BL602/BL604
```
make CHIP=bl602 BOARD=bl602dk
```
- BL702/BL704/BL706
```
make CHIP=bl702 BOARD=bl702dk
```
- BL616/BL618
```
make CHIP=bl616 BOARD=bl616dk
```
- BL808
```
make CHIP=bl808 BOARD=bl808dk CPU_ID=m0
make CHIP=bl808 BOARD=bl808dk CPU_ID=d0
```
## Flash
```
make flash CHIP=chip_name COMX=xxx # xxx is your com name
```

View file

@ -0,0 +1,11 @@
[cfg]
# 0: no erase, 1:programmed section erase, 2: chip erase
erase = 1
# skip mode set first para is skip addr, second para is skip len, multi-segment region with ; separated
skip_mode = 0x0, 0x0
# 0: not use isp mode, #1: isp mode
boot2_isp_mode = 0
[FW]
filedir = ./build/build_out/littlefs_$(CHIPNAME).bin
address = 0x000000

View file

@ -0,0 +1,41 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_KV_TEST
tristate "\"kv test!\" example"
default n
---help---
Enable the \"kv test!\" example
if EXAMPLES_KV_TEST
config EXAMPLES_KV_TEST_PROGNAME
string "Program name"
default "kv_test"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.
config EXAMPLES_KV_TEST_PRIORITY
int "kv_test task priority"
default 100
config EXAMPLES_KV_TEST_STACKSIZE
int "kv_test stack size"
default 4096
config KV_TEST_TEST_CASE_NUM
int "kv_test case num"
default 1
config KV_TEST_FILE_OPER_SUM
int "kv_test file RW sum"
default 20000
config KV_TEST_FILE_OPER_PERIOD
int "kv_test file RW period(ms)"
default 1000
endif

View file

@ -0,0 +1,16 @@
#include "arch_crypto.h"
#include "arch_dbg.h"
#include <mbedtls/aes.h>
#include <mbedtls/md5.h>
#include <stdint.h>
void arch_md5(uint8_t *input, int len, uint8_t hash[16])
{
mbedtls_md5_context context;
mbedtls_md5_init(&context);
mbedtls_md5_starts(&context);
mbedtls_md5_update(&context, input, len);
mbedtls_md5_finish(&context, hash);
}

View file

@ -0,0 +1,8 @@
#ifndef _ARCH_CRYPTO_H_
#define _ARCH_CRYPTO_H_
#include <stdint.h>
void arch_md5(uint8_t *input, int len, uint8_t hash[16]);
#endif /* _ARCH_CRYPTO_H_ */

View file

@ -0,0 +1,57 @@
#ifndef __ARCH_DBG_H__
#define __ARCH_DBG_H__
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "arch_time.h"
#include "arch_os.h"
#include <log.h>
#ifdef LOG_INFO
#undef LOG_INFO
#endif
#ifdef LOG_DEBUG
#undef LOG_DEBUG
#endif
static inline char * arch_printf_time(void)
{
struct tm *dtime;
int gmt_offset = 0;
time_t cur;
static char time[256];
memset(time, 0, sizeof(time));
cur = arch_os_utc_now();
dtime = gmtime((const time_t *)&cur);
snprintf(time, sizeof(time), "%02d:%02d:%02d.%03d ", dtime->tm_hour, dtime->tm_min, dtime->tm_sec, arch_os_ms_now() % 1000);
return time;
}
#define LOG_INFO_TAG(TAG, _fmt_, ...) \
do { \
LOG_I("[%s] %s: " _fmt_ "\r\n", arch_printf_time(), TAG, ##__VA_ARGS__); \
} while (0)
#define LOG_WARN_TAG(TAG, _fmt_, ...) \
do { \
LOG_W("[%s] %s: (%s,%d)" _fmt_ "\r\n", arch_printf_time(), TAG, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while (0)
#define LOG_ERROR_TAG(TAG, _fmt_, ...) \
do { \
LOG_E("[%s] %s: (%s,%d)" _fmt_ "\r\n", arch_printf_time(), TAG, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while (0)
#define LOG_DEBUG_TAG(TAG, _fmt_, ...) \
do { \
LOG_D("[%s] %s: " _fmt_ "\r\n", arch_printf_time(), TAG, ##__VA_ARGS__); \
} while (0)
#endif /* __APPLN_DBG_H__ */

View file

@ -0,0 +1,58 @@
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "arch_os.h"
/* Mutex */
static int arch_os_mutex_create_internal(arch_os_mutex_handle_t *phandle, void *attr)
{
*phandle = xSemaphoreCreateMutex();
return 0;
}
int arch_os_mutex_create(arch_os_mutex_handle_t *phandle)
{
arch_os_mutex_create_internal(phandle, NULL);
return 0;
}
int arch_os_mutex_delete(arch_os_mutex_handle_t handle)
{
int ret = 9;
if (handle == NULL)
return -1;
vSemaphoreDelete(handle);
return ret;
}
int arch_os_mutex_put(arch_os_mutex_handle_t handle)
{
if (handle == NULL)
return -1;
xSemaphoreGive(handle);
return 0;
}
int arch_os_mutex_get(arch_os_mutex_handle_t handle, uint32_t wait_ms)
{
xSemaphoreTake(handle, wait_ms);
return 0;
}
/* Thread */
int arch_os_thread_create(arch_os_thread_handle_t *pthread,
const char *name, void (*function)(void* arg),
uint32_t stack_size, void* arg, int priority)
{
int ret = 0;
xTaskCreate(function, name, stack_size, arg, priority, pthread);
return ret;
}

View file

@ -0,0 +1,39 @@
#ifndef __ARCH_OS_H__
#define __ARCH_OS_H__
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "arch_time.h"
#ifndef SCHED_NATIVE_PRIORITY_DEFAULT
#define SCHED_NATIVE_PRIORITY_DEFAULT (4)
#endif
#define ARCH_OS_PRIORITY_DEFAULT SCHED_NATIVE_PRIORITY_DEFAULT
#define ARCH_OS_NATIVE_PRIORITY_DEFAULT SCHED_NATIVE_PRIORITY_DEFAULT
#define ARCH_OS_WAIT_FOREVER (0xFFFFFFFF)
#define ARCH_OS_NO_WAIT (0)
typedef SemaphoreHandle_t arch_os_mutex_handle_t;
typedef TaskHandle_t arch_os_thread_handle_t;
#define ARCH_OS_FUNCTION_RETURN(v) ((void *)(intptr_t)v)
arch_os_thread_handle_t arch_os_thread_current(void);
int arch_os_thread_create(arch_os_thread_handle_t *pthread, const char *name,
void (*function)(void *), uint32_t stack_size, void *arg, int priority);
int arch_os_thread_delete(arch_os_thread_handle_t thread);
void arch_os_thread_finish(void);
void arch_os_enter_critical(void);
void arch_os_exit_critical(void);
int arch_os_mutex_create(arch_os_mutex_handle_t *phandle);
int arch_os_mutex_delete(arch_os_mutex_handle_t handle);
int arch_os_mutex_put(arch_os_mutex_handle_t handle);
int arch_os_mutex_get(arch_os_mutex_handle_t handle, uint32_t wait_ms);
#endif /* __ARCH_OS_H__ */

View file

@ -0,0 +1,136 @@
#include "lfs.h"
#include "arch_psm.h"
extern lfs_t lfs;
#ifndef CONFIG_KV_PSM_PATH
#define CONFIG_KV_PSM_PATH ""
#endif
#ifndef CONFIG_KV_PSM_KEY_LEN
#define CONFIG_KV_PSM_KEY_LEN 64
#endif
#define PSM_KV_FORMAT "%s/%s.%s"
static uint8_t kvs_crc8(uint8_t *buf, uint16_t len)
{
uint8_t crc = 0x00;
uint8_t i = 0;
while (len--) {
crc ^= *buf++;
for (i = 8; i > 0; i--) {
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc <<= 1;
}
}
return crc;
}
int arch_psm_get_value(const char *name_space, const char *key, void *value, size_t length)
{
char name[CONFIG_KV_PSM_KEY_LEN];
int payload = -1;
uint8_t crc;
int fd, ret;
struct lfs_info info;
lfs_file_t file;
snprintf(name, CONFIG_KV_PSM_KEY_LEN, PSM_KV_FORMAT, CONFIG_KV_PSM_PATH, name_space, key);
fd = lfs_file_open(&lfs, &file, name, LFS_O_RDONLY);
if (fd < 0)
return -1;
ret = lfs_stat(&lfs, name, &info);
if (ret < 0)
goto bail;
payload = info.size - 1;
if (payload > length)
goto bail;
ret = lfs_file_read(&lfs, &file, value, payload);
if (ret != payload)
goto bail;
ret = lfs_file_read(&lfs, &file, &crc, sizeof(uint8_t));
if (ret != sizeof(uint8_t))
goto bail;
if (crc != kvs_crc8(value, payload))
ret = -1;
bail:
lfs_file_close(&lfs, &file);
return ret > 0 ? payload : ret;
}
int arch_psm_get_str(const char *name_space, const char *key, char *str, size_t str_size)
{
return arch_psm_get_value(name_space, key, str, str_size);
}
int arch_psm_set_value(const char *name_space, const char *key, const void *value, size_t length)
{
char name[CONFIG_KV_PSM_KEY_LEN];
uint8_t crc;
int fd, ret, ret_close;
int oflags;
struct lfs_info info;
lfs_file_t file;
snprintf(name, CONFIG_KV_PSM_KEY_LEN, PSM_KV_FORMAT, CONFIG_KV_PSM_PATH, name_space, key);
ret = lfs_stat(&lfs, name, &info);
if (ret < 0) {
oflags = LFS_O_RDWR | LFS_O_CREAT;
} else {
oflags = LFS_O_RDWR | LFS_O_TRUNC;
}
fd = lfs_file_open(&lfs, &file, name, oflags);
if (fd < 0)
return -1;
ret = lfs_file_write(&lfs, &file, value, length);
if (ret != length)
goto bail;
crc = kvs_crc8((uint8_t *)value, length);
ret = lfs_file_write(&lfs, &file, &crc, sizeof(uint8_t));
bail:
ret_close = lfs_file_close(&lfs, &file);
if ((ret_close < 0) && (oflags & LFS_O_CREAT)) {
lfs_remove(&lfs, name);
}
return ret < 0 ? ret : (ret_close < 0 ? ret_close : length);
}
int arch_psm_set_str(const char *name_space, const char *key, const char *str)
{
return arch_psm_set_value(name_space, key, str, strlen(str) + 1);
}
int arch_psm_erase_key(const char *name_space, const char *key)
{
char name[CONFIG_KV_PSM_KEY_LEN];
snprintf(name, CONFIG_KV_PSM_KEY_LEN, PSM_KV_FORMAT, CONFIG_KV_PSM_PATH, name_space, key);
lfs_remove(&lfs, name);
return 0;
}
int arch_psm_del_value(const char *name_space, const char *key)
{
return arch_psm_erase_key(name_space, key);
}

View file

@ -0,0 +1,14 @@
#ifndef __ARCH_PSM_H__
#define __ARCH_PSM_H__
int arch_psm_init(void);
int arch_psm_get_value(const char* name_space, const char* key, void *value, size_t length);
int arch_psm_set_value(const char* name_space, const char* key, const void* value, size_t length);
int arch_psm_del_value(const char* name_space, const char* key);
int arch_psm_get_str(const char* name_space, const char* key, char *str, size_t str_size);
int arch_psm_set_str(const char* name_space, const char* key, const char* str);
int arch_psm_erase_key(const char* name_space, const char* key);
#endif /* __ARCH_PSM_H__ */

View file

@ -0,0 +1,99 @@
#include "FreeRTOS.h"
#include "arch_time.h"
/* Time */
uint64_t arch_os_time_system(void)
{
uint64_t curr;
curr = xTaskGetTickCount();
return curr;
}
uint32_t arch_os_ms_now(void)
{
return (uint32_t)arch_os_time_system();
}
uint32_t arch_os_time_now(void)
{
return arch_os_ms_now() / 1000;
}
#if 0
uint32_t arch_os_utc_now(void)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (uint32_t)ts.tv_sec;
}
int arch_os_utc_set(uint32_t utc)
{
struct timespec ts;
ts.tv_sec = utc;
ts.tv_nsec = 0;
return clock_settime(CLOCK_REALTIME, &ts);
}
#else
/*
utc机制:
time机制 utc_offset utc时钟
utc time
*/
static uint32_t utc_offset = 0;
uint32_t arch_os_utc_now(void)
{
return utc_offset + arch_os_time_now();
}
//通过utc标准时间 校准系统,
//返回系统时间偏移 (正数 表示系统时间快过UTC时间值)
int arch_os_utc_set(uint32_t utc)
{
int ret = 0;
uint32_t time_now = arch_os_time_now();
struct timespec ts;
if (utc_offset != 0) { //非第一次读取标准时间
ret = utc_offset + time_now - utc; //本地时间 和 标准时间 差
//暂时不调整斜率,保持简单策略
//arch_os_time_tune(utc - utc_offset); //用标准时间间隔 调整系统时间
utc_offset = utc - time_now; //再一次设置标准时间
} else
utc_offset = utc - time_now; //第一次设置标准时间
/* set CLOCK_REALTIME */
vTaskStepTick(time_now);
return ret;
}
#endif
uint32_t arch_os_ms_elapsed(uint32_t last_ms)
{
uint32_t now = arch_os_ms_now();
if (last_ms < now)
return now - last_ms;
else
return 0xFFFFFFFF - last_ms + now + 1;
}
void arch_os_ms_sleep(uint32_t ms)
{
vTaskDelay(ms);
}

View file

@ -0,0 +1,14 @@
#ifndef __ARCH_TIME_H__
#define __ARCH_TIME_H__
#include <stdint.h>
uint32_t arch_os_ms_now(void);
void arch_os_ms_sleep(uint32_t ms);
uint32_t arch_os_ms_elapsed(uint32_t last_ms);
uint32_t arch_os_time_now(void);
uint64_t arch_os_time_system(void);
int arch_os_utc_set(uint32_t utc);
uint32_t arch_os_utc_now(void);
#endif /* __ARCH_TIME_H__ */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
#ifndef _KV_TEST_H_
#define _KV_TEST_H_
int kv_test(void);
void kv_test5_proc(void *arg);
#endif

View file

@ -0,0 +1,46 @@
/****************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include "kv_test.h"
#include "shell.h"
#include "log.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* kv_test_main
****************************************************************************/
void cmd_kv_test(int argc, char **argv)
{
LOG_I("%s begin\n", __FUNCTION__);
int ret = 0;
int num = 0;
kv_test();
while (1) {
arch_os_ms_sleep(20000);
num++;
}
}

Binary file not shown.

141
examples/littlefs/main.c Normal file
View file

@ -0,0 +1,141 @@
/****************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "shell.h"
#include "bflb_mtimer.h"
#include "bflb_flash.h"
#include "bflb_l1c.h"
#include "bflb_uart.h"
#include "board.h"
#include "log.h"
#include "lfs.h"
#include "lfs_port.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/*!< use memory area after 512k */
#define LITTLEFS_FLASH_SIZE (0x71000)
#define LITTLEFS_FLASH_BLOCK_SIZE 4096
#define LITTLEFS_FLASH_BLOCK_COUNT (LITTLEFS_FLASH_SIZE / LITTLEFS_FLASH_BLOCK_SIZE)
#define LITTLEFS_FLASH_BLOCK_CYCLE 500
/****************************************************************************
* Private Data
****************************************************************************/
static struct bflb_device_s *uart0;
lfs_t lfs;
lfs_file_t file;
/*!< configuration of the filesystem is provided by this struct */
const struct lfs_config cfg = {
// block device operations
.read = lfs_xip_flash_read,
.prog = lfs_xip_flash_prog,
.erase = lfs_xip_flash_erase,
.sync = lfs_xip_flash_sync,
// block device configuration
.read_size = 16,
.prog_size = 16,
.lookahead_size = 16,
.cache_size = 16,
.block_size = LITTLEFS_FLASH_BLOCK_SIZE,
.block_count = LITTLEFS_FLASH_BLOCK_COUNT,
.block_cycles = LITTLEFS_FLASH_BLOCK_CYCLE,
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
extern void shell_init_with_task(struct bflb_device_s *shell);
extern void cmd_kv_test(int argc, char **argv);
SHELL_CMD_EXPORT_ALIAS(cmd_kv_test, kv_test, shell kv test);
/****************************************************************************
* Functions
****************************************************************************/
int main(void)
{
board_init();
uart0 = bflb_device_get_by_name("uart0");
shell_init_with_task(uart0);
// mount the filesystem
int err = lfs_mount(&lfs, &cfg);
// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err) {
LOG_W("try to reformat \r\n");
err = lfs_format(&lfs, &cfg);
if (err) {
LOG_F("reformat fail\r\n");
_CALL_ERROR();
}
LOG_I("reformat success\r\n");
err = lfs_mount(&lfs, &cfg);
if (err) {
LOG_F("mount fail\r\n");
_CALL_ERROR();
}
}
LOG_I("mount success\r\n");
// read current count
uint32_t boot_count = 0;
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
// update boot count
boot_count += 1;
lfs_file_rewind(&lfs, &file);
lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
// remember the storage is not updated until the file is closed successfully
lfs_file_close(&lfs, &file);
// release any resources we were using
// lfs_unmount(&lfs);
// print the boot count
printf("boot_count: %d\n", boot_count);
vTaskStartScheduler();
while (1) {
}
}

View file

@ -0,0 +1,27 @@
set(CONFIG_VLIBC 1)
set(CONFIG_BFLOG 0)
set(CONFIG_FREERTOS 1)
set(CONFIG_MBEDTLS 1)
set(CONFIG_SHELL 1)
set(CONFIG_LITTLEFS 1)
set(CONFIG_LITTLEFS_FLASH_ADDRESS 0x378000)
# Config
## mbedtls
set(CONFIG_MBEDTLS_AES_USE_HW 0)
set(CONFIG_MBEDTLS_BIGNUM_USE_HW 0)
set(CONFIG_MBEDTLS_ECC_USE_HW 0)
set(CONFIG_MBEDTLS_SHA1_USE_HW 0)
set(CONFIG_MBEDTLS_SHA256_USE_HW 0)
set(CONFIG_MBEDTLS_SHA512_USE_HW 0)
# Define
add_definitions(-DCONFIG_EXAMPLES_KV_TEST_PRIORITY=4)
add_definitions(-DCONFIG_EXAMPLES_KV_TEST_STACKSIZE=4096)
add_definitions(-DCONFIG_KV_TEST_TEST_CASE_NUM=5)
add_definitions(-DCONFIG_KV_TEST_FILE_OPER_SUM=20000)
add_definitions(-DCONFIG_KV_TEST_FILE_OPER_PERIOD=1000)
add_definitions(-DCONFIG_KV_PSM_KEY_LEN=64)
# add_definitions(-DLFS_YES_TRACE)