-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdcard.c
859 lines (674 loc) · 25 KB
/
sdcard.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
sdcard.c - streaming plugin for SDCard/FatFs
Part of grblHAL
Copyright (c) 2018-2024 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 SDCARD_ENABLE
#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
#ifdef ARDUINO
#include "../grbl/report.h"
#include "../grbl/protocol.h"
#include "../grbl/state_machine.h"
#include "../grbl/vfs.h"
#else
#include "grbl/report.h"
#include "grbl/protocol.h"
#include "grbl/state_machine.h"
#include "grbl/vfs.h"
#endif
#include "ymodem.h"
#include "macros.h"
#include "fs_fatfs.h"
#if defined(NEW_FATFS)
static char *dev = "";
#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 */
#define MAX_PATHLEN 128
char const *const filetypes[] = {
"nc",
"ncc",
"ngc",
"cnc",
"gcode",
"txt",
"text",
"tap",
"macro",
""
};
static vfs_file_t *cncfile;
typedef enum {
Filename_Filtered = 0,
Filename_Valid,
Filename_Invalid
} file_status_t;
typedef struct
{
FATFS *fs;
vfs_file_t *handle;
char name[50];
size_t size;
size_t pos;
uint32_t line;
uint8_t eol;
} file_t;
static file_t file = {
.fs = NULL,
.handle = NULL,
.size = 0,
.pos = 0
};
static bool frewind = false, webui = false;
static io_stream_t active_stream;
static sdcard_events_t sdcard;
static driver_reset_ptr driver_reset;
static on_realtime_report_ptr on_realtime_report;
static on_state_change_ptr state_change_requested;
static on_program_completed_ptr on_program_completed;
static enqueue_realtime_command_ptr enqueue_realtime_command;
static on_report_options_ptr on_report_options;
static on_stream_changed_ptr on_stream_changed;
static stream_read_ptr read_redirected;
static status_message_ptr status_message = NULL;
static void sdcard_end_job (bool flush);
static void sdcard_report (stream_write_ptr stream_write, report_tracking_flags_t report);
static void trap_state_change_request(uint_fast16_t state);
static status_code_t trap_status_messages (status_code_t status_code);
static void sdcard_on_program_completed (program_flow_t program_flow, bool check_mode);
//static report_t active_reports;
PROGMEM static const status_detail_t status_detail[] = {
{ Status_SDMountError, "SD Card mount failed." },
{ Status_SDReadError, "SD Card file open/read failed." },
{ Status_SDFailedOpenDir, "SD Card directory listing failed." },
{ Status_SDDirNotFound, "SD Card directory not found." },
{ Status_SDFileEmpty, "SD Card file empty." }
};
static error_details_t error_details = {
.errors = status_detail,
.n_errors = sizeof(status_detail) / sizeof(status_detail_t)
};
#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
static file_status_t filename_valid (char *filename)
{
return strlen(filename) > 40 || strchr(filename, CMD_STATUS_REPORT) || strchr(filename, CMD_CYCLE_START) || strchr(filename, CMD_FEED_HOLD)
? Filename_Invalid
: Filename_Valid;
}
static file_status_t allowed (char *filename, bool is_file)
{
uint_fast8_t idx = 0;
char filetype[8], *ftptr;
file_status_t status = is_file ? Filename_Filtered : Filename_Valid;
if(is_file && (ftptr = strrchr(filename, '.'))) {
ftptr++;
if(strlen(ftptr) > sizeof(filetype) - 1)
return status;
while(ftptr[idx]) {
filetype[idx] = LCAPS(ftptr[idx]);
idx++;
}
filetype[idx] = '\0';
idx = 0;
while(status == Filename_Filtered && filetypes[idx][0]) {
if(!strcmp(filetype, filetypes[idx]))
status = Filename_Valid;
idx++;
}
}
return status == Filename_Valid ? filename_valid(filename) : status;
}
static int scan_dir (char *path, uint_fast8_t depth, char *buf, bool filtered)
{
int res = 0;
bool subdirs = false;
vfs_dir_t *dir;
vfs_dirent_t *dirent;
file_status_t status;
if((dir = vfs_opendir(*path == '\0' ? "/" : path)) == NULL)
return vfs_errno;
// Pass 1: Scan files
while(true) {
if((dirent = vfs_readdir(dir)) == NULL || dirent->name[0] == '\0')
break;
subdirs |= dirent->st_mode.directory;
if(!dirent->st_mode.directory && (status = filtered ? allowed(dirent->name, true) : filename_valid(dirent->name)) != Filename_Filtered) {
if(snprintf(buf, BUFLEN, "[FILE:%s/%s|SIZE:" UINT32FMT "%s]" ASCII_EOL, path, dirent->name, (uint32_t)dirent->size, status == Filename_Invalid ? "|UNUSABLE" : "") < BUFLEN)
hal.stream.write(buf);
}
}
vfs_closedir(dir);
dir = NULL;
if((subdirs = (subdirs && --depth)))
subdirs = (dir = vfs_opendir(*path == '\0' ? "/" : path)) != NULL;
// Pass 2: Scan directories
while(subdirs) {
if((dirent = vfs_readdir(dir)) == NULL || dirent->name[0] == '\0')
break;
if(dirent->st_mode.directory) { // It is a directory
size_t pathlen = strlen(path);
if(pathlen + strlen(dirent->name) > (MAX_PATHLEN - 1))
break;
sprintf(&path[pathlen], "/%s", dirent->name);
if((res = scan_dir(path, depth, buf, filtered)) != 0)
break;
path[pathlen] = '\0';
}
}
if(dir)
vfs_closedir(dir);
return res;
}
static void file_close (void)
{
if(file.handle) {
vfs_close(file.handle);
if(hal.stream.file == file.handle)
hal.stream.file = NULL;
file.handle = NULL;
}
}
static bool file_open (char *filename)
{
if(file.handle)
file_close();
if((cncfile = vfs_open(filename, "r")) != NULL) {
vfs_stat_t st;
vfs_stat(filename, &st);
file.handle = cncfile;
file.size = st.st_size;
file.pos = 0;
file.line = 0;
file.eol = false;
char *leafname = strrchr(filename, '/');
strncpy(file.name, leafname ? leafname + 1 : filename, sizeof(file.name));
file.name[sizeof(file.name) - 1] = '\0';
}
return file.handle != NULL;
}
static int16_t file_read (void)
{
signed char c[1];
if(vfs_read(&c, 1, 1, file.handle) == 1)
file.pos = vfs_tell(file.handle);
else
*c = -1;
if(*c == '\r' || *c == '\n')
file.eol++;
else
file.eol = 0;
return (int16_t)*c;
}
static bool sdcard_mount (void)
{
if(sdcard.on_mount) {
sdcard.on_mount(&file.fs);
if(file.fs != NULL)
fs_fatfs_mount("/");
return file.fs != NULL;
}
if(file.fs == NULL)
file.fs = malloc(sizeof(FATFS));
#ifdef NEW_FATFS
if(file.fs && f_mount(file.fs, dev, 1) != FR_OK) {
#else
if(file.fs && f_mount(0, file.fs) != FR_OK) {
#endif
free(file.fs);
file.fs = NULL;
}
if(file.fs != NULL)
fs_fatfs_mount("/");
return file.fs != NULL;
}
static void sdcard_auto_mount (void *data)
{
if(!sdcard_mount())
report_message("SD card automount failed", Message_Info);
}
static bool sdcard_unmount (void)
{
bool ok = true;
if(file.fs) {
if(sdcard.on_unmount)
ok = sdcard.on_unmount(&file.fs);
#ifdef NEW_FATFS
else
f_mount(file.fs, dev, 0);
#endif
if(ok && file.fs) {
free(file.fs);
file.fs = NULL;
vfs_unmount("/");
}
}
return file.fs == NULL;
}
static status_code_t sdcard_ls (bool filtered)
{
char path[MAX_PATHLEN] = "", name[BUFLEN]; // NB! also used as work area when recursing directories
return scan_dir(path, 10, name, filtered) == FR_OK ? Status_OK : Status_SDFailedOpenDir;
}
static void sdcard_end_job (bool flush)
{
file_close();
if(grbl.on_realtime_report == sdcard_report)
grbl.on_realtime_report = on_realtime_report;
if(grbl.on_program_completed == sdcard_on_program_completed)
grbl.on_program_completed = on_program_completed;
if(grbl.on_state_change == trap_state_change_request)
grbl.on_state_change = state_change_requested;
grbl.on_stream_changed = on_stream_changed;
memcpy(&hal.stream, &active_stream, sizeof(io_stream_t)); // Restore stream pointers,
active_stream.type = StreamType_Null; // ...
hal.stream.set_enqueue_rt_handler(enqueue_realtime_command); // real time command handling and
if(grbl.report.status_message == trap_status_messages) // ...
grbl.report.status_message = status_message; // normal status message handling.
else
report_init_fns();
status_message = NULL;
if(flush) // Flush input buffer?
hal.stream.reset_read_buffer(); // Yes, do it.
on_realtime_report = NULL;
state_change_requested = NULL;
webui = frewind = false;
if(grbl.on_stream_changed)
grbl.on_stream_changed(hal.stream.type);
}
static int16_t sdcard_read (void)
{
int16_t c = SERIAL_NO_DATA;
sys_state_t state = state_get();
if(file.eol == 1)
file.line++;
if(file.handle) {
if(state == STATE_IDLE || (state & (STATE_CYCLE|STATE_HOLD|STATE_CHECK_MODE|STATE_TOOL_CHANGE)))
c = file_read();
if(c == -1) { // EOF or error reading or grblHAL problem
file_close();
if(file.eol == 0) // Return newline if line was incorrectly terminated
c = '\n';
}
} else if((state == STATE_IDLE || state == STATE_CHECK_MODE) && grbl.on_program_completed == sdcard_on_program_completed) { // TODO: end on ok count match line count?
sdcard_on_program_completed(ProgramFlow_CompletedM30, state == STATE_CHECK_MODE);
grbl.report.feedback_message(Message_ProgramEnd);
}
return c;
}
static int16_t await_cycle_start (void)
{
return -1;
}
// Drop input from current stream except realtime commands
ISR_CODE static bool ISR_FUNC(drop_input_stream)(char c)
{
enqueue_realtime_command(c);
return true;
}
static void trap_state_change_request (sys_state_t state)
{
if(state == STATE_CYCLE) {
if(hal.stream.read == await_cycle_start)
hal.stream.read = read_redirected;
if(grbl.on_state_change== trap_state_change_request) {
grbl.on_state_change = state_change_requested;
state_change_requested = NULL;
}
}
if(state_change_requested)
state_change_requested(state);
}
static status_code_t trap_status_messages (status_code_t status_code)
{
if(hal.stream.read != read_redirected)
status_code = status_message(status_code);
else if(status_code != Status_OK) {
// TODO: all errors should terminate job?
char buf[50]; // TODO: check if extended error reports are permissible
sprintf(buf, "error:%d in SD file at line " UINT32FMT ASCII_EOL, (uint8_t)status_code, file.line);
hal.stream.write(buf);
sdcard_end_job(true);
grbl.report.status_message(status_code);
}
return status_code;
}
static void sdcard_report (stream_write_ptr stream_write, report_tracking_flags_t report)
{
if(hal.stream.read == read_redirected) {
char *pct_done = ftoa((float)file.pos / (float)file.size * 100.0f, 1);
if(state_get() != STATE_IDLE && !strncmp(pct_done, "100.0", 5))
strcpy(pct_done, "99.9");
stream_write("|SD:");
stream_write(pct_done);
stream_write(",");
stream_write(file.name);
} else if(hal.stream.read == await_cycle_start)
stream_write("|SD:Pending");
if(on_realtime_report)
on_realtime_report(stream_write, report);
}
static void sdcard_restart_msg (void *data)
{
grbl.report.feedback_message(Message_CycleStartToRerun);
}
static void sdcard_on_program_completed (program_flow_t program_flow, bool check_mode)
{
#if WEBUI_ENABLE // TODO: somehow add run time check?
frewind = false; // Not (yet?) supported.
#else
frewind = frewind || program_flow == ProgramFlow_CompletedM2; // || program_flow == ProgramFlow_CompletedM30;
#endif
if(frewind) {
vfs_seek(file.handle, 0);
file.pos = file.line = 0;
file.eol = false;
hal.stream.read = await_cycle_start;
if(grbl.on_state_change != trap_state_change_request) {
state_change_requested = grbl.on_state_change;
grbl.on_state_change = trap_state_change_request;
}
protocol_enqueue_foreground_task(sdcard_restart_msg, NULL);
} else
sdcard_end_job(true);
if(on_program_completed)
on_program_completed(program_flow, check_mode);
}
ISR_CODE static bool ISR_FUNC(await_toolchange_ack)(char c)
{
if(c == CMD_TOOL_ACK) {
hal.stream.read = active_stream.read; // Restore normal stream input for tool change (jog etc)
active_stream.set_enqueue_rt_handler(enqueue_realtime_command); // ...
} else
return enqueue_realtime_command(c);
return true;
}
static bool sdcard_suspend (bool suspend)
{
if(suspend) {
hal.stream.read = stream_get_null; // Set read function to return empty,
active_stream.reset_read_buffer(); // flush input buffer,
active_stream.set_enqueue_rt_handler(await_toolchange_ack); // and set handler to wait for tool change acknowledge.
} else {
hal.stream.read = read_redirected; // Resume reading from SD card
hal.stream.set_enqueue_rt_handler(drop_input_stream); // ..
}
return true;
}
static void terminate_job (void *data)
{
if(state_get() == STATE_CYCLE) {
// Halt motion so that executing stop does not result in loss of position
system_set_exec_state_flag(EXEC_MOTION_CANCEL);
do {
if(!protocol_execute_realtime()) // Check for system abort
break;
} while (state_get() != STATE_IDLE);
}
sys.flags.keep_input = On;
system_set_exec_state_flag(EXEC_STOP);
sdcard_end_job(false);
report_message("SD card job terminated due to connection change", Message_Info);
}
static bool check_input_stream (char c)
{
bool ok;
if(!(ok = enqueue_realtime_command(c))) {
if(hal.stream.read != stream_get_null) {
hal.stream.read = stream_get_null;
protocol_enqueue_foreground_task(terminate_job, NULL);
}
}
return ok;
}
static void stream_changed (stream_type_t type)
{
if(type != StreamType_File && file.handle != NULL) {
// Reconnect from WebUI?
if(webui && (type != StreamType_WebSocket || hal.stream.state.webui_connected)) {
active_stream.set_enqueue_rt_handler(enqueue_realtime_command); // Restore previous real time handler,
memcpy(&active_stream, &hal.stream, sizeof(io_stream_t)); // save current stream pointers
hal.stream.type = StreamType_File; // then redirect to read from SD card instead
hal.stream.file = file.handle; // ...
hal.stream.read = read_redirected; // ...
if(hal.stream.suspend_read) // If active stream support tool change suspend
hal.stream.suspend_read = sdcard_suspend; // then we do as well
else //
hal.stream.suspend_read = NULL; // else not
if(type == StreamType_WebSocket) // If WebUI came back online
enqueue_realtime_command = hal.stream.set_enqueue_rt_handler(drop_input_stream); // restore normal operation
else // else
enqueue_realtime_command = hal.stream.set_enqueue_rt_handler(check_input_stream); // check for stream takeover
} else // Terminate job.
protocol_enqueue_foreground_task(terminate_job, NULL);
}
if(on_stream_changed)
on_stream_changed(type);
}
status_code_t stream_file (sys_state_t state, char *fname)
{
status_code_t retval = Status_Unhandled;
if (!(state == STATE_IDLE || state == STATE_CHECK_MODE))
retval = Status_SystemGClock;
else if(fname && file_open(fname)) {
gc_state.last_error = Status_OK; // Start with no errors
grbl.report.status_message(Status_OK); // and confirm command to originator.
webui = hal.stream.state.webui_connected; // Did WebUI start this job?
if(!(grbl.on_file_open && (retval = grbl.on_file_open(fname, file.handle, true)) == Status_OK)) {
memcpy(&active_stream, &hal.stream, sizeof(io_stream_t)); // Save current stream pointers
hal.stream.type = StreamType_File; // then redirect to read from SD card instead
hal.stream.file = file.handle; // ...
hal.stream.read = sdcard_read; // ...
if(hal.stream.suspend_read) // If active stream support tool change suspend
hal.stream.suspend_read = sdcard_suspend; // then we do as well
else //
hal.stream.suspend_read = NULL; // else not.
on_realtime_report = grbl.on_realtime_report;
grbl.on_realtime_report = sdcard_report; // Add percent complete to real time report
on_program_completed = grbl.on_program_completed;
grbl.on_program_completed = sdcard_on_program_completed;
status_message = grbl.report.status_message; // Add trap for status messages
grbl.report.status_message = trap_status_messages; // so we can terminate on errors.
enqueue_realtime_command = hal.stream.set_enqueue_rt_handler(drop_input_stream); // Drop input from current stream except realtime commands
if(grbl.on_stream_changed)
grbl.on_stream_changed(hal.stream.type);
read_redirected = hal.stream.read;
if(grbl.on_stream_changed != stream_changed) {
on_stream_changed = grbl.on_stream_changed;
grbl.on_stream_changed = stream_changed;
}
retval = Status_OK;
} else
file.handle = NULL;
} else
retval = Status_SDReadError;
return retval;
}
static status_code_t sd_cmd_file_filtered (sys_state_t state, char *args)
{
status_code_t retval = Status_Unhandled;
if(args)
retval = stream_file(state, args);
else {
frewind = false;
retval = sdcard_ls(true); // (re)use line buffer for reporting filenames
}
return retval;
}
static status_code_t sd_cmd_file_all (sys_state_t state, char *args)
{
status_code_t retval = Status_Unhandled;
if(args)
retval = stream_file(state, args);
else {
frewind = false;
retval = sdcard_ls(false); // (re)use line buffer for reporting filenames
}
return retval;
}
static status_code_t sd_cmd_mount (sys_state_t state, char *args)
{
frewind = false;
return sdcard_mount() ? Status_OK : Status_SDMountError;
}
static status_code_t sd_cmd_unmount (sys_state_t state, char *args)
{
frewind = false;
return sdcard_unmount() ? Status_OK : Status_SDMountError;
}
static status_code_t sd_cmd_rewind (sys_state_t state, char *args)
{
frewind = true;
return Status_OK;
}
static status_code_t sd_cmd_to_output (sys_state_t state, char *args)
{
status_code_t retval = Status_Unhandled;
if (!(state == STATE_IDLE || state == STATE_CHECK_MODE))
retval = Status_SystemGClock;
else if(args) {
if(file_open(args)) {
if(!(grbl.on_file_open && (retval = grbl.on_file_open(args, file.handle, false)) == Status_OK)) {
int16_t c;
char buf[2] = {0};
while((c = file_read()) != -1) {
buf[0] = (char)c;
hal.stream.write(buf);
}
file_close();
retval = Status_OK;
} else
file.handle = NULL;
} else
retval = Status_SDReadError;
}
return retval;
}
#if FF_FS_READONLY == 0 && FF_FS_MINIMIZE == 0
static status_code_t sd_cmd_unlink (sys_state_t state, char *args)
{
status_code_t retval = Status_Unhandled;
if (!(state == STATE_IDLE || state == STATE_CHECK_MODE))
retval = Status_SystemGClock;
else if(args)
retval = vfs_unlink(args) ? Status_OK : Status_SDReadError;
return retval;
}
#endif
static void sdcard_reset (void)
{
if(hal.stream.type == StreamType_File && active_stream.type != StreamType_Null) {
if(file.line > 0) {
char buf[70];
sprintf(buf, "Reset during streaming of SD file at line: " UINT32FMT, file.line);
report_message(buf, Message_Plain);
} else if(frewind)
grbl.report.feedback_message(Message_None);
sdcard_end_job(true);
}
driver_reset();
}
static void onReportOptions (bool newopt)
{
on_report_options(newopt);
if(newopt)
#if SDCARD_ENABLE == 2 && FF_FS_READONLY == 0
hal.stream.write(hal.stream.write_char == NULL ? ",SD" : ",SD,YM");
#else
hal.stream.write(",SD");
#endif
else
hal.stream.write("[PLUGIN:SDCARD v1.14]" ASCII_EOL);
}
const sys_command_t sdcard_command_list[] = {
{"F", sd_cmd_file_filtered, {}, {
.str = "list files on SD card, filtered"
ASCII_EOL "$F=<filename> - run SD card file"
} },
{"F+", sd_cmd_file_all, {}, { .str = "$F+ - list all files on SD card" } },
{"FM", sd_cmd_mount, { .noargs = On }, { .str = "mount SD card" } },
{"FU", sd_cmd_unmount, { .noargs = On }, { .str = "unmount SD card" } },
{"FR", sd_cmd_rewind, { .noargs = On }, { .str = "enable rewind mode for next SD card file to run" } },
#if FF_FS_READONLY == 0 && FF_FS_MINIMIZE == 0
{"FD", sd_cmd_unlink, {}, { .str = "$FD=<filename> - delete SD card file" } },
#endif
{"F<", sd_cmd_to_output, {}, { .str = "$F<=<filename> - dump SD card file to output" } },
};
static sys_commands_t sdcard_commands = {
.n_commands = sizeof(sdcard_command_list) / sizeof(sys_command_t),
.commands = sdcard_command_list
};
sdcard_events_t *sdcard_init (void)
{
active_stream.type = StreamType_Null;
hal.driver_cap.sd_card = On;
driver_reset = hal.driver_reset;
hal.driver_reset = sdcard_reset;
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;
errors_register(&error_details);
system_register_commands(&sdcard_commands);
#if SDCARD_ENABLE == 2 && FF_FS_READONLY == 0
if(hal.stream.write_char != NULL)
ymodem_init();
#endif
fs_macros_init();
if(settings.fs_options.sd_mount_on_boot)
protocol_enqueue_foreground_task(sdcard_auto_mount, NULL);
return &sdcard;
}
bool sdcard_busy (void)
{
return hal.stream.type == StreamType_File;
}
sdcard_job_t *sdcard_get_job_info (void)
{
static sdcard_job_t job;
if(sdcard_busy()) {
strcpy(job.name, file.name);
job.size = file.size;
job.pos = file.pos;
job.line = file.line;
}
return sdcard_busy() ? &job : NULL;
}
FATFS *sdcard_getfs (void)
{
if(file.fs == NULL)
sdcard_mount();
return file.fs;
}
#endif