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
15 #include "freertos/FreeRTOS.h"
16 #include "esp_log.h"
17 #include "esp_partition.h"
18 #include "esp_spiffs.h"
19 #include "esp_vfs.h"
20 #include "spiffs_api.h"
21
22 static const char* TAG = "SPIFFS";
23
spiffs_api_lock(spiffs * fs)24 void spiffs_api_lock(spiffs *fs)
25 {
26 (void) xSemaphoreTake(((esp_spiffs_t *)(fs->user_data))->lock, portMAX_DELAY);
27 }
28
spiffs_api_unlock(spiffs * fs)29 void spiffs_api_unlock(spiffs *fs)
30 {
31 xSemaphoreGive(((esp_spiffs_t *)(fs->user_data))->lock);
32 }
33
spiffs_api_read(spiffs * fs,uint32_t addr,uint32_t size,uint8_t * dst)34 s32_t spiffs_api_read(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *dst)
35 {
36 esp_err_t err = esp_partition_read(((esp_spiffs_t *)(fs->user_data))->partition,
37 addr, dst, size);
38 if (unlikely(err)) {
39 ESP_LOGE(TAG, "failed to read addr %08x, size %08x, err %d", addr, size, err);
40 return -1;
41 }
42 return 0;
43 }
44
spiffs_api_write(spiffs * fs,uint32_t addr,uint32_t size,uint8_t * src)45 s32_t spiffs_api_write(spiffs *fs, uint32_t addr, uint32_t size, uint8_t *src)
46 {
47 esp_err_t err = esp_partition_write(((esp_spiffs_t *)(fs->user_data))->partition,
48 addr, src, size);
49 if (unlikely(err)) {
50 ESP_LOGE(TAG, "failed to write addr %08x, size %08x, err %d", addr, size, err);
51 return -1;
52 }
53 return 0;
54 }
55
spiffs_api_erase(spiffs * fs,uint32_t addr,uint32_t size)56 s32_t spiffs_api_erase(spiffs *fs, uint32_t addr, uint32_t size)
57 {
58 esp_err_t err = esp_partition_erase_range(((esp_spiffs_t *)(fs->user_data))->partition,
59 addr, size);
60 if (err) {
61 ESP_LOGE(TAG, "failed to erase addr %08x, size %08x, err %d", addr, size, err);
62 return -1;
63 }
64 return 0;
65 }
66
spiffs_api_check(spiffs * fs,spiffs_check_type type,spiffs_check_report report,uint32_t arg1,uint32_t arg2)67 void spiffs_api_check(spiffs *fs, spiffs_check_type type,
68 spiffs_check_report report, uint32_t arg1, uint32_t arg2)
69 {
70 static const char * spiffs_check_type_str[3] = {
71 "LOOKUP",
72 "INDEX",
73 "PAGE"
74 };
75
76 static const char * spiffs_check_report_str[7] = {
77 "PROGRESS",
78 "ERROR",
79 "FIX INDEX",
80 "FIX LOOKUP",
81 "DELETE ORPHANED INDEX",
82 "DELETE PAGE",
83 "DELETE BAD FILE"
84 };
85
86 if (report != SPIFFS_CHECK_PROGRESS) {
87 ESP_LOGE(TAG, "CHECK: type:%s, report:%s, %x:%x", spiffs_check_type_str[type],
88 spiffs_check_report_str[report], arg1, arg2);
89 } else {
90 ESP_LOGV(TAG, "CHECK PROGRESS: report:%s, %x:%x",
91 spiffs_check_report_str[report], arg1, arg2);
92 }
93 }
94