spiffs_api.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "freertos/FreeRTOS.h"
  15. #include "esp_log.h"
  16. #include "esp_partition.h"
  17. #include "esp_spiffs.h"
  18. #include "esp_vfs.h"
  19. #include "spiffs_api.h"
  20. static const char* TAG = "SPIFFS";
  21. void spiffs_api_lock(spiffs *fs)
  22. {
  23. (void) xSemaphoreTake(((esp_spiffs_t *)(fs->user_data))->lock, portMAX_DELAY);
  24. }
  25. void spiffs_api_unlock(spiffs *fs)
  26. {
  27. xSemaphoreGive(((esp_spiffs_t *)(fs->user_data))->lock);
  28. }
  29. s32_t spiffs_api_read(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *dst)
  30. {
  31. esp_err_t err = esp_partition_read(((esp_spiffs_t *)(fs->user_data))->partition,
  32. addr, dst, size);
  33. if (err) {
  34. ESP_LOGE(TAG, "failed to read addr %08x, size %08x, err %d", addr, size, err);
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. s32_t spiffs_api_write(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *src)
  40. {
  41. esp_err_t err = esp_partition_write(((esp_spiffs_t *)(fs->user_data))->partition,
  42. addr, src, size);
  43. if (err) {
  44. ESP_LOGE(TAG, "failed to write addr %08x, size %08x, err %d", addr, size, err);
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. s32_t spiffs_api_erase(spiffs *fs, uint32_t addr, uint32_t size)
  50. {
  51. esp_err_t err = esp_partition_erase_range(((esp_spiffs_t *)(fs->user_data))->partition,
  52. addr, size);
  53. if (err) {
  54. ESP_LOGE(TAG, "failed to erase addr %08x, size %08x, err %d", addr, size, err);
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. void spiffs_api_check(spiffs *fs, spiffs_check_type type,
  60. spiffs_check_report report, uint32_t arg1, uint32_t arg2)
  61. {
  62. static const char * spiffs_check_type_str[3] = {
  63. "LOOKUP",
  64. "INDEX",
  65. "PAGE"
  66. };
  67. static const char * spiffs_check_report_str[7] = {
  68. "PROGRESS",
  69. "ERROR",
  70. "FIX INDEX",
  71. "FIX LOOKUP",
  72. "DELETE ORPHANED INDEX",
  73. "DELETE PAGE",
  74. "DELETE BAD FILE"
  75. };
  76. if (report != SPIFFS_CHECK_PROGRESS) {
  77. ESP_LOGE(TAG, "CHECK: type:%s, report:%s, %x:%x", spiffs_check_type_str[type],
  78. spiffs_check_report_str[report], arg1, arg2);
  79. } else {
  80. ESP_LOGV(TAG, "CHECK PROGRESS: report:%s, %x:%x",
  81. spiffs_check_report_str[report], arg1, arg2);
  82. }
  83. }