]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
12 #include "print-utils.h"
17 * status <pd#> - Show socket status
21 command_ret_t
do_status(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
26 (void) cmdtp
; (void) flag
;
31 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
32 res
= disk_status(dev
);
33 printf_P(PSTR("Socket status: %02x\n"), res
);
35 return CMD_RET_SUCCESS
;
39 * init <pd#> - Initialize disk
43 command_ret_t
do_init(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
48 (void) cmdtp
; (void) flag
;
53 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
55 if (disk_status(dev
) & STA_NODISK
) {
56 printf_P(PSTR("No Disk\n"));
57 return CMD_RET_FAILURE
;
60 res
= disk_initialize(dev
);
61 printf_P(PSTR("rc=%.2x\n"), res
);
63 if (res
& (STA_NODISK
| STA_NOINIT
))
64 return CMD_RET_FAILURE
;
66 return CMD_RET_SUCCESS
;
70 * info <pd#> - Show disk info
74 command_ret_t
do_info(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
80 unsigned char uca
[64];
85 (void) cmdtp
; (void) flag
;
90 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
92 res
= disk_status(dev
);
93 if (res
& (STA_NODISK
| STA_NOINIT
)) {
94 printf_P(res
& STA_NODISK
?
95 PSTR("No disk\n") : PSTR("Not initialized\n"));
96 return CMD_RET_FAILURE
;
99 if (disk_ioctl(dev
, GET_SECTOR_COUNT
, &dat
.ul
) == RES_OK
)
100 printf_P(PSTR("Drive size: %lu sectors\n"), dat
.ul
);
101 if (disk_ioctl(dev
, GET_BLOCK_SIZE
, &dat
.ul
) == RES_OK
)
102 printf_P(PSTR("Erase block: %lu sectors\n"), dat
.ul
);
103 if (disk_ioctl(dev
, MMC_GET_TYPE
, &dat
.uc
) == RES_OK
)
104 printf_P(PSTR("Card type: %u\n"), dat
.uc
);
105 if (disk_ioctl(dev
, MMC_GET_CSD
, dat
.uca
) == RES_OK
)
106 dump_ram(dat
.uca
, 0, 16, "CSD:");
107 if (disk_ioctl(dev
, MMC_GET_CID
, dat
.uca
) == RES_OK
)
108 dump_ram(dat
.uca
, 0, 16, "CID:");
109 if (disk_ioctl(dev
, MMC_GET_OCR
, dat
.uca
) == RES_OK
)
110 dump_ram(dat
.uca
, 0, 4, "OCR:");
111 if (disk_ioctl(dev
, MMC_GET_SDSTAT
, dat
.uca
) == RES_OK
)
112 dump_ram(dat
.uca
, 0, 64, "SD Status:");
114 return CMD_RET_SUCCESS
;
119 * dump <pd#> [<sector> [count]] - Dump sector
123 command_ret_t
do_dump(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
125 static BYTE dev_last
;
126 static DWORD sec_last
;
127 BYTE buffer
[_MAX_SS
];
135 (void) cmdtp
; (void) flag
;
138 return CMD_RET_USAGE
;
140 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
146 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
147 /* If another parameter, it is the sector to dump. */
149 sec
= eval_arg(argv
[2], NULL
);
151 count
= (UINT
) eval_arg(argv
[3], NULL
);
154 for ( ; count
; count
--, sec
++) {
155 res
= disk_read(dev
, buffer
, sec
, 1);
158 printf_P(PSTR("rc=%.2x\n"), res
);
159 return CMD_RET_FAILURE
;
162 sprintf_P(header
, PSTR("Sector: %lu"), sec
);
163 dump_ram(buffer
, 0, _MAX_SS
, header
);
168 return CMD_RET_SUCCESS
;
172 * read drive sector count memaddr - Read disk into memory
176 command_ret_t
do_read(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
183 BYTE buffer
[_MAX_SS
];
185 static DWORD sec_last
;
186 static uint32_t addr_last
;
191 return CMD_RET_USAGE
;
193 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
198 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
199 /* If another parameter, it is the sector to dump. */
201 sec
= eval_arg(argv
[2], NULL
);
203 count
= eval_arg(argv
[3], NULL
);
205 addr
= eval_arg(argv
[4], NULL
);
208 for (nr
= 0; nr
< count
;) {
210 if ((res
= disk_read(dev
, buffer
, sec
, 1)) == RES_OK
) {
211 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
212 my_puts_P(PSTR("Bus timeout\n"));
213 return CMD_RET_FAILURE
;
215 z80_write_block(buffer
, addr
/*+ base*/, _MAX_SS
);
216 z80_bus_cmd(Release
);
217 sec
++; addr
+= _MAX_SS
;
222 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr
, res
);
224 printf_P(PSTR("Last sector not written!\n"));
229 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
234 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
238 command_ret_t
do_write(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
245 BYTE buffer
[_MAX_SS
];
247 static DWORD sec_last
;
248 static uint32_t addr_last
;
253 return CMD_RET_USAGE
;
255 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
260 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
261 /* If another parameter, it is the sector to dump. */
263 sec
= eval_arg(argv
[2], NULL
);
265 count
= eval_arg(argv
[3], NULL
);
267 addr
= eval_arg(argv
[4], NULL
);
270 for (nr
= 0; nr
< count
;) {
272 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
273 my_puts_P(PSTR("Bus timeout\n"));
274 return CMD_RET_FAILURE
;
276 z80_read_block(buffer
, addr
/*+ base*/, _MAX_SS
);
277 z80_bus_cmd(Release
);
279 res
= disk_write(dev
, buffer
, sec
, 1);
282 sec
++; addr
+= _MAX_SS
;
285 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr
, res
);
290 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
297 * sync <pd#> - CTRL_SYNC
301 command_ret_t
do_ioctl_sync(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
305 (void) cmdtp
; (void) flag
;
308 return CMD_RET_USAGE
;
310 dev
= (BYTE
) eval_arg(argv
[1], NULL
);
311 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev
, CTRL_SYNC
, 0));
313 return CMD_RET_SUCCESS
;
317 cmd_tbl_t cmd_tbl_sd
[] = {
319 status
, 2, CTBL_REPEAT
, do_status
,
324 init
, 2, CTBL_REPEAT
, do_init
,
329 info
, 2, CTBL_REPEAT
, do_info
,
334 dump
, CONFIG_SYS_MAXARGS
, CTBL_REPEAT
, do_dump
,
336 "drive [sector [count ]]"
339 read
, 2, CTBL_REPEAT
, do_read
,
340 "Read disk sector(s) into meomory",
341 "drive [sector [count [memaddr]]]"
344 write
, 2, CTBL_REPEAT
, do_write
,
345 "Write sector(s) from meomory to disk",
346 "drive [sector [count [memaddr]]]"
349 sync
, 2, CTBL_REPEAT
, do_ioctl_sync
,
350 "Device control: SYNC",
355 help
, CONFIG_SYS_MAXARGS
, CTBL_REPEAT
, do_help
,
356 "Print sub command description/usage",
358 " - print brief description of all sub commands\n"
359 "sd help command ...\n"
360 " - print detailed usage of sub cmd 'command'"
363 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
364 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
365 FSTR("Alias for 'help'"),
366 #ifdef CONFIG_SYS_LONGHELP
368 #endif /* CONFIG_SYS_LONGHELP */
370 #ifdef CONFIG_AUTO_COMPLETE
374 /* Mark end of table */
375 CMD_TBL_END(cmd_tbl_sd
)
379 command_ret_t
do_sd(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
382 return CMD_RET_USAGE
;