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