]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
6b75d6ed51afab2e354fd7e1eaa7fb4a4d8b697e
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
14 #include "print-utils.h"
18 * status <pd#> - Show socket status
22 command_ret_t
do_status(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
27 (void) cmdtp
; (void) flag
;
32 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
33 res
= disk_status(dev
);
34 printf_P(PSTR("Socket status: %02x\n"), res
);
36 return CMD_RET_SUCCESS
;
40 * init <pd#> - Initialize disk
44 command_ret_t
do_init(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
49 (void) cmdtp
; (void) flag
;
54 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
56 if (disk_status(dev
) & STA_NODISK
) {
57 printf_P(PSTR("No Disk\n"));
58 return CMD_RET_FAILURE
;
61 res
= disk_initialize(dev
);
62 printf_P(PSTR("rc=%.2x\n"), res
);
64 if (res
& (STA_NODISK
| STA_NOINIT
))
65 return CMD_RET_FAILURE
;
67 return CMD_RET_SUCCESS
;
71 * info <pd#> - Show disk info
75 command_ret_t
do_info(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
81 unsigned char uca
[64];
86 (void) cmdtp
; (void) flag
;
91 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
93 res
= disk_status(dev
);
94 if (res
& (STA_NODISK
| STA_NOINIT
)) {
95 printf_P(res
& STA_NODISK
?
96 PSTR("No disk\n") : PSTR("Not initialized\n"));
97 return CMD_RET_FAILURE
;
100 if (disk_ioctl(dev
, GET_SECTOR_COUNT
, &dat
.ul
) == RES_OK
)
101 printf_P(PSTR("Drive size: %lu sectors\n"), dat
.ul
);
102 if (disk_ioctl(dev
, GET_BLOCK_SIZE
, &dat
.ul
) == RES_OK
)
103 printf_P(PSTR("Erase block: %lu sectors\n"), dat
.ul
);
104 if (disk_ioctl(dev
, MMC_GET_TYPE
, &dat
.uc
) == RES_OK
)
105 printf_P(PSTR("Card type: %u\n"), dat
.uc
);
106 if (disk_ioctl(dev
, MMC_GET_CSD
, dat
.uca
) == RES_OK
)
107 dump_ram(dat
.uca
, 0, 16, "CSD:");
108 if (disk_ioctl(dev
, MMC_GET_CID
, dat
.uca
) == RES_OK
)
109 dump_ram(dat
.uca
, 0, 16, "CID:");
110 if (disk_ioctl(dev
, MMC_GET_OCR
, dat
.uca
) == RES_OK
)
111 dump_ram(dat
.uca
, 0, 4, "OCR:");
112 if (disk_ioctl(dev
, MMC_GET_SDSTAT
, dat
.uca
) == RES_OK
)
113 dump_ram(dat
.uca
, 0, 64, "SD Status:");
115 return CMD_RET_SUCCESS
;
120 * dump <pd#> [<sector> [count]] - Dump sector
124 command_ret_t
do_dump(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
126 static BYTE dev_last
;
127 static DWORD sec_last
;
128 BYTE buffer
[_MAX_SS
];
136 (void) cmdtp
; (void) flag
;
139 return CMD_RET_USAGE
;
141 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
147 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
148 /* If another parameter, it is the sector to dump. */
150 sec
= strtoul(argv
[2], 0, 10);
152 count
= (UINT
) strtoul(argv
[3], 0, 10);
155 for ( ; count
; count
--, sec
++) {
156 res
= disk_read(dev
, buffer
, sec
, 1);
159 printf_P(PSTR("rc=%.2x\n"), res
);
160 return CMD_RET_FAILURE
;
163 sprintf_P(header
, PSTR("Sector: %lu"), sec
);
164 dump_ram(buffer
, 0, _MAX_SS
, header
);
169 return CMD_RET_SUCCESS
;
173 * read drive sector count memaddr - Read disk into memory
177 command_ret_t
do_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
184 BYTE buffer
[_MAX_SS
];
186 static DWORD sec_last
;
187 static uint32_t addr_last
;
192 return CMD_RET_USAGE
;
194 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
199 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
200 /* If another parameter, it is the sector to dump. */
202 sec
= strtoul(argv
[2], 0, 10);
204 count
= strtoul(argv
[3], 0, 10);
206 addr
= strtoul(argv
[4], 0, 16);
209 for (nr
= 0; nr
< count
;) {
211 if ((res
= disk_read(dev
, buffer
, sec
, 1)) == RES_OK
) {
212 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
213 my_puts_P(PSTR("Bus timeout\n"));
214 return CMD_RET_FAILURE
;
216 z80_write_block(buffer
, addr
/*+ base*/, _MAX_SS
);
217 z80_bus_cmd(Release
);
218 sec
++; addr
+= _MAX_SS
;
223 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr
, res
);
225 printf_P(PSTR("Last sector not written!\n"));
230 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
235 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
239 command_ret_t
do_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
246 BYTE buffer
[_MAX_SS
];
248 static DWORD sec_last
;
249 static uint32_t addr_last
;
254 return CMD_RET_USAGE
;
256 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
261 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
262 /* If another parameter, it is the sector to dump. */
264 sec
= strtoul(argv
[2], 0, 10);
266 count
= strtoul(argv
[3], 0, 10);
268 addr
= strtoul(argv
[4], 0, 16);
271 for (nr
= 0; nr
< count
;) {
273 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
274 my_puts_P(PSTR("Bus timeout\n"));
275 return CMD_RET_FAILURE
;
277 z80_read_block(buffer
, addr
/*+ base*/, _MAX_SS
);
278 z80_bus_cmd(Release
);
280 res
= disk_write(dev
, buffer
, sec
, 1);
283 sec
++; addr
+= _MAX_SS
;
286 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr
, res
);
291 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
298 * sync <pd#> - CTRL_SYNC
302 command_ret_t
do_ioctl_sync(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
306 (void) cmdtp
; (void) flag
;
309 return CMD_RET_USAGE
;
311 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
312 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev
, CTRL_SYNC
, 0));
314 return CMD_RET_SUCCESS
;
319 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
321 cmd_tbl_t cmd_sd_sub
[] = {
323 status
, 2, 1, do_status
,
338 dump
, CONFIG_SYS_MAXARGS
, 1, do_dump
,
340 "<drive> [sector [count ]]"
344 "Read disk sector(s) into meomory",
345 "<drive> [sector [count [memaddr]]]"
348 write
, 2, 1, do_write
,
349 "Write sector(s) from meomory to disk",
350 "<drive> [sector [count [memaddr]]]"
353 sync
, 2, 1, do_ioctl_sync
,
354 "Device control: SYNC",
359 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
360 "Print sub command description/usage",
362 " - print brief description of all sub commands\n"
363 "sd help command ...\n"
364 " - print detailed usage of sub cmd 'command'"
367 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
368 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
369 FSTR("Alias for 'help'"),
370 #ifdef CONFIG_SYS_LONGHELP
372 #endif /* CONFIG_SYS_LONGHELP */
373 #ifdef CONFIG_AUTO_COMPLETE
380 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
382 return _do_help(cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
), cmdtp
, flag
, argc
, argv
);
386 command_ret_t
do_sd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
391 return CMD_RET_USAGE
;
393 /* drop initial "sd" arg */
397 cp
= find_cmd_tbl(argv
[0], cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
));
400 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
402 return CMD_RET_USAGE
;