lfs_filebd.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Block device emulated in a file
  3. *
  4. * Copyright (c) 2022, The littlefs authors.
  5. * Copyright (c) 2017, Arm Limited. All rights reserved.
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef LFS_FILEBD_H
  9. #define LFS_FILEBD_H
  10. #include "lfs.h"
  11. #include "lfs_util.h"
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. // Block device specific tracing
  17. #ifndef LFS_FILEBD_TRACE
  18. #ifdef LFS_FILEBD_YES_TRACE
  19. #define LFS_FILEBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  20. #else
  21. #define LFS_FILEBD_TRACE(...)
  22. #endif
  23. #endif
  24. // filebd config
  25. struct lfs_filebd_config {
  26. // Minimum size of a read operation in bytes.
  27. lfs_size_t read_size;
  28. // Minimum size of a program operation in bytes.
  29. lfs_size_t prog_size;
  30. // Size of an erase operation in bytes.
  31. lfs_size_t erase_size;
  32. // Number of erase blocks on the device.
  33. lfs_size_t erase_count;
  34. };
  35. // filebd state
  36. typedef struct lfs_filebd {
  37. int fd;
  38. const struct lfs_filebd_config *cfg;
  39. } lfs_filebd_t;
  40. // Create a file block device
  41. int lfs_filebd_create(const struct lfs_config *cfg, const char *path,
  42. const struct lfs_filebd_config *bdcfg);
  43. // Clean up memory associated with block device
  44. int lfs_filebd_destroy(const struct lfs_config *cfg);
  45. // Read a block
  46. int lfs_filebd_read(const struct lfs_config *cfg, lfs_block_t block,
  47. lfs_off_t off, void *buffer, lfs_size_t size);
  48. // Program a block
  49. //
  50. // The block must have previously been erased.
  51. int lfs_filebd_prog(const struct lfs_config *cfg, lfs_block_t block,
  52. lfs_off_t off, const void *buffer, lfs_size_t size);
  53. // Erase a block
  54. //
  55. // A block must be erased before being programmed. The
  56. // state of an erased block is undefined.
  57. int lfs_filebd_erase(const struct lfs_config *cfg, lfs_block_t block);
  58. // Sync the block device
  59. int lfs_filebd_sync(const struct lfs_config *cfg);
  60. #ifdef __cplusplus
  61. } /* extern "C" */
  62. #endif
  63. #endif