mcp23s17.h 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/*     This file is part of mux-sw.

    mux-sw is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    mux-sw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with mux-sw.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __MCP23S17_H__
#define __MCP23S17_H__



namespace MCP23S17 {

#define SERIAL_DEBUG 0
  
//setup register values for this chip
// BANK=1 mode
/*
const int IODIRA   = 0x00;
const int IPOLA    = 0x01;
const int GPINTENA = 0x02;
const int DEFVALA  = 0x03;
const int INTCONA  = 0x04;
const int IOCON   = 0x05;
const int GPPUA    = 0x06;
const int INTFA    = 0x07;
const int INTCAPA  = 0x08;
const int GPIOA    = 0x09;
const int OLATA    = 0x0A;

const int IODIRB   = 0x10;
const int IPOLB    = 0x11;
const int GPINTENB = 0x12;
const int DEFVALB  = 0x13;
const int INTCONB  = 0x14;
const int IOCONB   = 0x15;
const int GPPUB    = 0x16;
const int INTFB    = 0x17;
const int INTCAPB  = 0x18;
const int GPIOB    = 0x19;
const int OLATB    = 0x1A;
*/
const int IODIRA   = 0x00;
const int IPOLA    = 0x02;
const int GPINTENA = 0x04;
const int DEFVALA  = 0x06;
const int INTCONA  = 0x08;
const int IOCON   = 0x0A;
const int GPPUA    = 0x0C;
const int INTFA    = 0x0E;
const int INTCAPA  = 0x10;
const int GPIOA    = 0x12;
const int OLATA    = 0x14;

const int IODIRB   = 0x01;
const int IPOLB    = 0x03;
const int GPINTENB = 0x05;
const int DEFVALB  = 0x07;
const int INTCONB  = 0x09;
const int IOCONB   = 0x0B;
const int GPPUB    = 0x0D;
const int INTFB    = 0x0F;
const int INTCAPB  = 0x11;
const int GPIOB    = 0x13;
const int OLATB    = 0x15;

const uint8_t PINMODE_INPUT = 0xFF;
const uint8_t PINMODE_OUTPUT = 0x00;

const uint8_t OPCODE_ADDR = 0b01000000;
const uint8_t OPCODE_A0   = 0b00000010;
const uint8_t OPCODE_A1   = 0b00000100;
const uint8_t OPCODE_A2   = 0b00001000;
const uint8_t OPCODE_READ = 0b00000001;

// "B" relays
const uint8_t PINA[9] = {
  0b00000000,
  0b00000000, // OP 1
  0b00000000, // OP 2
  0b00000000, // OP 3
  0b00000000, // OP 4
  0b00000001, // OP 5
  0b00000111, // OP 6
  0b01001001, // OP 7
  0b01110001, // OP 8
};

// "A" relays"
const uint8_t PINB[9] = {
  0b00000000,
  0b00000001, // OP 1
  0b00000110, // OP 2
  0b00011000, // OP 3
  0b01101000, // OP 4
  0b10000000, // OP 5
  0b00000000, // OP 6
  0b00000000, // OP 7
  0b00000000, // OP 8
};

class MCP23S17 {
public:
  MCP23S17( int8_t SDIO, int8_t CSB, int8_t SCLK)  {
    spi = new SPIBitBang(SDIO, CSB, SCLK);
    //write_register(IOCON, 0b11110010);
  }
  ~MCP23S17() {
    delete spi;
  }
  void set_pin_mode(uint8_t mode) {
    
    write_register(IODIRA, mode);
    write_register(IODIRB, mode);
  }
  void set_gpio(uint8_t stateA, uint8_t stateB) {
    write_register(GPIOA, stateA);
    write_register(GPIOB, stateB);
  }

  
  void write_register(uint8_t reg, uint8_t data) {
    uint8_t opcode = OPCODE_ADDR; // | OPCODE_A0 | OPCODE_A1;
    if (SERIAL_DEBUG) {
      Serial.print("opcode ");
      Serial.print(opcode, HEX);
      Serial.print(" reg ");
      Serial.print(reg, HEX);
      Serial.print(" data= ");
      Serial.print(data, HEX);
      Serial.print("\n");
    }
    spi->write_three(opcode, reg, data);

  }

  private:
    SPIBitBang* spi;
}; // end class
} // end namespace
#endif