1# Development Guidelines<a name="EN-US_TOPIC_0000001152860497"></a> 2 3- [Sample Code](#section1034515054620) 4 5Before porting LittleFS to a new hardware device, declare **lfs\_config**: 6 7``` 8const struct lfs_config cfg = { 9 // block device operations 10 .read = user_provided_block_device_read, 11 .prog = user_provided_block_device_prog, 12 .erase = user_provided_block_device_erase, 13 .sync = user_provided_block_device_sync, 14 15 // block device configuration 16 .read_size = 16, 17 .prog_size = 16, 18 .block_size = 4096, 19 .block_count = 128, 20 .cache_size = 16, 21 .lookahead_size = 16, 22 .block_cycles = 500, 23}; 24``` 25 26**.read**, **.prog**, **.erase**, and **.sync** correspond to the underlying read, write, erase, and synchronization APIs of the hardware platform, respectively. 27 28**read\_size** indicates the number of bytes read each time. You can set it to a value greater than the physical read unit to improve performance. This value determines the size of the read cache. However, if the value is too large, more memory is consumed. 29 30**prog\_size** indicates the number of bytes written each time. You can set it to a value greater than the physical write unit to improve performance. This value determines the size of the write cache and must be an integral multiple of **read\_size**. However, if the value is too large, more memory is consumed. 31 32**block\_size** indicates the number of bytes in each erase block. The value can be greater than that of the physical erase unit. However, a smaller value is recommended because each file occupies at least one block. The value must be an integral multiple of **prog\_size**. 33 34**block\_count** indicates the number of blocks that can be erased, which depends on the capacity of the block device and the size of the block to be erased \(**block\_size**\). 35 36## Sample Code<a name="section1034515054620"></a> 37 38The sample code is as follows: 39 40``` 41#include "lfs.h" 42#include "stdio.h" 43 44lfs_t lfs; 45lfs_file_t file; 46 47const struct lfs_config cfg = { 48 // block device operations 49 .read = user_provided_block_device_read, 50 .prog = user_provided_block_device_prog, 51 .erase = user_provided_block_device_erase, 52 .sync = user_provided_block_device_sync, 53 54 // block device configuration 55 .read_size = 16, 56 .prog_size = 16, 57 .block_size = 4096, 58 .block_count = 128, 59 .cache_size = 16, 60 .lookahead_size = 16, 61 .block_cycles = 500, 62}; 63 64int main(void) { 65 // mount the filesystem 66 int err = lfs_mount(&lfs, &cfg); 67 68 // reformat if we can't mount the filesystem 69 // this should only happen on the first boot 70 if (err) { 71 lfs_format(&lfs, &cfg); 72 lfs_mount(&lfs, &cfg); 73 } 74 75 // read current count 76 uint32_t boot_count = 0; 77 lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT); 78 lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count)); 79 80 // update boot count 81 boot_count += 1; 82 lfs_file_rewind(&lfs, &file); 83 lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count)); 84 85 // remember the storage is not updated until the file is closed successfully 86 lfs_file_close(&lfs, &file); 87 88 // release any resources we were using 89 lfs_unmount(&lfs); 90 91 // print the boot count 92 printf("boot_count: %d\n", boot_count); 93} 94``` 95 96