SD Card interfacing

Programming the 6502 microprocessor and its relatives in assembly and other languages.
User avatar
Arlet
Posts: 2353
Joined: 16 Nov 2010
Location: Gouda, The Netherlands
Contact:

Re: SD Card interfacing

Post by Arlet »

enso wrote:
Arlet, your 6502 core is perhaps the smallest usable general purpose core for FPGAs (I like Picoblaze, but it is only good for very small applications), and at 45MHz it can go head to head with many modern microcontrollers. There are a lot of 6502 tools out there as well. I know I am preaching to the choir, but it is hardly that odd to chose a 6502 core for a small FPGA.
If you already need an FPGA for something else, and also need a simple control processor, the 6502 is a decent choice. I've actually used it for a commercial project for the reasons you've stated. But if your project doesn't otherwise need an FPGA, I can't think of a good reason to use a 6502, except for fun.
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: SD Card interfacing

Post by ElEctric_EyE »

Arlet wrote:
I've actually used it for a commercial project for the reasons you've stated. But if your project doesn't otherwise need an FPGA, I can't think of a good reason to use a 6502, except for fun.
I'm curious, what commercial project?
If you're a novice to FPGA design and you have programmed the 6502 before, this is a perfect reason to use a softcore 6502 IMO. The bridge is already there for programming.
User avatar
Arlet
Posts: 2353
Joined: 16 Nov 2010
Location: Gouda, The Netherlands
Contact:

Re: SD Card interfacing

Post by Arlet »

ElEctric_EyE wrote:
I'm curious, what commercial project?
Custom camera interface over serial port. Board had a VGA camera module, some SDRAM and an FPGA for grabbing video. Over the serial link you could talk to the 6502, and request (part of) the image. I needed a simple CPU, and I had recently finished my 6502 core, so I decided to use that. The core was capable enough, and I was already familiar with it.
Justin
Posts: 32
Joined: 20 Sep 2013

Re: SD Card interfacing

Post by Justin »

Hey guys,

Does anyone have any empirical data on SD card performance with 6502s? I have a 1Mhz system and am bit banging SPI using a 6522. I am getting about 170ms read time per 512-byte sector. I'm curious what timings others are getting who are bit-banging or using Daryl's 65SPI chip. I'm also curious how long a sector read takes on an 16mhz (or so) AVR.

The perf isn't terrible as compared to 6502 systems from decades ago, but it is definitely noticeable. My brain keeps filling in the head seeking noise as it follows chains in the FAT :-)
lak
Posts: 14
Joined: 11 Sep 2011
Location: Singapore

Re: SD Card interfacing

Post by lak »

Hi,
I was using a SD card with FatFs on a Apple 2 with a VIA 6522 card.
The VIA shift register is used for sending a byte to the SD card, whereas a bit-bang routine (from some C64 group on the Net) was used for receive.
A low ohm resistor is placed between CB1 (SR clock for Send) and Port B0 (Bit-Bang Clk for Receive) just to isolate the outputs. (I've no better way to connect the pins (B0 and CB1) to the SCK the SD card).
Diodes are used to isolate the 5V outputs from the 3.3V inputs of the SD card (with pullups to 3.3V)
A drawing is desperately needed here! Anyway it works with NMOS and CMOS 6522.

Below is a snippet of the bit-banged code for receiving a byte from the SD card (rcvr_mmc).
Just follow the pin connections on Port B. ~12 clocks per bit when unrolled.

Code: Select all

=======================================================
; Faster loading when used SR as output compared to when SR used as input
;  SCK Bit 0  // Serial Clock (output)                SD Pin 5 
; MOSI CB2 // Master Out / Slave In (output)       SD Pin 2
; MISO Bit 7 // Master In / Slave Out (input)        SD Pin 7
;   CS Bit 2  // Slave Select                         SD Pin 1
; CB1 - clock jumpered to SCK (through 40 ohm resistor )
; CB2 - MOSI 

via_ora equ $c0f1 
via_orb equ $c0f0
via_dra equ $c0f3
via_drb equ $c0f2
via_sr  equ $c0fa
via_acr equ $c0fb
       
; Preserve X-Reg if used ..phx ...plx 
; ACC and Y-Reg always destroyed 

via_init:
        lda     #$7f
        sta     via_drb
        lda     #$18    ; shift reg clocked by Phase 2
        sta     via_acr
        rts
 
 
xmit_mmc:
        sta via_sr
        rts
 
  
;xmit_mmc: 
;        tay
;        and #$80
;        sta via_orb 
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb 
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya        ;(2)
;        asl a      ;(2)
;        and #$80   ;(2)
;        sta via_orb ;(4)
;        inc via_orb ;(6)   
        
;        rts

;rcvr_mmc:  ; shift register in using phase 2 - ACR=0x08
;        lda     via_sr
;        nop             ; min 14 cycles between for 1 byte
;        nop
;        nop
;        nop
;        nop 
;        nop 
;        nop
;        lda     via_sr       
;        rts 


rcvr_mmc:  
 
        ldy        #$01     ; set clk hi
        lda        #$00                         
        sta        via_orb  ; clk lo, cs lo

        sty        via_orb     ; clk hi, data will be in bit 7 shortly   (4)
        asl        via_orb     ; data in bit 7, clk lo after shift left  (6)
        rol        a           ; carry into A                            (2)

        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        
                
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, last bit comes in
        cpy        via_orb     ; (4) no need to clear clk 
        rol        a
        eor        #$01        ; fix first last bit0
        
        rts

       end
Last edited by lak on Thu Nov 28, 2013 4:15 pm, edited 3 times in total.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: SD Card interfacing

Post by GARTHWILSON »

lak, put

Code: Select all

 and 
around your code to keep the spaces and make it monospaced so things line up the way you intended. Make sure the "Disable BBCode" box is not checked. If you're signed in, you can go back and edit your post above.

Thanks for the code.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
BigDumbDinosaur
Posts: 9426
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: SD Card interfacing

Post by BigDumbDinosaur »

I reformatted his source code.

Code: Select all

=======================================================
; Faster loading when used SR as output compared to when SR used as input
;  SCK Bit 0  // Serial Clock (output)                SD Pin 5 
; MOSI CB2 // Master Out / Slave In (output)       SD Pin 2
; MISO Bit 7 // Master In / Slave Out (input)        SD Pin 7
;   CS Bit 2  // Slave Select                         SD Pin 1
; CB1 - clock jumpered to SCK (through 40 ohm resistor )
; CB2 - MOSI 

via_ora equ $c0f1 
via_orb equ $c0f0
via_dra equ $c0f3
via_drb equ $c0f2
via_sr  equ $c0fa
via_acr equ $c0fb
       
; Preserve X-Reg if used ..phx ...plx 
; ACC and Y-Reg always destroyed 

via_init:
        lda     #$7f
        sta     via_drb
        lda     #$18    ; shift reg clocked by Phase 2
        sta     via_acr
        rts
 
 
xmit_mmc:
        sta via_sr
        rts
 
  
;xmit_mmc: 
;        tay
;        and #$80
;        sta via_orb 
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb 
        
;        tya
;        asl a
;        tay
;        and #$80
;        sta via_orb
;        inc via_orb
        
;        tya        ;(2)
;        asl a      ;(2)
;        and #$80   ;(2)
;        sta via_orb ;(4)
;        inc via_orb ;(6)   
        
;        rts

;rcvr_mmc:  ; shift register in using phase 2 - ACR=0x08
;        lda     via_sr
;        nop             ; min 14 cycles between for 1 byte
;        nop
;        nop
;        nop
;        nop 
;        nop 
;        nop
;        lda     via_sr       
;        rts 


rcvr_mmc:  
 
        ldy        #$01     ; set clk hi
        lda        #$00                         
        sta        via_orb  ; clk lo, cs lo

        sty        via_orb     ; clk hi, data will be in bit 7 shortly   (4)
        asl        via_orb     ; data in bit 7, clk lo after shift left  (6)
        rol        a           ; carry into A                            (2)

        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        
                
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, data will be in bit 7 shortly
        asl        via_orb     ; data in bit 7, clk lo after shift left
        rol        a           ; carry into A
        
        sty        via_orb     ; clk hi, last bit comes in
        cpy        via_orb     ; (4) no need to clear clk 
        rol        a
        eor        #$01        ; fix first last bit0
        
        rts

       end
x86?  We ain't got no x86.  We don't NEED no stinking x86!
lak
Posts: 14
Joined: 11 Sep 2011
Location: Singapore

Re: SD Card interfacing

Post by lak »

BDD, thanks for formatting the code.
I was in a hurry. Hope it's useful.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: SD Card interfacing

Post by GARTHWILSON »

lak, you put

Code: Select all

 and 
around your code, but you did not uncheck the "Disable BBCode" box. Do that and you'll get the appearance you wanted.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
HansO
Posts: 206
Joined: 31 Oct 2003

Re: SD Card interfacing

Post by HansO »

Lak,

Thanks for sharing the code! Good read!
scotws
Posts: 576
Joined: 07 Jan 2013
Location: Just outside Berlin, Germany
Contact:

Re: SD Card interfacing

Post by scotws »

As a FYI addition to this thread, I have found that Adafruit has a breakout board for MicroSD cards that can handle 3.3V and 5V (which means it should work with the 5V-only 65SPI, if I understand things correctly): https://www.adafruit.com/product/254 There is also a tutorial, though obviously for the Arduino: https://learn.adafruit.com/adafruit-mic ... l?view=all
dolomiah
Posts: 102
Joined: 18 Nov 2015
Location: UK
Contact:

Re: SD Card interfacing

Post by dolomiah »

Hi, interesting thread. I have a bit-banged SPIO interface wired to a similar board from hobbytronics. My 6502 is running @ 2.7Mhz and the raw transfer speed is about 8.5KB/s. But as mentioned by others, that raw speed is not achievable in real world situations - the FAT16 driver software requires multiple reads and updates to FAT and Directory tables to do practical things like creating files. I hope the log below helps:

https://hackaday.io/project/5789-6502-h ... -interface
tink
Posts: 3
Joined: 06 Oct 2016

Re: SD Card interfacing

Post by tink »

Hi gentlement, I have an issue at hand that is even more intriguing than how to use an SD card via bit banging, and that is,

How do you *emulate an SD card using bit banging*?

I.e. you have an ARM chip plugged in as SD card to a host machine, and that host machine (e.g. Windows whatever) will see a block device/disk however that disk's contents is delivered by the ARM chip software, and it would source the content from anywhere, e.g. from its own memory or even an SD card that it itself has.


Finally what about using Teensy 3.5, Raspberry Pi, LM3S811 (used in this related project http://webpages.uncc.edu/~jmconrad/ECGR ... ROLLER.pdf / ftp://ftp.circuitcellar.com/pub/Circuit ... ne-209.zip) - or what would be the tiniest footprint and cheapest hardware to do this? (Preferably with a microSD slot on it still.)

Please let me know, thanks!!


(Related threads: https://forum.pjrc.com/threads/37739-Ca ... ck-device) , http://raspberrypi.stackexchange.com/qu ... -raspberry , http://forum.espruino.com/conversations/293982/ )
tink
Posts: 3
Joined: 06 Oct 2016

Re: SD Card interfacing

Post by tink »

Made a separate thread of how to emulate an SD card here: viewtopic.php?f=4&t=4269
resman
Posts: 154
Joined: 12 Dec 2015
Location: Lake Tahoe
Contact:

Re: SD Card interfacing

Post by resman »

I took a slightly different approach. I first implemented a bit-banged SPI interface to slave an Arduino Uno to an Apple II using the game port. I used my own command set to allow the Apple II access to the Arduino's I/O. Then I figured out a good reason to talk to an SD card, but being sorta lazy, I decided to take advantage of the SdFat library for the Arduino so I didn't have to implement the FAT filesystem on the Apple II. Extending my custom command set to export the high level SdFat calls to the Apple so a simple library could access the files on an SD card was pretty easy. The intention was to use the SD card more as a sneaker-net way to transfer files back and forth to my Apples from my PC. I got a lot of functionality without a great deal of work. My write up is here:

http://schmenk.is-a-geek.com/wordpress/?p=239

Dave...
Post Reply