更新硬件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,229 @@
/*
* 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 "luat_base.h"
#include "luat_rtos.h"
#include "luat_fs.h"
#include "luat_crypto.h"
#include "luat_debug.h"
#include "luat_mem.h"
static int print_fs_info()
{
luat_fs_info_t fs_info = {0};
luat_fs_info("/", &fs_info);
// 打印文件系统空间信息
LUAT_DEBUG_PRINT("fs_info %s %d %d %d %d",
fs_info.filesystem,
fs_info.type,
fs_info.total_block,
fs_info.block_used,
fs_info.block_size);
}
static int recur_fs(const char* dir_path)
{
luat_fs_dirent_t *fs_dirent = LUAT_MEM_MALLOC(sizeof(luat_fs_dirent_t)*100);
memset(fs_dirent, 0, sizeof(luat_fs_dirent_t)*100);
int lsdir_cnt = luat_fs_lsdir(dir_path, fs_dirent, 0, 100);
if (lsdir_cnt > 0)
{
char path[255] = {0};
LUAT_DEBUG_PRINT("dir_path=%s, lsdir_cnt=%d", dir_path, lsdir_cnt);
for (size_t i = 0; i < lsdir_cnt; i++)
{
memset(path, 0, sizeof(path));
switch ((fs_dirent+i)->d_type)
{
// 文件类型
case 0:
snprintf(path, sizeof(path)-1, "%s%s", dir_path, (fs_dirent+i)->d_name);
LUAT_DEBUG_PRINT("\tfile=%s, size=%d", path, luat_fs_fsize(path));
break;
// 文件夹类型
case 1:
// 移芯618平台不支持文件夹创建操作以下注释掉的两行代码未经测试
// snprintf(path, sizeof(path)-1, "%s/%s/", dir_path, (fs_dirent+i)->d_name);
// recur_fs(path);
break;
default:
break;
}
}
}
LUAT_MEM_FREE(fs_dirent);
fs_dirent = NULL;
return lsdir_cnt;
}
// 演示文件操作, luat_fs_XXX 文件函数
void exmaple_fs_luat_file(void) {
FILE* fp = NULL;
uint8_t *buff = NULL;
const char* filepath = "lfs_test.txt";
const char* newpath = "newpath";
char tmp[100];
int ret = 0;
size_t size = 0;
LUAT_DEBUG_PRINT("check file exists? %s", filepath);
// 通过获取大小, 判断文件是否存在, 存在就删除之
if (luat_fs_fexist(filepath)) {
LUAT_DEBUG_PRINT("remove %s", filepath);
luat_fs_remove(filepath);
}
//----------------------------------------------
// 文件写入演示
//----------------------------------------------
// 开始写入文件
LUAT_DEBUG_PRINT("test lfs file write");
fp = luat_fs_fopen(filepath, "wb+");
if (!fp) {
LUAT_DEBUG_PRINT("file open failed %s", filepath);
return;
}
// 产生随机数据, 模拟业务写入
LUAT_DEBUG_PRINT("call malloc and rngGenRandom");
buff = malloc(24 * 100);
if (buff == NULL) {
LUAT_DEBUG_PRINT("out of memory ?");
luat_fs_fclose(fp);
goto exit;
}
for (size_t i = 0; i < 100; i++)
{
luat_crypto_trng(buff + i*24, 24);
}
// 按块写入数据
LUAT_DEBUG_PRINT("call luat_fs_write");
for (size_t i = 0; i < 24; i++)
{
ret = luat_fs_fwrite((const void*)(buff + i * 100), 100, 1, fp);
if (ret < 0) {
LUAT_DEBUG_PRINT("fail to write ret %d", ret);
luat_fs_fclose(fp);
goto exit;
}
}
// 关闭文件
luat_fs_fclose(fp);
//----------------------------------------------
// 文件读取演示
//----------------------------------------------
// 读取文件
fp = luat_fs_fopen(filepath, "r");
if (!fp) {
LUAT_DEBUG_PRINT("file open failed %s", filepath);
goto exit;
}
for (size_t i = 0; i < 24; i++)
{
ret = luat_fs_fread(tmp, 100, 1, fp);
if (ret < 0) {
LUAT_DEBUG_PRINT("fail to write ret %d", ret);
luat_fs_fclose(fp);
goto exit;
}
if (memcmp(tmp, buff + i * 100, 100) != 0) {
LUAT_DEBUG_PRINT("file data NOT match");
}
}
// 直接定位 offset=100的位置, 重新读取
luat_fs_fseek(fp, 100, SEEK_SET);
ret = luat_fs_fread(tmp, 100, 1, fp);
if (memcmp(tmp, buff + 100, 100) != 0) {
LUAT_DEBUG_PRINT("file data NOT match at offset 100");
}
ret = luat_fs_ftell(fp);
if (ret != 200) {
// 按前面的逻辑, 先设置到100, 然后读取100, 当前偏移量应该是200
LUAT_DEBUG_PRINT("file seek NOT match at offset 200");
}
// 关闭句柄
luat_fs_fclose(fp);
//----------------------------------------------
// 文件截断演示
//----------------------------------------------
// // 直接用路径裁剪
luat_fs_truncate(filepath, 300);
//----------------------------------------------
// 文件改名演示
//----------------------------------------------
// 文件还在, 测试改名
luat_fs_remove(newpath);
luat_fs_rename(filepath, newpath);
// 读取文件, 老路径应该得到文件大小不大于0
size = luat_fs_fsize(filepath);
if (size > 0) {
LUAT_DEBUG_PRINT("file shall not exist");
}
// 读取文件, 新路径应该得到文件大小等于300
size = luat_fs_fsize(newpath);
if (size != 300) {
LUAT_DEBUG_PRINT("file shall 300 byte but %d", size);
}
luat_fs_remove(filepath);
luat_fs_remove(newpath);
//------------------------------------------------------
// 演示完毕, 清理资源
exit:
if (buff != NULL) {
free(buff);
}
LUAT_DEBUG_PRINT("file example exited");
return;
}
void exmaple_fs_luat_main(void) {
luat_fs_init(); // 必须先初始化
print_fs_info();
recur_fs("/");
exmaple_fs_luat_file();
luat_rtos_task_sleep(1000);
}

View File

@@ -0,0 +1,62 @@
/*
* 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 "luat_base.h"
#include "luat_rtos.h"
#include "luat_debug.h"
extern void exmaple_fs_luat_main(void);
static void fs_example(void *param)
{
luat_rtos_task_sleep(1500);
exmaple_fs_luat_main();
while(1)
{
luat_rtos_task_sleep(1000);
LUAT_DEBUG_PRINT("fs demo is done");
}
}
static void task_demoE_init(void)
{
luat_rtos_task_handle handle;
luat_rtos_task_create(&handle, 4*1024, 50, "fs", fs_example, NULL, 0);
}
//启动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()