spiffs_api.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "esp_log.h"
  8. #include "esp_partition.h"
  9. #include "esp_spiffs.h"
  10. #include "esp_vfs.h"
  11. #include "spiffs_api.h"
  12. static const char* TAG = "SPIFFS";
  13. void spiffs_api_lock(spiffs *fs)
  14. {
  15. (void) xSemaphoreTake(((esp_spiffs_t *)(fs->user_data))->lock, portMAX_DELAY);
  16. }
  17. void spiffs_api_unlock(spiffs *fs)
  18. {
  19. xSemaphoreGive(((esp_spiffs_t *)(fs->user_data))->lock);
  20. }
  21. s32_t spiffs_api_read(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *dst)
  22. {
  23. esp_err_t err = esp_partition_read(((esp_spiffs_t *)(fs->user_data))->partition,
  24. addr, dst, size);
  25. if (unlikely(err)) {
  26. ESP_LOGE(TAG, "failed to read addr %08x, size %08x, err %d", addr, size, err);
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. s32_t spiffs_api_write(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *src)
  32. {
  33. esp_err_t err = esp_partition_write(((esp_spiffs_t *)(fs->user_data))->partition,
  34. addr, src, size);
  35. if (unlikely(err)) {
  36. ESP_LOGE(TAG, "failed to write addr %08x, size %08x, err %d", addr, size, err);
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. s32_t spiffs_api_erase(spiffs *fs, uint32_t addr, uint32_t size)
  42. {
  43. esp_err_t err = esp_partition_erase_range(((esp_spiffs_t *)(fs->user_data))->partition,
  44. addr, size);
  45. if (err) {
  46. ESP_LOGE(TAG, "failed to erase addr %08x, size %08x, err %d", addr, size, err);
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. void spiffs_api_check(spiffs *fs, spiffs_check_type type,
  52. spiffs_check_report report, uint32_t arg1, uint32_t arg2)
  53. {
  54. static const char * spiffs_check_type_str[3] = {
  55. "LOOKUP",
  56. "INDEX",
  57. "PAGE"
  58. };
  59. static const char * spiffs_check_report_str[7] = {
  60. "PROGRESS",
  61. "ERROR",
  62. "FIX INDEX",
  63. "FIX LOOKUP",
  64. "DELETE ORPHANED INDEX",
  65. "DELETE PAGE",
  66. "DELETE BAD FILE"
  67. };
  68. if (report != SPIFFS_CHECK_PROGRESS) {
  69. ESP_LOGE(TAG, "CHECK: type:%s, report:%s, %x:%x", spiffs_check_type_str[type],
  70. spiffs_check_report_str[report], arg1, arg2);
  71. } else {
  72. ESP_LOGV(TAG, "CHECK PROGRESS: report:%s, %x:%x",
  73. spiffs_check_report_str[report], arg1, arg2);
  74. }
  75. }