2022-02-21 16:09:24 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_partition.h"
|
|
|
|
#include "esp_spiffs.h"
|
|
|
|
#include "spiffs_api.h"
|
|
|
|
|
2018-06-21 04:51:44 -04:00
|
|
|
static const char* TAG = "SPIFFS";
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
void spiffs_api_lock(spiffs *fs)
|
|
|
|
{
|
2018-06-27 08:47:30 -04:00
|
|
|
(void) xSemaphoreTake(((esp_spiffs_t *)(fs->user_data))->lock, portMAX_DELAY);
|
2018-05-22 04:47:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void spiffs_api_unlock(spiffs *fs)
|
|
|
|
{
|
|
|
|
xSemaphoreGive(((esp_spiffs_t *)(fs->user_data))->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32_t spiffs_api_read(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *dst)
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
esp_err_t err = esp_partition_read(((esp_spiffs_t *)(fs->user_data))->partition,
|
2018-05-22 04:47:30 -04:00
|
|
|
addr, dst, size);
|
2019-10-15 17:01:05 -04:00
|
|
|
if (unlikely(err)) {
|
2022-10-06 10:16:32 -04:00
|
|
|
ESP_LOGE(TAG, "failed to read addr 0x%08" PRIx32 ", size 0x%08" PRIx32 ", err %d", addr, size, err);
|
2018-05-22 04:47:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32_t spiffs_api_write(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *src)
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
esp_err_t err = esp_partition_write(((esp_spiffs_t *)(fs->user_data))->partition,
|
2018-05-22 04:47:30 -04:00
|
|
|
addr, src, size);
|
2019-10-15 17:01:05 -04:00
|
|
|
if (unlikely(err)) {
|
2022-10-06 10:16:32 -04:00
|
|
|
ESP_LOGE(TAG, "failed to write addr 0x%08" PRIx32 ", size 0x%08" PRIx32 ", err %d", addr, size, err);
|
2018-05-22 04:47:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32_t spiffs_api_erase(spiffs *fs, uint32_t addr, uint32_t size)
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
esp_err_t err = esp_partition_erase_range(((esp_spiffs_t *)(fs->user_data))->partition,
|
2018-05-22 04:47:30 -04:00
|
|
|
addr, size);
|
|
|
|
if (err) {
|
2022-10-06 10:16:32 -04:00
|
|
|
ESP_LOGE(TAG, "failed to erase addr 0x%08" PRIx32 ", size 0x%08" PRIx32 ", err %d", addr, size, err);
|
2018-05-22 04:47:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
void spiffs_api_check(spiffs *fs, spiffs_check_type type,
|
2018-05-22 04:47:30 -04:00
|
|
|
spiffs_check_report report, uint32_t arg1, uint32_t arg2)
|
|
|
|
{
|
|
|
|
static const char * spiffs_check_type_str[3] = {
|
|
|
|
"LOOKUP",
|
|
|
|
"INDEX",
|
|
|
|
"PAGE"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * spiffs_check_report_str[7] = {
|
|
|
|
"PROGRESS",
|
|
|
|
"ERROR",
|
|
|
|
"FIX INDEX",
|
|
|
|
"FIX LOOKUP",
|
|
|
|
"DELETE ORPHANED INDEX",
|
|
|
|
"DELETE PAGE",
|
|
|
|
"DELETE BAD FILE"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (report != SPIFFS_CHECK_PROGRESS) {
|
2022-10-06 10:16:32 -04:00
|
|
|
ESP_LOGE(TAG, "CHECK: type:%s, report:%s, %" PRIx32 ":%" PRIx32, spiffs_check_type_str[type],
|
2018-05-22 04:47:30 -04:00
|
|
|
spiffs_check_report_str[report], arg1, arg2);
|
|
|
|
} else {
|
2022-10-06 10:16:32 -04:00
|
|
|
ESP_LOGV(TAG, "CHECK PROGRESS: report:%s, %" PRIx32 ":%" PRIx32,
|
2018-05-22 04:47:30 -04:00
|
|
|
spiffs_check_report_str[report], arg1, arg2);
|
|
|
|
}
|
|
|
|
}
|