Tue, 02 Nov 2021 14:47:43 +0100
Version 0.3.20 fix for equipment records create and delete.
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" | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
19 | #include "diskio_impl.h" |
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
20 | #include "diskio_sdmmc.h" |
0 | 21 | |
22 | #include "config.h" | |
23 | ||
24 | ||
25 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | SDCARD_State *sdcard_state; ///< SD card status |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
27 | JSON_log *json_log; ///< JSON log array |
0 | 28 | EventGroupHandle_t xEventGroupSDcard; ///< SD card events. |
29 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
30 | int card_handle = -1; |
0 | 31 | static const char *TAG = "task_sdcard"; |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
32 | static sdmmc_card_t* card = NULL; |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
33 | static uint8_t pdrv = FF_DRV_NOT_USED; |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
34 | char drv[3] = {'0', ':', 0}; |
0 | 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; | |
33
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
50 | extern uint32_t TimeBrewing; |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
51 | extern bool System_TimeOk; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
52 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
53 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
54 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
55 | void log_msg(const char *tag, const char *format, ...) |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
56 | { |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
57 | char *outstr, logfn[64], stamp[32]; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
58 | va_list va_ptr; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
59 | FILE *fp; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
60 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
61 | outstr = calloc(1024, sizeof(char)); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
62 | va_start(va_ptr, format); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
63 | vsnprintf(outstr, 1023, format, va_ptr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
64 | ESP_LOGI(tag, "%s", outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
65 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
66 | if (System_TimeOk && sdcard_state->card_present == true) { |
64
326c38d3681b
Added more plain logfile entries. Change some message details. Added anohe function to eliminate duplicated code. Moved the logfile to the web so it can be downloaded or viewed.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
67 | snprintf(logfn, 64, "/sdcard/w/log/sys%04d%02d%02d.log", timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday); |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
68 | snprintf(stamp, 31, "%02d-%02d %02d:%02d:%02d", timeinfo.tm_mday, timeinfo.tm_mon+1, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
69 | fp = fopen(logfn, "a+"); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
70 | if (fp) { |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
71 | fprintf(fp, "%s %s: %s\n", stamp, tag, outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
72 | fclose(fp); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
73 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
74 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
75 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
76 | va_end(va_ptr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
77 | free(outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
78 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
79 | |
0 | 80 | |
81 | ||
82 | void log_begin(time_t t) | |
83 | { | |
84 | struct tm timeinfo; | |
85 | ||
86 | /* | |
87 | * If there is an open logfile from an crashed brew, open that one. | |
88 | */ | |
89 | if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { | |
90 | sprintf(sdcard_state->logfile, "%s", runtime.Logfile); | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
91 | log_msg(TAG, "Resumed %s", sdcard_state->logfile); |
0 | 92 | } else { |
93 | localtime_r(&t, &timeinfo); | |
94 | if (timeinfo.tm_year > (2016 - 1900)) { | |
95 | // Valid time, construct the filename. | |
96 | strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); | |
97 | } else { | |
98 | sprintf(sdcard_state->logfile, "brewlog"); | |
99 | } | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
100 | log_msg(TAG, "Create %s", sdcard_state->logfile); |
0 | 101 | sprintf(runtime.Logfile, "%s", sdcard_state->logfile); |
102 | } | |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | void log_close(void) | |
108 | { | |
109 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
110 | } | |
111 | ||
112 | ||
113 | ||
114 | void log_clean(void) | |
115 | { | |
116 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
117 | } | |
118 | ||
119 | ||
120 | ||
121 | void log_json(void) | |
122 | { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
123 | char filename[64], strftime_buf[64]; |
0 | 124 | FILE *f; |
125 | bool addcomma = true; | |
126 | ||
127 | if (strlen(sdcard_state->logfile)) { | |
128 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
129 | f = fopen(filename, "r"); | |
130 | if (f == NULL) { | |
131 | // Create the file and add the header | |
132 | f = fopen(filename, "a"); | |
133 | if (f) { | |
134 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
135 | fprintf(f, "{\"brew\":[{\"Recipe\":\"%s %s\",\"Date\":\"%s\",\"brewdata\":[\n", recipe.Code, recipe.Name, strftime_buf); |
0 | 136 | addcomma = false; |
137 | fclose(f); | |
138 | } else { | |
139 | ESP_LOGE(TAG, "Failed to create %s error %d", filename, errno); | |
140 | return; | |
141 | } | |
142 | } else { | |
143 | fclose(f); // Was open for reading. | |
144 | } | |
145 | ||
146 | f = fopen(filename, "a"); | |
147 | if (f) { | |
148 | if (addcomma) { | |
149 | fprintf(f, ",\n"); | |
150 | } | |
151 | addcomma = true; | |
152 | fprintf(f, " {\"MLT_sp\":\"%.3f\",\"MLT_pv\":\"%.3f\",\"MLT_pwm\":\"%d\",\"MLT_tr\":\"%d\",\"Pump\":\"%d\",", | |
153 | json_log->mlt_sp, json_log->mlt_pv, json_log->mlt_power, json_log->mlt_tempreached, json_log->pump_run); | |
154 | if (json_log->hlt_sp > 0.0) { | |
155 | fprintf(f, "\"HLT_sp\":\"%.3f\",\"HLT_pv\":\"%.3f\",\"HLT_pwm\":\"%d\",", json_log->hlt_sp, json_log->hlt_pv, json_log->hlt_power); | |
156 | } | |
157 | fprintf(f, "\"Label\":\"%s\"}", json_log->time); | |
158 | fclose(f); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | void log_annotation(int annotation_type, char *label) | |
166 | { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
167 | char filename[64]; |
0 | 168 | char bordercolor[9], color[9], pos[8]; |
169 | FILE *f; | |
170 | bool addcomma = true; | |
33
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
171 | int Hour = (TimeBrewing / 3600); |
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
172 | int Minute = ((TimeBrewing % 3600) / 60); |
0 | 173 | |
174 | if (strlen(sdcard_state->logfile)) { | |
175 | sprintf(filename, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
176 | ||
177 | switch (annotation_type) { | |
178 | case ANNOTATION_STAGE: snprintf(bordercolor, 8, "#8942f4"); | |
179 | snprintf(color, 8, "#00215b"); | |
180 | snprintf(pos, 7, "bottom"); | |
181 | break; | |
182 | ||
183 | case ANNOTATION_EVENT: snprintf(bordercolor, 8, "#42f445"); | |
184 | snprintf(color, 8, "#00215b"); | |
185 | snprintf(pos, 7, "top"); | |
186 | break; | |
187 | ||
188 | case ANNOTATION_SYSTEM: snprintf(bordercolor, 8, "black"); | |
189 | snprintf(color, 8, "red"); | |
190 | snprintf(pos, 7, "center"); | |
191 | break; | |
192 | } | |
193 | ||
194 | // Check if the file exists to see if we need to insert a comma. | |
195 | f = fopen(filename, "r"); | |
196 | if (f == NULL) { | |
197 | addcomma = false; | |
198 | } else { | |
199 | fclose(f); | |
200 | } | |
201 | ||
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
202 | snprintf(json_log->time, 11, "%02d:%02d", Hour, Minute); |
0 | 203 | f = fopen(filename, "a"); |
204 | if (f) { | |
205 | if (addcomma) { | |
206 | fprintf(f, ",\n"); | |
207 | } | |
208 | addcomma = true; | |
209 | 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); | |
210 | fclose(f); | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | ||
216 | ||
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
217 | static esp_err_t mount_prepare_mem(const char *base_path, BYTE *out_pdrv, char **out_dup_path) |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
218 | { |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
219 | esp_err_t err = ESP_OK; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
220 | char* dup_path = NULL; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
221 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
222 | // connect SDMMC driver to FATFS |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
223 | BYTE pdrv = FF_DRV_NOT_USED; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
224 | if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED) { |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
225 | ESP_LOGD(TAG, "the maximum count of volumes is already mounted"); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
226 | return ESP_ERR_NO_MEM; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
227 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
228 | } |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
229 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
230 | // not using ff_memalloc here, as allocation in internal RAM is preferred |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
231 | card = (sdmmc_card_t*)malloc(sizeof(sdmmc_card_t)); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
232 | if (card == NULL) { |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
233 | ESP_LOGD(TAG, "could not malloc sdmmc_card_t"); |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
234 | err = ESP_ERR_NO_MEM; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
235 | goto cleanup; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
236 | } |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
237 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
238 | dup_path = strdup(base_path); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
239 | if (!dup_path){ |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
240 | ESP_LOGD(TAG, "could not copy base_path"); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
241 | err = ESP_ERR_NO_MEM; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
242 | goto cleanup; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
243 | } |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
244 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
245 | *out_pdrv = pdrv; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
246 | *out_dup_path = dup_path; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
247 | return ESP_OK; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
248 | cleanup: |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
249 | free(card); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
250 | card = NULL; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
251 | free(dup_path); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
252 | return err; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
253 | } |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
254 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
255 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
256 | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
257 | static esp_err_t my_init_sdspi_host(int slot, const void *slot_config, int *out_slot) |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
258 | { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
259 | esp_err_t err = sdspi_host_init_device((const sdspi_device_config_t*)slot_config, out_slot); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
260 | if (err != ESP_OK) { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
261 | ESP_LOGE(TAG, "Failed to attach sdspi device onto an SPI bus (rc=0x%x), please initialize the \ |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
262 | bus first and check the device parameters.", err); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
263 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
264 | return err; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
265 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
266 | |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
267 | |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
268 | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
269 | /** |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
270 | * @brief This is a local modified version of the esp_vfs_fat_sdspi_mount() function in |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
271 | * 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
|
272 | * for our application. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
273 | * @param base_path The mount path |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
274 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
275 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
276 | * @return Error condition. |
0 | 277 | */ |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
278 | esp_err_t my_vfs_fat_sdspi_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config) |
0 | 279 | { |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
280 | esp_err_t err; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
281 | char* dup_path = NULL; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
282 | |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
283 | if (card != NULL) { |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
284 | ESP_LOGE(TAG, "card not NULL"); |
0 | 285 | return ESP_ERR_INVALID_STATE; |
286 | } | |
287 | ||
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
288 | err = mount_prepare_mem(base_path, &pdrv, &dup_path); |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
289 | if (err != ESP_OK) { |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
290 | ESP_LOGE(TAG, "mount_prepare_mem 0x%x", err); |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
291 | return err; |
0 | 292 | } |
293 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
294 | //the init() function is usually empty, doesn't require any deinit to revert it |
0 | 295 | err = (*host_config->init)(); |
296 | if (err != ESP_OK) { | |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
297 | ESP_LOGE(TAG, "host_config->init() 0x%x", err); |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
298 | goto fail; |
0 | 299 | } |
300 | ||
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
301 | // configure SD host |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
302 | err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle); |
0 | 303 | if (err != ESP_OK) { |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
304 | ESP_LOGE(TAG, "my_init_sdspi_host() 0x%x", err); |
0 | 305 | goto fail; |
306 | } | |
307 | return ESP_OK; | |
308 | ||
309 | fail: | |
310 | host_config->deinit(); | |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
311 | free(card); |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
312 | card = NULL; |
0 | 313 | return err; |
314 | } | |
315 | ||
316 | ||
317 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
318 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
319 | * @brief Mount SD card. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
320 | * @param base_path The mountpoint |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
321 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
322 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
323 | * @param mount_config Mount configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
324 | * @param[out] out_card Card information |
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 | */ |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
327 | esp_err_t my_esp_vfs_fat_sdspi_mount(const char* base_path, |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
328 | const sdmmc_host_t* host_config_input, |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
329 | const sdspi_device_config_t* slot_config, |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
330 | const esp_vfs_fat_mount_config_t* mount_config, |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
331 | sdmmc_card_t** out_card) |
0 | 332 | { |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
333 | const sdmmc_host_t* host_config = host_config_input; |
0 | 334 | FATFS* fs = NULL; |
335 | esp_err_t err = ESP_OK; | |
336 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
337 | if (card == NULL) { |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
338 | ESP_LOGE(TAG, "card NULL"); |
0 | 339 | return ESP_ERR_INVALID_STATE; |
340 | } | |
341 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
342 | /* |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
343 | * The `slot` argument inside host_config should be replaced by the SD SPI handled returned |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
344 | * above. But the input pointer is const, so create a new variable. |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
345 | */ |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
346 | sdmmc_host_t new_config; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
347 | if (card_handle != host_config->slot) { |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
348 | new_config = *host_config_input; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
349 | host_config = &new_config; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
350 | new_config.slot = card_handle; |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
351 | } |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
352 | |
0 | 353 | // probe and initialize card |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
354 | err = sdmmc_card_init(host_config, card); |
0 | 355 | if (err != ESP_OK) { |
356 | if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log | |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
357 | ESP_LOGI(TAG, "sdmmc_card_init failed 0x%x", err); |
0 | 358 | } |
359 | goto fail; | |
360 | } | |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
361 | |
0 | 362 | if (out_card != NULL) { |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
363 | *out_card = card; |
0 | 364 | } |
365 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
366 | /* |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
367 | * mount to vfs fat |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
368 | */ |
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
369 | ff_diskio_register_sdmmc(pdrv, card); |
0 | 370 | ESP_LOGD(TAG, "using pdrv=%i", pdrv); |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
371 | drv[0] = (char)('0' + pdrv); |
0 | 372 | |
373 | // connect FATFS to VFS | |
374 | err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); | |
375 | if (err == ESP_ERR_INVALID_STATE) { | |
376 | // it's okay, already registered with VFS | |
377 | } else if (err != ESP_OK) { | |
378 | ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); | |
379 | goto fail; | |
380 | } | |
381 | ||
382 | // Try to mount partition | |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
383 | // See: esp-idf/components/fatfs/src/ff.h |
0 | 384 | FRESULT res = f_mount(fs, drv, 1); |
385 | if (res != FR_OK) { | |
386 | err = ESP_FAIL; | |
387 | ESP_LOGD(TAG, "f_mount failed (%d)", res); | |
388 | goto fail; | |
389 | } | |
390 | return ESP_OK; | |
391 | ||
392 | fail: | |
393 | if (fs) { | |
394 | f_mount(NULL, drv, 0); | |
395 | } | |
396 | esp_vfs_fat_unregister_path(base_path); | |
397 | ff_diskio_unregister(pdrv); | |
398 | return err; | |
399 | } | |
400 | ||
401 | ||
402 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
403 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
404 | * @brief Unmount a mounted SD card, |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
405 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
406 | */ |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
407 | esp_err_t my_esp_vfs_fat_sdspi_unmount(void) |
0 | 408 | { |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
409 | if (card == NULL) { |
0 | 410 | return ESP_ERR_INVALID_STATE; |
411 | } | |
412 | // unmount | |
413 | f_mount(0, drv, 0); | |
414 | return ESP_OK; | |
415 | } | |
416 | ||
417 | ||
418 | ||
419 | int FileCopy(char *ff, char *tf) | |
420 | { | |
421 | FILE *f, *t; | |
422 | uint8_t buf[512]; | |
423 | size_t bytes; | |
424 | ||
425 | f = fopen(ff, "r"); | |
426 | if (f == NULL) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
427 | ESP_LOGW(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); |
0 | 428 | return 1; |
429 | } | |
430 | ||
431 | t = fopen(tf, "w+"); | |
432 | if (t == NULL) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
433 | ESP_LOGW(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); |
0 | 434 | fclose(f); |
435 | return 1; | |
436 | } | |
437 | ||
438 | while ((bytes = fread(&buf, 1, 512, f))) { | |
439 | fwrite(&buf, 1, bytes, t); | |
440 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
441 | } | |
442 | ||
443 | fclose(f); | |
444 | fclose(t); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | ||
449 | ||
450 | void task_sdcard(void *pvParameter) | |
451 | { | |
452 | sdmmc_card_t* card; | |
453 | esp_err_t ret; | |
454 | EventBits_t uxBits; | |
455 | char filename[64]; | |
456 | ||
457 | sdcard_state = malloc(sizeof(SDCARD_State)); | |
458 | sdcard_state->host_ok = false; | |
459 | sdcard_state->card_present = false; | |
460 | sdcard_state->logfile[0] = '\0'; | |
461 | ||
462 | json_log = malloc(sizeof(JSON_log)); | |
463 | ||
88
7f02dbee58d0
Fix missing log entries. More code space saving.
Michiel Broek <mbroek@mbse.eu>
parents:
87
diff
changeset
|
464 | ESP_LOGI(TAG, "Start SD card"); |
0 | 465 | sdmmc_host_t host = SDSPI_HOST_DEFAULT(); |
466 | host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
467 | spi_bus_config_t bus_cfg = { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
468 | .mosi_io_num = SDCARD_PIN_NUM_MOSI, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
469 | .miso_io_num = SDCARD_PIN_NUM_MISO, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
470 | .sclk_io_num = SDCARD_PIN_NUM_CLK, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
471 | .quadwp_io_num = -1, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
472 | .quadhd_io_num = -1, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
473 | .max_transfer_sz = 4000, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
474 | }; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
475 | ret = spi_bus_initialize(host.slot, &bus_cfg, SDCARD_DMA_CHANNEL); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
476 | if (ret != ESP_OK) { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
477 | ESP_LOGE(TAG, "Failed to initialize bus."); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
478 | vTaskDelete(NULL); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
479 | return; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
480 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
481 | |
0 | 482 | // This initializes the slot without card detect (CD) and write protect (WP) signals. |
483 | // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
484 | sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
485 | slot_config.gpio_cs = SDCARD_PIN_NUM_CS; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
486 | slot_config.host_id = host.slot; |
0 | 487 | |
488 | /* | |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
489 | * No errors from the sdspi_transaction driver. |
0 | 490 | */ |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
491 | esp_log_level_set("sdspi_transaction", ESP_LOG_NONE); |
0 | 492 | |
493 | /* | |
494 | * Options for mounting the filesystem. | |
495 | * If format_if_mount_failed is set to true, SD card will be partitioned and | |
496 | * formatted in case when mounting fails. | |
497 | */ | |
498 | esp_vfs_fat_sdmmc_mount_config_t mount_config = { | |
499 | .format_if_mount_failed = false, | |
500 | .max_files = 5, | |
501 | .allocation_unit_size = 16 * 1024 | |
502 | }; | |
503 | ||
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
504 | ret = my_vfs_fat_sdspi_init("/sdcard", &host, &slot_config); |
0 | 505 | if (ret == ESP_OK) { |
506 | ESP_LOGI(TAG, "SPI card interface ready"); | |
507 | sdcard_state->host_ok = true; | |
508 | } else { | |
509 | ESP_LOGE(TAG, "SPI card interface failed, abort task"); | |
510 | vTaskDelete(NULL); | |
511 | return; | |
512 | } | |
513 | xEventGroupSDcard = xEventGroupCreate(); | |
514 | ||
515 | /* | |
516 | * Task loop, continues check of the inserted cards. | |
517 | */ | |
518 | while (1) { | |
519 | ||
520 | if (sdcard_state->card_present == false) { | |
521 | /* | |
522 | * If the card is not mounted, try it. | |
523 | */ | |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
524 | ret = my_esp_vfs_fat_sdspi_mount("/sdcard", &host, &slot_config, &mount_config, &card); |
0 | 525 | if (ret == ESP_OK) { |
526 | ESP_LOGI(TAG, "SD card mounted on /sdcard"); | |
527 | sdcard_state->card_present = true; | |
528 | ||
529 | DIR* dir = opendir("/sdcard/w/log"); | |
530 | if (dir == NULL) { | |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
531 | ret = mkdir("/sdcard/w", 0755); |
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
532 | ESP_LOGI(TAG, "mkdir(/sdcard/w)=%d", ret); |
0 | 533 | ret = mkdir("/sdcard/w/log", 0755); |
106
191dccfe4252
Schema aangepast voor pullup weerstanden voor de SD kaart. Code verbeteringen in task_sdcard.
Michiel Broek <mbroek@mbse.eu>
parents:
105
diff
changeset
|
534 | ESP_LOGI(TAG, "mkdir(/sdcard/w/log)=%d", ret); |
0 | 535 | } else { |
536 | closedir(dir); | |
537 | } | |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
538 | esp_log_level_set("sdmmc_sd", ESP_LOG_INFO); |
0 | 539 | } |
540 | } else { | |
541 | /* | |
542 | * Check if the mounted card is still in the slot. | |
543 | */ | |
544 | DIR* dir = opendir("/sdcard/w/log"); | |
545 | if (dir == NULL) { | |
546 | ESP_LOGI(TAG, "SD card missing, unmount"); | |
105
9e00845dc1ee
SD card completly switched to the SPI driver API.
Michiel Broek <mbroek@mbse.eu>
parents:
92
diff
changeset
|
547 | my_esp_vfs_fat_sdspi_unmount(); |
0 | 548 | sdcard_state->card_present = false; |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
549 | esp_log_level_set("sdmmc_sd", ESP_LOG_NONE); |
0 | 550 | } else { |
551 | closedir(dir); | |
552 | } | |
553 | } | |
554 | ||
555 | uxBits = xEventGroupWaitBits(xEventGroupSDcard, | |
556 | TASK_SDCARD_LOG_CLEAN | TASK_SDCARD_LOG_CLOSE, pdFALSE, pdFALSE, 1000 / portTICK_PERIOD_MS); | |
557 | ||
558 | if (uxBits & TASK_SDCARD_LOG_CLEAN) { | |
559 | DIR *dir = opendir("/spiffs/log"); | |
560 | char lf[32]; | |
561 | ||
562 | if (dir != NULL) { | |
563 | struct dirent *de = readdir(dir); | |
564 | while (de) { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
565 | sprintf(lf, "/spiffs/log/"); |
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
566 | strncat(lf, de->d_name, 31 - strlen(lf)); |
0 | 567 | if (unlink(lf) == ESP_OK) { |
568 | ESP_LOGI(TAG, "Removed old %s", lf); | |
569 | } | |
570 | de = readdir(dir); | |
571 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
572 | } | |
573 | closedir(dir); | |
574 | } | |
575 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
576 | } | |
577 | ||
578 | if (uxBits & TASK_SDCARD_LOG_CLOSE) { | |
579 | // Close the logfile. | |
580 | if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
581 | log_msg(TAG, "Closing logfile"); |
0 | 582 | char destname[64]; |
583 | sprintf(destname, "/sdcard/w/log"); | |
584 | int rc = mkdir(destname, 0755); | |
585 | if (rc && (errno != EEXIST)) { | |
586 | ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); | |
587 | } else { | |
588 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
589 | // First close the JSON data records | |
590 | FILE *f = fopen(filename, "a+"); | |
591 | if (f) { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
592 | fprintf(f, "],\"annotations\":[\n"); // End of brewdata |
0 | 593 | // Insert annotation records |
594 | sprintf(destname, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
595 | FILE *a = fopen(destname, "r"); | |
596 | char buf[256]; | |
597 | if (a) { | |
598 | while(true) { | |
599 | if (fgets(buf, sizeof(buf), a)) { | |
600 | fprintf(f, "%s", buf); | |
601 | } else { | |
602 | break; | |
603 | } | |
604 | } | |
605 | fclose(a); | |
606 | unlink(destname); | |
607 | } | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
608 | fprintf(f, "]}]}\n"); // End of annotations and brew |
0 | 609 | fclose(f); |
610 | } | |
611 | sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); | |
612 | if (FileCopy(filename, destname) == 0) { | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
613 | log_msg(TAG, "JSON file copied to %s", destname); |
0 | 614 | unlink(filename); |
615 | } | |
616 | } | |
617 | } | |
618 | sdcard_state->logfile[0] = '\0'; // Clear logfile name | |
619 | runtime.Logfile[0] = '\0'; | |
620 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
625 |