lfs_rambd.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Block device emulated in RAM
  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_RAMBD_H
  9. #define LFS_RAMBD_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_RAMBD_TRACE
  18. #ifdef LFS_RAMBD_YES_TRACE
  19. #define LFS_RAMBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  20. #else
  21. #define LFS_RAMBD_TRACE(...)
  22. #endif
  23. #endif
  24. // rambd config
  25. struct lfs_rambd_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. // Optional statically allocated buffer for the block device.
  35. void *buffer;
  36. };
  37. // rambd state
  38. typedef struct lfs_rambd {
  39. uint8_t *buffer;
  40. const struct lfs_rambd_config *cfg;
  41. } lfs_rambd_t;
  42. // Create a RAM block device
  43. int lfs_rambd_create(const struct lfs_config *cfg,
  44. const struct lfs_rambd_config *bdcfg);
  45. // Clean up memory associated with block device
  46. int lfs_rambd_destroy(const struct lfs_config *cfg);
  47. // Read a block
  48. int lfs_rambd_read(const struct lfs_config *cfg, lfs_block_t block,
  49. lfs_off_t off, void *buffer, lfs_size_t size);
  50. // Program a block
  51. //
  52. // The block must have previously been erased.
  53. int lfs_rambd_prog(const struct lfs_config *cfg, lfs_block_t block,
  54. lfs_off_t off, const void *buffer, lfs_size_t size);
  55. // Erase a block
  56. //
  57. // A block must be erased before being programmed. The
  58. // state of an erased block is undefined.
  59. int lfs_rambd_erase(const struct lfs_config *cfg, lfs_block_t block);
  60. // Sync the block device
  61. int lfs_rambd_sync(const struct lfs_config *cfg);
  62. #ifdef __cplusplus
  63. } /* extern "C" */
  64. #endif
  65. #endif