lfs_rambd.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 (optional)
  25. struct lfs_rambd_config {
  26. // 8-bit erase value to simulate erasing with. -1 indicates no erase
  27. // occurs, which is still a valid block device
  28. int32_t erase_value;
  29. // Optional statically allocated buffer for the block device.
  30. void *buffer;
  31. };
  32. // rambd state
  33. typedef struct lfs_rambd {
  34. uint8_t *buffer;
  35. const struct lfs_rambd_config *cfg;
  36. } lfs_rambd_t;
  37. // Create a RAM block device using the geometry in lfs_config
  38. int lfs_rambd_create(const struct lfs_config *cfg);
  39. int lfs_rambd_createcfg(const struct lfs_config *cfg,
  40. const struct lfs_rambd_config *bdcfg);
  41. // Clean up memory associated with block device
  42. int lfs_rambd_destroy(const struct lfs_config *cfg);
  43. // Read a block
  44. int lfs_rambd_read(const struct lfs_config *cfg, lfs_block_t block,
  45. lfs_off_t off, void *buffer, lfs_size_t size);
  46. // Program a block
  47. //
  48. // The block must have previously been erased.
  49. int lfs_rambd_prog(const struct lfs_config *cfg, lfs_block_t block,
  50. lfs_off_t off, const void *buffer, lfs_size_t size);
  51. // Erase a block
  52. //
  53. // A block must be erased before being programmed. The
  54. // state of an erased block is undefined.
  55. int lfs_rambd_erase(const struct lfs_config *cfg, lfs_block_t block);
  56. // Sync the block device
  57. int lfs_rambd_sync(const struct lfs_config *cfg);
  58. #ifdef __cplusplus
  59. } /* extern "C" */
  60. #endif
  61. #endif