I am working on a USB Credit Card reader for iOS and it’s going to require some serious hacking, and using SPI. SPI is a simple protocol that many electronic components / microchips use to talk to each other. I’ve never used it before, and am new to microchips so I hesitate to jump right in without some experience. Electronics and microchips can be a pain / impossible to debug so I want to get something working before I start really hacking so that I always have a point of reference to come back to. While the best solution would be a protocol analyzer so I can visualize the communication between devices and troubleshoot that way, instead, I’m going to setup a loop back on a Raspberry Pi using both the Raspberry Pi’s built in SPI feature, and bit banging on a few of the Pi’s GPIO pins. This should result in me being able to send data from the built-in SPI feature and receive it on the bit-banging program. For quick reference, SPI can be a 3 wire system. With a clock pin, a read and a write pin (SCLK, MOSI, MISO).
But, before I get there, I’m first going to setup just the Raspberry Pi’s SPI feature to echo and run some test programs to make sure I’m using that correctly.
To do this, we just need to short the read and write (MOSI / MISO) pins together. They are side by side on the GPIO header, so you can short them with just a jumper. Unfortunatly, I couldn’t find any, so I used a random connector that fit and shorted the wires together (See my lincoln log case, sweet huh?). Be very careful here, there are a few places on the GPIO where GND and 5v or 3.3V are present and attaching this jumper would be a bad thing there.
Now, we need to enable the SPI port, it’s disabled by default, just do “sudo raspi-config” and go to Advanced Options, then look for the SPI option and enable it, then reboot. This should take all of 10 seconds (other then the reboot).
Once you are rebooted, type “ls /dev” you should see “spidev0.0” and “spidev0.1” SPI has a 4th wire called “Chip Select”, If you have more then 2 devices on the SPI “bus” the master device can set chip select high for the chip you want to send / receive data to / from. Both of the SPI devices are the same, they just make different GPIO pins high to select a different chip on the bus… blah blah blah… In our case, we’re just ignoring this.
At this point, we should be able to echo data… There is a SPI test program as part of the linux kernel, but we need to get an older version, so “wget -O spidev_test.c https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/spi/spidev_test.c?id=95b1ed2ac7ffe3205afc6f5a20320fbdb984da92 ” then, “gcc -o spidev_test spidev_test.c”, and finally “./spidev_test -D /dev/spidev0.0” You should get results like the ones on this page: https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md If you remove the jumper and run it again, you should get all zeros… Now, it’s time to do some bit banging….
First, let’s verify that connecting MISO to 3.3v will produce what we want. Remember when we have MISO and MOSI not shorted we get all zeros back from the test program. If we connect 3.3v to MISO we should get all FFs.
And we do!!! Yay!!! Now, lets connect MISO to a GPIO pin and see if we can get the pin to go High (+3.3v) on demand.
First, let’s install a python library to help us work with the GPIO easier. Follow Step 3 here: http://openmicros.org/index.php/articles/94-ciseco-product-documentation/raspberry-pi/217-getting-started-with-raspberry-pi-gpio-and-python
Now, I was tired of doing all this through a terminal, so I took a break and installed samba, according to these instructions: http://raspberrywebserver.com/serveradmin/share-your-raspberry-pis-files-and-folders-across-a-network.html
The script I created is:
import RPi.GPIO as GPIO
I then connected Pin 18 (to RPi.GPIO this is Pin 12). Ran my python program, and ran the test program. Viola!!! It works. All FFs again.
Now we change the Program to:
import RPi.GPIO as GPIO
Run it, and while it’s running run our test script. We should get a mix of FFs, and 00s, and between the sets of FFs and 00s there will be smaller numbers (like FC).
To Be Continued…