]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
5494a13fda6b4139c6b3368674523fe5011cc0d1
8 #include "print-utils.h"
12 * di <pd#> - Initialize disk
16 command_ret_t
do_init(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
21 (void) cmdtp
; (void) flag
;
26 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
27 res
= disk_status(dev
);
28 printf_P(PSTR("disk_status=%.2x\n"), res
);
30 if ((res
& STA_NODISK
) == 0) {
31 res
= disk_initialize(dev
);
33 printf_P(PSTR("rc=%.2x\n"), res
);
35 return CMD_RET_FAILURE
;
38 return CMD_RET_SUCCESS
;
42 * ds <pd#> - Show disk status
46 command_ret_t
do_status(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
51 unsigned char uca
[64];
56 (void) cmdtp
; (void) flag
;
61 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
64 if (disk_ioctl(dev
, GET_SECTOR_COUNT
, &dat
.ul
) == RES_OK
)
65 printf_P(PSTR("Drive size: %lu sectors\n"), dat
.ul
);
66 if (disk_ioctl(dev
, GET_BLOCK_SIZE
, &dat
.ul
) == RES_OK
)
67 printf_P(PSTR("Erase block: %lu sectors\n"), dat
.ul
);
68 if (disk_ioctl(dev
, MMC_GET_TYPE
, &dat
.uc
) == RES_OK
)
69 printf_P(PSTR("Card type: %u\n"), dat
.uc
);
70 if (disk_ioctl(dev
, MMC_GET_CSD
, dat
.uca
) == RES_OK
)
71 dump_ram(dat
.uca
, 0, 16, "CSD:");
72 if (disk_ioctl(dev
, MMC_GET_CID
, dat
.uca
) == RES_OK
)
73 dump_ram(dat
.uca
, 0, 16, "CID:");
74 if (disk_ioctl(dev
, MMC_GET_OCR
, dat
.uca
) == RES_OK
)
75 dump_ram(dat
.uca
, 0, 4, "OCR:");
76 if (disk_ioctl(dev
, MMC_GET_SDSTAT
, dat
.uca
) == RES_OK
)
77 dump_ram(dat
.uca
, 0, 64, "SD Status:");
79 return CMD_RET_SUCCESS
;
84 * dump <pd#> [<sector> [count]] - Dump sector
88 command_ret_t
do_dump(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
91 static DWORD sec_last
;
100 (void) cmdtp
; (void) flag
;
103 return CMD_RET_USAGE
;
105 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
111 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
112 /* If another parameter, it is the sector to dump. */
114 sec
= strtoul(argv
[2], 0, 10);
116 count
= (UINT
) strtoul(argv
[3], 0, 10);
119 for ( ; count
; count
--, sec
++) {
120 res
= disk_read(dev
, buffer
, sec
, 1);
123 printf_P(PSTR("rc=%.2x\n"), res
);
124 return CMD_RET_FAILURE
;
127 sprintf_P(header
, PSTR("Sector: %lu"), sec
);
128 dump_ram(buffer
, 0, _MAX_SS
, header
);
133 return CMD_RET_SUCCESS
;
137 * read drive sector count memaddr - Read disk into memory
141 command_ret_t
do_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
148 BYTE buffer
[_MAX_SS
];
150 static DWORD sec_last
;
151 static uint32_t addr_last
;
156 return CMD_RET_USAGE
;
158 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
163 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
164 /* If another parameter, it is the sector to dump. */
166 sec
= strtoul(argv
[2], 0, 10);
168 count
= strtoul(argv
[3], 0, 10);
170 addr
= strtoul(argv
[4], 0, 16);
173 for (nr
= 0; nr
< count
;) {
175 if ((res
= disk_read(dev
, buffer
, sec
, 1)) == RES_OK
) {
176 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
177 my_puts_P(PSTR("Bus timeout\n"));
178 return CMD_RET_FAILURE
;
180 z80_write_block(buffer
, addr
/*+ base*/, _MAX_SS
);
181 z80_bus_cmd(Release
);
182 sec
++; addr
+= _MAX_SS
;
187 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr
, res
);
189 printf_P(PSTR("Last sector not written!\n"));
194 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
199 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
203 command_ret_t
do_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
210 BYTE buffer
[_MAX_SS
];
212 static DWORD sec_last
;
213 static uint32_t addr_last
;
218 return CMD_RET_USAGE
;
220 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
225 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
226 /* If another parameter, it is the sector to dump. */
228 sec
= strtoul(argv
[2], 0, 10);
230 count
= strtoul(argv
[3], 0, 10);
232 addr
= strtoul(argv
[4], 0, 16);
235 for (nr
= 0; nr
< count
;) {
237 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
238 my_puts_P(PSTR("Bus timeout\n"));
239 return CMD_RET_FAILURE
;
241 z80_read_block(buffer
, addr
/*+ base*/, _MAX_SS
);
242 z80_bus_cmd(Release
);
244 res
= disk_write(dev
, buffer
, sec
, 1);
247 sec
++; addr
+= _MAX_SS
;
250 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr
, res
);
255 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
262 * dcs <pd#> - CTRL_SYNC
266 command_ret_t
do_ioctl_sync(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
270 (void) cmdtp
; (void) flag
;
273 return CMD_RET_USAGE
;
275 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
276 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev
, CTRL_SYNC
, 0));
278 return CMD_RET_SUCCESS
;
283 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
285 cmd_tbl_t cmd_sd_sub
[] = {
292 status
, 2, 1, do_status
,
297 dump
, CONFIG_SYS_MAXARGS
, 1, do_dump
,
303 "read disk sector(s) into meomory",
304 "drive [sector [count [memaddr]]]"
307 write
, 2, 1, do_write
,
308 "write sector(s) from meomory to disk",
309 "drive [sector [count [memaddr]]]"
312 sync
, 2, 1, do_ioctl_sync
,
313 "Device control: SYNC",
318 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
319 "print sub command description/usage",
321 " - print brief description of all sub commands\n"
322 "sd help command ...\n"
323 " - print detailed usage of sub cmd 'command'"
326 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
327 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
328 FSTR("alias for 'help'"),
329 #ifdef CONFIG_SYS_LONGHELP
331 #endif /* CONFIG_SYS_LONGHELP */
332 #ifdef CONFIG_AUTO_COMPLETE
339 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
341 return _do_help(cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
), cmdtp
, flag
, argc
, argv
);
345 command_ret_t
do_sd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
350 return CMD_RET_USAGE
;
352 /* drop initial "sd" arg */
356 cp
= find_cmd_tbl(argv
[0], cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
));
359 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
361 return CMD_RET_USAGE
;