/*
  Init the GPIO chip. It also init SPI bus.
	There's a lot of stuff running inside this function, it checks
	for legal pins and SPI bus, can handle alternative SPI bus and pins,
	init correctly SPI bus and only once (in case of multiple instances).
	Parameters
	avoidSpiInit: if you use this library after any other SPI devices
	that already have SPI inited, you may use this option that will avoid SPI.begin().
	Normally you better don't use at all this option...
*/
void gpio_MCP23SXX::begin(bool avoidSpiInit)
{
	if (_chip == MCPNONE) return;//unknow chip
	_readCmd =  (_adrs << 1) | 1;
	_writeCmd = _adrs << 1;
	_gpioDirection	= 0xFFFF;//all inputs
	_gpioState		= 0x0000;//bogus
	gpio_MCP23SXX_instance += 1;//instance counter
	if (gpio_MCP23SXX_instance > 1) avoidSpiInit = true;//do not init SPI again
	#if defined(SPI_LEGACY_METHOD)
	//using internal SPI methods
		if (beginSpi(avoidSpiInit) != 0xFF) return;//if != 0xFF cannot continue
		//set SPI speed...
		#if defined(SPI_HAS_TRANSACTION)
			setSpiSettings(SPISettings(_maxGpioSPIspeed, MSBFIRST, SPI_MODE0));
		#else
			//pre - SPI transaction era...
			SPI.setClockDivider(SPI_CLOCK_DIV4);
			SPI.setBitOrder(MSBFIRST);
			SPI.setDataMode(SPI_MODE0);
		#endif
	#else
	//using high speed external SPI libraries
		#if (defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__))
			_spi.postInstance(_cs,255,_mosi,_sclk,_miso);
		#elif (defined(__AVR__) || defined(ESP8266))
			_spi.postInstance(_cs,255);
		#endif
		//begin SPI and set SPI speed...
		#if defined(SPI_HAS_TRANSACTION)
			_spi.begin(SPISettings(_maxGpioSPIspeed, MSBFIRST, SPI_MODE0),avoidSpiInit);
		#else
			_spi.begin(avoidSpiInit);
		#endif
	#endif
/*
The IOCON register!
                 7     6     5   	4      3    2    1      0
MCP23S08 IOCON = NA   NA     SEQOP DISSLW HAEN ODR INTPOL  NA
MCP23S09 IOCON = NA   NA     SEQOP NA     NA   ODR INTPOL  INTCC
MCP23S17 IOCON = BANK MIRROR SEQOP DISSLW HAEN ODR INTPOL  NA
MCP23S18 IOCON = BANK MIRROR SEQOP NA     NA   ODR INTPOL  INTCC
*/
	if (_chip == MCP23S09 || _chip == MCP23S18){
		//these chip doesn't use HAEN
		gpioSetup(_SEQOP | _INTCC);//enable INTCC always
	} else {
		_useHaen == 1 ? gpioSetup(_SEQOP | _HAEN) : gpioSetup(_SEQOP);
	}
	//as default, GPIO init as High-Impedance input
	gpioPinMode(INPUT);
}
Beispiel #2
0
void rfidInit() {
    gpioOutputType(RFID_SCL, gpioOutputType_openDrain);
    gpioOutputType(RFID_SDA, gpioOutputType_openDrain);
    gpioPinMode(RFID_SCL, gpioMode_alternate);
    gpioPinMode(RFID_SDA, gpioMode_alternate);
    gpioAlternate(RFID_SCL, 1);
    gpioAlternate(RFID_SDA, 1);


    uint8_t buff[32] = {0};
    i2cInit(rfidi2c, i2cSpeed_std);
    
    readRegister(0x2040, buff, 2);

    readRegister(0x2004, buff, 0x14);
    
    if ((buff[6] == 0x00 &&
        buff[7] == 0x00 &&
        buff[8] == 0x00 &&
        buff[9] == 0x00) ||
        buff[10] != 0x00 ||
        buff[11] != 0x00 ||
        buff[12] != 0x00 ||
        buff[13] != 0x00) {
        writeShort(0x2008, 0xCAFE);
        delay(10);
        writeShort(0x200A, 0xD00D);
        delay(10);

        uint16_t i = 0x200C;
        for (; i < 0x2018; i+=2) {
            writeShort(i, 0x0000);
            delay(10);
        }
    }
}