From db1404a01cf6d285263fc1545b6bed8ec11a2495 Mon Sep 17 00:00:00 2001 From: Eduardo Contreras Date: Wed, 3 Aug 2022 23:07:02 -0500 Subject: [PATCH 1/7] add files and debbug --- src/Electroniccats_PN7150.cpp | 13 +- src/Electroniccats_PN7150.h | 1 + src/I2C_dev.cpp | 349 ++++++++++++++++++++++++++++++++++ src/I2C_dev.h | 73 +++++++ 4 files changed, 431 insertions(+), 5 deletions(-) create mode 100644 src/I2C_dev.cpp create mode 100644 src/I2C_dev.h diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 838521b..5cd5fdb 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -77,12 +77,12 @@ 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); - nmbrBytesWritten = Wire.write(txBuffer, (int)(txBufferLevel)); + Wire.beginTransmission((uint8_t)_I2Caddress); //configura transmision + nmbrBytesWritten = Wire.write(txBuffer, (int)(txBufferLevel)); //carga en buffer if (nmbrBytesWritten == txBufferLevel) { byte resultCode; - resultCode = Wire.endTransmission(); + resultCode = Wire.endTransmission(); //envio de datos segun yo return resultCode; } else @@ -93,11 +93,14 @@ uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLe uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const { - uint32_t bytesReceived; // keeps track of how many bytes we actually received + uint8_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 - + //I2Cdev::writeBytes(_I2Caddress, 0x00, 3, &bytesReceived); + //I2Cdev::readBytes(_I2Caddress, 0x00, 3, &bytesReceived); + //Imprimir datos de bytes received, tratar de extraer con funcion read + //Leer e inyectar directo al buffer los siguientes 3 rxBuffer[0] = Wire.read(); rxBuffer[1] = Wire.read(); rxBuffer[2] = Wire.read(); diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index a38dc7a..dab2734 100755 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -27,6 +27,7 @@ #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 #else #include +#include #if defined(__AVR__) || defined(__i386__) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32) #define WIRE Wire #else // Arduino Due diff --git a/src/I2C_dev.cpp b/src/I2C_dev.cpp new file mode 100644 index 0000000..2a8b85b --- /dev/null +++ b/src/I2C_dev.cpp @@ -0,0 +1,349 @@ +// Raspberry Pi Pico port for: +// I2Cdev library collection - Main I2C device class +// Abstracts bit and byte I2C R/W functions into a convenient class +// 2013-06-05 by Jeff Rowberg +// +// Changelog: +// 2021-09-29 - Initial port release by Gino Ipóliti. + +/* ============================================ +I2Cdev device library code is placed under the MIT license +Copyright (c) 2013 Jeff Rowberg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +=============================================== +*/ + +#include "I2C_dev.h" + +/** Default constructor. + */ +I2Cdev::I2Cdev() { +} + +/** Read a single bit from an 8-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param bitNum Bit position to read (0-7) + * @param data Container for single bit value + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (true = success) + */ +int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint32_t timeout) { + uint8_t b; + uint8_t count = readByte(devAddr, regAddr, &b, timeout); + *data = b & (1 << bitNum); + return count; +} + +/** Read a single bit from a 16-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param bitNum Bit position to read (0-15) + * @param data Container for single bit value + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (true = success) + */ +int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint32_t timeout) { + uint16_t b; + uint8_t count = readWord(devAddr, regAddr, &b, timeout); + *data = b & (1 << bitNum); + return count; +} + +/** Read multiple bits from an 8-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param bitStart First bit position to read (0-7) + * @param length Number of bits to read (not more than 8) + * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (true = success) + */ +int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint32_t timeout) { + // 01101001 read byte + // 76543210 bit numbers + // xxx args: bitStart=4, length=3 + // 010 masked + // -> 010 shifted + uint8_t count, b; + if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { + uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); + b &= mask; + b >>= (bitStart - length + 1); + *data = b; + } + return count; +} + +/** Read multiple bits from a 16-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param bitStart First bit position to read (0-15) + * @param length Number of bits to read (not more than 16) + * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (1 = success, 0 = failure, -1 = timeout) + */ +int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint32_t timeout) { + // 1101011001101001 read byte + // fedcba9876543210 bit numbers + // xxx args: bitStart=12, length=3 + // 010 masked + // -> 010 shifted + uint8_t count; + uint16_t w; + if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { + uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); + w &= mask; + w >>= (bitStart - length + 1); + *data = w; + } + return count; +} + +/** Read single byte from an 8-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param data Container for byte value read from device + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (true = success) + */ +int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint32_t timeout) { + return readBytes(devAddr, regAddr, 1, data, timeout); +} + +/** Read single word from a 16-bit device register. + * @param devAddr I2C slave device address + * @param regAddr Register regAddr to read from + * @param data Container for word value read from device + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Status of read operation (true = success) + */ +int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint32_t timeout) { + return readWords(devAddr, regAddr, 1, data, timeout); +} + +/** Read multiple bytes from an 8-bit device register. + * @param devAddr I2C slave device address + * @param regAddr First register regAddr to read from + * @param length Number of bytes to read + * @param data Buffer to store read data in + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Number of bytes read (-1 indicates failure) + */ +int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint32_t timeout) { + int8_t count = 0; + + i2c_write_blocking(i2c_default, devAddr, ®Addr, 1, true); + count = i2c_read_timeout_us(i2c_default, devAddr, data, length, false, timeout * 1000); + + return count; +} + +/** Read multiple bytes from an 8-bit device register. + * @param devAddr I2C slave device address + * @param regAddr First register regAddr to read from + * @param length Number of bytes to read + * @param data Buffer to store read data in + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Number of bytes read (-1 indicates failure) + */ +int8_t I2Cdev::read(uint8_t devAddr, uint8_t length, uint8_t *data, uint32_t timeout) { + int8_t count = 0; + + count = i2c_read_timeout_us(i2c_default, devAddr, data, length, false, timeout * 1000); + + return count; +} + +/** Read multiple words from a 16-bit device register. + * @param devAddr I2C slave device address + * @param regAddr First register regAddr to read from + * @param length Number of words to read + * @param data Buffer to store read data in + * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) + * @return Number of words read (-1 indicates failure) + */ +int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint32_t timeout) { + int8_t count = 0, j = 0; + uint8_t data_buf[length*2]; + + i2c_write_blocking(i2c_default, devAddr, ®Addr, 1, true); + count = i2c_read_timeout_us(i2c_default, devAddr, data_buf, length*2, false, timeout * 1000); + for(int i=0; i> 8; + data_buf[j+1] = data[i]; + j++; + } + status = i2c_write_blocking(i2c_default, devAddr, data_buf, new_len, false); + + return status; +} + +/** Default timeout value for read operations. + * Set this to 0 to disable timeout detection. + */ +uint32_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT; diff --git a/src/I2C_dev.h b/src/I2C_dev.h new file mode 100644 index 0000000..1397933 --- /dev/null +++ b/src/I2C_dev.h @@ -0,0 +1,73 @@ +// Raspberry Pi Pico port for: +// I2Cdev library collection - Main I2C device class +// Abstracts bit and byte I2C R/W functions into a convenient class +// 2013-06-05 by Jeff Rowberg +// +// Changelog: +// 2021-09-29 - Initial port release by Gino Ipóliti. + +/* ============================================ +I2Cdev device library code is placed under the MIT license +Copyright (c) 2013 Jeff Rowberg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +=============================================== +*/ + +#ifndef _I2CDEV_PICO_WRAPPER_H_ +#define _I2CDEV_PICO_WRAPPER_H_ + +#include +#include +#include +#include +#include "pico/stdlib.h" +#include "hardware/i2c.h" + +// 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];") +#define I2CDEV_DEFAULT_READ_TIMEOUT ((uint32_t)1000000) // RP2040 I2C functions with timeout use microseconds so we have to multiply by 10^3 + +class I2Cdev { + public: + I2Cdev(); + + static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); + static int8_t read(uint8_t devAddr, uint8_t length, uint8_t *data, uint32_t timeout); + + static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data); + static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data); + static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data); + static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data); + static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data); + static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data); + static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data); + static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data); + + static uint32_t readTimeout; + +}; + +#endif /* _I2CDEV_PICO_WRAPPER_H_ */ From 74ffabf82d75659ceea89225a8ca94e3c458e709 Mon Sep 17 00:00:00 2001 From: Eduardo Contreras Date: Thu, 4 Aug 2022 16:39:04 -0500 Subject: [PATCH 2/7] erase I2C dev --- src/I2C_dev.cpp | 349 ------------------------------------------------ src/I2C_dev.h | 73 ---------- 2 files changed, 422 deletions(-) delete mode 100644 src/I2C_dev.cpp delete mode 100644 src/I2C_dev.h diff --git a/src/I2C_dev.cpp b/src/I2C_dev.cpp deleted file mode 100644 index 2a8b85b..0000000 --- a/src/I2C_dev.cpp +++ /dev/null @@ -1,349 +0,0 @@ -// Raspberry Pi Pico port for: -// I2Cdev library collection - Main I2C device class -// Abstracts bit and byte I2C R/W functions into a convenient class -// 2013-06-05 by Jeff Rowberg -// -// Changelog: -// 2021-09-29 - Initial port release by Gino Ipóliti. - -/* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2013 Jeff Rowberg - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -=============================================== -*/ - -#include "I2C_dev.h" - -/** Default constructor. - */ -I2Cdev::I2Cdev() { -} - -/** Read a single bit from an 8-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param bitNum Bit position to read (0-7) - * @param data Container for single bit value - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (true = success) - */ -int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint32_t timeout) { - uint8_t b; - uint8_t count = readByte(devAddr, regAddr, &b, timeout); - *data = b & (1 << bitNum); - return count; -} - -/** Read a single bit from a 16-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param bitNum Bit position to read (0-15) - * @param data Container for single bit value - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (true = success) - */ -int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint32_t timeout) { - uint16_t b; - uint8_t count = readWord(devAddr, regAddr, &b, timeout); - *data = b & (1 << bitNum); - return count; -} - -/** Read multiple bits from an 8-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param bitStart First bit position to read (0-7) - * @param length Number of bits to read (not more than 8) - * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (true = success) - */ -int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint32_t timeout) { - // 01101001 read byte - // 76543210 bit numbers - // xxx args: bitStart=4, length=3 - // 010 masked - // -> 010 shifted - uint8_t count, b; - if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { - uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); - b &= mask; - b >>= (bitStart - length + 1); - *data = b; - } - return count; -} - -/** Read multiple bits from a 16-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param bitStart First bit position to read (0-15) - * @param length Number of bits to read (not more than 16) - * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (1 = success, 0 = failure, -1 = timeout) - */ -int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint32_t timeout) { - // 1101011001101001 read byte - // fedcba9876543210 bit numbers - // xxx args: bitStart=12, length=3 - // 010 masked - // -> 010 shifted - uint8_t count; - uint16_t w; - if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { - uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); - w &= mask; - w >>= (bitStart - length + 1); - *data = w; - } - return count; -} - -/** Read single byte from an 8-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param data Container for byte value read from device - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (true = success) - */ -int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint32_t timeout) { - return readBytes(devAddr, regAddr, 1, data, timeout); -} - -/** Read single word from a 16-bit device register. - * @param devAddr I2C slave device address - * @param regAddr Register regAddr to read from - * @param data Container for word value read from device - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Status of read operation (true = success) - */ -int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint32_t timeout) { - return readWords(devAddr, regAddr, 1, data, timeout); -} - -/** Read multiple bytes from an 8-bit device register. - * @param devAddr I2C slave device address - * @param regAddr First register regAddr to read from - * @param length Number of bytes to read - * @param data Buffer to store read data in - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Number of bytes read (-1 indicates failure) - */ -int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint32_t timeout) { - int8_t count = 0; - - i2c_write_blocking(i2c_default, devAddr, ®Addr, 1, true); - count = i2c_read_timeout_us(i2c_default, devAddr, data, length, false, timeout * 1000); - - return count; -} - -/** Read multiple bytes from an 8-bit device register. - * @param devAddr I2C slave device address - * @param regAddr First register regAddr to read from - * @param length Number of bytes to read - * @param data Buffer to store read data in - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Number of bytes read (-1 indicates failure) - */ -int8_t I2Cdev::read(uint8_t devAddr, uint8_t length, uint8_t *data, uint32_t timeout) { - int8_t count = 0; - - count = i2c_read_timeout_us(i2c_default, devAddr, data, length, false, timeout * 1000); - - return count; -} - -/** Read multiple words from a 16-bit device register. - * @param devAddr I2C slave device address - * @param regAddr First register regAddr to read from - * @param length Number of words to read - * @param data Buffer to store read data in - * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) - * @return Number of words read (-1 indicates failure) - */ -int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint32_t timeout) { - int8_t count = 0, j = 0; - uint8_t data_buf[length*2]; - - i2c_write_blocking(i2c_default, devAddr, ®Addr, 1, true); - count = i2c_read_timeout_us(i2c_default, devAddr, data_buf, length*2, false, timeout * 1000); - for(int i=0; i> 8; - data_buf[j+1] = data[i]; - j++; - } - status = i2c_write_blocking(i2c_default, devAddr, data_buf, new_len, false); - - return status; -} - -/** Default timeout value for read operations. - * Set this to 0 to disable timeout detection. - */ -uint32_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT; diff --git a/src/I2C_dev.h b/src/I2C_dev.h deleted file mode 100644 index 1397933..0000000 --- a/src/I2C_dev.h +++ /dev/null @@ -1,73 +0,0 @@ -// Raspberry Pi Pico port for: -// I2Cdev library collection - Main I2C device class -// Abstracts bit and byte I2C R/W functions into a convenient class -// 2013-06-05 by Jeff Rowberg -// -// Changelog: -// 2021-09-29 - Initial port release by Gino Ipóliti. - -/* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2013 Jeff Rowberg - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -=============================================== -*/ - -#ifndef _I2CDEV_PICO_WRAPPER_H_ -#define _I2CDEV_PICO_WRAPPER_H_ - -#include -#include -#include -#include -#include "pico/stdlib.h" -#include "hardware/i2c.h" - -// 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];") -#define I2CDEV_DEFAULT_READ_TIMEOUT ((uint32_t)1000000) // RP2040 I2C functions with timeout use microseconds so we have to multiply by 10^3 - -class I2Cdev { - public: - I2Cdev(); - - static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint32_t timeout=I2Cdev::readTimeout); - static int8_t read(uint8_t devAddr, uint8_t length, uint8_t *data, uint32_t timeout); - - static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data); - static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data); - static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data); - static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data); - static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data); - static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data); - static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data); - static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data); - - static uint32_t readTimeout; - -}; - -#endif /* _I2CDEV_PICO_WRAPPER_H_ */ From b4355116fa94468a4f2115345fe2507c0ddd3d15 Mon Sep 17 00:00:00 2001 From: Eduardo Contreras Date: Thu, 4 Aug 2022 16:40:41 -0500 Subject: [PATCH 3/7] add debug --- src/Electroniccats_PN7150.cpp | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 5cd5fdb..1686376 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -78,11 +78,17 @@ uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLe { uint32_t nmbrBytesWritten = 0; Wire.beginTransmission((uint8_t)_I2Caddress); //configura transmision - nmbrBytesWritten = Wire.write(txBuffer, (int)(txBufferLevel)); //carga en buffer + 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 + #ifdef DEBUG2 + Serial.println("[DEBUG] write data code = 0x"+String(resultCode,HEX)); + #endif return resultCode; } else @@ -93,25 +99,38 @@ uint8_t Electroniccats_PN7150::writeData(uint8_t txBuffer[], uint32_t txBufferLe uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const { - uint8_t bytesReceived; // keeps track of how many bytes we actually received + 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 - //I2Cdev::writeBytes(_I2Caddress, 0x00, 3, &bytesReceived); - //I2Cdev::readBytes(_I2Caddress, 0x00, 3, &bytesReceived); + //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(); + #ifdef DEBUG2 + for(int i=0;i<3;i++){ + Serial.println("[DEBUG] Byte["+String(i)+"] = 0x"+String(rxBuffer[i],HEX)); + } + #endif uint8_t payloadLength = rxBuffer[2]; if (payloadLength > 0) { 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(); + #ifdef DEBUG2 + Serial.println("[DEBUG] payload["+String(index)+"] = 0x"+String(rxBuffer[index],HEX)); + #endif index++; } index = 0; @@ -212,12 +231,12 @@ uint8_t Electroniccats_PN7150::connectNCI() gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; //0xROM_CODE_V gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; //0xFW_MAJOR_NO gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; //0xFW_MINOR_NO -#ifdef SerialUSB + Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); -#endif + return SUCCESS; } @@ -442,6 +461,9 @@ bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char Dat bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { + #ifdef DEBUG2 + Serial.println("[DEBUG] cardModeReceive exec"); + #endif bool status = NFC_ERROR; uint8_t Ans[MAX_NCI_FRAME_SIZE]; @@ -451,9 +473,9 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char /* Is data packet ? */ if ((rxBuffer[0] == 0x00) && (rxBuffer[1] == 0x00)) { -#ifdef SerialUSB + Serial.println(rxBuffer[2]); -#endif + *pDataSize = rxBuffer[2]; memcpy(pData, &rxBuffer[3], *pDataSize); status = NFC_SUCCESS; From 286896b8f477e3dc73434bc6d860acc7c94c7c91 Mon Sep 17 00:00:00 2001 From: Eduardo Contreras Date: Thu, 4 Aug 2022 16:41:01 -0500 Subject: [PATCH 4/7] fix bug on overflow writing answer --- src/Electroniccats_PN7150.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 1686376..cfcb4ac 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -467,7 +467,7 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char bool status = NFC_ERROR; uint8_t Ans[MAX_NCI_FRAME_SIZE]; - (void)writeData(Ans, sizeof(Ans)); + (void)writeData(Ans, 255); getMessage(2000); /* Is data packet ? */ From c8e6a196f17b26044d1b306318b28e3d8e16741e Mon Sep 17 00:00:00 2001 From: Eduardo Contreras Date: Thu, 4 Aug 2022 16:41:08 -0500 Subject: [PATCH 5/7] erase include --- src/Electroniccats_PN7150.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index dab2734..a38dc7a 100755 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -27,7 +27,6 @@ #include // Credits Brian "nox771" : see https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3 #else #include -#include #if defined(__AVR__) || defined(__i386__) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32) #define WIRE Wire #else // Arduino Due From 2047158bb63516d8881ddc0c9aa51f735102daeb Mon Sep 17 00:00:00 2001 From: Sabas Date: Fri, 5 Aug 2022 12:16:32 -0500 Subject: [PATCH 6/7] add githubaction support rp2040 and remove AVR --- .github/workflows/LibraryBuild.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml index 2e954cb..5bfd5e9 100644 --- a/.github/workflows/LibraryBuild.yml +++ b/.github/workflows/LibraryBuild.yml @@ -28,22 +28,16 @@ jobs: # You may add a suffix behind the fqbn with "|" to specify one board for e.g. different compile options like arduino:avr:uno|trace ############################################################################################################# arduino-boards-fqbn: - - arduino:avr:uno - - arduino:avr:leonardo - arduino:samd:nano_33_iot - arduino:mbed:envie_m7 - esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 + - rp2040:generic # Specify parameters for each board. # Parameters can be: platform-url, examples-exclude and examples-build-properties # With examples-exclude you may exclude specific examples for a board. Use a space separated list. ############################################################################################################# include: - - arduino-boards-fqbn: arduino:avr:uno - sketches-exclude: MifareClassic_write_block,DetectTags,MifareClassic_read_block - - - arduino-boards-fqbn: arduino:avr:leonardo - - arduino-boards-fqbn: arduino:samd:nano_33_iot - arduino-boards-fqbn: arduino:mbed:envie_m7 @@ -51,6 +45,9 @@ jobs: - arduino-boards-fqbn: esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 platform-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json + - arduino-boards-fqbn: rp2040:generic + platform-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json + # Do not cancel all jobs / architectures if one job fails fail-fast: false From 189e7a451ca438d6bac1cff8944118ca6323b503 Mon Sep 17 00:00:00 2001 From: Sabas Date: Fri, 5 Aug 2022 12:21:13 -0500 Subject: [PATCH 7/7] rp2040 githubaction --- .github/workflows/LibraryBuild.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml index 5bfd5e9..769309b 100644 --- a/.github/workflows/LibraryBuild.yml +++ b/.github/workflows/LibraryBuild.yml @@ -31,7 +31,7 @@ jobs: - arduino:samd:nano_33_iot - arduino:mbed:envie_m7 - esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 - - rp2040:generic + - rp2040:rp2040:generic # Specify parameters for each board. # Parameters can be: platform-url, examples-exclude and examples-build-properties @@ -45,7 +45,7 @@ jobs: - arduino-boards-fqbn: esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 platform-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json - - arduino-boards-fqbn: rp2040:generic + - arduino-boards-fqbn: rp2040:rp2040:generic platform-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json # Do not cancel all jobs / architectures if one job fails