This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
sd.c
118 lines (93 loc) · 3 KB
/
sd.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
/** \file Coordinating reading and writing of SD cards.
*/
#include "sd.h"
#ifdef SD
#include "delay.h"
#include "pinio.h"
#include "serial.h"
#include "sersendf.h"
#include "gcode_parse.h"
static FATFS sdfile;
static FRESULT result;
/** Initialize SPI for SD card reading.
*/
void sd_init(void) {
SET_OUTPUT(SD_CARD_SELECT_PIN);
WRITE(SD_CARD_SELECT_PIN, 1);
}
/** Mount the SD card.
*/
void sd_mount(void) {
result = pf_mount(&sdfile);
if (result != FR_OK)
sersendf_P(PSTR("E: SD init failed. (%su)\n"), result);
}
/** Unmount the SD card.
This makes just sure subsequent reads to the card do nothing, instead of
trying and failing. Not mandatory, just removing the card is fine, as well
as inserting and mounting another one without previous unmounting.
*/
void sd_unmount(void) {
pf_unmount(&sdfile);
}
/** List a given directory.
\param path The path to list. Toplevel path is "/".
A slash is added to directory names, to make it easier for users to
recognize them.
*/
void sd_list(const char* path) {
FILINFO fno;
DIR dir;
result = pf_opendir(&dir, path);
if (result == FR_OK) {
for (;;) {
result = pf_readdir(&dir, &fno);
if (result != FR_OK || fno.fname[0] == 0)
break;
serial_writestr((uint8_t *)fno.fname);
if (fno.fattrib & AM_DIR)
serial_writechar('/');
serial_writechar('\n');
delay_ms(2); // Time for sending the characters.
}
}
else {
sersendf_P(PSTR("E: failed to open dir. (%su)\n"), result);
}
}
/** Open a file for reading.
\param filename Name of the file to open and to read G-code from.
Before too long this will cause the printer to read G-code from this file
until done or until stopped by G-code coming in over the serial line.
*/
void sd_open(const char* filename) {
result = pf_open(filename);
if (result != FR_OK) {
sersendf_P(PSTR("E: failed to open file. (%su)\n"), result);
}
}
/** Read a line of G-code from a file.
\param A pointer to the parser function. This function should accept an
uint8_t with the character to parse and return an uint8_t whether
end of line (EOL) was reached.
\return Whether end of line (EOF) was reached or an error happened.
Juggling with a buffer smaller than 512 bytes means that the underlying
SD card handling code reads a full sector (512 bytes) in each operation,
but throws away everything not fitting into this buffer. Next read operation
reads the very same sector, but keeps a different part. That's ineffective.
Much better is to parse straight as it comes from the card. This way there
is no need for a buffer at all. Sectors are still read multiple times, but
at least one line is read in one chunk (unless it crosses a sector boundary).
*/
uint8_t sd_read_gcode_line(void) {
result = pf_parse_line(&gcode_parse_char);
if (result == FR_END_OF_FILE) {
return 1;
}
else if (result != FR_OK) {
sersendf_P(PSTR("E: failed to parse from file. (%su)\n"), result);
return 1;
}
return 0;
}
#endif /* SD */