-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsdcard.c
More file actions
325 lines (251 loc) · 8.41 KB
/
sdcard.c
File metadata and controls
325 lines (251 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
sdcard.c - streaming plugin for SDCard/FatFs
Part of grblHAL
Copyright (c) 2018-2025 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sdcard.h"
#if FS_ENABLE & FS_SDCARD
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 80
#if defined(ESP_PLATFORM) || defined(STM32_PLATFORM) || defined(__LPC17XX__) || defined(__IMXRT1062__) || defined(__MSP432E401Y__)
#define NEW_FATFS
#endif
#include "grbl/report.h"
//#include "grbl/protocol.h"
#include "grbl/state_machine.h"
#include "grbl/stream_file.h"
#include "grbl/vfs.h"
#include "grbl/task.h"
#include "fs_fatfs.h"
#if defined(NEW_FATFS)
static char dev[10] = "";
#endif
// https://e2e.ti.com/support/tools/ccs/f/81/t/428524?Linking-error-unresolved-symbols-rom-h-pinout-c-
/* uses fatfs - http://www.elm-chan.org/fsw/ff/00index_e.html */
static FATFS *fatfs = NULL;
static bool mount_changed = false, realtime_report_subscribed = false, sd_detectable = false;
static xbar_t *detect_pin = NULL;
static sdcard_events_t sdcard;
static on_realtime_report_ptr on_realtime_report;
static on_report_options_ptr on_report_options;
static driver_setup_ptr driver_setup;
static settings_changed_ptr settings_changed;
static void onRealtimeReport (stream_write_ptr stream_write, report_tracking_flags_t report);
#ifdef __MSP432E401Y__
/*---------------------------------------------------------*/
/* User Provided Timer Function for FatFs module */
/*---------------------------------------------------------*/
/* This is a real time clock service to be called from */
/* FatFs module. Any valid time must be returned even if */
/* the system does not support a real time clock. */
DWORD fatfs_getFatTime (void)
{
return ((2007UL-1980) << 25) // Year = 2007
| (6UL << 21) // Month = June
| (5UL << 16) // Day = 5
| (11U << 11) // Hour = 11
| (38U << 5) // Min = 38
| (0U >> 1) // Sec = 0
;
}
#endif
FLASHMEM static bool sdcard_mount (void)
{
static FATFS *fs = NULL;
bool is_mounted = !!fatfs;
if(sdcard.on_mount) {
char *mdev = sdcard.on_mount(&fatfs);
if(fatfs != NULL) {
#ifdef NEW_FATFS
if(mdev)
strcpy(dev, mdev);
#endif
}
} else {
if(fs == NULL)
fs = malloc(sizeof(FATFS));
#ifdef NEW_FATFS
if(fs && (f_mount(fs, dev, 1) == FR_OK))
#else
if(fs && (f_mount(0, fs) == FR_OK))
#endif
fatfs = fs;
else
fatfs = NULL;
}
if((mount_changed = is_mounted != !!fatfs) && !realtime_report_subscribed) {
realtime_report_subscribed = true;
on_realtime_report = grbl.on_realtime_report;
grbl.on_realtime_report = onRealtimeReport; // Add mount status changes and job percent complete to real time report
}
if(fatfs != NULL)
fs_fatfs_mount("/");
return fatfs != NULL;
}
FLASHMEM static void sdcard_auto_mount (void *data)
{
if(fatfs == NULL && !sdcard_mount())
report_message("SD card automount failed", Message_Info);
}
static bool sdcard_unmount (void)
{
if(fatfs) {
if(sdcard.on_unmount)
mount_changed = sdcard.on_unmount(&fatfs);
#ifdef NEW_FATFS
else
mount_changed = f_unmount(dev) == FR_OK;
#else
else
mount_changed = f_mount(0, NULL) == FR_OK;
#endif
if(mount_changed && fatfs) {
fatfs = NULL;
vfs_unmount("/");
}
}
return fatfs == NULL;
}
FLASHMEM static status_code_t sd_cmd_mount (sys_state_t state, char *args)
{
return sdcard_mount() ? Status_OK : Status_SDMountError;
}
FLASHMEM static status_code_t sd_cmd_unmount (sys_state_t state, char *args)
{
return fatfs ? (sdcard_unmount() ? Status_OK : Status_SDMountError) : Status_SDNotMounted;
}
#if FF_FS_READONLY == 0 && FF_USE_MKFS == 1
FLASHMEM static status_code_t sd_cmd_format (sys_state_t state, char *args)
{
status_code_t status = Status_InvalidStatement;
if(fatfs) {
vfs_drive_t *drive = vfs_get_drive("/");
if(drive->fs && !strcmp(args, "yes")) {
report_message("Formatting SD card...", Message_Info);
if(vfs_drive_format(drive) == 0)
status = !sdcard_mount() ? Status_SDMountError : Status_OK;
else
status = Status_FsFormatFailed;
report_message("", Message_Plain);
}
} else
status = Status_SDNotMounted;
return status;
}
#endif
FLASHMEM static void sd_detect (void *mount)
{
if((uint32_t)mount == 0)
sdcard_unmount();
else if(fatfs == NULL)
sdcard_mount();
}
ISR_CODE void ISR_FUNC(sdcard_detect)(bool mount)
{
task_add_immediate(sd_detect, (void *)mount);
}
static void onRealtimeReport (stream_write_ptr stream_write, report_tracking_flags_t report)
{
if(report.all || mount_changed) {
stream_write("|SD:");
stream_write(uitoa((sd_detectable ? 2 : 0) + !!fatfs));
mount_changed = false;
}
if(on_realtime_report)
on_realtime_report(stream_write, report);
}
FLASHMEM static void sd_detect_pin (xbar_t *pin, void *data)
{
if(pin->id == Input_SdCardDetect) {
sd_detectable = true;
if(pin->get_value)
detect_pin = pin;
}
}
FLASHMEM static void onSettingsChanged (settings_t *settings, settings_changed_flags_t changed)
{
static bool mount_attempted = false; // in case some other code hooked into hal.settings_changed
settings_changed(settings, changed);
if(!mount_attempted) {
mount_attempted = true;
sdcard_mount();
}
}
FLASHMEM static bool onDriverSetup (settings_t *settings)
{
bool ok;
settings_changed = hal.settings_changed;
hal.settings_changed = onSettingsChanged;
ok = driver_setup(settings);
if(hal.settings_changed == onSettingsChanged)
hal.settings_changed = settings_changed;
return ok;
}
// Attempt early mount before other clients access a shared SPI bus.
FLASHMEM void sdcard_early_mount (void)
{
if(detect_pin == NULL || detect_pin->get_value(detect_pin) == 0.0f) {
if (driver_setup == NULL){ //has not been called
driver_setup = hal.driver_setup;
hal.driver_setup = onDriverSetup;
}
}
}
FLASHMEM static void onReportOptions (bool newopt)
{
on_report_options(newopt);
if(newopt)
hal.stream.write(",SD");
else
report_plugin("SDCARD", "1.26");
}
FLASHMEM sdcard_events_t *sdcard_init (void)
{
PROGMEM static const sys_command_t sdcard_command_list[] = {
{"FM", sd_cmd_mount, { .noargs = On }, { .str = "mount SD card" } },
{"FU", sd_cmd_unmount, { .noargs = On }, { .str = "unmount SD card" } },
#if FF_FS_READONLY == 0 && FF_USE_MKFS == 1
{"FF", sd_cmd_format, {}, { .str = "$FF=yes - format SD card" } },
#endif
};
static sys_commands_t sdcard_commands = {
.n_commands = sizeof(sdcard_command_list) / sizeof(sys_command_t),
.commands = sdcard_command_list
};
PROGMEM static const status_detail_t status_detail[] = {
{ Status_SDMountError, "SD Card mount failed." },
{ Status_SDNotMounted, "SD Card not mounted." }
};
static error_details_t error_details = {
.errors = status_detail,
.n_errors = sizeof(status_detail) / sizeof(status_detail_t)
};
hal.driver_cap.sd_card = On;
hal.enumerate_pins(false, sd_detect_pin, NULL);
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;
errors_register(&error_details);
system_register_commands(&sdcard_commands);
if(settings.fs_options.sd_mount_on_boot)
task_run_on_startup(sdcard_auto_mount, NULL);
return &sdcard;
}
FLASHMEM FATFS *sdcard_getfs (void)
{
if(fatfs == NULL)
sdcard_mount();
return fatfs;
}
#endif // FS_ENABLE & FS_SDCARD