void Adafruit_NFCShield_I2C::wirereaddata(uint8_t* buff, uint8_t n) {
  uint16_t timer = 0;
  
  delay(2); 

#ifdef PN532DEBUG
  Serial.print("Reading: ");
#endif
  // Start read (n+1 to take into account leading 0x01 with I2C)
  Wire.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint8_t)(n+2));
  // Discard the leading 0x01
  wirerecv();
  for (uint8_t i=0; i<n; i++) {
    delay(1);
    buff[i] = wirerecv();
#ifdef PN532DEBUG
    Serial.print(" 0x");
    Serial.print(buff[i], HEX);
#endif
  }
  // Discard trailing 0x00 0x00
  // wirerecv();
    
#ifdef PN532DEBUG
  Serial.println();
#endif
}
//Added to default library
//From the Adafruit_RGBLCDShield library we learn that the
//buttons are on bits 0..4, which are on GPIOA,
//so we use GPINTENA, INTCONA and IOCONA.
void Adafruit_MCP23017::enableButtonInterrupt() {
  uint8_t data;
  //Enable the interrupts on the button pins 0..4
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_GPINTENA);
  Wire.endTransmission();
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  data = wirerecv();
  data |= 0x1F; //Bits 0..4 high, to enable IOC

  //Write the new value back
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_GPINTENA);
  wiresend(data);
  Wire.endTransmission();
  
  //We set INTCONA bits 0..4 to 0 = State change interrupt
  //(instead of compare to state)
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_INTCONA);
  Wire.endTransmission();
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  data = wirerecv();
  data &= ~0x1F; //Force bits 0..4 to low
  
  //Write the new value back
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_INTCONA);
  wiresend(data);
  Wire.endTransmission();

  //We set the INTA pin to Active-Low
  //first read current register value
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_IOCONA);
  Wire.endTransmission();
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  data = wirerecv();

  //Bit 1 = INTPOL = Low = Active-Low
  //(When disabled or no interrupt, signal is high)
  data &= ~0x02;
  //Bit 2 = ODR = Low = Open Drain disabled
  data &= ~0x04;
  //Bit 6 = MIRROR = Low = INTA/INTB seperate
  data &= ~0x40;
  
  //Write the new value back
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_IOCONA);
  wiresend(data);
  Wire.endTransmission();
}
/**
 * Reads a given register
 */
uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){
	// read the current GPINTEN
	Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
	wiresend(addr);
	Wire.endTransmission();
	Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
	return wirerecv();
}
/**
 * Reads all 16 pins (port A and B) into a single 16 bits variable.
 */
uint16_t Adafruit_MCP23017::readGPIOAB() {
	uint16_t ba = 0;
	uint8_t a;

	// read the current GPIO output latches
	Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
	wiresend(MCP23017_GPIOA);
	Wire.endTransmission();

	Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 2);
	a = wirerecv();
	ba = wirerecv();
	ba <<= 8;
	ba |= a;

	return ba;
}
Exemplo n.º 5
0
//-----------------------------------------------------
//	This function read a single register
//  RegAdd  = Register address
//	i2caddr = device address (0 to 7. See A0, A1 e A2 configurations)
//	This function return the value of selected register
uint8_t MCP23017::ReadSingleReg(uint8_t RegAdd, uint8_t i2caddr) {
	if (i2caddr > 7) { i2caddr = 7; }
	Wire.beginTransmission(MCP23017_HW_ADD | i2caddr);
	wiresend(RegAdd);
	Wire.endTransmission();
	Wire.requestFrom(MCP23017_HW_ADD | i2caddr, 1);
	return wirerecv();	
}
/**
 * Read a single port, A or B, and return its current 8 bit value.
 * Parameter b should be 0 for GPIOA, and 1 for GPIOB.
 */
uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) {

	// read the current GPIO output latches
	Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
	if (b == 0)
		wiresend(MCP23017_GPIOA);
	else {
		wiresend(MCP23017_GPIOB);
	}
	Wire.endTransmission();

	Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
	return wirerecv();
}
//Added to default library
void Adafruit_MCP23017::disableButtonInterrupt() {
  uint8_t data;

  //Disable the interrupts on the button pins 0..4
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_GPINTENA);
  Wire.endTransmission();
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  data = wirerecv();
  data &= ~0x1F; //Bits 0..4 low, to disable IOC

  //Write the new value back
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(MCP23017_GPINTENA);
  wiresend(data);
  Wire.endTransmission();
}
Exemplo n.º 8
0
//-----------------------------------------------------
//	This function sets a single bit into selected register
//  RegAdd  = Register address
//	Bit     = Bit to set
//	i2caddr = device address (0 to 7. See A0, A1 e A2 configurations)
void MCP23017::SetSingleBit(uint8_t RegAdd, uint8_t Bit, uint8_t i2caddr) {
	uint8_t RegData;
	if (i2caddr > 7) { i2caddr = 7; }
	if (Bit > 7) { Bit = 7; }
	
	Wire.beginTransmission(MCP23017_HW_ADD | i2caddr);
	wiresend(RegAdd);
	Wire.endTransmission();
	Wire.requestFrom(MCP23017_HW_ADD | i2caddr, 1);
	RegData = wirerecv();

	RegData |= (0x01 << Bit);

	Wire.beginTransmission(MCP23017_HW_ADD | i2caddr);
	wiresend(RegAdd);
	wiresend(RegData);
	Wire.endTransmission();	
}
Exemplo n.º 9
0
uint8_t MCP23017::digitalRead(uint8_t p) {
	uint8_t gpioaddr;

	// only 16 bits!
	if (p > 15)
		return 0;

	if (p < 8)
		gpioaddr = MCP23017_GPIOA;
	else {
		gpioaddr = MCP23017_GPIOB;
		p -= 8;
	}

	// read the current GPIO
	Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
	wiresend(gpioaddr);
	Wire.endTransmission();

	Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
	return (wirerecv() >> p) & 0x1;
}
Exemplo n.º 10
0
void MCP23017::digitalWrite(uint8_t p, uint8_t d) 
{
  uint8_t gpio;
  uint8_t gpioaddr, olataddr;

  // only 16 bits!
  if (p > 15)
    return;

  if (p < 8) {
    olataddr = MCP23017_OLATA;
    gpioaddr = MCP23017_GPIOA;
  } else {
    olataddr = MCP23017_OLATB;
    gpioaddr = MCP23017_GPIOB;
    p -= 8;
  }

  // read the current GPIO output latches
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(olataddr);	
  Wire.endTransmission();
  
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
   gpio = wirerecv();

  // set the pin and direction
  if (d == HIGH) {
    gpio |= 1 << p; 
  } else {
    gpio &= ~(1 << p);
  }

  // write the new GPIO
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(gpioaddr);
  wiresend(gpio);	
  Wire.endTransmission();
}
Exemplo n.º 11
0
void MCP23017::pullUp(uint8_t p, uint8_t d) 
{
  uint8_t gppu;
  uint8_t gppuaddr;

  // only 16 bits!
  if (p > 15)
    return;

  if (p < 8)
    gppuaddr = MCP23017_GPPUA;
  else {
    gppuaddr = MCP23017_GPPUB;
    p -= 8;
  }


  // read the current pullup resistor set
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(gppuaddr);	
  Wire.endTransmission();
  
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  gppu = wirerecv();

  // set the pin and direction
  if (d == HIGH) {
    gppu |= 1 << p; 
  } else {
    gppu &= ~(1 << p);
  }

  // write the new GPIO
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(gppuaddr);
  wiresend(gppu);	
  Wire.endTransmission();
}
Exemplo n.º 12
0
void MCP23017::pinMode(uint8_t p, uint8_t d) 
{
  uint8_t iodir;
  uint8_t iodiraddr;

  // only 16 bits!
  if (p > 15)
    return;

  if (p < 8)
    iodiraddr = MCP23017_IODIRA;
  else {
    iodiraddr = MCP23017_IODIRB;
    p -= 8;
  }

  // read the current IODIR
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(iodiraddr);	
  Wire.endTransmission();
  
  Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
  iodir = wirerecv();

  // set the pin and direction
  if (d == INPUT) {
    iodir |= 1 << p; 
  } else {
    iodir &= ~(1 << p);
  }

  // write the new IODIR
  Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
  wiresend(iodiraddr);
  wiresend(iodir);	
  Wire.endTransmission();
}