]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
Adaptions for fatfs R0.15
[z180-stamp.git] / avr / cmd_sd.c
1 /*
2 * (C) Copyright 2014, 2018 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "cmd_sd.h"
8
9 #include "diskio.h"
10 #include "eval_arg.h"
11 #include "print-utils.h"
12 #include "z80-if.h"
13
14
15 /*
16 * status <pd#> - Show socket status
17 *
18 */
19 static
20 command_ret_t do_status(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
21 {
22 DSTATUS res;
23 BYTE dev;
24
25 (void) cmdtp; (void) flag;
26
27 if (argc < 2)
28 return CMD_RET_USAGE;
29
30 dev = (BYTE) eval_arg(argv[1], NULL);
31 res = disk_status(dev);
32 printf_P(PSTR("Socket status: %02x\n"), res);
33
34 return CMD_RET_SUCCESS;
35 }
36
37 /*
38 * init <pd#> - Initialize disk
39 *
40 */
41 static
42 command_ret_t do_init(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
43 {
44 DSTATUS res;
45 BYTE dev;
46
47 (void) cmdtp; (void) flag;
48
49 if (argc < 2)
50 return CMD_RET_USAGE;
51
52 dev = (BYTE) eval_arg(argv[1], NULL);
53
54 if (disk_status(dev) & STA_NODISK)
55 cmd_error(CMD_RET_FAILURE, 0, PSTR("No disk"));
56
57 res = disk_initialize(dev);
58 printf_P(PSTR("rc=%.2x\n"), res);
59
60 if (res & (STA_NODISK | STA_NOINIT))
61 return CMD_RET_FAILURE;
62
63 return CMD_RET_SUCCESS;
64 }
65
66 /*
67 * info <pd#> - Show disk info
68 *
69 */
70 static
71 command_ret_t do_info(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
72 {
73 DSTATUS res;
74 BYTE dev;
75
76 union {
77 unsigned char uca[64];
78 unsigned long ul;
79 unsigned char uc;
80 } dat;
81
82 (void) cmdtp; (void) flag;
83
84 if (argc < 2)
85 return CMD_RET_USAGE;
86
87 dev = (BYTE) eval_arg(argv[1], NULL);
88
89 res = disk_status(dev);
90 if (res & (STA_NODISK | STA_NOINIT)) {
91 printf_P(res & STA_NODISK ?
92 PSTR("No disk\n") : PSTR("Not initialized\n"));
93 return CMD_RET_FAILURE;
94 }
95
96 if (disk_ioctl(dev, GET_SECTOR_COUNT, &dat.ul) == RES_OK)
97 printf_P(PSTR("Drive size: %lu sectors\n"), dat.ul);
98 if (disk_ioctl(dev, GET_BLOCK_SIZE, &dat.ul) == RES_OK)
99 printf_P(PSTR("Erase block: %lu sectors\n"), dat.ul);
100 if (disk_ioctl(dev, MMC_GET_TYPE, &dat.uc) == RES_OK)
101 printf_P(PSTR("Card type: %u\n"), dat.uc);
102 if (disk_ioctl(dev, MMC_GET_CSD, dat.uca) == RES_OK)
103 dump_ram(dat.uca, 0, 16, "CSD:");
104 if (disk_ioctl(dev, MMC_GET_CID, dat.uca) == RES_OK)
105 dump_ram(dat.uca, 0, 16, "CID:");
106 if (disk_ioctl(dev, MMC_GET_OCR, dat.uca) == RES_OK)
107 dump_ram(dat.uca, 0, 4, "OCR:");
108 if (disk_ioctl(dev, MMC_GET_SDSTAT, dat.uca) == RES_OK)
109 dump_ram(dat.uca, 0, 64, "SD Status:");
110
111 return CMD_RET_SUCCESS;
112 }
113
114
115 /*
116 * dump <pd#> [<sector> [count]] - Dump sector
117 *
118 */
119 static
120 command_ret_t do_dump(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
121 {
122 static BYTE dev_last;
123 static DWORD sec_last;
124 BYTE buffer[FF_MAX_SS];
125 char header[20];
126
127 DRESULT res;
128 BYTE dev;
129 DWORD sec;
130 UINT count;
131
132 (void) cmdtp; (void) flag;
133
134 if (argc < 2)
135 return CMD_RET_USAGE;
136
137 dev = (BYTE) eval_arg(argv[1], NULL);
138 if (dev != dev_last)
139 sec_last = 0;
140 sec = sec_last;
141 count = 1;
142
143 if ((flag & CMD_FLAG_REPEAT) == 0) {
144 /* If another parameter, it is the sector to dump. */
145 if (argc > 2)
146 sec = eval_arg(argv[2], NULL);
147 if (argc > 3)
148 count = (UINT) eval_arg(argv[3], NULL);
149 }
150
151 for ( ; count; count--, sec++) {
152 res = disk_read(dev, buffer, sec, 1);
153
154 if (res)
155 cmd_error(CMD_RET_FAILURE, 0, PSTR("rc=%.2x"), res);
156
157 sprintf_P(header, PSTR("Sector: %lu"), sec);
158 dump_ram(buffer, 0, FF_MAX_SS, header);
159 }
160 dev_last = dev;
161 sec_last = sec;
162
163 return CMD_RET_SUCCESS;
164 }
165
166 /*
167 * read drive sector count memaddr - Read disk into memory
168 *
169 */
170 static
171 command_ret_t do_read(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
172 {
173 DRESULT res;
174 BYTE dev;
175 DWORD sec;
176 uint32_t addr;
177 int count, nr;
178 BYTE buffer[FF_MAX_SS];
179
180 static DWORD sec_last;
181 static uint32_t addr_last;
182
183 (void) cmdtp;
184
185 if (argc < 2)
186 return CMD_RET_USAGE;
187
188 dev = (BYTE) eval_arg(argv[1], NULL);
189 sec = sec_last;
190 count = 1;
191 addr = addr_last;
192
193 if ((flag & CMD_FLAG_REPEAT) == 0) {
194 /* If another parameter, it is the sector to dump. */
195 if (argc > 2)
196 sec = eval_arg(argv[2], NULL);
197 if (argc > 3)
198 count = eval_arg(argv[3], NULL);
199 if (argc > 4)
200 addr = eval_arg(argv[4], NULL);
201 }
202
203 for (nr = 0; nr < count;) {
204 nr++;
205 if ((res = disk_read(dev, buffer, sec, 1)) == RES_OK) {
206 z80_bus_request_or_exit();
207 z80_write_block(buffer, addr /*+ base*/, FF_MAX_SS);
208 z80_bus_cmd(Release);
209 sec++; addr += FF_MAX_SS;
210 } else
211 break;
212 }
213
214 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr, res);
215 if (res)
216 printf_P(PSTR("Last sector not written!\n"));
217
218 sec_last = sec;
219 addr_last = addr;
220
221 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
222 }
223
224
225 /*
226 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
227 *
228 */
229 static
230 command_ret_t do_write(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
231 {
232 DRESULT res;
233 BYTE dev;
234 DWORD sec;
235 uint32_t addr;
236 int count, nr;
237 BYTE buffer[FF_MAX_SS];
238
239 static DWORD sec_last;
240 static uint32_t addr_last;
241
242 (void) cmdtp;
243
244 if (argc < 2)
245 return CMD_RET_USAGE;
246
247 dev = (BYTE) eval_arg(argv[1], NULL);
248 sec = sec_last;
249 addr = addr_last;
250 count = 1;
251
252 if ((flag & CMD_FLAG_REPEAT) == 0) {
253 /* If another parameter, it is the sector to dump. */
254 if (argc > 2)
255 sec = eval_arg(argv[2], NULL);
256 if (argc > 3)
257 count = eval_arg(argv[3], NULL);
258 if (argc > 4)
259 addr = eval_arg(argv[4], NULL);
260 }
261
262 for (nr = 0; nr < count;) {
263 nr++;
264 z80_bus_request_or_exit();
265 z80_read_block(buffer, addr /*+ base*/, FF_MAX_SS);
266 z80_bus_cmd(Release);
267
268 res = disk_write(dev, buffer, sec, 1);
269 if (res != RES_OK)
270 break;
271 sec++; addr += FF_MAX_SS;
272 }
273
274 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr, res);
275
276 sec_last = sec;
277 addr_last = addr;
278
279 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
280 }
281
282
283
284 /*
285 * Disk ioctl
286 * sync <pd#> - CTRL_SYNC
287 *
288 */
289 static
290 command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
291 {
292 BYTE dev;
293
294 (void) cmdtp; (void) flag;
295
296 if (argc < 2)
297 return CMD_RET_USAGE;
298
299 dev = (BYTE) eval_arg(argv[1], NULL);
300 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev, CTRL_SYNC, 0));
301
302 return CMD_RET_SUCCESS;
303 }
304
305
306 cmd_tbl_t cmd_tbl_sd[] = {
307 CMD_TBL_ITEM(
308 status, 2, CTBL_RPT, do_status,
309 "Socket status",
310 "drive"
311 ),
312 CMD_TBL_ITEM(
313 init, 2, CTBL_RPT, do_init,
314 "Initialize disk",
315 "drive"
316 ),
317 CMD_TBL_ITEM(
318 info, 2, CTBL_RPT, do_info,
319 "Disk info",
320 "drive"
321 ),
322 CMD_TBL_ITEM(
323 dump, CONFIG_SYS_MAXARGS, CTBL_RPT, do_dump,
324 "Dump sector(s)",
325 "drive [sector [count ]]"
326 ),
327 CMD_TBL_ITEM(
328 read, 2, CTBL_RPT, do_read,
329 "Read disk sector(s) into meomory",
330 "drive [sector [count [memaddr]]]"
331 ),
332 CMD_TBL_ITEM(
333 write, 2, CTBL_RPT, do_write,
334 "Write sector(s) from meomory to disk",
335 "drive [sector [count [memaddr]]]"
336 ),
337 CMD_TBL_ITEM(
338 sync, 2, CTBL_RPT, do_ioctl_sync,
339 "Device control: SYNC",
340 "drive"
341 ),
342
343 CMD_TBL_ITEM(
344 help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help,
345 "Print sub command description/usage",
346 "\n"
347 " - print brief description of all sub commands\n"
348 "sd help command ...\n"
349 " - print detailed usage of sub cmd 'command'"
350 ),
351
352 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
353 {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help,
354 NULL,
355 #ifdef CONFIG_SYS_LONGHELP
356 FSTR(""),
357 #endif /* CONFIG_SYS_LONGHELP */
358 NULL,
359 #ifdef CONFIG_AUTO_COMPLETE
360 NULL,
361 #endif
362 },
363 /* Mark end of table */
364 CMD_TBL_END(cmd_tbl_sd)
365 };
366
367
368 command_ret_t do_sd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
369 {
370 puts_P(PSTR("Huch?"));
371 return CMD_RET_USAGE;
372 }