Timing related SD card initialisation bug, new board release can fail to initialise and loop.

This commit is contained in:
Philip Smart
2021-05-04 09:07:47 +01:00
parent ea18f2fee6
commit a596ce312f
11 changed files with 67 additions and 35 deletions

View File

@@ -47,12 +47,12 @@ HEADER1: IF BUILD_MZ80A = 1
HEADER2: IF BUILD_RFS = 1 HEADER2: IF BUILD_RFS = 1
IF BUILD_80C = 0 IF BUILD_80C = 0
DB "MS-BASIC(RFS40))", 0DH, 0DH ; Title/Name (17 bytes). DB "MS-BASIC(RFS40)", 0DH, 0DH ; Title/Name (17 bytes).
DW CODEEND - CODESTART ; Size of program. DW CODEEND - CODESTART ; Size of program.
DW CODESTART ; Load address of program. DW CODESTART ; Load address of program.
DW CODESTART ; Exec address of program. DW CODESTART ; Exec address of program.
ELSE ELSE
DB "MS-BASIC(RFS80))", 0DH, 0DH ; Title/Name (17 bytes). DB "MS-BASIC(RFS80)", 0DH, 0DH ; Title/Name (17 bytes).
DW CODEEND - CODESTART ; Size of program. DW CODEEND - CODESTART ; Size of program.
DW CODESTART ; Load address of program. DW CODESTART ; Load address of program.
DW CODESTART ; Exec address of program. DW CODESTART ; Exec address of program.

View File

@@ -108,25 +108,27 @@ CBIOS3_0: LD A,(BNKCTRLRST)
LD A,000H ; CS to low (active) LD A,000H ; CS to low (active)
CALL SPICS CALL SPICS
LD BC,01FFFH ; Number of retries before deciding card is not present. LD BC,SD_RETRIES ; Number of retries before deciding card is not present.
SD_INIT1: LD A,CMD0 ; Command 0 SD_INIT1: LD A,CMD0 ; Command 0
LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD. LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD.
LD DE,00000H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD. LD DE,00000H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD.
CALL SDCMD
PUSH BC PUSH BC
;
CALL SDCMD
;
LD A,(SDBUF+6) ; Get response code. LD A,(SDBUF+6) ; Get response code.
DEC A ; Set Z flag to test if response is 0x01 DEC A ; Set Z flag to test if response is 0x01
JP Z,SD_INIT2 ; Command response 0x01? Exit if match.
POP BC POP BC
JP Z,SD_INIT2 ; Command response 0x01? Exit if match.
DEC BC DEC BC
LD A,B LD A,B
OR C OR C
JR NZ,SD_INIT1 ; Retry for BC times. JR NZ,SD_INIT1 ; Retry for BC times.
LD A,1 LD A,1
JP SD_EXIT ; Error, card is not responding to CMD0 JP SD_EXIT ; Error, card is not responding to CMD0
SD_INIT2: POP BC
; Now send CMD8 to get card details. This command can only be sent SD_INIT2: ; Now send CMD8 to get card details. This command can only be sent
; when the card is idle. ; when the card is idle.
LD A,CMD8 ; CMD8 has 0x00001AA as parameter, load up registers and call command routine. LD A,CMD8 ; CMD8 has 0x00001AA as parameter, load up registers and call command routine.
LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD. LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD.
@@ -254,10 +256,25 @@ SDCMD1: PUSH BC
POP BC POP BC
DJNZ SDCMD1 DJNZ SDCMD1
PUSH HL PUSH HL
SDCMD2: CALL SPIIN LD HL,SD_RETRIES
SDCMD2: PUSH HL
CALL SPIIN
POP HL
CP 0FFH CP 0FFH
JR Z,SDCMD2 JR NZ,SDCMD4
JR SDCMD4 DEC HL
LD A,H
OR L
JR NZ,SDCMD2
;
; Error as we are not receiving data.
;
POP HL
POP BC
LD A,1 ; Force return code to be error.
LD (SDBUF+6),A
RET
SDCMD3: PUSH BC SDCMD3: PUSH BC
PUSH HL PUSH HL
CALL SPIIN ; CALL SPIIN ;
@@ -286,15 +303,14 @@ SDACMD: PUSH AF
POP DE POP DE
LD A,(SDBUF+6) ; Should be a response of 0 or 1. LD A,(SDBUF+6) ; Should be a response of 0 or 1.
CP 2 CP 2
JR NC,SDACMD0 JR NC,SDACMDE
POP AF POP AF
CALL SDCMD CALL SDCMD
LD A,(SDBUF+6) ; Should be a response of 0 whereby the card has left idle. SDACMD0: LD A,(SDBUF+6) ; Should be a response of 0 whereby the card has left idle.
OR A OR A
RET RET
SDACMD0: POP AF SDACMDE: POP AF
CP 1 JR SDACMD0
RET
; Method to send Application Command 41 to the SD card. This command involves retries and delays ; Method to send Application Command 41 to the SD card. This command involves retries and delays
; hence coded seperately. ; hence coded seperately.

View File

@@ -437,6 +437,7 @@ CPM_SD_IMGSZ EQU CPM_SD_TRK * CPM_SD_SEC * SD_SECSIZE
SD_SECSIZE EQU 512 ; Default size of an SD Sector SD_SECSIZE EQU 512 ; Default size of an SD Sector
SD_SECPTRK EQU CPM_SD_SEC ; Sectors of SD_SECSIZE per virtual track. SD_SECPTRK EQU CPM_SD_SEC ; Sectors of SD_SECSIZE per virtual track.
SD_TRACKS EQU CPM_SD_TRK ; Number of virtual tracks per disk image. SD_TRACKS EQU CPM_SD_TRK ; Number of virtual tracks per disk image.
SD_RETRIES EQU 00100H ; Number of retries before giving up.
;----------------------------------------------- ;-----------------------------------------------

View File

@@ -411,11 +411,11 @@ SDDIR_ENTSZ EQU 32
; ;
; Rom Filing System constants for the SD Card. ; Rom Filing System constants for the SD Card.
; ;
SDDIR_DIRENT EQU 256 ; Directory entries in the RFS directory. SDDIR_DIRENT EQU 256 ; Directory entries in the RFS directory.
SDDIR_DIRENTSZ EQU 32 ; Size of a directory entry. SDDIR_DIRENTSZ EQU 32 ; Size of a directory entry.
SDDIR_DIRSIZE EQU SDDIR_DIRENT * SDDIR_DIRENTSZ ; Total size of the directory. SDDIR_DIRSIZE EQU SDDIR_DIRENT * SDDIR_DIRENTSZ ; Total size of the directory.
SDDIR_BLOCKSZ EQU 65536 ; Size of a file block per directory entry. SDDIR_BLOCKSZ EQU 65536 ; Size of a file block per directory entry.
SDDIR_IMGSZ EQU SDDIR_DIRSIZE + (SDDIR_DIRENT * SDDIR_BLOCKSZ) ; Total size of the RFS image. SDDIR_IMGSZ EQU SDDIR_DIRSIZE + (SDDIR_DIRENT * SDDIR_BLOCKSZ) ; Total size of the RFS image.
OBJCD EQU 001H ; MZF contains a binary object. OBJCD EQU 001H ; MZF contains a binary object.
BTX1CD EQU 002H ; MZF contains an MZ-80K/80A BASIC program. BTX1CD EQU 002H ; MZF contains an MZ-80K/80A BASIC program.

View File

@@ -227,8 +227,19 @@ SIGNON2: LD DE,MSGSON ; Sign
SIGNON3: LD HL,PRINTMSG SIGNON3: LD HL,PRINTMSG
CALL BKSW0to6 CALL BKSW0to6
; Initialise SD card, report any errors.
LD HL, SDINIT ; SD Card Initialisation LD HL, SDINIT ; SD Card Initialisation
CALL BKSW0to2 ; Call the initialisation routine. CALL BKSW0to2 ; Call the initialisation routine.
LD A,L
OR A ; 0 = No error.
JR Z,ST1X
; Place error code in C to print as a number and report with the error message.
ADD A,'0'
LD C,A
LD DE,MSGSDINITER
LD HL,PRINTMSG
CALL BKSW0to6
; Command processor, table based. ; Command processor, table based.
; A line is inpt then a comparison made with entries in the table. If a match is found then the bank and function ; A line is inpt then a comparison made with entries in the table. If a match is found then the bank and function

View File

@@ -228,10 +228,10 @@ SDINIT: LD A,0FFH ; CS to
; ;
CALL SPIINIT ; Train SD with our clock. CALL SPIINIT ; Train SD with our clock.
; ;
XOR A ; CS to active (low) LD A,0 ; CS to active (low)
CALL SPICS CALL SPICS
LD BC,SD_RETRIES ; Number of retries before giving up, card not responding.
; ;
LD BC,SD_RETRIES ; Number of retries before giving up, card not responding.
SDINIT1: LD A,CMD0 ; Command 0 SDINIT1: LD A,CMD0 ; Command 0
LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD. LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD.
LD DE,00000H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD. LD DE,00000H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD.
@@ -241,8 +241,9 @@ SDINIT1: LD A,CMD0 ; Comma
; ;
LD A,(SDBUF+6) ; Get response code. LD A,(SDBUF+6) ; Get response code.
DEC A ; Set Z flag to test if response is 0x01 DEC A ; Set Z flag to test if response is 0x01
JP Z,SDINIT2 ; Command response 0x01? Exit if match.
POP BC POP BC
JP Z,SDINIT2 ; Command response 0x01? Exit if match.
DEC BC DEC BC
LD A,B LD A,B
OR C OR C
@@ -250,8 +251,7 @@ SDINIT1: LD A,CMD0 ; Comma
LD A,1 LD A,1
JP SD_EXIT ; Error, card is not responding to CMD0 JP SD_EXIT ; Error, card is not responding to CMD0
SDINIT2: POP BC SDINIT2: ; Now send CMD8 to get card details. This command can only be sent
; Now send CMD8 to get card details. This command can only be sent
; when the card is idle. ; when the card is idle.
LD A,CMD8 ; CMD8 has 0x00001AA as parameter, load up registers and call command routine. LD A,CMD8 ; CMD8 has 0x00001AA as parameter, load up registers and call command routine.
LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD. LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD.
@@ -328,14 +328,15 @@ SDINIT12: DEC BC ; 6T
LD A,4 ; Exit code, failed to initialise v1 MMC card. LD A,4 ; Exit code, failed to initialise v1 MMC card.
JP SD_EXIT JP SD_EXIT
SDINIT13: LD A,CMD16 ; No response from the card for an ACMD41/CMD1 so try CMD16 with parameter 0x00000200 SDINIT13: POP BC
LD A,CMD16 ; No response from the card for an ACMD41/CMD1 so try CMD16 with parameter 0x00000200
LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD. LD HL,00000H ; NB. Important, HL should be coded as LH due to little endian and the way it is used in SDCMD.
LD DE,00002H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD. LD DE,00002H ; NB. Important, DE should be coded as ED due to little endian and the way it is used in SDCMD.
CALL SDCMD CALL SDCMD
LD A,(SDBUF+6) LD A,(SDBUF+6)
OR A OR A
JR Z,SDINIT14 JR Z,SDINIT14
XOR A LD A,0
LD (SDCAP),A ; No capabilities on this unknown card. LD (SDCAP),A ; No capabilities on this unknown card.
SDINIT14: XOR A SDINIT14: XOR A
JR SD_EXIT JR SD_EXIT
@@ -396,6 +397,7 @@ SDCMD2: PUSH HL
; Error as we are not receiving data. ; Error as we are not receiving data.
; ;
POP HL POP HL
POP BC
LD A,1 ; Force return code to be error. LD A,1 ; Force return code to be error.
LD (SDBUF+6),A LD (SDBUF+6),A
RET RET
@@ -408,6 +410,8 @@ SDCMD4: POP HL
INC HL INC HL
POP BC ; Get back number of expected bytes. HL = place in buffer to store response. POP BC ; Get back number of expected bytes. HL = place in buffer to store response.
DJNZ SDCMD3 DJNZ SDCMD3
LD A,DOUT_HIGH | CLOCK_LOW | CS_HIGH
OUT (SPI_OUT),A
RET RET
; Method to send an Application Command to the SD Card. This involves sending CMD55 followed by the required command. ; Method to send an Application Command to the SD Card. This involves sending CMD55 followed by the required command.
@@ -426,15 +430,14 @@ SDACMD: PUSH AF
POP DE POP DE
LD A,(SDBUF+6) ; Should be a response of 0 or 1. LD A,(SDBUF+6) ; Should be a response of 0 or 1.
CP 2 CP 2
JR NC,SDACMD0 JR NC,SDACMDE
POP AF POP AF
CALL SDCMD CALL SDCMD
LD A,(SDBUF+6) ; Should be a response of 0 whereby the card has left idle. SDACMD0: LD A,(SDBUF+6) ; Should be a response of 0 whereby the card has left idle.
OR A OR A
RET RET
SDACMD0: POP AF SDACMDE: POP AF
CP 1 JR SDACMD0
RET
; Method to send Application Command 41 to the SD card. This command involves retries and delays ; Method to send Application Command 41 to the SD card. This command involves retries and delays
; hence coded seperately. ; hence coded seperately.

View File

@@ -451,12 +451,13 @@ ATBL: DB 0CCH ; NUL '\0' (null character)
; Message table ; Message table
; ;
;-------------------------------------- ;--------------------------------------
MSGSON: DB "+ RFS ", 0ABh, "2.1 **", 00DH, 000H ; Version 2.x-> as we are now using the v2.x PCB with 4 devices on-board MSGSONTZ: DB "+ TZ" ; Version 2.x with version 2.1+ of tranZPUter board installed.
MSGSONTZ: DB "+ TZ+RFS ", 0ABh, "2.1 **", 00DH, 000H ; Version 2.x with version 2.1+ of tranZPUter board installed. MSGSON: DB "+ RFS ", 0ABh, "2.1a **", 00DH, 000H ; Version 2.x-> as we are now using the v2.x PCB with 4 devices on-board
MSGNOTFND: DB "Not Found", 00DH, 000H MSGNOTFND: DB "Not Found", 00DH, 000H
MSGRDIRLST: DB "ROM Directory:", 00DH, 000H MSGRDIRLST: DB "ROM Directory:", 00DH, 000H
MSGTRM: DB 00DH, 000H MSGTRM: DB 00DH, 000H
MSGBADCMD: DB "???", 00DH, 000H MSGBADCMD: DB "???", 00DH, 000H
MSGSDINITER:DB "SD Card Error ", 0F9H, 00DH, 000H
MSGCDIRLST: DB "SD Card Directory ",0F9H,":", 00DH, 000H MSGCDIRLST: DB "SD Card Directory ",0F9H,":", 00DH, 000H
MSGSDRERR: DB "SD Read error, Sec:",0FBH, 000H MSGSDRERR: DB "SD Read error, Sec:",0FBH, 000H
MSGSDWERR: DB "SD Write error, Sec:",0FBH, 000H MSGSDWERR: DB "SD Write error, Sec:",0FBH, 000H

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.