Skip to content

Commit

Permalink
Merge pull request #45 from PontusO/master
Browse files Browse the repository at this point in the history
Adds support for using any I2C port
  • Loading branch information
sabas1080 authored Nov 15, 2022
2 parents dc9ecb5 + d33c3c6 commit b0aab1b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
55 changes: 28 additions & 27 deletions src/Electroniccats_PN7150.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* NXP PN7150 Driver
* Porting authors:
* NXP PN7150 Driver
* Porting authors:
* Salvador Mendoza - @Netxing - salmg.net
* Andres Sabas - Electronic Cats - Electroniccats.com
*
* November 2020
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*
Expand Down Expand Up @@ -49,23 +49,25 @@ unsigned char DiscoveryTechnologiesP2P[] = { //P2P
MODE_LISTEN | TECH_ACTIVE_NFCA,
MODE_LISTEN | TECH_ACTIVE_NFCF};

Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress) : _IRQpin(IRQpin),
_VENpin(VENpin),
_I2Caddress(I2Caddress)
Electroniccats_PN7150::Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin,
uint8_t I2Caddress, TwoWire *wire) : _IRQpin(IRQpin), _VENpin(VENpin), _I2Caddress(I2Caddress), _wire(wire)
{
pinMode(_IRQpin, INPUT);
pinMode(_VENpin, OUTPUT);
if (_VENpin != 255)
pinMode(_VENpin, OUTPUT);
}

uint8_t Electroniccats_PN7150::begin()
{
Wire.begin();
digitalWrite(_VENpin, HIGH);
delay(1);
digitalWrite(_VENpin, LOW);
delay(1);
digitalWrite(_VENpin, HIGH);
delay(3);
_wire->begin();
if (_VENpin != 255) {
digitalWrite(_VENpin, HIGH);
delay(1);
digitalWrite(_VENpin, LOW);
delay(1);
digitalWrite(_VENpin, HIGH);
delay(3);
}
return SUCCESS;
}

Expand All @@ -77,15 +79,15 @@ bool Electroniccats_PN7150::hasMessage() const
uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLevel) const
{
uint32_t nmbrBytesWritten = 0;
Wire.beginTransmission((uint8_t)_I2Caddress); //configura transmision
nmbrBytesWritten = Wire.write(txBuffer, (size_t)(txBufferLevel)); //carga en buffer
_wire->beginTransmission((uint8_t)_I2Caddress); //configura transmision
nmbrBytesWritten = _wire->write(txBuffer, (size_t)(txBufferLevel)); //carga en buffer
#ifdef DEBUG2
Serial.println("[DEBUG] written bytes = 0x"+String(nmbrBytesWritten,HEX));
#endif
if (nmbrBytesWritten == txBufferLevel)
{
byte resultCode;
resultCode = Wire.endTransmission(); //envio de datos segun yo
resultCode = _wire->endTransmission(); //envio de datos segun yo
#ifdef DEBUG2
Serial.println("[DEBUG] write data code = 0x"+String(resultCode,HEX));
#endif
Expand All @@ -102,16 +104,15 @@ uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const
uint32_t bytesReceived; // keeps track of how many bytes we actually received
if (hasMessage())
{ // only try to read something if the PN7150 indicates it has something
bytesReceived = Wire.requestFrom(_I2Caddress, (uint8_t)3); // first reading the header, as this contains how long the payload will be

bytesReceived = _wire->requestFrom(_I2Caddress, (uint8_t)3); // first reading the header, as this contains how long the payload will be
//Imprimir datos de bytes received, tratar de extraer con funcion read
//Leer e inyectar directo al buffer los siguientes 3
#ifdef DEBUG2
Serial.println("[DEBUG] bytesReceived = 0x"+String(bytesReceived,HEX));
#endif
rxBuffer[0] = Wire.read();
rxBuffer[1] = Wire.read();
rxBuffer[2] = Wire.read();
rxBuffer[0] = _wire->read();
rxBuffer[1] = _wire->read();
rxBuffer[2] = _wire->read();
#ifdef DEBUG2
for(int i=0;i<3;i++){
Serial.println("[DEBUG] Byte["+String(i)+"] = 0x"+String(rxBuffer[i],HEX));
Expand All @@ -120,14 +121,14 @@ uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const
uint8_t payloadLength = rxBuffer[2];
if (payloadLength > 0)
{
bytesReceived += Wire.requestFrom(_I2Caddress, (uint8_t)payloadLength); // then reading the payload, if any
bytesReceived += _wire->requestFrom(_I2Caddress, (uint8_t)payloadLength); // then reading the payload, if any
#ifdef DEBUG2
Serial.println("[DEBUG] payload bytes = 0x"+String(bytesReceived-3,HEX));
#endif
uint32_t index = 3;
while (index < bytesReceived)
{
rxBuffer[index] = Wire.read();
rxBuffer[index] = _wire->read();
#ifdef DEBUG2
Serial.println("[DEBUG] payload["+String(index)+"] = 0x"+String(rxBuffer[index],HEX));
#endif
Expand Down Expand Up @@ -1264,7 +1265,7 @@ bool Electroniccats_PN7150::ConfigureSettings(void)
}
#endif

/* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp)
/* All further settings are not versatile, so configuration only applied if there are changes (application build timestamp)
or in case of PN7150B0HN/C11004 Anti-tearing recovery procedure inducing RF setings were restored to their default value */
#if (NXP_CORE_CONF_EXTN | NXP_CLK_CONF | NXP_TVDD_CONF | NXP_RF_CONF)
/* First read timestamp stored in NFC Controller */
Expand Down
19 changes: 8 additions & 11 deletions src/Electroniccats_PN7150.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#define Electroniccats_PN7150_H
/**
* NXP PN7150 Driver
* Porting uthors:
* Porting uthors:
* Salvador Mendoza - @Netxing - salmg.net
* Andres Sabas - Electronic Cats - Electroniccats.com
*
* November 2020
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*
* A few methods and ideas were extract from
* A few methods and ideas were extract from
* https://github.com/Strooom/PN7150
*
*/
Expand All @@ -27,13 +27,9 @@
#include <i2c_t3.h> // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3
#else
#include <Wire.h>
#if defined(__AVR__) || defined(__i386__) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32)
#define WIRE Wire
#else // Arduino Due
#define WIRE Wire1
#endif // TODO : i2c_t3.h ensures a maximum I2C message of 259, which is sufficient. Other I2C implementations have shorter buffers (32 bytes)
#endif

#define NO_PN7150_RESET_PIN 255
/* Following definitions specifies which settings will apply when NxpNci_ConfigureSettings()
* API is called from the application
*/
Expand Down Expand Up @@ -250,6 +246,7 @@ class Electroniccats_PN7150
{
private:
uint8_t _IRQpin, _VENpin, _I2Caddress;
TwoWire *_wire;
uint8_t rxBuffer[MaxPayloadSize + MsgHeaderSize]; // buffer where we store bytes received until they form a complete message
void setTimeOut(unsigned long); // set a timeOut for an expected next event, eg reception of Response after sending a Command
bool isTimeOut() const;
Expand All @@ -261,7 +258,7 @@ class Electroniccats_PN7150
uint8_t gNfcController_fw_version[3] = {0};

public:
Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress);
Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire);
int GetFwVersion();
uint8_t begin(void);
uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0)
Expand Down

0 comments on commit b0aab1b

Please sign in to comment.