To set up I2C on the Raspberry Pi, I used a guide from SK Pang Electronics. After following the guide, and running “i2cdetect,” all I2C chips on the bus should register it’s address.
The “-y” flag stops the command from issuing warnings about probing the bus. The number, in this case 1, indicates which bus to scan. The Raspberry Pi actually has two separate I2C buses. On the original Model B Raspberry Pis, the bus broken out to GPIO header pins 3, and 5 are set as bus ‘0.’ On revision 2 of the Raspberry Pi, these pins now map to bus ‘1.’ Keep that in mind when adapting code between the newer and older model Pis.
Another thing to note is the address of the chips. MCP23017 chips register on the bus starting at address 0x20. There are three hardware address pins on the chips that when pulled high or low change the addressing of that particular chip. Giving a possibility of 8 of these chips on a single bus. I meant for these chips to read sequentially, but I derped when visualizing the binary addresses, thus there is no chip at address 0x21. It’s no big deal, though.
For the next step, I installed the SNESDev-RPi controller drivers….and this caused problems. Not only did the controllers not register, but now my I2C bus no longer worked. Over the course of the next few days I debugged the hardware as I thought I may have caused a short, or even fried some GPIO pins.
It wasn’t until I delved into the source of SNESDev-RPi where I found the problem. The pinout on the blog was no longer used by the code:
// pin out used in SNESDev article on blog// pads.clock = RPI_GPIO_P1_18;// pads.strobe = RPI_GPIO_P1_16;// pads.data1 = RPI_GPIO_P1_22;// pads.data2 = RPI_GPIO_P1_15;
// pin out used pi gamecon GPIO driverpads.clock = RPI_GPIO_P1_19;pads.strobe = RPI_GPIO_P1_23;pads.data1 = RPI_GPIO_P1_07;pads.data2 = RPI_GPIO_P1_05;
The pinout was apparently changed to follow the convention of another coder’s “gamecon” GPIO driver. I had already wired my circuit around the old pinout, and on top of that “RPI_GPIO_P1_05” is also the I2C SDL pin. This explained why I2C stopped working, as well.
I commented out the gamecon pin mappings, and un-commented the original pin maps. I recompiled, and tested. Not only did the I2C bus report my expanders, my controllers now worked! It was actually a good thing that I went through the source, as I found that a little referenced feature of the driver was the ability to use a GPIO pin as a button. As it was configured by the driver, pressing the button sends an “ESC” key press to the terminal, and holding the button then releasing causing the system to shut down. (It would have been nice to know about this while working on the power management!)
I would come to revisit this code later on the wiring up of the Reset Button..
Next up, cartridge reading!