I SPI with my little I

This whole SPI thing is a bit complicated for what is essentially a quite simple thing. As it has a master/slave system, the naming can be slightly confusing as the labels are flipped for the slave devices.It’s effectively just 4 wires:

  1. Serial Clock (aka SCK, SCL, SCLK), same for master & slave
  2. Master Output, Slave Input (aka MOSI, SDI for master, SDO for slave)
  3. Master Input, Slave Output (aka MISO, SDO for master, SDI for slave)
  4. Slave select (aka SS, CS), same for master & slave

As multiple devices can be on the same bus, the slave select is used to determine which of the devices should be active. In programmer terms, a kind of semaphore signalling to all the devices which one is supposed to talk.

A minor complication is that there can be a 3 wire version, in which the SDI and SDO are combined into SDIO. The datasheet for the accelerometer I’m usingPDF alert has a nice diagram with the difference:
SPI four wire vs three wire

The accelerometer board supports both I2C and SPI but the pins printed use the I2C labels. This is great if you want to hook it up via SPI but slightly confusing if you don’t.
The manual has the following pin out for the actual chip:

Pin No. Mnemonic Description
1 VDD I/O Digital Interface Supply Voltage.
2 GND Must be connected to ground.
3 Reserved Reserved. This pin must be connected to VS or left open.
4 GND Must be connected to ground.
5 GND Must be connected to ground.
6 VS Supply Voltage.
7 CS Chip Select.
8 INT1 Interrupt 1 Output.
9 INT2 Interrupt 2 Output.
10 NC Not Internally Connected.
11 Reserved Reserved. This pin must be connected to ground or left open.
12 SDO/ALT ADDRESS Serial Data Output/Alternate I2C Address Select.
13 SDA/SDI/SDIO Serial Data (I2C)/Serial Data Input (SPI 4-Wire)/Serial Data Input and Output (SPI 3-Wire).
14 SCL/SCLK Serial Communications Clock

A-ha!

It stands to reason that they just used the first label. As I want to hook it up, I need to hook up CS to the slave select, SDA to the SDIO port and SCL to the clock. I’ll be able to leave the SDO pin empty or connect to the ground.

So hardware problem solved, now it’s a software problem. Yeay?

The central idea would be to stick to the ICPS header, so that I get the maximum of pins available for my other acquisition. In order to the the 3 wires, I’ll have to use one of the MOSI/MOSI pins as the CS, with the other one being SDIO. SCK can remain SCL.

The sensor library provides by adafruit does support both I2C and SPI but the SPI constructor demands the traditional 4 wires. It doesn’t use the SPI library but instead the raw writing to pins. I think that’s actually a good thing, as the SPI library doesn’t seem to list any options for 3/4 wire. To do the appropriate things, I’ll have to alter the library to cope with the MOSI and MISO pins being one and the same. Or do I?

The central method in the library is spixfer(). That simply writes to the mosi pin and reads from the miso.

static uint8_t spixfer(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t data) {
  uint8_t reply = 0;
  for (int i=7; i>=0; i--) {
    reply <<= 1;
    digitalWrite(clock, LOW);
    digitalWrite(mosi, data & (1<<i));
    digitalWrite(clock, HIGH);
    if (digitalRead(miso))
      reply |= 1;
  }
  return reply;
}


Well, that could work, couldn’t it?

Just to confirm, according to the Instructables:

The extra pins have been mapped to the ICSP header. Their mappings are:

    D14 – MISO – PB3
    D15 – SCK – PB1
    D16 – MOSI – PB2
    D17 – SS – PB0

(At this point you may wonder “but I thought you said there wasn’t a Slave select in the header?” Well, there isn’t. The pin is present on the board but it’s hooked up to pin 17, which is the RX LED. If I wanted to use it, I’d have to do some resoldering to get access to the pin.)
That means we get the following mapping (in tabular form):

Left row Right row
14 MOSI Vcc
15 SCK MOSI 16
Reset GND
  1. MISO = CS
  2. MOSI = SDIO
  3. SCK = SCK

This means I should be able to use the constructor like so:
Adafruit_ADXL345_Unified(16, 15, 15, 14);


A short while later…

Well, no magic smoke has been released, but the device isn’t being reported as connected. Because the subtle differences between the 3 and 4 wire version, I’ll have to make some small alterations to the sensor code.

TO BE CONTINUED..
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: