文章目录
1.1 文件系统
1.2 内建方法和对象
1.3 使用ampy
1.4 文件相关的常用方法 & 模块
1.1 文件系统
mpy的文件系统为vfs(虚拟文件系统), vfs基于oofatfs库实现, 而oofatfs又是源自开源的fatfs文件系统,即整个vfs是基于fatfs扩展而来. fatfs文件系统原本需要用户移植的文件diskio.c , mpy重新封装了一层vfs_fat*, 一方面为MicroPython 的python module提供实现支持,另一方面也为用户进行porting(hal)层的实现提供了统一的实现入口.用户实现自己的文件系统需要:
开启VFS 宏支持
#ifndef MICROPY_VFS
#define MICROPY_VFS (1)
#endif
#define MICROPY_VFS_FAT (MICROPY_VFS)
#if MICROPY_VFS
#define MICROPY_FATFS_RPATH (2)
#define MICROPY_FATFS_MAX_SS (4096)
#define MICROPY_FATFS_ENABLE_LFN (1)
#endif
根据开发板实际的存储设备(SD卡或Flash)实现设备基本的读写和初始化驱动
根据存储设备驱动接口实现 fs module的对象方法: *readblocks, *writeblocks , *ioctl, 并实现vfs初始化和挂载函数
static bool b_pyb_flash_is_initialised = false;
void pyb_flash_init(void) {
//ignore
b_pyb_flash_is_initialised = true;
}
uint32_t pyb_flash_get_block_size(void) {
return XFLASH_BLOCK_SIZE;
}
uint32_t pyb_flash_get_block_count(void) {
return XFLASH_SIZE/XFLASH_BLOCK_SIZE;
}
void pyb_flash_flush(void) {
//nothing to do
}
/*
* dest : buffer ptr to save read data
* block_num : the start number of block to read ,scope :[0 ~ 1023]
*/
bool pyb_flash_read_block(uint8_t *dest, uint32_t block_num) {
//ignore
}
/*
* dest : buffer ptr to save read data
* block_num : the start number of block to read ,scope :[0 ~ 1023]
* num_blocks : how many blocks to read
*/
mp_uint_t pyb_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
uint32_t i = 0;
mp_uint_t ret = 0;
for (i = 0; i < num_blocks; i++) {
if (!pyb_flash_read_block(dest + i * XFLASH_BLOCK_SIZE, block_num + i)) {
ret = 1; // error
break;
}
}
return ret; // success
}
/*
* src : buffe