2019-05-08 05:26:52 +10:00
|
|
|
# Accessing a MicroSD card
|
|
|
|
|
|
|
|
SD cards are great because they are accessible directly. No supporting IC is
|
|
|
|
necessary. The easiest way to access them is through the SPI protocol.
|
|
|
|
|
|
|
|
Due to the way IO works in z80, implementing SPI through it as a bit awkward:
|
|
|
|
You can't really keep pins high and low on an IO line. You need some kind of
|
|
|
|
intermediary between z80 IOs and SPI.
|
|
|
|
|
|
|
|
There are many ways to achieve this. This recipe explains how to build your own
|
2020-08-23 21:55:17 +10:00
|
|
|
hacked off SPI relay for the RC2014. It can then be used with the SD Card
|
|
|
|
subsystem (B420) to drive a SD card.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
|
|
|
## Gathering parts
|
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
* A RC2014 Classic
|
2019-05-08 05:26:52 +10:00
|
|
|
* A MicroSD breakout board. I use Adafruit's.
|
|
|
|
* A proto board + header pins with 39 positions so we can make a RC2014 card.
|
|
|
|
* Diodes, resistors and stuff
|
|
|
|
* 40106 (Inverter gates)
|
2020-09-19 09:22:56 +10:00
|
|
|
* 74xx138 (Decoder)
|
|
|
|
* 74xx375 (Latches)
|
|
|
|
* 74xx125 (Buffer)
|
2019-05-08 05:26:52 +10:00
|
|
|
* 74xx161 (Binary counter)
|
|
|
|
* 74xx165 (Parallel input shift register)
|
|
|
|
* 74xx595 (Shift register)
|
|
|
|
|
|
|
|
## Building the SPI relay
|
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
![SPI relay](spirelay.jpg)
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
The schematic above works well with the SD Card subsystem (B420). Of course,
|
|
|
|
it's not the only possible design that works, but I think it's one of the most
|
|
|
|
straighforwards.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
This relay communicates through the z80 bus with 2 ports, `DATA` and `CTL` and
|
|
|
|
allows up to 4 devices to be connected to it at once, although only one device
|
|
|
|
can ever be active at once. This schema only has 2 (and the real prototype I've
|
|
|
|
built from it), but the '375 has room for 4. In this schema, `DATA` is port 4,
|
|
|
|
`CTL` is port `5`.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
We activate a device by sending a bitmask to `CTL`, this will end up in the
|
|
|
|
'375 latches and activate the `SS` pin of one of the device, or deactivate them
|
|
|
|
all if `0` is sent.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
You then initiate a SPI exchange by sending a byte to send to the `DATA` port.
|
|
|
|
This byte will end up in the '165 and the '161 counter will be activated,
|
|
|
|
triggering a clock for the SPI exchange. At each clock, a bit is sent to `MOSI`
|
|
|
|
from the '161 and received from `MISO` into the '595, which is the byte sent to
|
|
|
|
the z80 bus when we read from `DATA`.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
When the '161 is wired to the system clock, as it is in the schema, two `NOP`s
|
|
|
|
are a sufficient delay between your `DATA` write and subsequent `DATA` read.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
However, if you build yourself some kind of clock override and run the '161 at
|
|
|
|
something slower than the system clock, those 2 `NOP`s will be too quick. That's
|
|
|
|
where that '125 comes into play. When reading `CTL`, it spits `RUNNING` into
|
|
|
|
`D0`. This allows you to know when the result of the SPI exchange is ready to be
|
|
|
|
fetched. Make sure you `AND` away other bits, because they'll be garbage.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
The '138 is to determine our current IORQ mode (`DATA`/`CTL` and `WR/RO`), the
|
|
|
|
'106 is to provide for those `NOT`s sprinkled around.
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
Please note that this design is inspired by [this design][inspiration].
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
Advice 1: Make `SCK` polarity configurable at all 3 endpoints (the 595, the 165
|
2020-08-23 21:55:17 +10:00
|
|
|
and SPI connector). Those jumpers will be useful when you need to mess with
|
|
|
|
polarity in your many tinkering sessions to come.
|
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
Advice 2: Make input `CLK` override-able. SD cards are plenty fast enough for
|
|
|
|
us to use the system clock, but you might want to interact with devices that
|
2020-08-23 21:55:17 +10:00
|
|
|
require a slower clock.
|
|
|
|
|
2020-05-14 00:14:23 +10:00
|
|
|
## Building your binary
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-08-17 10:54:06 +10:00
|
|
|
The binary built in the base recipe doesn't have SDC drivers. You'll need to
|
|
|
|
assemble a binary with those drivers. To do so, you'll modify the xcomp unit
|
|
|
|
of the base recipe. Look at `xcomp.fs`, you'll see that we load a block. That's
|
|
|
|
our xcomp block (likely, B599). Open it.
|
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
First, we need drivers for the SPI relay. This is done by declaring `SPI_DATA`
|
|
|
|
and `SPI_CTL`, which are respectively `4` and `5` in our relay design.
|
2020-08-23 21:55:17 +10:00
|
|
|
|
2020-09-19 09:22:56 +10:00
|
|
|
You also need to tell the SDC subsystem which SPI device to activate by defining
|
|
|
|
the `SDC_DEVID` (1, 2, 4, 8 for device 0, 1, 2 or 3)
|
2020-08-23 21:55:17 +10:00
|
|
|
|
2020-09-25 02:26:43 +10:00
|
|
|
You can then load the driver with `616 LOAD`. This driver provides
|
2020-09-19 09:22:56 +10:00
|
|
|
`(spix)` and `(spie)` which are then used in the SDC driver.
|
2020-06-30 08:48:00 +10:00
|
|
|
|
2020-08-17 04:30:33 +10:00
|
|
|
The SDC driver is at B420. It gives you a load range. This means that what
|
2020-05-15 01:32:51 +10:00
|
|
|
you need to insert in `xcomp` will look like:
|
2020-05-14 00:14:23 +10:00
|
|
|
|
2020-08-17 04:30:33 +10:00
|
|
|
423 436 LOADR ( sdc )
|
2020-05-14 00:14:23 +10:00
|
|
|
|
2020-05-15 01:32:51 +10:00
|
|
|
You also need to add `BLK$` to the init sequence.
|
2020-05-14 00:14:23 +10:00
|
|
|
|
2020-08-17 10:54:06 +10:00
|
|
|
Build it (run `make pack` in `cvm/` first to ensure an up-to-date blkfs) and
|
|
|
|
write it to EEPROM.
|
2020-05-14 00:14:23 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
## Testing in the emulator
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
The RC2014 emulator includes SDC emulation. You can attach a SD card image to
|
|
|
|
it by invoking it with a second argument:
|
2019-05-08 07:28:07 +10:00
|
|
|
|
2020-06-29 06:34:41 +10:00
|
|
|
../../../emul/hw/rc2014/classic os.bin ../../../cvm/blkfs
|
2019-05-08 07:28:07 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
You will then run with a SD card having the contents from `/blk`.
|
2019-05-08 07:28:07 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
## Usage
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
First, the SD card needs to be initialized
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
SDC$
|
2019-05-08 05:26:52 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
If there is no error message, we're fine. Then, we need to hook `BLK@*` and
|
|
|
|
`BLK!*` into the SDC driver:
|
2019-05-29 03:13:34 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
' SDC@ BLK@* !
|
|
|
|
' SDC! BLK!* !
|
2019-05-29 03:13:34 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
And thats it! You have full access to disk block mechanism:
|
2019-05-29 03:13:34 +10:00
|
|
|
|
2020-06-30 08:48:00 +10:00
|
|
|
105 LOAD
|
2020-04-20 06:56:37 +10:00
|
|
|
BROWSE
|
2019-05-29 03:13:34 +10:00
|
|
|
|
2020-04-20 06:56:37 +10:00
|
|
|
(at this moment, the driver is a bit slow though...)
|
2019-06-03 01:53:36 +10:00
|
|
|
|
2020-04-26 06:03:01 +10:00
|
|
|
## How do I fill my SD card with Collapse OS' FS?
|
|
|
|
|
2020-08-31 06:53:27 +10:00
|
|
|
Very easy. You see that `/cvm/blkfs` file? You dump it to your raw device.
|
2020-04-26 06:03:01 +10:00
|
|
|
For example, if the device you get when you insert your SD card is `/dev/sdb`,
|
|
|
|
then you type `cat emul/blkfs | sudo tee /dev/sdb > /dev/null`.
|
|
|
|
|
2019-05-08 05:26:52 +10:00
|
|
|
[inspiration]: https://www.ecstaticlyrics.com/electronics/SPI/fast_z80_interface.html
|