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