dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

SPI mcp23s17

Wed Mar 09, 2016 7:18 am

Hi all,

Is there an example on using mcp23s17 with wiringPi2 or spidev? I need a piece of code showing connection, input, output.

Furthermore, are there some commands on how to show the spi devices connected on the bus like i2cdetect but for spi?

Thank you in advance.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: SPI mcp23s17

Wed Mar 09, 2016 7:44 am

The PiFace Digital has a MCP23S17 chip in it.
You can find the software here: http://www.piface.org.uk/

User avatar
karrika
Posts: 1070
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: SPI mcp23s17

Wed Mar 09, 2016 8:16 am

I found out the hard way that in order to set up multiple MCP23S17 chips you need to set up all the chip modes in a strange way at startup. There is some bug in the errata and if you have multiple chips wired for different addresses A0,A1,A2 then recognizing these addresses does not work out of the box. You need to run "setChipModes()" to make them work.

Code: Select all

void writereg(int chip, int reg, unsigned char data)
{
  unsigned char buf[3];
  int status;
  int i;
  buf[0] = (0x40 | (chip << 1)) & 0xff;
  buf[1] = reg & 0xff;
  buf[2] = data;
  //printf("%02x, %02x, %02X - ", buf[0], buf[1], buf[2]);
  status = wiringPiSPIDataRW(0, buf, 3);
  //printf("%d %02x, %02x, %02X\n", status, buf[0], buf[1], buf[2]);
}

void setChipModes()
{
  int chip;
  // Set BANK=1 no flip flop of addresses
  for (chip = 0; chip < 8; chip++) {
    writereg(chip, 0xa, 0xa8); // IOCON
  }
}

wiringPiSetup () ;
wiringPiSPISetup(0, 16000000); // up to 32000000
setChipModes();

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: SPI mcp23s17

Wed Mar 09, 2016 2:35 pm

Solved using this library https://github.com/petrockblog/RPi-MCP23S17

No solution yet to list all the connected spi devices.

Return to “Python”