|
移植 FILEX 文件系统后,因为要使用 LUA 直接执行文件的功能,就需要实现标准库中的文件操作。经过摸索已经实现了用到的需求(不是全部的标准库文件操作),实现流程如下:
1、开启 microlib 库,不开启微库的实现流程与这里描述的不一样,复杂得多。
2、移植好 FILEX。 具体移植过程不详述,可参考论坛中其他道友的帖子。
3、实现 fopen, fclose, fwrite, fread, fseek 五个文件操作函数。 其中 fopen 只实现了 "r", "r+", "w" 和 “w+” 四种模式,且行为与标准库不完全一样,但已满足目前的需求,可自行添加更多功能。
4、实现 fputs 和 fgets 函数。主要用于 printf。
5、代码中的 BSP_COM_Send 是串口发送函数,要自己实现。 如果要用到 lua 的 print 函数,则要在 fwrite 中调用 BSP_COM_Send ,printf 要在 fputs 中调用 BSP_COM_Send 。6、time, exit 和 system 是移植 LUA 需要添加的函数。
代码如下:
- #include <stdio.h>
- #include <string.h>
复制代码
代码贴上后发布出来咋只有 2 行? 重贴一次
- #include <stdio.h>
- #include <string.h>
- #include "fx_api.h"
- VOID _fx_spiflash_driver(FX_MEDIA *media_ptr);
- static unsigned char media_memory[4096];
- static FX_MEDIA fs_disk;
- static uint8_t fs_is_inited = 0;
- static TX_BLOCK_POOL fs_block_pool;
- /*
- 文件系统初始化
- 每次打开文件时都要进行初始化,如果filex已经初始化,则仅进行计数累加,如果未初始化则初始化filex
- */
- static UINT fs_init(void)
- {
- TX_INTERRUPT_SAVE_AREA
- UINT status = FX_SUCCESS;
- static uint8_t fs_block_inited = 0;
- TX_DISABLE
- /* 创建文件系统的块内存,用于分配 FX_FILE 文件句柄 */
- if(fs_block_inited == 0)
- {
- status = tx_block_pool_create(&fs_block_pool, "fs block pool", sizeof(FX_FILE), (void*)BSP_FILEX_BLOCK_ADDR, BSP_FILEX_BLOCK_SIZE);
- if(status != TX_SUCCESS)
- {
- TX_RESTORE
- return status;
- }
- fs_block_inited = 1;
- }
- /* 打开 FILEX 媒体设备 */
- if(fs_is_inited == 0)
- {
- /* Initialize FileX. */
- fx_system_initialize();
- /* Open the SPIFLASH disk. */
- status = fx_media_open(&fs_disk, "SPIFLASH DISK", _fx_spiflash_driver, 0, media_memory, sizeof(media_memory));
- /* Check the media open status. */
- if (status != FX_SUCCESS)
- {
- fx_media_format(&fs_disk,
- _fx_spiflash_driver, // Driver entry
- 0, // RAM disk memory pointer
- media_memory, // Media buffer pointer
- sizeof(media_memory), // Media buffer size
- "D_SPIFLASH", // Volume Name
- 1, // Number of FATs
- 32, // Directory Entries
- 0, // Hidden sectors
- 4096*2, // Total sectors
- 4096, // Sector size
- 1, // Sectors per cluster
- 1, // Heads
- 1); // Sectors per track
- /* Open the SPIFLASH disk again. */
- status = fx_media_open(&fs_disk, "SPIFLASH DISK", _fx_spiflash_driver, 0, media_memory, sizeof(media_memory));
- }
- if(status == FX_SUCCESS)
- {
- fs_is_inited = 1;
- }
- }
- else
- {
- fs_is_inited++;
- }
- TX_RESTORE
- return status;
- }
- /*
- 文件系统关闭
- */
- static void fs_deinit(void)
- {
- TX_INTERRUPT_SAVE_AREA
- TX_DISABLE
- if(fs_is_inited)
- {
- fs_is_inited--;
- if(fs_is_inited == 0)
- {
- /* Close the media. */
- fx_media_close(&fs_disk);
- }
- }
- TX_RESTORE
- }
- #define O_RDONLY 0
- #define O_RDWR 1
- #define O_CREATE 2
- FILE* fopen(const char* filename, const char* mode)
- {
- UINT status;
- FX_FILE *file = TX_NULL;
- int fmode = O_RDONLY;
- if(fs_init() != FX_SUCCESS)
- {
- return 0;
- }
- if(strcmp(mode, "r") == 0) fmode = O_RDONLY;
- else if(strcmp(mode, "r+") == 0) fmode = O_RDWR;
- else if(strcmp(mode, "w") == 0) fmode = O_CREATE | O_RDWR;
- else if(strcmp(mode, "w+") == 0) fmode = O_CREATE | O_RDWR;
- else
- {
- fs_deinit();
- return 0;
- }
- if(fmode & O_CREATE)
- {
- /* Create file */
- status = fx_file_create(&fs_disk, (char*)filename);
- /* Check the create status. */
- if (status != FX_SUCCESS)
- {
- /* Check for an already created status. */
- if (status != FX_ALREADY_CREATED)
- {
- fs_deinit();
- return 0;
- }
- }
- }
- /* 为文件指针分配内存 */
- status = tx_block_allocate(&fs_block_pool, (void**)&file, TX_NO_WAIT);
- if(status != TX_SUCCESS)
- {
- fs_deinit();
- return 0;
- }
- /* Open the test file. */
- if(fmode & O_RDWR)
- {
- status = fx_file_open(&fs_disk, file, (char*)filename, FX_OPEN_FOR_WRITE);
- }
- else
- {
- status = fx_file_open(&fs_disk, file, (char*)filename, FX_OPEN_FOR_READ);
- }
- /* Check the file open status. */
- if (status != FX_SUCCESS)
- {
- tx_block_release(file);
- fs_deinit();
- return 0;
- }
- /* Seek to the beginning of the file. */
- status = fx_file_seek(file, 0);
- /* Check the file seek status. */
- if (status != FX_SUCCESS)
- {
- tx_block_release(file);
- fs_deinit();
- return 0;
- }
- return (FILE*)file;
- }
- int fclose(FILE* stream)
- {
- /* Close the file. */
- fx_file_close((FX_FILE*)stream);
- tx_block_release((void*)stream);
- fs_deinit();
- return 0;
- }
- size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- if((stream == &__stdout) || (stream == &__stderr))
- {
- // lua 的 print 会调用到这里
- BSP_COM_Send((uint8_t*)ptr, size*nmemb);
- return nmemb;
- }
- if(stream == &__stdin)
- {
- return -1;
- }
- if(fx_file_write((FX_FILE*)stream, (void*)ptr, size*nmemb) != FX_SUCCESS)
- {
- return 0;
- }
- else
- {
- return nmemb;
- }
- }
- size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- ULONG actual;
- if(fx_file_read((FX_FILE*)stream, (void*)ptr, size*nmemb, &actual) != FX_SUCCESS)
- {
- return 0;
- }
- else
- {
- return actual/size;
- }
- }
- int fseek(FILE *stream, long int offset, int whence)
- {
- ULONG off;
- UINT seek_from;
- switch(whence)
- {
- case SEEK_SET:
- seek_from = FX_SEEK_BEGIN;
- off = offset;
- break;
- case SEEK_END:
- seek_from = FX_SEEK_END;
- off = offset;
- break;
- case SEEK_CUR:
- if(offset < 0)
- {
- seek_from = FX_SEEK_BACK;
- off = -offset;
- }
- else
- {
- seek_from = FX_SEEK_FORWARD;
- off = offset;
- }
- default:
- break;
- }
- /* position is relative to the start of file fh */
- if(fx_file_relative_seek((FX_FILE*)stream, offset, seek_from) != FX_SUCCESS)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- int fputc(int ch, FILE* stream)
- {
- if((stream == &__stdout) || (stream == &__stderr))
- {
- // printf 函数会调用到这里
- BSP_COM_Send((uint8_t*)&ch, 1);
- return ch;
- }
- if(stream == &__stdin)
- {
- return -1;
- }
- {
- UCHAR dat = ch;
- if(fx_file_write((FX_FILE*)stream, &dat, 1) != FX_SUCCESS)
- {
- return -1;
- }
- return ch;
- }
- }
- int fgetc(FILE *stream)
- {
- UINT status;
- UCHAR ch;
- ULONG actual;
- status = fx_file_read((FX_FILE*)stream, &ch, 1, &actual);
- if((status != FX_SUCCESS) || (actual != 1))
- {
- return -1;
- }
- return ch;
- }
- time_t time(time_t * time)
- {
- return 0;
- }
- void exit(int status)
- {
- while(1);
- }
- int system(const char * string)
- {
- return 0;
- }
复制代码
|
|