$npx -y skills add easyzoom/aix-skills --skill littlefs-integrationUse when integrating, porting, configuring, or debugging littlefs on MCU flash, external NOR/NAND, SD-backed block devices, bare-metal, or RTOS projects
| 1 | # littlefs Integration |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Use this skill to integrate littlefs safely on embedded block devices. The core task is to make the `lfs_config` match the real storage geometry and prove mount, format, write, sync, power-loss, and remount behavior. |
| 6 | |
| 7 | ## When To Use |
| 8 | |
| 9 | Use this skill when: |
| 10 | |
| 11 | - The user wants to add or debug littlefs on MCU internal flash, external SPI/QSPI NOR, NAND-like storage, EEPROM emulation, SD-backed block devices, or RTOS storage. |
| 12 | - The issue involves mount failures, `LFS_ERR_CORRUPT`, read/write errors, wear leveling, block geometry, formatting, or data loss after reboot. |
| 13 | - The project uses `lfs.h`, `lfs_file_*`, `lfs_dir_*`, or `struct lfs_config`. |
| 14 | |
| 15 | Do not use this skill for generic flash programming failures before the block device driver works. Use `mcu-flashing-debug` first. |
| 16 | |
| 17 | ## First Questions |
| 18 | |
| 19 | Ask for: |
| 20 | |
| 21 | - Storage type, size, erase block size, program size, read size, and erase value. |
| 22 | - Whether storage is internal flash, external NOR/NAND, SD card, RAM disk, or simulator. |
| 23 | - Existing `lfs_config`, block device read/prog/erase/sync functions, and error codes. |
| 24 | - RTOS or bare-metal environment, locking needs, and whether dynamic allocation is allowed. |
| 25 | - Whether formatting is allowed. Formatting can destroy existing data. |
| 26 | - Current symptom and exact littlefs error code. |
| 27 | |
| 28 | ## Integration Checklist |
| 29 | |
| 30 | 1. Confirm block geometry. |
| 31 | Set `read_size`, `prog_size`, `block_size`, `block_count`, `cache_size`, `lookahead_size`, and `block_cycles` from the real storage device, not guesses. |
| 32 | |
| 33 | 1. Validate block operations. |
| 34 | `read`, `prog`, `erase`, and `sync` must use block-relative offsets correctly and return negative error codes on failure. |
| 35 | |
| 36 | 1. Respect flash rules. |
| 37 | Programming can only change erased bits in the supported direction, writes must be aligned, and erase must cover whole erase blocks. |
| 38 | |
| 39 | 1. Decide mount policy. |
| 40 | Try `lfs_mount` first. Only call `lfs_format` after explicit user approval or when the product policy allows first-boot formatting. |
| 41 | |
| 42 | 1. Add locking if needed. |
| 43 | Protect littlefs calls with a mutex if multiple tasks, interrupt contexts, or shell commands can access the filesystem. |
| 44 | |
| 45 | 1. Prove persistence. |
| 46 | Write a file, close/sync it, unmount if possible, reset, remount, and read back. |
| 47 | |
| 48 | ## Porting Checks |
| 49 | |
| 50 | - `context` points to the storage driver state if needed. |
| 51 | - Block index and offset are translated to physical address correctly. |
| 52 | - Erase block size matches the physical erase granularity. |
| 53 | - Cache size is a multiple of read/prog sizes and fits RAM budget. |
| 54 | - Lookahead size is valid and large enough for block count. |
| 55 | - Bad-block behavior is understood if using NAND-like storage. |
| 56 | - Interrupts or power loss cannot interrupt a critical flash operation without product-level recovery handling. |
| 57 | |
| 58 | ## Common Failures |
| 59 | |
| 60 | - Mount fails because `block_size` or `block_count` does not match the actual partition. |
| 61 | - Verify/readback fails because program alignment is wrong. |
| 62 | - Data disappears after reboot because file was not closed or synced. |
| 63 | - Filesystem corruption is caused by two tasks accessing littlefs without locking. |
| 64 | - Internal flash writes corrupt running code because the partition overlaps firmware. |
| 65 | - Formatting on every boot erases user data. |
| 66 | |
| 67 | ## Verification |
| 68 | |
| 69 | Before claiming littlefs works: |
| 70 | |
| 71 | - State the storage geometry and `lfs_config` values. |
| 72 | - Confirm mount behavior and whether format was skipped, approved, or performed. |
| 73 | - Confirm create/write/close/readback succeeds. |
| 74 | - Confirm data survives reset or remount. |
| 75 | - If RTOS is used, confirm the locking policy. |
| 76 | - List any destructive operations avoided or approved. |
| 77 | |
| 78 | ## Example |
| 79 | |
| 80 | User: |
| 81 | |
| 82 | ```text |
| 83 | littlefs 挂载总是 LFS_ERR_CORRUPT。 |
| 84 | ``` |
| 85 | |
| 86 | Agent: |
| 87 | |
| 88 | 1. Asks for storage geometry, `lfs_config`, block driver functions, partition address, and whether format is allowed. |
| 89 | 1. Checks block size/count and read/prog/erase alignment. |
| 90 | 1. Mounts before formatting, and asks before erasing existing data. |
| 91 | 1. Verifies write, sync, reboot, remount, and readback. |