Sun, 21 Oct 2018 13:07:50 +0200
Updated documentation
0 | 1 | /** |
2 | * @file task_sdcard.c | |
3 | * @brief SD/MMC driver. This driver is for a slot for the user on | |
4 | * the front panel. It will detect an inserted card and then | |
4 | 5 | * mount it. It also detects the removal of the card and then |
6 | * unmounts it. Be carefull, only do this in the main menu. | |
0 | 7 | * Also, brew logging is handled here and finished brewlogs |
8 | * are copied to the SD card if it is mounted. | |
9 | * Recipes to import must go into the /sdcard/recipe folder | |
10 | * and have extension .xml and the contents be a beerxml file. | |
4 | 11 | * Backup and restore is also done to this card. |
0 | 12 | */ |
13 | ||
14 | ||
15 | #include "vfs_fat_internal.h" | |
16 | #include "driver/sdmmc_host.h" | |
17 | #include "driver/sdspi_host.h" | |
18 | #include "sdmmc_cmd.h" | |
19 | #include "diskio.h" | |
20 | ||
21 | #include "config.h" | |
22 | ||
23 | ||
24 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | SDCARD_State *sdcard_state; ///< SD card status |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | JSON_log *json_log; ///< JSON log array |
0 | 27 | EventGroupHandle_t xEventGroupSDcard; ///< SD card events. |
28 | ||
29 | static const char *TAG = "task_sdcard"; | |
30 | static sdmmc_card_t* s_card = NULL; | |
31 | static uint8_t s_pdrv = 0; | |
32 | static char * s_base_path = NULL; | |
33 | ||
34 | BYTE pdrv = 0xFF; | |
35 | ||
36 | ||
37 | #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
38 | #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
39 | #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
40 | #define SDCARD_PIN_NUM_CLK 14 ///< CLOCK |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
41 | #define SDCARD_PIN_NUM_CS 13 ///< Chip select pin |
0 | 42 | #define SDCARD_DMA_CHANNEL 2 ///< Channel 1 is used by the TFT |
43 | ||
44 | ||
45 | const int TASK_SDCARD_LOG_CLEAN = BIT0; ///< Clean spiffs logfile directory | |
46 | const int TASK_SDCARD_LOG_CLOSE = BIT1; ///< Close the logfile. | |
47 | ||
48 | extern time_t now; | |
49 | extern struct tm timeinfo; | |
50 | ||
51 | ||
52 | ||
53 | void log_begin(time_t t) | |
54 | { | |
55 | struct tm timeinfo; | |
56 | ||
57 | /* | |
58 | * If there is an open logfile from an crashed brew, open that one. | |
59 | */ | |
60 | if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { | |
61 | sprintf(sdcard_state->logfile, "%s", runtime.Logfile); | |
62 | ESP_LOGI(TAG, "Resumed %s", sdcard_state->logfile); | |
63 | } else { | |
64 | localtime_r(&t, &timeinfo); | |
65 | if (timeinfo.tm_year > (2016 - 1900)) { | |
66 | // Valid time, construct the filename. | |
67 | strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); | |
68 | } else { | |
69 | sprintf(sdcard_state->logfile, "brewlog"); | |
70 | } | |
71 | ESP_LOGI(TAG, "Create %s", sdcard_state->logfile); | |
72 | sprintf(runtime.Logfile, "%s", sdcard_state->logfile); | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | void log_close(void) | |
79 | { | |
80 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
81 | } | |
82 | ||
83 | ||
84 | ||
85 | void log_clean(void) | |
86 | { | |
87 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
88 | } | |
89 | ||
90 | ||
91 | ||
92 | void log_json(void) | |
93 | { | |
94 | char filename[32], strftime_buf[64]; | |
95 | FILE *f; | |
96 | bool addcomma = true; | |
97 | ||
98 | if (strlen(sdcard_state->logfile)) { | |
99 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
100 | f = fopen(filename, "r"); | |
101 | if (f == NULL) { | |
102 | // Create the file and add the header | |
103 | f = fopen(filename, "a"); | |
104 | if (f) { | |
105 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); | |
106 | fprintf(f, "{\n"); | |
107 | fprintf(f, " \"brew\": [{\n"); | |
108 | fprintf(f, " \"Recipe\":\"%s\",\n", recipe.Name); | |
109 | fprintf(f, " \"Date\":\"%s\",\n", strftime_buf); | |
110 | fprintf(f, " \"brewdata\":[\n"); | |
111 | addcomma = false; | |
112 | fclose(f); | |
113 | } else { | |
114 | ESP_LOGE(TAG, "Failed to create %s error %d", filename, errno); | |
115 | return; | |
116 | } | |
117 | } else { | |
118 | fclose(f); // Was open for reading. | |
119 | } | |
120 | ||
121 | f = fopen(filename, "a"); | |
122 | if (f) { | |
123 | if (addcomma) { | |
124 | fprintf(f, ",\n"); | |
125 | } | |
126 | addcomma = true; | |
127 | fprintf(f, " {\"MLT_sp\":\"%.3f\",\"MLT_pv\":\"%.3f\",\"MLT_pwm\":\"%d\",\"MLT_tr\":\"%d\",\"Pump\":\"%d\",", | |
128 | json_log->mlt_sp, json_log->mlt_pv, json_log->mlt_power, json_log->mlt_tempreached, json_log->pump_run); | |
129 | if (json_log->hlt_sp > 0.0) { | |
130 | fprintf(f, "\"HLT_sp\":\"%.3f\",\"HLT_pv\":\"%.3f\",\"HLT_pwm\":\"%d\",", json_log->hlt_sp, json_log->hlt_pv, json_log->hlt_power); | |
131 | } | |
132 | fprintf(f, "\"Label\":\"%s\"}", json_log->time); | |
133 | printf("{\"MLT_sp\":\"%.3f\",\"MLT_pv\":\"%.3f\",\"MLT_pwm\":\"%d\",\"MLT_tr\":\"%d\",\"Pump\":\"%d\",", | |
134 | json_log->mlt_sp, json_log->mlt_pv, json_log->mlt_power, json_log->mlt_tempreached, json_log->pump_run); | |
135 | if (json_log->hlt_sp > 0.0) { | |
136 | printf("\"HLT_sp\":\"%.3f\",\"HLT_pv\":\"%.3f\",\"HLT_pwm\":\"%d\",", json_log->hlt_sp, json_log->hlt_pv, json_log->hlt_power); | |
137 | } | |
138 | printf("\"Label\":\"%s\"}\n", json_log->time); | |
139 | fclose(f); | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | ||
145 | ||
146 | void log_annotation(int annotation_type, char *label) | |
147 | { | |
148 | char filename[32]; | |
149 | char bordercolor[9], color[9], pos[8]; | |
150 | FILE *f; | |
151 | bool addcomma = true; | |
152 | ||
153 | if (strlen(sdcard_state->logfile)) { | |
154 | sprintf(filename, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
155 | ||
156 | switch (annotation_type) { | |
157 | case ANNOTATION_STAGE: snprintf(bordercolor, 8, "#8942f4"); | |
158 | snprintf(color, 8, "#00215b"); | |
159 | snprintf(pos, 7, "bottom"); | |
160 | break; | |
161 | ||
162 | case ANNOTATION_EVENT: snprintf(bordercolor, 8, "#42f445"); | |
163 | snprintf(color, 8, "#00215b"); | |
164 | snprintf(pos, 7, "top"); | |
165 | break; | |
166 | ||
167 | case ANNOTATION_SYSTEM: snprintf(bordercolor, 8, "black"); | |
168 | snprintf(color, 8, "red"); | |
169 | snprintf(pos, 7, "center"); | |
170 | break; | |
171 | } | |
172 | ||
173 | // Check if the file exists to see if we need to insert a comma. | |
174 | f = fopen(filename, "r"); | |
175 | if (f == NULL) { | |
176 | addcomma = false; | |
177 | } else { | |
178 | fclose(f); | |
179 | } | |
180 | ||
181 | f = fopen(filename, "a"); | |
182 | if (f) { | |
183 | if (addcomma) { | |
184 | fprintf(f, ",\n"); | |
185 | } | |
186 | addcomma = true; | |
187 | fprintf(f, "{\"type\":\"line\",\"mode\":\"vertical\",\"scaleID\":\"x-axis-0\",\"value\":\"%s\",\"borderColor\":\"%s\",\"borderWidth\":2,\"label\":{\"backgroundColor\":\"%s\",\"content\":\"%s\",\"position\":\"%s\",\"enabled\":true}}", json_log->time, bordercolor, color, label, pos); | |
188 | fclose(f); | |
189 | printf( "{\"type\":\"line\",\"mode\":\"vertical\",\"scaleID\":\"x-axis-0\",\"value\":\"%s\",\"borderColor\":\"%s\",\"borderWidth\":2,\"label\":{\"backgroundColor\":\"%s\",\"content\":\"%s\",\"position\":\"%s\",\"enabled\":true}}\n", json_log->time, bordercolor, color, label, pos); | |
190 | } | |
191 | } | |
192 | } | |
193 | ||
194 | ||
195 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
196 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
197 | * @brief This is a local modified version of the esp_vfs_fat_sdmmc_mount() function in |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
198 | * the FreeRTOS components library. It is here so we can better handle errors |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
199 | * for our application. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
200 | * @param base_path The mount path |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
201 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
202 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
203 | * @return Error condition. |
0 | 204 | */ |
205 | esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config) | |
206 | { | |
207 | if (s_card != NULL) { | |
208 | return ESP_ERR_INVALID_STATE; | |
209 | } | |
210 | ||
211 | // connect SDMMC driver to FATFS | |
212 | pdrv = 0xFF; | |
213 | if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) { | |
214 | ESP_LOGI(TAG, "the maximum count of volumes is already mounted"); | |
215 | return ESP_ERR_NO_MEM; | |
216 | } | |
217 | ||
218 | s_base_path = strdup(base_path); | |
219 | if (!s_base_path) { | |
220 | ESP_LOGI(TAG, "could not copy base_path"); | |
221 | return ESP_ERR_NO_MEM; | |
222 | } | |
223 | esp_err_t err = ESP_OK; | |
224 | s_card = malloc(sizeof(sdmmc_card_t)); | |
225 | if (s_card == NULL) { | |
226 | err = ESP_ERR_NO_MEM; | |
227 | goto fail; | |
228 | } | |
229 | ||
230 | err = (*host_config->init)(); | |
231 | if (err != ESP_OK) { | |
232 | ESP_LOGI(TAG, "host init returned rc=0x%x", err); | |
233 | goto fail; | |
234 | } | |
235 | ||
236 | // configure SD slot | |
237 | if (host_config->flags == SDMMC_HOST_FLAG_SPI) { | |
238 | err = sdspi_host_init_slot(host_config->slot, (const sdspi_slot_config_t*) slot_config); | |
239 | } else { | |
240 | err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config); | |
241 | } | |
242 | if (err != ESP_OK) { | |
243 | ESP_LOGI(TAG, "slot_config returned rc=0x%x", err); | |
244 | goto fail; | |
245 | } | |
246 | return ESP_OK; | |
247 | ||
248 | fail: | |
249 | host_config->deinit(); | |
250 | free(s_card); | |
251 | s_card = NULL; | |
252 | return err; | |
253 | } | |
254 | ||
255 | ||
256 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
257 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
258 | * @brief Mount SD card. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
259 | * @param base_path The mountpoint |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
260 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
261 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
262 | * @param mount_config Mount configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
263 | * @param[out] out_card Card information |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
264 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
265 | */ |
0 | 266 | esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path, |
267 | const sdmmc_host_t* host_config, | |
268 | const void* slot_config, | |
269 | const esp_vfs_fat_mount_config_t* mount_config, | |
270 | sdmmc_card_t** out_card) | |
271 | { | |
272 | FATFS* fs = NULL; | |
273 | esp_err_t err = ESP_OK; | |
274 | ||
275 | if (s_card == NULL) { | |
276 | return ESP_ERR_INVALID_STATE; | |
277 | } | |
278 | ||
279 | // probe and initialize card | |
280 | err = sdmmc_card_init(host_config, s_card); | |
281 | if (err != ESP_OK) { | |
282 | if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log | |
283 | ESP_LOGI(TAG, "sdmmc_card_init failed 0x(%x)", err); | |
284 | } | |
285 | goto fail; | |
286 | } | |
287 | if (out_card != NULL) { | |
288 | *out_card = s_card; | |
289 | } | |
290 | ||
291 | ff_diskio_register_sdmmc(pdrv, s_card); | |
292 | s_pdrv = pdrv; | |
293 | ESP_LOGD(TAG, "using pdrv=%i", pdrv); | |
294 | char drv[3] = {(char)('0' + pdrv), ':', 0}; | |
295 | ||
296 | // connect FATFS to VFS | |
297 | err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); | |
298 | if (err == ESP_ERR_INVALID_STATE) { | |
299 | // it's okay, already registered with VFS | |
300 | } else if (err != ESP_OK) { | |
301 | ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); | |
302 | goto fail; | |
303 | } | |
304 | ||
305 | // Try to mount partition | |
306 | FRESULT res = f_mount(fs, drv, 1); | |
307 | if (res != FR_OK) { | |
308 | err = ESP_FAIL; | |
309 | ESP_LOGD(TAG, "f_mount failed (%d)", res); | |
310 | goto fail; | |
311 | } | |
312 | return ESP_OK; | |
313 | ||
314 | fail: | |
315 | if (fs) { | |
316 | f_mount(NULL, drv, 0); | |
317 | } | |
318 | esp_vfs_fat_unregister_path(base_path); | |
319 | ff_diskio_unregister(pdrv); | |
320 | return err; | |
321 | } | |
322 | ||
323 | ||
324 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
325 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
326 | * @brief Unmount a mounted SD card, |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
327 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
328 | */ |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
329 | esp_err_t my_esp_vfs_fat_sdmmc_unmount(void) |
0 | 330 | { |
331 | if (s_card == NULL) { | |
332 | return ESP_ERR_INVALID_STATE; | |
333 | } | |
334 | // unmount | |
335 | char drv[3] = {(char)('0' + s_pdrv), ':', 0}; | |
336 | f_mount(0, drv, 0); | |
337 | return ESP_OK; | |
338 | } | |
339 | ||
340 | ||
341 | ||
342 | int FileCopy(char *ff, char *tf) | |
343 | { | |
344 | FILE *f, *t; | |
345 | uint8_t buf[512]; | |
346 | size_t bytes; | |
347 | ||
348 | f = fopen(ff, "r"); | |
349 | if (f == NULL) { | |
350 | ESP_LOGE(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); | |
351 | return 1; | |
352 | } | |
353 | ||
354 | t = fopen(tf, "w+"); | |
355 | if (t == NULL) { | |
356 | ESP_LOGE(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); | |
357 | fclose(f); | |
358 | return 1; | |
359 | } | |
360 | ||
361 | while ((bytes = fread(&buf, 1, 512, f))) { | |
362 | fwrite(&buf, 1, bytes, t); | |
363 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
364 | } | |
365 | ||
366 | fclose(f); | |
367 | fclose(t); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | ||
372 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
373 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
374 | * @brief Sync directories. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
375 | * @param fromdir Source directory |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
376 | * @param todir Destination directory |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
377 | */ |
0 | 378 | void SyncDirs(char *fromdir, char *todir) |
379 | { | |
380 | char ff[64], tf[64]; | |
381 | struct stat fs, ts; | |
382 | int rc; | |
383 | ||
384 | ESP_LOGI(TAG, "SyncDirs(%s, %s)", fromdir, todir); | |
385 | ||
386 | DIR *dir = opendir(fromdir); | |
387 | struct dirent* de = readdir(dir); | |
388 | while (de) { | |
389 | if (de->d_type == DT_REG) { | |
390 | sprintf(ff, "%s/%s", fromdir, de->d_name); | |
391 | if (stat(ff, &fs) == ESP_OK) { | |
392 | ||
393 | sprintf(tf, "%s/%s", todir, de->d_name); | |
394 | if (stat(tf, &ts) != ESP_OK) { | |
395 | ts.st_size = 0; | |
396 | } | |
397 | ||
398 | if (fs.st_size && (fs.st_size != ts.st_size)) { | |
399 | rc = FileCopy(ff, tf); | |
400 | ESP_LOGI(TAG, "Copy %s to %s, %ld bytes, rc=%d", ff, todir, fs.st_size, rc); | |
401 | } | |
402 | } | |
403 | } | |
404 | de = readdir(dir); | |
405 | vTaskDelay(50 / portTICK_PERIOD_MS); | |
406 | } | |
407 | closedir(dir); | |
408 | ||
409 | /* | |
410 | * Now see if we need to remove files. | |
411 | */ | |
412 | dir = opendir(todir); | |
413 | de = readdir(dir); | |
414 | while (de) { | |
415 | sprintf(tf, "%s/%s", todir, de->d_name); | |
416 | sprintf(ff, "%s/%s", fromdir, de->d_name); | |
417 | ||
418 | if (stat(ff, &fs) != ESP_OK) { | |
419 | if (unlink(tf) == ESP_OK) { | |
420 | ESP_LOGI(TAG, "Removed %s", tf); | |
421 | } | |
422 | } | |
423 | ||
424 | de = readdir(dir); | |
425 | vTaskDelay(50 / portTICK_PERIOD_MS); | |
426 | } | |
427 | closedir(dir); | |
428 | } | |
429 | ||
430 | ||
431 | ||
432 | void task_sdcard(void *pvParameter) | |
433 | { | |
434 | sdmmc_card_t* card; | |
435 | esp_err_t ret; | |
436 | EventBits_t uxBits; | |
437 | char filename[64]; | |
438 | ||
439 | sdcard_state = malloc(sizeof(SDCARD_State)); | |
440 | sdcard_state->host_ok = false; | |
441 | sdcard_state->card_present = false; | |
442 | sdcard_state->logfile[0] = '\0'; | |
443 | ||
444 | json_log = malloc(sizeof(JSON_log)); | |
445 | ||
446 | ESP_LOGI(TAG, "Starting SD card"); | |
447 | sdmmc_host_t host = SDSPI_HOST_DEFAULT(); | |
448 | host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. | |
449 | sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); | |
450 | slot_config.gpio_miso = SDCARD_PIN_NUM_MISO; | |
451 | slot_config.gpio_mosi = SDCARD_PIN_NUM_MOSI; | |
452 | slot_config.gpio_sck = SDCARD_PIN_NUM_CLK; | |
453 | slot_config.gpio_cs = SDCARD_PIN_NUM_CS; | |
454 | slot_config.dma_channel = SDCARD_DMA_CHANNEL; | |
455 | // This initializes the slot without card detect (CD) and write protect (WP) signals. | |
456 | // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. | |
457 | ||
458 | /* | |
459 | * No errors from the sdmmc_cmd driver. | |
460 | */ | |
461 | esp_log_level_set("sdmmc_cmd", ESP_LOG_NONE); | |
462 | ||
463 | /* | |
464 | * Options for mounting the filesystem. | |
465 | * If format_if_mount_failed is set to true, SD card will be partitioned and | |
466 | * formatted in case when mounting fails. | |
467 | */ | |
468 | esp_vfs_fat_sdmmc_mount_config_t mount_config = { | |
469 | .format_if_mount_failed = false, | |
470 | .max_files = 5, | |
471 | .allocation_unit_size = 16 * 1024 | |
472 | }; | |
473 | ||
474 | ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config); | |
475 | if (ret == ESP_OK) { | |
476 | ESP_LOGI(TAG, "SPI card interface ready"); | |
477 | sdcard_state->host_ok = true; | |
478 | } else { | |
479 | ESP_LOGE(TAG, "SPI card interface failed, abort task"); | |
480 | vTaskDelete(NULL); | |
481 | return; | |
482 | } | |
483 | ||
484 | xEventGroupSDcard = xEventGroupCreate(); | |
485 | ||
486 | /* | |
487 | * Task loop, continues check of the inserted cards. | |
488 | */ | |
489 | while (1) { | |
490 | ||
491 | if (sdcard_state->card_present == false) { | |
492 | /* | |
493 | * If the card is not mounted, try it. | |
494 | */ | |
495 | ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); | |
496 | if (ret == ESP_OK) { | |
497 | ESP_LOGI(TAG, "SD card mounted on /sdcard"); | |
498 | sdcard_state->card_present = true; | |
499 | ||
500 | DIR* dir = opendir("/sdcard/w/log"); | |
501 | if (dir == NULL) { | |
502 | ret = mkdir("/sdcard/w/log", 0755); | |
503 | printf("Dir created ret=%d\n", ret); | |
504 | } else { | |
505 | closedir(dir); | |
506 | } | |
507 | ||
508 | ||
509 | SyncDirs("/sdcard/w", "/spiffs/w"); | |
510 | SyncDirs("/sdcard/w/js", "/spiffs/w/js"); | |
511 | SyncDirs("/sdcard/w/css", "/spiffs/w/css"); | |
512 | // SyncDirs("/sdcard/w/js/modl" , "/spiffs/w/js/modl"); // | |
513 | // SyncDirs("/sdcard/w/js/utils", "/spiffs/w/js/utils"); | |
514 | // SyncDirs("/sdcard/w/js/zlib", "/spiffs/w/js/zlib"); | |
515 | // SyncDirs("/sdcard/w/core", "/spiffs/w/core"); | |
516 | // SyncDirs("/sdcard/w/core/input", "/spiffs/w/core/input"); | |
517 | // SyncDirs("/sdcard/w/core/util", "/spiffs/w/core/util"); | |
518 | // SyncDirs("/sdcard/w/app", "/spiffs/w/app"); | |
519 | // SyncDirs("/sdcard/w/app/images", "/spiffs/w/app/images"); | |
520 | // SyncDirs("/sdcard/w/app/locale", "/spiffs/w/app/locale"); | |
521 | // SyncDirs("/sdcard/w/app/sounds", "/spiffs/w/app/sounds"); // | |
522 | SyncDirs("/sdcard/w/app/styles", "/spiffs/w/app/styles"); | |
523 | // SyncDirs("/sdcard/fonts", "/spiffs/fonts"); // | |
524 | ||
525 | } | |
526 | } else { | |
527 | /* | |
528 | * Check if the mounted card is still in the slot. | |
529 | */ | |
530 | DIR* dir = opendir("/sdcard/w/log"); | |
531 | if (dir == NULL) { | |
532 | ESP_LOGI(TAG, "SD card missing, unmount"); | |
533 | my_esp_vfs_fat_sdmmc_unmount(); | |
534 | sdcard_state->card_present = false; | |
535 | } else { | |
536 | closedir(dir); | |
537 | } | |
538 | } | |
539 | ||
540 | uxBits = xEventGroupWaitBits(xEventGroupSDcard, | |
541 | TASK_SDCARD_LOG_CLEAN | TASK_SDCARD_LOG_CLOSE, pdFALSE, pdFALSE, 1000 / portTICK_PERIOD_MS); | |
542 | ||
543 | if (uxBits & TASK_SDCARD_LOG_CLEAN) { | |
544 | DIR *dir = opendir("/spiffs/log"); | |
545 | char lf[32]; | |
546 | ||
547 | if (dir != NULL) { | |
548 | struct dirent *de = readdir(dir); | |
549 | while (de) { | |
550 | sprintf(lf, "/spiffs/log/%s", de->d_name); | |
551 | if (unlink(lf) == ESP_OK) { | |
552 | ESP_LOGI(TAG, "Removed old %s", lf); | |
553 | } | |
554 | de = readdir(dir); | |
555 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
556 | } | |
557 | closedir(dir); | |
558 | } | |
559 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
560 | } | |
561 | ||
562 | if (uxBits & TASK_SDCARD_LOG_CLOSE) { | |
563 | // Close the logfile. | |
564 | if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { | |
565 | ESP_LOGI(TAG, "Closing logfile"); | |
566 | char destname[64]; | |
567 | sprintf(destname, "/sdcard/w/log"); | |
568 | int rc = mkdir(destname, 0755); | |
569 | if (rc && (errno != EEXIST)) { | |
570 | ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); | |
571 | } else { | |
572 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
573 | // First close the JSON data records | |
574 | FILE *f = fopen(filename, "a+"); | |
575 | if (f) { | |
576 | fprintf(f, " ],\n"); // End of brewdata | |
577 | fprintf(f, " \"annotations\":[\n"); | |
578 | // Insert annotation records | |
579 | sprintf(destname, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
580 | FILE *a = fopen(destname, "r"); | |
581 | char buf[256]; | |
582 | if (a) { | |
583 | while(true) { | |
584 | if (fgets(buf, sizeof(buf), a)) { | |
585 | fprintf(f, "%s", buf); | |
586 | } else { | |
587 | break; | |
588 | } | |
589 | } | |
590 | fclose(a); | |
591 | unlink(destname); | |
592 | } | |
593 | fprintf(f, " ]\n"); // End of annotations | |
594 | fprintf(f, " }]\n"); // End of brew | |
595 | fprintf(f, "}\n"); | |
596 | fclose(f); | |
597 | } | |
598 | sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); | |
599 | if (FileCopy(filename, destname) == 0) { | |
600 | ESP_LOGI(TAG, "JSON file copied to %s", destname); | |
601 | unlink(filename); | |
602 | } | |
603 | } | |
604 | } | |
605 | sdcard_state->logfile[0] = '\0'; // Clear logfile name | |
606 | runtime.Logfile[0] = '\0'; | |
607 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
612 |