Close

Bit Banging SPI and Reading SD Cards

A project log for Herring 6502 Computer

Modular 8-bit computer system based on the WDC 65C02 and a mix of modern hardware.

colin-maykishColin Maykish 08/20/2022 at 21:330 Comments

In my continuing effort to avoid completing features, I've added another distraction: SD card storage. My real plan is to have SPI available for all kinds of peripherals (displays and network adapters come to mind), but I already had an SD breakout board and it seemed like the logical place to start.

Adafruit SD breakout connected to 6502 through 6522 GPIO pins

I did try to get SD cards working some time ago using the 6522 to bit bang the SPI protocol, but I was not successful. It's possible the hardware I was using at the time was faulty. I have two SD breakout boards, one from Adafruit, and a generic version from eBay. I was not able to get the clone working passed a certain point. Seems like it's worth paying a few bucks for the Adafruit version.

One important note: a pullup resistor on the MISO line is apparently very important, even with the breakout boards. The card will fail to initialize without it.

 

This time I ignored all concerns for speed and efficient code and wrote the SPI and SD libraries in C. It's still bit-banging SPI through the 6522. I'm able to initialize the card and read arbitrary sectors of data. There's currently no filesystem support other than some simple parsing of the FAT metadata in the first sector. I'll need to decide how much deeper into the FAT implementation I want to go. It would be nice to have file I/O, but worst case I can treat the SD card like a big serial EEPROM and read in raw data.

><(((°> run
Setting up the SD card...
Sending SD init command...
SD card ready!
Reading block: 0
Block read complete: 0
Disk Info:
OEM: MSDOS5.0
Bytes per sector: 512
Sectors per cluster: 16
Reserved sectors: 2304
Sectors per FAT: 0
Number of sectors: 30840
Filesystem type: 
Reading block: 0

As always, the code is available on Github: https://github.com/crmaykish/herring-6502/tree/main/firmware

Look for the spi.h and sd.h files.

Discussions