更新硬件SDK

This commit is contained in:
kerwincui
2023-03-04 03:44:56 +08:00
parent dcdf6e1b7c
commit e39d3d2f03
1900 changed files with 663153 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
# 文件系统API说明
## 概述
* 文件系统的主体是littlefs, 总空间288k, 可用100k左右.
* 有多套API, 各有特色, 但不应该混用
## Osa文件API
位于 `osasys.h`, 只有少量函数, 与posix API 类似. 不涉及目录操作
```c
OSAFILE OsaFopen(const char *fileName, const char* mode);
INT32 OsaFtell(OSAFILE fp);
INT32 OsaFclose(OSAFILE fp);
UINT32 OsaFread(void *buf, UINT32 size, UINT32 count, OSAFILE fp);
UINT32 OsaFwrite(void *buf, UINT32 size, UINT32 count, OSAFILE fp);
INT32 OsaFseek(OSAFILE fp, INT32 offset, UINT8 seekType);
UINT32 OsaFremove(const char *fileName);
```
通用调用逻辑:
```c
#define BUFF_SIZE (512)
char buff[BUFF_SIZE];
UINT32 rlen = 0;
OSAFILE fp = OsaFopen("fs_test", "r");
if (fp) { // 若成功, 必不为NULL
// 读写数据
while ((rlen = OsaFread(buff, BUFF_SIZE, 1, fp)) > 0) {
// 处理读取到的数据
}
OsaFclose(fp);
}
```
## LiffteFs 文件API
在原始的lifftefs上, 封装成 `lfs_port.h`
注意:
* 不要调用 `LFS_init` `LFS_deinit` , 底层会自行处理
* API 均以 `LFS_` 开头, 为同步阻塞且线程安全的函数. 与原生lifftefs的API基本上一一对应
通用调用逻辑:
```c
#define BUFF_SIZE (512)
char buff[BUFF_SIZE];
UINT32 rlen = 0;
int ret = 0;
lfs_file_t *file = malloc(sizeof(lfs_file_t));
if (file == NULL) {
// 内存分配出错
return -1;
}
ret = LFS_fileOpen(file, "fs_test", LFS_O_RDONLY);
if (ret == LFS_ERR_OK) { // 若成功, 必不为NULL
// 读写数据
while ((rlen = LFS_fileRead(file, buff, BUFF_SIZE)) > 0) {
// 处理读取到的数据
}
LFS_fileClose(file);
}
free(file); // 注意释放内存
```

View File

@@ -0,0 +1,350 @@
#ifndef __RTE_DEVICE_H
#define __RTE_DEVICE_H
#include "ec618.h"
/* Peripheral IO Mode Select, Must Configure First !!!
Note, when receiver works in DMA_MODE, interrupt is also enabled to transfer tailing bytes.
*/
#define POLLING_MODE 0x1
#define DMA_MODE 0x2
#define IRQ_MODE 0x3
#define UNILOG_MODE 0x4
#define RTE_UART0_TX_IO_MODE UNILOG_MODE
#define RTE_UART0_RX_IO_MODE IRQ_MODE
#define USART0_RX_TRIG_LVL (30)
#define RTE_UART1_TX_IO_MODE DMA_MODE
#define RTE_UART1_RX_IO_MODE DMA_MODE
#define RTE_UART2_TX_IO_MODE POLLING_MODE
#define RTE_UART2_RX_IO_MODE DMA_MODE
#define RTE_SPI0_IO_MODE POLLING_MODE
#define RTE_SPI1_IO_MODE POLLING_MODE
#define RTE_I2C0_IO_MODE POLLING_MODE
#define RTE_I2C1_IO_MODE POLLING_MODE
// I2C0 (Inter-integrated Circuit Interface) [Driver_I2C0]
// Configuration settings for Driver_I2C0 in component ::Drivers:I2C
#define RTE_I2C0 1
// { PAD_PIN28}, // 0 : gpio13 / 2 : I2C0 SCL
// { PAD_PIN27}, // 0 : gpio12 / 2 : I2C0 SDA
#define RTE_I2C0_SCL_BIT 28 // AUDIO use 28
#define RTE_I2C0_SCL_FUNC PAD_MUX_ALT2
#define RTE_I2C0_SDA_BIT 27 // AUDIO use 27
#define RTE_I2C0_SDA_FUNC PAD_MUX_ALT2
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_I2C0_DMA_TX_EN 0
#define RTE_I2C0_DMA_TX_REQID DMA_REQUEST_I2C0_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_I2C0_DMA_RX_EN 0
#define RTE_I2C0_DMA_RX_REQID DMA_REQUEST_I2C0_RX
// I2C1 (Inter-integrated Circuit Interface) [Driver_I2C1]
// Configuration settings for Driver_I2C1 in component ::Drivers:I2C
#define RTE_I2C1 1
// { PAD_PIN20}, // 0 : gpio5 / 2 : I2C1 SCL
// { PAD_PIN19}, // 0 : gpio4 / 2 : I2C1 SDA
#define RTE_I2C1_SCL_BIT 20
#define RTE_I2C1_SCL_FUNC PAD_MUX_ALT2
#define RTE_I2C1_SDA_BIT 19
#define RTE_I2C1_SDA_FUNC PAD_MUX_ALT2
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_I2C1_DMA_TX_EN 1
#define RTE_I2C1_DMA_TX_REQID DMA_REQUEST_I2C1_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_I2C1_DMA_RX_EN 1
#define RTE_I2C1_DMA_RX_REQID DMA_REQUEST_I2C1_RX
// UART0 (Universal asynchronous receiver transmitter) [Driver_USART0]
// Configuration settings for Driver_USART0 in component ::Drivers:USART
#define RTE_UART0_CTS_PIN_EN 0
#define RTE_UART0_RTS_PIN_EN 0
// { PAD_PIN27}, // 0 : gpio12 / 3 : UART0 RTSn
// { PAD_PIN28}, // 0 : gpio13 / 3 : UART0 CTSn
// { PAD_PIN29}, // 0 : gpio14 / 3 : UART0 RXD
// { PAD_PIN30}, // 0 : gpio15 / 3 : UART0 TXD
#define RTE_UART0_RTS_BIT 27
#define RTE_UART0_RTS_FUNC PAD_MUX_ALT3
#define RTE_UART0_CTS_BIT 28
#define RTE_UART0_CTS_FUNC PAD_MUX_ALT3
#define RTE_UART0_RX_BIT 29
#define RTE_UART0_RX_FUNC PAD_MUX_ALT3
#define RTE_UART0_TX_BIT 30
#define RTE_UART0_TX_FUNC PAD_MUX_ALT3
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART0_DMA_TX_REQID DMA_REQUEST_USART0_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART0_DMA_RX_REQID DMA_REQUEST_USART0_RX
// UART1 (Universal asynchronous receiver transmitter) [Driver_USART1]
// Configuration settings for Driver_USART1 in component ::Drivers:USART
#define RTE_UART1_CTS_PIN_EN 1
#define RTE_UART1_RTS_PIN_EN 1
// { PAD_PIN31}, // 0 : gpio16 / 1 : UART1 RTS
// { PAD_PIN32}, // 0 : gpio17 / 1 : UART1 CTS
// { PAD_PIN33}, // 0 : gpio18 / 1 : UART1 RXD
// { PAD_PIN34}, // 0 : gpio19 / 1 : UART1 TXD
#define RTE_UART1_RTS_BIT 31
#define RTE_UART1_RTS_FUNC PAD_MUX_ALT1
#define RTE_UART1_CTS_BIT 32
#define RTE_UART1_CTS_FUNC PAD_MUX_ALT1
#define RTE_UART1_RX_BIT 33
#define RTE_UART1_RX_FUNC PAD_MUX_ALT1
#define RTE_UART1_TX_BIT 34
#define RTE_UART1_TX_FUNC PAD_MUX_ALT1
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART1_DMA_TX_REQID DMA_REQUEST_USART1_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART1_DMA_RX_REQID DMA_REQUEST_USART1_RX
// UART2 (Universal asynchronous receiver transmitter) [Driver_USART2]
// Configuration settings for Driver_USART2 in component ::Drivers:USART
#define RTE_UART2_CTS_PIN_EN 0
#define RTE_UART2_RTS_PIN_EN 0
// { PAD_PIN25}, // 0 : gpio10 / 3 : UART2 RXD
// { PAD_PIN26}, // 0 : gpio11 / 3 : UART2 TXD
#define RTE_UART2_RX_BIT 25
#define RTE_UART2_RX_FUNC PAD_MUX_ALT3
#define RTE_UART2_TX_BIT 26
#define RTE_UART2_TX_FUNC PAD_MUX_ALT3
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART2_DMA_TX_REQID DMA_REQUEST_USART2_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_UART2_DMA_RX_REQID DMA_REQUEST_USART2_RX
// SPI0 (Serial Peripheral Interface) [Driver_SPI0]
// Configuration settings for Driver_SPI0 in component ::Drivers:SPI
#define RTE_SPI0 1
// { PAD_PIN21}, // 0 : gpio16 / 1 : UART1 RTS / 2 : SPI0 SSn
// { PAD_PIN22}, // 0 : gpio11 / 1 : UART1 CTS / 2 : SPI0 MOSI
// { PAD_PIN23}, // 0 : gpio14 / 1 : UART1 RXD / 2 : SPI0 MISO
// { PAD_PIN24}, // 0 : gpio15 / 1 : UART1 TXD / 2 : SPI0 SCLK
#define RTE_SPI0_SSN_BIT 21
#define RTE_SPI0_SSN_FUNC PAD_MUX_ALT2
#define RTE_SPI0_MOSI_BIT 22
#define RTE_SPI0_MOSI_FUNC PAD_MUX_ALT2
#define RTE_SPI0_MISO_BIT 23
#define RTE_SPI0_MISO_FUNC PAD_MUX_ALT2
#define RTE_SPI0_SCLK_BIT 24
#define RTE_SPI0_SCLK_FUNC PAD_MUX_ALT2
#define RTE_SPI0_SSN_GPIO_INSTANCE 1
#define RTE_SPI0_SSN_GPIO_INDEX 0
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_SPI0_DMA_TX_REQID DMA_REQUEST_SPI0_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_SPI0_DMA_RX_REQID DMA_REQUEST_SPI0_RX
// SPI1 (Serial Peripheral Interface) [Driver_SPI1]
// Configuration settings for Driver_SPI1 in component ::Drivers:SPI
#define RTE_SPI1 1
// { PAD_PIN13}, // 0 : gpio2 / 1 : UART0 RTSn / 3 : SPI1 SSn
// { PAD_PIN14}, // 0 : gpio3 / 1 : UART0 CTSn / 3 : SPI1 MOSI
// { PAD_PIN15}, // 0 : gpio4 / 1 : UART0 RXD / 3 : SPI1 MISO
// { PAD_PIN16}, // 0 : gpio5 / 1 : UART0 TXD / 3 : SPI1 SCLK
#define RTE_SPI1_SSN_BIT 13
#define RTE_SPI1_SSN_FUNC PAD_MUX_ALT3
#define RTE_SPI1_MOSI_BIT 14
#define RTE_SPI1_MOSI_FUNC PAD_MUX_ALT3
#define RTE_SPI1_MISO_BIT 15
#define RTE_SPI1_MISO_FUNC PAD_MUX_ALT3
#define RTE_SPI1_SCLK_BIT 16
#define RTE_SPI1_SCLK_FUNC PAD_MUX_ALT3
#define RTE_SPI1_SSN_GPIO_INSTANCE 0
#define RTE_SPI1_SSN_GPIO_INDEX 2
// DMA
// Tx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_SPI1_DMA_TX_REQID DMA_REQUEST_SPI1_TX
// Rx
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
#define RTE_SPI1_DMA_RX_REQID DMA_REQUEST_SPI1_RX
// PWM0 Controller [Driver_PWM0]
// Configuration settings for Driver_PWM0 in component ::Drivers:PWM
#define RTE_PWM 1
#define EFUSE_INIT_MODE POLLING_MODE
#define L2CTLS_INIT_MODE POLLING_MODE
#define FLASH_BARE_RW_MODE 1
#define RTE_UART0 1
#define RTE_UART1 1
#define RTE_UART2 1
/* to enable external thermal */
#define EXTERNAL_NTC_EXIST 0
#if (RTE_UART1 == 1)
#define UART1_DTR_PAD_INDEX 26 // GPIO11
#define UART1_DTR_GPIO_INSTANCE 0
#define UART1_DTR_GPIO_PIN 11
#define UART1_RI_PAD_INDEX 44 // AONIO 4 = GPIO24
#define UART1_RI_GPIO_INSTANCE 1
#define UART1_RI_GPIO_PIN 8
#define UART1_RI_PWM_INSTANCE 1
#define UART1_RI_PWM_CLK_ID FCLK_TIMER1
#define UART1_RI_PWM_CLK_SEL FCLK_TIMER1_SEL_26M
#define UART1_DCD_PAD_INDEX 45 // AONIO 5 = GPIO25
#define UART1_DCD_GPIO_INSTANCE 1
#define UART1_DCD_GPIO_PIN 9
#endif
#if (RTE_UART2 == 1)
#define UART2_DTR_PAD_INDEX 25 // GPIO10
#define UART2_DTR_GPIO_INSTANCE 0
#define UART2_DTR_GPIO_PIN 10
#define UART2_RI_PAD_INDEX 43 // AONIO 3 = GPIO23
#define UART2_RI_GPIO_INSTANCE 1
#define UART2_RI_GPIO_PIN 7
#define UART2_RI_PWM_INSTANCE 0
#define UART2_RI_PWM_CLK_ID FCLK_TIMER0
#define UART2_RI_PWM_CLK_SEL FCLK_TIMER0_SEL_26M
#define UART2_DCD_PAD_INDEX 47 // AONIO 7 = GPIO27
#define UART2_DCD_GPIO_INSTANCE 1
#define UART2_DCD_GPIO_PIN 11
#endif
#define NETLIGHT_PAD_INDEX 46 // AONIO 6 = GPIO26
#define NETLIGHT_PAD_ALT_FUNC PAD_MUX_ALT5
#define NETLIGHT_PWM_INSTANCE 3
//USIM1 OPTION1
#define USIM1_URST_OP1_PAD_INDEX 19 // GPIO4
#define USIM1_URST_OP1_GPIO_INSTANCE 0
#define USIM1_URST_OP1_GPIO_PIN 4
#define USIM1_UCLK_OP1_PAD_INDEX 20 // GPIO5
#define USIM1_UCLK_OP1_GPIO_INSTANCE 0
#define USIM1_UCLK_OP1_GPIO_PIN 5
#define USIM1_UIO_OP1_PAD_INDEX 21 // GPIO6
#define USIM1_UIO_OP1_GPIO_INSTANCE 0
#define USIM1_UIO_OP1_GPIO_PIN 6
//USIM1 OPTION2
#define USIM1_UIO_OP2_PAD_INDEX 27 // GPIO12
#define USIM1_UIO_OP2_GPIO_INSTANCE 0
#define USIM1_UIO_OP2_GPIO_PIN 12
#define USIM1_URST_OP2_PAD_INDEX 28 // GPIO13
#define USIM1_URST_OP2_GPIO_INSTANCE 0
#define USIM1_URST_OP2_GPIO_PIN 13
#define USIM1_UCLK_OP2_PAD_INDEX 29 // GPIO14
#define USIM1_UCLK_OP2_GPIO_INSTANCE 0
#define USIM1_UCLK_OP2_GPIO_PIN 14
//USIM1 clock latched by AONIO, for example, use AONIO-6 test on EVB
#define AONIO_6_PAD_INDEX 46 // AONIO 6 = GPIO26
#define AONIO_6_GPIO_INSTANCE 1
#define AONIO_6_GPIO_PIN 10
#define RTE_CSPI0 0
#define RTE_CSPI0_MCLK_PAD_ADDR 39
#define RTE_CSPI0_MCLK_FUNC PAD_MUX_ALT1
#define RTE_CSPI0_PCLK_PAD_ADDR 35
#define RTE_CSPI0_PCLK_FUNC PAD_MUX_ALT1
#define RTE_CSPI0_CS_PAD_ADDR 36
#define RTE_CSPI0_CS_FUNC PAD_MUX_ALT1
#define RTE_CSPI0_SDO0_PAD_ADDR 37
#define RTE_CSPI0_SDO0_FUNC PAD_MUX_ALT1
#define RTE_CSPI0_SDO1_PAD_ADDR 38
#define RTE_CSPI0_SDO1_FUNC PAD_MUX_ALT1
// DMA CSPI0 Request ID
#define RTE_CSPI0_DMA_RX_REQID DMA_REQUEST_I2S0_RX
// CSPI1 Configuration
#define RTE_CSPI1 1
#define RTE_CSPI1_MCLK_PAD_ADDR 18
#define RTE_CSPI1_MCLK_FUNC PAD_MUX_ALT1
#define RTE_CSPI1_PCLK_PAD_ADDR 19
#define RTE_CSPI1_PCLK_FUNC PAD_MUX_ALT1
#define RTE_CSPI1_CS_PAD_ADDR 20
#define RTE_CSPI1_CS_FUNC PAD_MUX_ALT1
#define RTE_CSPI1_SDO0_PAD_ADDR 21
#define RTE_CSPI1_SDO0_FUNC PAD_MUX_ALT1
#define RTE_CSPI1_SDO1_PAD_ADDR 22
#define RTE_CSPI1_SDO1_FUNC PAD_MUX_ALT1
// DMA CSPI1 Request ID
#define RTE_CSPI1_DMA_RX_REQID DMA_REQUEST_I2S1_RX
#endif /* __RTE_DEVICE_H */

View File

@@ -0,0 +1,185 @@
/*
* Copyright (c) 2022 OpenLuat & AirM2M
*
* 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.
*/
/*
LFS_xxx 系列函数, 分文件函数和文件夹, 还有几个文件系统相关的函数.
**注意**: 不要调用 LFS_init LFS_deinit LFS_format 函数!!!
*/
#include "common_api.h"
#include "FreeRTOS.h"
#include "task.h"
#include "lfs_port.h"
#include "rng.h"
// 演示文件操作, LFS_XXX 文件函数
void exmaple_fs_lfs_file(void) {
lfs_file_t fp = {0}; // 注意, 如果需要跨函数使用, lfs_file_t需要malloc
uint8_t *buff = NULL;
const char* filepath = "lfs_test.txt";
struct lfs_info info = {0};
char tmp[100];
int ret = 0;
DBG("check file exists? %s", filepath);
// 通过获取大小, 判断文件是否存在, 存在就删除之
if (LFS_stat(filepath, &info) == LFS_ERR_OK) {
if (info.size > 0) {
DBG("remove %s", filepath);
LFS_remove(filepath);
}
}
//----------------------------------------------
// 文件写入演示
//----------------------------------------------
// 开始写入文件
DBG("test lfs file write");
ret = LFS_fileOpen(&fp, filepath, LFS_O_CREAT | LFS_O_RDWR | LFS_O_TRUNC);
if (ret != LFS_ERR_OK) {
DBG("file open failed %d", ret);
return;
}
// 产生随机数据, 模拟业务写入
DBG("call malloc and rngGenRandom");
buff = malloc(24 * 100);
if (buff == NULL) {
DBG("out of memory ?");
LFS_fileClose(&fp);
goto exit;
}
for (size_t i = 0; i < 100; i++)
{
rngGenRandom(buff + i*24);
}
// 按块写入数据
DBG("call LFS_fileWrite");
for (size_t i = 0; i < 24; i++)
{
ret = LFS_fileWrite(&fp, (const void*)(buff + i * 100), 100);
if (ret != 100) {
DBG("fail to write ret %d", ret);
LFS_fileClose(&fp);
goto exit;
}
}
// 为保证正确性, 关闭前可先执行sync操作
LFS_fileSync(&fp);
// 关闭文件
LFS_fileClose(&fp);
//----------------------------------------------
// 文件读取演示
//----------------------------------------------
// 读取文件
ret = LFS_fileOpen(&fp, filepath, LFS_O_RDONLY);
if (ret != LFS_ERR_OK) {
DBG("file open failed %d", ret);
goto exit;
}
for (size_t i = 0; i < 24; i++)
{
ret = LFS_fileRead(&fp, tmp, 100);
if (ret != 100) {
DBG("fail to write ret %d", ret);
LFS_fileClose(&fp);
goto exit;
}
if (memcmp(tmp, buff + i * 100, 100) != 0) {
DBG("file data NOT match");
}
}
// 直接定位 offset=100的位置, 重新读取
LFS_fileSeek(&fp, 100, LFS_SEEK_SET);
ret = LFS_fileRead(&fp, tmp, 100);
if (memcmp(tmp, buff + 100, 100) != 0) {
DBG("file data NOT match at offset 100");
}
ret = LFS_fileTell(&fp);
if (ret != 200) {
// 按前面的逻辑, 先设置到100, 然后读取100, 当前偏移量应该是200
DBG("file seek NOT match at offset 200");
}
// 关闭句柄
LFS_fileClose(&fp);
//----------------------------------------------
// 文件截断演示
//----------------------------------------------
// LFS_fileTruncate需要以读写方式打开文件,否则会崩
ret = LFS_fileOpen(&fp, filepath, LFS_O_RDWR);
// 缩减文件到300字节
LFS_fileTruncate(&fp, 300);
ret = LFS_fileSize(&fp);
if (ret != 300) {
// 裁剪后文件大小应该是300
DBG("file size NOT 300, but %d", ret);
}
LFS_fileClose(&fp);
//----------------------------------------------
// 文件改名演示
//----------------------------------------------
// 文件还在, 测试改名
LFS_remove("newpath");
LFS_rename(filepath, "newpath");
// 读取文件, 老路径应该得到文件大小不大于0
info.size = 0;
LFS_stat(filepath, &info);
if (info.size > 0) {
DBG("file shall not exist");
}
// 读取文件, 新路径应该得到文件大小等于300
info.size = 0;
LFS_stat("newpath", &info);
if (info.size != 300) {
DBG("file shall 300 byte but %d", info.size);
}
LFS_remove(filepath);
LFS_remove("newpath");
//------------------------------------------------------
// 演示完毕, 清理资源
exit:
if (buff != NULL) {
free(buff);
}
DBG("file example exited");
return;
}
// 演示文件操作, LFS_XXX 文件夹函数
void exmaple_fs_lfs_dir(void) {
// TODO
}
void exmaple_fs_lfs_main(void) {
exmaple_fs_lfs_file();
vTaskDelay(1000);
exmaple_fs_lfs_dir();
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (c) 2022 OpenLuat & AirM2M
*
* 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.
*/
/*
本demo使用OsaXXX系列API进行演示.
提示: 底层文件系统是基于lifftefs, 具有掉电不丢数据的特性.
在写入新数据时, 若文件路径已经存在, 在打开该文件进行写操作时,原有数据并不会马上被清除.
该特性, 在极端情况下, 会出现2倍的flash空间消耗. 所以, 下面是覆写文件时的建议
1. 若该文件重要, 那按常规写法进行, 且重要文件应保持小体积.
2. 若该文件不重要, 那应该先执行 OsaRemove, 然后在OsaFopen
*/
// 标准include
#include "common_api.h"
#include "FreeRTOS.h"
#include "task.h"
// 引入osa文件API
#include "osasys.h"
// 引入随机数API
#include "rng.h"
#define FILE_NAME "test.txt"
#define BUFF_SIZE (24)
// 这个buff的用途是存放file write的随机数据, 与 file read的数据进行对比
// buff size选24是因为rngGenRandom固定24字节
static uint8_t tmpbuff[BUFF_SIZE];
static void exmaple_file_write(void) {
//----------------------------------------------
// 文件写入演示
//----------------------------------------------
// 可选, 先移除现有文件, 详情看本文件头部的提示
// OsaRemove(FILE_NAME);
// OsaFopen的用法用fopen一致, 支持 rb/wb/wb+/a/a+等操作
OSAFILE fp = OsaFopen(FILE_NAME, "wb+");
if (fp == NULL) {
DBG("open file for write, failed");
return;
}
// 随机数生成器, 注意其输入是24字节的数组.
rngGenRandom(tmpbuff);
// 写入数据, 与 fwrite 一个样, 返回值写入的块数量(1), 而非字节数
uint32_t ret = OsaFwrite(tmpbuff, BUFF_SIZE, 1, fp);
if (ret != 1) {
DBG("fail to write, excpt 1 but %d", BUFF_SIZE, ret);
}
// 操作完成后,务必关闭文件句柄
OsaFclose(fp);
return;
}
static void exmaple_file_read(void) {
//----------------------------------------------
// 文件读取演示
//----------------------------------------------
// OsaFopen的用法用fopen一致, 支持 rb/wb/wb+/a/a+等操作
OSAFILE fp = OsaFopen(FILE_NAME, "rb");
if (fp == NULL) {
DBG("open file for read, failed");
return;
}
char buff[BUFF_SIZE];
// 读取数据, 与 fread 一个样, 返回值是块数量(1), 而非字节数
uint32_t ret = OsaFread(buff, BUFF_SIZE, 1, fp);
if (ret != 1) {
DBG("fail to read, excpt %d but %d", BUFF_SIZE, ret);
}
// 与 fwrite的数据进行对比, 理应一致
if (memcmp(buff, tmpbuff, BUFF_SIZE)) {
DBG("file content NOT match!!");
}
// 最后务必关闭文件句柄
OsaFclose(fp);
return;
}
void exmaple_fs_osa_main(void) {
vTaskDelay(1000);
exmaple_file_write();
vTaskDelay(1000);
exmaple_file_read();
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2022 OpenLuat & AirM2M
*
* 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.
*/
// 本文件为入口函数
// 具体演示在 example_fs_osa.c 和 example_fs_lfs.c
#include "common_api.h"
#include "FreeRTOS.h"
#include "task.h"
extern void exmaple_fs_osa_main(void);
extern void exmaple_fs_lfs_main(void);
static void usb_serial_input_dummy_cb(uint8_t channel, uint8_t *input, uint32_t len)
{
DBG("usb serial get %dbyte, test mode, send back", len);
usb_serial_output(channel, input, len);
}
static void hw_demoA_init(void)
{
DBG("this hw demo1");
set_usb_serial_input_callback(usb_serial_input_dummy_cb);
}
static void fs_example(void *param)
{
vTaskDelay(1500);
exmaple_fs_osa_main();
vTaskDelay(500);
exmaple_fs_lfs_main();
while(1)
{
vTaskDelay(1000);
DBG("fs demo is done");
}
vTaskDelete(NULL);
}
static void task_demoE_init(void)
{
xTaskCreate(fs_example, "", 8*1024, NULL, 20, NULL);
}
// static void task_demoF_init(void)
// {
// xTaskCreate(task2, "", 256, NULL, 20, NULL);
// }
//启动hw_demoA_init启动位置硬件初始1级
INIT_HW_EXPORT(hw_demoA_init, "1");
//启动hw_demoB_init启动位置硬件初始2级
// INIT_HW_EXPORT(hw_demoB_init, "2");
//启动dr_demoC_init启动位置驱动1级
// INIT_DRV_EXPORT(dr_demoC_init, "1");
//启动dr_demoD_init启动位置驱动2级
// INIT_DRV_EXPORT(dr_demoD_init, "2");
//启动task_demoE_init启动位置任务1级
INIT_TASK_EXPORT(task_demoE_init, "1");
//启动task_demoF_init启动位置任务2级
// INIT_TASK_EXPORT(task_demoF_init, "2");

View File

@@ -0,0 +1,20 @@
local TARGET_NAME = "example_filesystem"
local LIB_DIR = "$(buildir)/".. TARGET_NAME .. "/"
local LIB_NAME = "lib" .. TARGET_NAME .. ".a "
target(TARGET_NAME)
set_kind("static")
set_targetdir(LIB_DIR)
--加入代码和头文件
add_includedirs("/inc",{public = true})
add_files("/src/*.c",{public = true})
--路径可以随便写,可以加任意路径的代码,下面代码等效上方代码
-- add_includedirs(SDK_TOP .. "project/" .. TARGET_NAME .. "/inc",{public = true})
-- add_files(SDK_TOP .. "project/" .. TARGET_NAME .. "/src/*.c",{public = true})
--可以继续增加add_includedirs和add_files
--自动链接
LIB_USER = LIB_USER .. SDK_TOP .. LIB_DIR .. LIB_NAME .. " "
--甚至可以加入自己的库
target_end()