From 74b2463953cb98d09bdb7869e83b7c32e6807580 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 18 Oct 2019 16:09:21 +0700 Subject: [PATCH 1/2] add second hardware i2c as Wire1 for cplayground bluefruit --- libraries/Wire/Wire.h | 13 +++++-------- libraries/Wire/Wire_nRF52.cpp | 14 +++++++++++++- variants/circuitplayground_nrf52840/variant.h | 9 ++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index f2af3a41..39a353ec 100755 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -34,15 +34,9 @@ class TwoWire : public Stream { public: -#if defined(NRF52) || defined(NRF52_SERIES) TwoWire(NRF_TWIM_Type * p_twim, NRF_TWIS_Type * p_twis, IRQn_Type IRQn, uint8_t pinSDA, uint8_t pinSCL); -#else - TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL); -#endif void begin(); -#if defined(NRF52) || defined(NRF52_SERIES) void begin(uint8_t); -#endif void end(); void setClock(uint32_t); @@ -60,11 +54,10 @@ class TwoWire : public Stream virtual int read(void); virtual int peek(void); virtual void flush(void); -#if defined(NRF52) || defined(NRF52_SERIES) + void onReceive(void(*)(int)); void onRequest(void(*)(void)); void onService(void); -#endif using Print::write; @@ -105,4 +98,8 @@ class TwoWire : public Stream extern TwoWire Wire; #endif +#if WIRE_INTERFACES_COUNT > 1 +extern TwoWire Wire1; +#endif + #endif diff --git a/libraries/Wire/Wire_nRF52.cpp b/libraries/Wire/Wire_nRF52.cpp index e593cedc..b128d393 100755 --- a/libraries/Wire/Wire_nRF52.cpp +++ b/libraries/Wire/Wire_nRF52.cpp @@ -392,9 +392,9 @@ void TwoWire::onService(void) } } +#if WIRE_INTERFACES_COUNT > 0 TwoWire Wire(NRF_TWIM0, NRF_TWIS0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, PIN_WIRE_SDA, PIN_WIRE_SCL); -#if WIRE_INTERFACES_COUNT > 0 extern "C" { void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) @@ -404,4 +404,16 @@ extern "C" } #endif +#if WIRE_INTERFACES_COUNT > 1 +TwoWire Wire1(NRF_TWIM1, NRF_TWIS1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, PIN_WIRE1_SDA, PIN_WIRE1_SCL); + +extern "C" +{ + void SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler(void) + { + Wire1.onService(); + } +} #endif + +#endif // NRF52_SERIES diff --git a/variants/circuitplayground_nrf52840/variant.h b/variants/circuitplayground_nrf52840/variant.h index 5f3dd2ff..c7fdd8cc 100644 --- a/variants/circuitplayground_nrf52840/variant.h +++ b/variants/circuitplayground_nrf52840/variant.h @@ -112,10 +112,13 @@ static const uint8_t SCK = PIN_SPI_SCK ; /* * Wire Interfaces */ -#define WIRE_INTERFACES_COUNT 1 +#define WIRE_INTERFACES_COUNT 2 -#define PIN_WIRE_SDA (28) -#define PIN_WIRE_SCL (26) +#define PIN_WIRE_SDA (2) +#define PIN_WIRE_SCL (3) + +#define PIN_WIRE1_SDA (28) +#define PIN_WIRE1_SCL (26) // QSPI Pins #define PIN_QSPI_SCK 29 From e43abed51c76a704b1a5aa599537243a4c287163 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 21 Oct 2019 10:35:12 +0700 Subject: [PATCH 2/2] add i2c master scan --- .../Wire/examples/master_scan/master_scan.ino | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 libraries/Wire/examples/master_scan/master_scan.ino diff --git a/libraries/Wire/examples/master_scan/master_scan.ino b/libraries/Wire/examples/master_scan/master_scan.ino new file mode 100644 index 00000000..1567477d --- /dev/null +++ b/libraries/Wire/examples/master_scan/master_scan.ino @@ -0,0 +1,28 @@ +#include + +TwoWire *wi = &Wire; +//TwoWire *wi = &Wire1; + +void setup() +{ + Serial.begin(115200); // start serial for output + while ( !Serial ) delay(10); // for nrf52840 with native usb + wi->begin(); // join i2c bus (address optional for master) +} + +void loop() +{ + Serial.println("Scanning address from 0 to 127"); + for (int addr = 1; addr < 128; addr++) + { + wi->beginTransmission(addr); + if ( 0 == wi->endTransmission() ) + { + Serial.print("Found: 0x"); + Serial.print(addr, HEX); + Serial.println(); + } + } + + delay(5000); +}