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