Home

Awesome

littlefs API + bindings for Nim

This is still experimental since I have not tested it or used it at all.

Observe copyright and distribution license of littlefs from their GitHub repository:

Copyright (c) 2022, The littlefs authors. Copyright (c) 2017, Arm Limited. All rights reserved.

Boot count example

This example is translated from the littlefs GitHub README

import littlefs/all
import littlefs/configs/file_config

import std/os

const fsPath = "testfs.bin"

var f = open(fsPath, if fileExists(fsPath): fmReadWriteExisting else: fmReadWrite)
var lfs = LittleFs(cfg: makeFileLfsConfig(f, block_count=1024))
lfs.boot()
var file = lfs.open("boot_count", fmReadWrite)
var boot_count = read[int](file)
echo "boot count: ", boot_count
inc boot_count
file.rewind()
file.write(boot_count)

Project directory

examples
src
  ---> littlefs
    ---> api                High level API
    ---> bindings           Thin wrapping
      ---> lfs_nimutil.nim  Nim implementation of lfs_util.h
    ---> configs            Modules for premade configs
tests                       Nothing yet
buildsys.nims               Build system will go here if needed

Building with buildsys.nims

This library comes with a Nim implementation of lfs_util.h. To use it define lfsUseNimUtils. The build tasks and the api both observe this definition.

The following will clone littlefs and build it. This task will build two versions of the littlefs C library, one of which is compiled expecting the Nim implementation of lfs_util.h. Library files will be dropped at build/liblfs.a and build/liblfsNim.a respectively:

nim buildLfsLibs buildsys.nims

To build the littlefs C source with asserts, errors and warnings disabled:

nim -d:danger buildLfsLibs buildsys.nims

To build the fuse driver run:

nim buildFuse buildsys.nims

or

nim -d:lfsUseNimUtils -d:release buildFuse buildsys.nims


You can import "config.nims", or if in nimble path, littlefs/api/build_help/config.nims to have the c sources automatically added via cincludes and the correct library given the state of lfsUseNimUtils.

The api file common.nim will import and export bindings/lfs_nimutil.nim when lfsUseNimUtils is defined so that it is easy to compile with this option enabled

FYIs

Future plans