$npx -y skills add easyzoom/aix-skills --skill fatfs-integrationUse when integrating, porting, configuring, or debugging FatFs FAT/exFAT filesystems, diskio drivers, SD cards, SPI flash disks, or embedded file I/O failures
| 1 | # FatFs Integration |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Use this skill to integrate FatFs by proving the disk I/O layer before trusting filesystem behavior. FatFs is portable C, but the target-specific `diskio` functions, sector geometry, mount policy, and media timing decide whether it works reliably. |
| 6 | |
| 7 | ## When To Use |
| 8 | |
| 9 | Use this skill when: |
| 10 | |
| 11 | - The user wants to add or debug FatFs on SD card, eMMC, USB MSC, RAM disk, SPI flash, or block devices. |
| 12 | - The issue involves `f_mount`, `f_open`, `f_read`, `f_write`, `FR_DISK_ERR`, `FR_NOT_READY`, `FR_NO_FILESYSTEM`, long filenames, Unicode, or exFAT. |
| 13 | - The project uses `ff.c`, `ffconf.h`, `diskio.c`, or `FATFS`. |
| 14 | |
| 15 | Do not use this skill when the raw block device cannot read/write sectors yet; debug the storage driver first. |
| 16 | |
| 17 | ## First Questions |
| 18 | |
| 19 | Ask for: |
| 20 | |
| 21 | - Media type and transport: SDIO, SPI SD, eMMC, USB MSC, RAM disk, or flash translation layer. |
| 22 | - Sector size, sector count, erase/block size, and whether writes are cached. |
| 23 | - `ffconf.h`, `diskio.c`, and current FatFs return code. |
| 24 | - RTOS or bare-metal runtime and whether reentrancy is enabled. |
| 25 | - Whether formatting is allowed. Formatting can destroy user data. |
| 26 | - Long filename, Unicode, exFAT, timestamp, and multi-volume requirements. |
| 27 | |
| 28 | ## Integration Checklist |
| 29 | |
| 30 | 1. Prove disk I/O. |
| 31 | `disk_initialize`, `disk_status`, `disk_read`, `disk_write`, and `disk_ioctl` must work independently. |
| 32 | |
| 33 | 1. Match sector geometry. |
| 34 | FatFs expects logical sectors and correct sector count. `GET_SECTOR_COUNT`, `GET_SECTOR_SIZE`, and `CTRL_SYNC` matter. |
| 35 | |
| 36 | 1. Mount before formatting. |
| 37 | Try `f_mount` first. Ask before `f_mkfs` or any operation that can erase media. |
| 38 | |
| 39 | 1. Configure `ffconf.h` intentionally. |
| 40 | Reentrancy, LFN buffers, exFAT, code page, relative paths, and timestamp support affect RAM and behavior. |
| 41 | |
| 42 | 1. Add locking if shared. |
| 43 | Enable and implement synchronization when multiple tasks can access the same volume. |
| 44 | |
| 45 | 1. Verify persistence. |
| 46 | Write, `f_sync`/`f_close`, unmount/reset, remount, and read back. |
| 47 | |
| 48 | ## Common Failures |
| 49 | |
| 50 | - `FR_DISK_ERR` from incomplete `disk_ioctl` or bad `CTRL_SYNC`. |
| 51 | - `FR_NO_FILESYSTEM` from wrong sector offset or unformatted media. |
| 52 | - Data loss because files are not closed or synced before power loss. |
| 53 | - LFN stack/heap buffers exceed MCU memory. |
| 54 | - SD card init works at low speed but data transfer fails after clock switch. |
| 55 | - Multi-task access corrupts files without FatFs reentrancy support. |
| 56 | |
| 57 | ## Verification |
| 58 | |
| 59 | Before claiming FatFs works: |
| 60 | |
| 61 | - State media, sector size/count, `ffconf.h` options, and mount path. |
| 62 | - Confirm disk read/write/ioctl tests. |
| 63 | - Confirm mount behavior and whether format was skipped, approved, or performed. |
| 64 | - Confirm create/write/sync/readback after reset or remount. |
| 65 | - Confirm locking policy when RTOS is used. |
| 66 | |
| 67 | ## Example |
| 68 | |
| 69 | User: |
| 70 | |
| 71 | ```text |
| 72 | FatFs f_mount 返回 FR_NO_FILESYSTEM。 |
| 73 | ``` |
| 74 | |
| 75 | Agent: |
| 76 | |
| 77 | 1. Asks for media type, sector offset, diskio implementation, and whether formatting is allowed. |
| 78 | 1. Tests raw sector read and `GET_SECTOR_COUNT`. |
| 79 | 1. Mounts before formatting and asks before `f_mkfs`. |