CON 'Version 7 updated for P2 silicon 'FSRW (SD File System Read Write) P2 port testing 'Serial Output code at top of file 'FSRW Code starts around line#166 'Uses one cog for serial and another one of SD 'Based on FSRW 1.5 (spin only version) and FSRW 2.6 '(fsrw 2.6 Copyright 2009 Tomas Rokicki and Jonathan Dummer ) 'Serial output set for 115200 baud 'Boot filename is located at the very end of this file 'Should work for FAT16 and FAT32 on SD or SDHC 'Note: Many status message outputs are commented out so can fit into one cog CON ''CLOCK SETTINGS _XTALFREQ = 20_000_000 ' crystal frequency _XDIV = 1 '\ '\ crystal divider to give 20.0MHz _XMUL = 20 '| 160MHz '| crystal / div * mul to give 640MHz _XDIVP = 2 '/ '/ crystal / div * mul /divp to give 320MHz _XOSC = %10 ' '15pF ' %00=OFF, %01=OSC, %10=15pF, %11=30pF _XSEL = %11 ' 'XI+PLL ' %00=rcfast(20+MHz), %01=rcslow(~20KHz), %10=XI(5ms), %11=XI+PLL(10ms) _XPPPP = ((_XDIVP>>1) + 15) & $F ' 1->15, 2->0, 4->1, 6->2...30->14 _CLOCKFREQ = _XTALFREQ / _XDIV * _XMUL / _XDIVP ' internal clock frequency _SETFREQ = 1<<24 + (_XDIV-1)<<18 + (_XMUL-1)<<8 + _XPPPP<<4 + _XOSC<<2 ' %0000_000e_dddddd_mmmmmmmmmm_pppp_cc_00 ' setup oscillator _ENAFREQ = _SETFREQ + _XSEL ' %0000_000e_dddddd_mmmmmmmmmm_pppp_cc_ss ' enable oscillator CON ''Links to info 'http://www.pjrc.com/tech/8051/ide/fat32.html 'https://technet.microsoft.com/en-us/library/cc940351.aspx 'https://technet.microsoft.com/en-us/library/cc776720(v=ws.10).aspx 'https://www.infineon.com/dgdl/AP1612510_XE166_SDCard_FAT16.pdf?fileId=db3a30431ce5fb52011d298d7fc91b6d CON 'This is serial output code posted by mindrobots in the forum SYS_CLK = _CLOCKFREQ nco_baud =round(float(SYS_CLK) / 115_200.0 * 65536.0) & $fffffc00 | 7 'adapted from garryj's V19 USB code bits=8 RX_PIN = 63 TX_PIN = 62 Mailbox1=$FF80 '$FFF80 'V27 only has 512kB, so had to change mailbox address Mailbox2=$FF81 '$FFF81 FSRW_COG=2 CON 'Pin settings for USB test board for P123: SdPin_MISO=58'55 'for V27 jumpered to pin 45 because INB doesn't work on pin #55 SdPin_CLK =61'54 SdPin_MOSI=59'53 SdPin_CS =60'52 dat ''Start of serial output code orgh 0 org 0 '+-------[ Set Xtal ]----------------------------------------------------------+ ' RJA: New for real P2 hubset #0 ' set 20MHz+ mode hubset ##_SETFREQ ' setup oscillator waitx ##20_000_000/100 ' ~10ms hubset ##_ENAFREQ ' enable oscillator '+-----------------------------------------------------------------------------+ cog_entry coginit #FSRW_COG,#@FsrwStart 'Start FSRW cog SerialMonitorOrg wrpin pm_tx, #TX_PIN 'set asynchronous tx mode in smart pin wxpin ##nco_baud, #TX_PIN 'set tx bit period wrpin pm_rx, #RX_PIN 'set asynchronous rx mode in smart pin wxpin ## nco_baud, #RX_PIN 'set rx bit period dirh #TX_PIN 'enable smartpin tx dirh #RX_PIN 'enable smartpin rx jmp #loopback 'for V27 had to make these two vars into registers pm_tx long %0000_0000_00_0_0000000000000_01_11110_0 'async tx byte mode, dir high pm_rx long %0111_0000_00_0_0000000000000_01_11111_0 'async rx byte mode, dir low, inputs pin 0 ' 'Main loop start loopback 'Look for command rdbyte tx_cmd, rx_target cmp tx_cmd,#0 wcz if_e jmp #loopback 'check for write byte command cmp tx_cmd,#1 wcz if_e rdbyte tx_char,hex_target if_e call #send_char 'check for write hex long cmp tx_cmd,#2 wcz if_e rdlong tx_hex, hex_target if_e call #send_LongHexSub 'check for write hex byte cmp tx_cmd,#3 wcz if_e rdbyte tx_hex, hex_target if_e call #send_ByteHexSub 'check for write string byte cmp tx_cmd,#4 wcz if_e rdlong tx_hex, hex_target if_e call #send_StringSub wrbyte #0,rx_target 'message received and processed jmp #loopback Send_StringSub 'send a string rdbyte tx_char,tx_hex cmp tx_char,#0 wcz if_z ret call #send_char add tx_hex,#1 jmp #Send_StringSub Send_LongHexSub 'Send 8 hex chars getnib tx_temp,tx_hex,#7 call #SendHexSub getnib tx_temp,tx_hex,#6 call #SendHexSub getnib tx_temp,tx_hex,#5 call #SendHexSub getnib tx_temp,tx_hex,#4 call #SendHexSub getnib tx_temp,tx_hex,#3 call #SendHexSub getnib tx_temp,tx_hex,#2 call #SendHexSub getnib tx_temp,tx_hex,#1 call #SendHexSub getnib tx_temp,tx_hex,#0 call #SendHexSub ret Send_ByteHexSub push tx_hex mov tx_temp,tx_hex shr tx_temp,#4 and tx_temp,#$0F call #SendHexSub pop tx_hex mov tx_temp,tx_hex and tx_temp,#$0F call #SendHexSub ret SendHexSub 'Send hex mov tx_char,tx_temp cmp tx_char,#9 wcz if_a add tx_char,#"A"-10 if_be add tx_char,#"0" call #Send_Char ret '******************************************************************************* ' Get one character from the input port. '******************************************************************************* rcv_char testb inb,#RX_PIN wc 'sample rx if_nc jmp #rcv_char akpin #RX_PIN rdpin rx_char,#RX_PIN ret '******************************************************************************* ' Output a single character to the tx_pin. '******************************************************************************* send_char rdpin temp,#tx_pin wc 'wait if busy if_c jmp #send_char wypin tx_char,#tx_pin ret wcz rx_target long Mailbox1 hex_target long Mailbox2 tx_cmd res 1 tx_char res 1 tx_hex res 1 timer res 1 rx_char res 1 tx_temp res 1 temp res 1 fit $1F0 CON 'FSRW Settings SECTORSHIFT = 9 ' DIRSIZE = 32 DIRSHIFT = 5 Messages=$1000'+$1000 'storage for string messages pTargetFile=$1500'+$1000 'Putting target filename here BlockBuffer=$2000'+$1000 'storage for 512 byte block dat 'FSRW Vars FsrwStart org jmp #@Fsrw_init 'uncomment here and start of next section for hubexec jmp #Fsrw_init 'putting variables here in case need hubexec later 'NOTE: Can't use RES here tx_target long Mailbox1 'Mailbox for serial output tx_hexTarget long Mailbox2 'Mailbox for Hex output m_miso long 1<> Sector Address Shift=9 (not a high capacity card)",13,0 C58HCMessage byte ">> Sector Address Shift=0 (high capacity card)",13,0 Cmd59Message byte "Sending Command 59: Turning off CRC",13,0 Cmd2Message byte "Sending Command 2: Getting Card ID >>",0 Cmd37Message byte 13,"Sending Commands 3&7: Get then select RCA (relative card address) >>",0 Block0Message byte 13,13,"Going high-speed SPI and reading sector #0 (Master Boot Record)",13,0 BCheckMessage byte "Passed FAT block check (Last 2 bytes are $55AA)",13,0 FailMessage byte 13,13,"Something wrong, restarting....",0 MBR0Message byte "Checking to see if MBR is boot sector (true if no partitions)",13,0 MBR1Message byte "Checking first partition for boot sector",13,0 Fat16ID byte "FAT16" Fat32ID byte "FAT32" FS0Message byte ">> No file system ID in this block",13,0 FS1Message byte "This block identifies as FAT16 Boot Sector",13,0 FS2Message byte "This block identifies as FAT32 Boot Sector",13,0 SectorMessage byte "Sector size confirmed to be 512 bytes",13,0 ClusterMessage byte "#sectors in each cluster is $",0 FatsMessage byte 13,"Two copies of FAT confirmed",13,0 SectorsMessage byte "#of 512 byte sectors in this partition= $",0 Fat1Message byte 13,"#Start of FAT1= $",0 REntriesMessage byte 13,"#of root entries= $",0 SPerFatMessage byte 13,"#of sectors per fat= $",0 DRegionMessage byte 13,"Start of data region= $",0 RootDirMessage byte 13,"Root directory starts at byte= $",0 RootEndMessage byte 13,"Root directory ends at byte= $",0 FileFoundMessage byte 13,"Target file found !",0 FileFailMessage byte 13,"Target file not found !",0 FilesizeMessage byte 13,"Filesize= $",0 FileLookMessage byte 13,"Looking for file...",0 DAT 'TargetFile orgh pTargetFile TargetFile byte "P2BOOT BIN" DAT 'Block Buffer orgh BlockBuffer