]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
46dbcfdc3ff30619f80f1593338e9a2a5a59a839
8 #include "print-utils.h"
12 * status <pd#> - Show socket status
16 command_ret_t
do_status(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("Socket status: %02x\n"), res
);
30 return CMD_RET_SUCCESS
;
34 * init <pd#> - Initialize disk
38 command_ret_t
do_init(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
43 (void) cmdtp
; (void) flag
;
48 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
50 if (disk_status(dev
) & STA_NODISK
) {
51 printf_P(PSTR("No Disk\n"));
52 return CMD_RET_FAILURE
;
55 res
= disk_initialize(dev
);
56 printf_P(PSTR("rc=%.2x\n"), res
);
58 if (res
& (STA_NODISK
| STA_NOINIT
))
59 return CMD_RET_FAILURE
;
61 return CMD_RET_SUCCESS
;
65 * info <pd#> - Show disk info
69 command_ret_t
do_info(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
75 unsigned char uca
[64];
80 (void) cmdtp
; (void) flag
;
85 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
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
;
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:");
109 return CMD_RET_SUCCESS
;
114 * dump <pd#> [<sector> [count]] - Dump sector
118 command_ret_t
do_dump(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
120 static BYTE dev_last
;
121 static DWORD sec_last
;
122 BYTE buffer
[_MAX_SS
];
130 (void) cmdtp
; (void) flag
;
133 return CMD_RET_USAGE
;
135 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
141 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
142 /* If another parameter, it is the sector to dump. */
144 sec
= strtoul(argv
[2], 0, 10);
146 count
= (UINT
) strtoul(argv
[3], 0, 10);
149 for ( ; count
; count
--, sec
++) {
150 res
= disk_read(dev
, buffer
, sec
, 1);
153 printf_P(PSTR("rc=%.2x\n"), res
);
154 return CMD_RET_FAILURE
;
157 sprintf_P(header
, PSTR("Sector: %lu"), sec
);
158 dump_ram(buffer
, 0, _MAX_SS
, header
);
163 return CMD_RET_SUCCESS
;
167 * read drive sector count memaddr - Read disk into memory
171 command_ret_t
do_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
178 BYTE buffer
[_MAX_SS
];
180 static DWORD sec_last
;
181 static uint32_t addr_last
;
186 return CMD_RET_USAGE
;
188 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
193 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
194 /* If another parameter, it is the sector to dump. */
196 sec
= strtoul(argv
[2], 0, 10);
198 count
= strtoul(argv
[3], 0, 10);
200 addr
= strtoul(argv
[4], 0, 16);
203 for (nr
= 0; nr
< count
;) {
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
;
210 z80_write_block(buffer
, addr
/*+ base*/, _MAX_SS
);
211 z80_bus_cmd(Release
);
212 sec
++; addr
+= _MAX_SS
;
217 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr
, res
);
219 printf_P(PSTR("Last sector not written!\n"));
224 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
229 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
233 command_ret_t
do_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
240 BYTE buffer
[_MAX_SS
];
242 static DWORD sec_last
;
243 static uint32_t addr_last
;
248 return CMD_RET_USAGE
;
250 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
255 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
256 /* If another parameter, it is the sector to dump. */
258 sec
= strtoul(argv
[2], 0, 10);
260 count
= strtoul(argv
[3], 0, 10);
262 addr
= strtoul(argv
[4], 0, 16);
265 for (nr
= 0; nr
< count
;) {
267 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
268 my_puts_P(PSTR("Bus timeout\n"));
269 return CMD_RET_FAILURE
;
271 z80_read_block(buffer
, addr
/*+ base*/, _MAX_SS
);
272 z80_bus_cmd(Release
);
274 res
= disk_write(dev
, buffer
, sec
, 1);
277 sec
++; addr
+= _MAX_SS
;
280 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr
, res
);
285 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
292 * sync <pd#> - CTRL_SYNC
296 command_ret_t
do_ioctl_sync(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
300 (void) cmdtp
; (void) flag
;
303 return CMD_RET_USAGE
;
305 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
306 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev
, CTRL_SYNC
, 0));
308 return CMD_RET_SUCCESS
;
313 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
315 cmd_tbl_t cmd_sd_sub
[] = {
317 status
, 2, 1, do_status
,
332 dump
, CONFIG_SYS_MAXARGS
, 1, do_dump
,
334 "<drive> [sector [count ]]"
338 "Read disk sector(s) into meomory",
339 "<drive> [sector [count [memaddr]]]"
342 write
, 2, 1, do_write
,
343 "Write sector(s) from meomory to disk",
344 "<drive> [sector [count [memaddr]]]"
347 sync
, 2, 1, do_ioctl_sync
,
348 "Device control: SYNC",
353 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
354 "Print sub command description/usage",
356 " - print brief description of all sub commands\n"
357 "sd help command ...\n"
358 " - print detailed usage of sub cmd 'command'"
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
366 #endif /* CONFIG_SYS_LONGHELP */
367 #ifdef CONFIG_AUTO_COMPLETE
374 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
376 return _do_help(cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
), cmdtp
, flag
, argc
, argv
);
380 command_ret_t
do_sd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
385 return CMD_RET_USAGE
;
387 /* drop initial "sd" arg */
391 cp
= find_cmd_tbl(argv
[0], cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
));
394 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
396 return CMD_RET_USAGE
;