Skip to content

Commit

Permalink
add:little_flash lfs对接
Browse files Browse the repository at this point in the history
  • Loading branch information
Dozingfiretruck committed May 11, 2024
1 parent a583a15 commit 2dca888
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
106 changes: 106 additions & 0 deletions components/little_flash/luat_little_flash_lfs2.c
@@ -0,0 +1,106 @@

#include "luat_base.h"
#include "luat_fs.h"
#include "luat_mem.h"

#define LUAT_LOG_TAG "little_flash"
#include "luat_log.h"

#ifdef LUAT_USE_FS_VFS
#include "lfs.h"
#include "little_flash.h"

static size_t lf_offset = 0;

// Read a block
static int lf_block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
little_flash_t* flash = (little_flash_t*)cfg->context;
// int ret = lf_read(flash, lf_offset + block * flash->chip.erase_gran + off, size, buffer);
int ret = little_flash_read(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
// LUAT_DEBUG_PRINT("lf_block_device_read ret %d", ret);
return ret;
}

static int lf_block_device_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
little_flash_t* flash = (little_flash_t*)cfg->context;
// int ret = lf_write(flash, lf_offset + block * flash->chip.erase_gran + off, size, buffer);
int ret = little_flash_write(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
// LUAT_DEBUG_PRINT("lf_block_device_prog ret %d", ret);
return ret;
}

static int lf_block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
little_flash_t* flash = (little_flash_t*)cfg->context;
// int ret = lf_erase(flash, lf_offset + block * flash->chip.erase_gran, flash->chip.erase_gran);
int ret = little_flash_erase(flash, lf_offset + block * flash->chip_info.erase_size, flash->chip_info.erase_size);
// LUAT_DEBUG_PRINT("lf_block_device_erase ret %d", ret);
return ret;
}

static int lf_block_device_sync(const struct lfs_config *cfg) {
return LFS_ERR_OK;
}

typedef struct LFS2 {
lfs_t lfs;
struct lfs_config cfg;
}LFS2_t;

lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
if (_lfs == NULL)
return NULL;
memset(_lfs, 0, sizeof(LFS2_t));
lf_offset = offset;
lfs_t *lfs = &_lfs->lfs;
struct lfs_config *lfs_cfg = &_lfs->cfg;

lfs_cfg->context = flash,
// block device operations
lfs_cfg->read = lf_block_device_read;
lfs_cfg->prog = lf_block_device_prog;
lfs_cfg->erase = lf_block_device_erase;
lfs_cfg->sync = lf_block_device_sync;

// block device configuration
lfs_cfg->read_size = flash->chip_info.read_size;
lfs_cfg->prog_size = flash->chip_info.prog_size;
lfs_cfg->block_size = flash->chip_info.erase_size;
lfs_cfg->block_count = (maxsize > 0 ? maxsize : (flash->chip_info.capacity - offset)) / flash->chip_info.erase_size;
lfs_cfg->block_cycles = 400;
lfs_cfg->cache_size = flash->chip_info.prog_size;
lfs_cfg->lookahead_size = flash->chip_info.prog_size;

lfs_cfg->name_max = 63;
lfs_cfg->file_max = 0;
lfs_cfg->attr_max = 0;

// LLOGD("block_size %d", lfs_cfg->block_size);
// LLOGD("block_count %d", lfs_cfg->block_count);
// LLOGD("capacity %d", flash->chip.capacity);
// LLOGD("erase_gran %d", flash->chip.erase_gran);

// ------
int err = lfs_mount(lfs, lfs_cfg);
LLOGD("lfs_mount %d",err);
if (err)
{
err = lfs_format(lfs, lfs_cfg);
// LLOGD("lfs_format %d",err);
if(err)
goto fail;
err = lfs_mount(lfs, lfs_cfg);
LLOGD("lfs_mount %d",err);
if(err)
goto fail;
}
return lfs;
fail :
luat_heap_free(_lfs);
return NULL;
//------
}

#endif


2 changes: 0 additions & 2 deletions components/little_flash/port/little_flash_config.h
Expand Up @@ -17,8 +17,6 @@ extern "C" {

#define LF_USE_HEAP /* enable malloc/free for little flash */

#define LF_USE_SPI /* enable SPI for little flash */

// #define LF_USE_QSPI /* enable QSPI for little flash */

// #define LF_USE_SFDP /* enable SFDP driver for little flash */
Expand Down
4 changes: 0 additions & 4 deletions components/little_flash/port/little_flash_port.c
Expand Up @@ -5,7 +5,6 @@
#include "luat_mem.h"
#include "luat_spi.h"

#ifdef LF_USE_SPI
static lf_err_t little_flash_spi_transfer(const little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len){
lf_err_t result = LF_ERR_OK;
luat_spi_device_t *spi_dev = (luat_spi_device_t*)lf->spi.user_data;
Expand All @@ -24,7 +23,6 @@ static lf_err_t little_flash_spi_transfer(const little_flash_t *lf,uint8_t *tx_b
}
return result;
}
#endif

static void little_flash_wait_10us(void){
uint32_t delay = 12;
Expand All @@ -46,9 +44,7 @@ static void little_flash_free(void* ptr){
#endif

lf_err_t little_flash_port_init(little_flash_t *lf){
#ifdef LF_USE_SPI
lf->spi.transfer = little_flash_spi_transfer;
#endif
lf->wait_10us = little_flash_wait_10us;
lf->wait_ms = little_flash_wait_ms;
#ifdef LF_USE_HEAP
Expand Down
2 changes: 0 additions & 2 deletions components/little_flash/src/little_flash.c
Expand Up @@ -107,9 +107,7 @@ lf_err_t little_flash_device_init(little_flash_t *lf){
little_flash_port_init(lf);
LF_ASSERT(lf->wait_10us);
LF_ASSERT(lf->wait_ms);
#ifdef LF_USE_SPI
LF_ASSERT(lf->spi.transfer);
#endif
#ifdef LF_USE_HEAP
LF_ASSERT(lf->malloc);
LF_ASSERT(lf->free);
Expand Down

0 comments on commit 2dca888

Please sign in to comment.