diff --git a/CHANGELOG.md b/CHANGELOG.md index 4719086..1234697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,42 +6,63 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.2.5] - 2023-07-31 + +- add ESP32 HSPI / VSPI support (Kudo's to Alex Uta, PR #22) + - add **performance_0.2.4.md** for ESP32 +- add example **MCP23S17_two_SELECT.ino** (#19) +- add example **MCP23S17_two_ADDRESS.ino** (#19) +- add **void enableHardwareAddress()** (#19) +- add **void disableHardwareAddress()** (#19) +- add keywords.txt (#20) +- update readme.md +- minor edits + + ## [0.2.4] - 2023-02-04 + - update readme.md - update GitHub actions - update license 2023 - ## [0.2.3] - 2022-10-20 + - add CHANGELOG.md - add **enableControlRegister(uint8_t mask)** - add **disableControlRegister(uint8_t mask)** - add mcp23S17_registers.h )moved register defines in one place. ## [0.2.2] - 2022-09-28 + - optimize digitalWrite - most used one only. ## [0.2.1] - 2022-06-29 + - add SPIClass as parameter for constructor (See #10) - redo constructors. - add getAddress() + optimized (_address << 1) - update readme.md ## [0.2.0] - 2022-06-28 + - fix #10 incorrect mask ---- ## [0.1.3] - 2022-04-13 + - fix compiling for NANO33 BLE ## [0.1.2] - 2022-01-12 + - change the URL for library manager ## [0.1.1] - 2022-01-10 + - add 16 bit interface ## [0.1.0] - 2021-12-30 + - initial version (a 2019 version did not make it) diff --git a/MCP23S17.cpp b/MCP23S17.cpp index 3920b53..5de982d 100644 --- a/MCP23S17.cpp +++ b/MCP23S17.cpp @@ -1,7 +1,7 @@ // // FILE: MCP23S17.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.2.4 +// VERSION: 0.2.5 // PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander // DATE: 2021-12-30 // URL: https://github.com/RobTillaart/MCP23S17 @@ -595,6 +595,62 @@ void MCP23S17::disableControlRegister(uint8_t mask) } +void MCP23S17::enableHardwareAddress() +{ + enableControlRegister(MCP23S17_IOCR_HAEN); +} + + +void MCP23S17::disableHardwareAddress() +{ + disableControlRegister(MCP23S17_IOCR_HAEN); +} + + +#if defined(ESP32) + +void MCP23S17::selectHSPI() +{ + _useHSPI = true; +} + + +void MCP23S17::selectVSPI() +{ + _useHSPI = false; +} + + +bool MCP23S17::usesHSPI() +{ + return _useHSPI; +} + + +bool MCP23S17::usesVSPI() +{ + return !_useHSPI; +} + + +void MCP23S17::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select) +{ + _clock = clk; + _dataOut = mosi; + _dataIn = miso; + _select = select; + pinMode(_select, OUTPUT); + digitalWrite(_select, HIGH); + + _mySPI->end(); // disable old SPI + + _mySPI->begin(clk, miso, mosi, select); // enable new pins +} + +#endif + + + //////////////////////////////////////////////////// // // PRIVATE diff --git a/MCP23S17.h b/MCP23S17.h index 79ced0f..3502b17 100644 --- a/MCP23S17.h +++ b/MCP23S17.h @@ -2,7 +2,7 @@ // // FILE: MCP23S17.h // AUTHOR: Rob Tillaart -// VERSION: 0.2.4 +// VERSION: 0.2.5 // PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander // DATE: 2021-12-30 // URL: https://github.com/RobTillaart/MCP23S17 @@ -12,7 +12,8 @@ #include "SPI.h" #include "MCP23S17_registers.h" -#define MCP23S17_LIB_VERSION (F("0.2.4")) + +#define MCP23S17_LIB_VERSION (F("0.2.5")) // ERROR CODES #define MCP23S17_OK 0x00 @@ -40,11 +41,11 @@ class MCP23S17 bool begin(); bool isConnected(); - uint8_t getAddress(); // typically returns 0x00 + uint8_t getAddress(); // default returns 0x00 // single pin interface - // mode = INPUT, OUTPUT or INPUT_PULLUP (==INPUT) + // mode: 0 = OUTPUT, 1 = INPUT, 1 = INPUT_PULLUP (==INPUT) bool pinMode(uint8_t pin, uint8_t mode); bool digitalWrite(uint8_t pin, uint8_t value); uint8_t digitalRead(uint8_t pin); @@ -91,17 +92,22 @@ class MCP23S17 // set/clear IOCR bit fields (0.2.3 experimental) void enableControlRegister(uint8_t mask); void disableControlRegister(uint8_t mask); + // 0.2.5 experimental + void enableHardwareAddress(); + void disableHardwareAddress(); // ESP32 specific - #if defined(ESP32) - void selectHSPI() { _useHSPI = true; }; - void selectVSPI() { _useHSPI = false; }; - bool usesHSPI() { return _useHSPI; }; - bool usesVSPI() { return !_useHSPI; }; +#if defined(ESP32) + + void selectHSPI(); + void selectVSPI(); + bool usesHSPI(); + bool usesVSPI(); - // to overrule ESP32 default hardware pins + // to overrule the ESP32s default hardware pins void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select); - #endif + +#endif private: diff --git a/README.md b/README.md index 0a47f99..2d12907 100644 --- a/README.md +++ b/README.md @@ -61,12 +61,28 @@ The two hardware constructors allow to call 4 different constructors. ``` -#### sharing select lines +#### Sharing SELECT lines -(not tested) -Technically two chips could use the same select pin and a different address. -The constructors would allow to setup such a configuration. -I assume that this is less used and IMHO not recommended. +(verified in #19) +Technically two chips could use the same SELECT pin and a different address. +Since 0.2.5 the constructors allow to setup such a configuration. +The added value is that one can use up to 8 devices (= 128 IO lines) with only +four lines (MISO, MOSI, CLOCK, SELECT). + +I assume that this configuration is less used and IMHO not recommended. +NB it is more difficult to detect which device is selected when debugging. + +To use the hardware addresses the Hardware Address ENable register must be set. +See datasheet 3.3.2 ADDRESSING SPI DEVICES, need to set IOCON.HAEN. + +The library supports two ways: +```cpp +MCP.enableControlRegister(MCP23S17_IOCR_HAEN); // or 0x08 +or +MCP.enableHardwareAddress(); // 0.2.5 version and up +``` + +See also **IO Control Register** section below. ### Single pin interface @@ -112,7 +128,7 @@ Returns true if successful. Returns true if successful. -### IO Control Register +### IO Control Register Since 0.2.3 the library supports setting bit fields in the IO control register. Read the datasheet carefully! @@ -133,6 +149,44 @@ Read the datasheet carefully! | MCP23S17_IOCR_NI | 0x01 | Not implemented. +Two dedicated functions are added since 0.2.5. + +- **void enableHardwareAddress()** set IOCR_HAEN bit. +- **void disableHardwareAddress()** clear IOCR_HAEN bit. + + +### ESP32 HW SPI port selection + +This functionality is new in 0.2.5. + +- **void selectHSPI()** in case hardware SPI, the ESP32 has two options HSPI and VSPI. +- **void selectVSPI()** see above. +- **bool usesHSPI()** returns true if HSPI is used. +- **bool usesVSPI()** returns true if VSPI is used. + +The **selectVSPI()** or the **selectHSPI()** needs to be called +BEFORE the **begin()** function. + + +#### Experimental + +- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)** +overrule GPIO pins of ESP32 for hardware SPI. +Needs to be called AFTER the **begin()** function. + +```cpp +void setup() +{ + MCP.selectVSPI(); + MCP.begin(15); + MCP.setGPIOpins(CLK, MISO, MOSI, SELECT); // SELECT should match the param of begin() +} +``` + +This interface can change in the future as the **select** pin is known +in the code. + + ### Error codes If one of the above functions return false, there might be an error. @@ -163,20 +217,27 @@ See examples. #### Should +- buy additional hardware - keep functional in sync with MCP23017_RT +- test with multiple devices. + - multi SELECT lines +- add example with interrupts + - test +- IOCON.HAEN, Hardware Address ENable. + - should this be enabled in **begin()** by default? 0.3.0 + - check address range in constructor. #### Could -- check need for writing in all functions (Polarity / pullup) +- check need for writing in all functions (Polarity / Pull-up) - check if bit mask changes. - what is performance gain vs footprint? -- implement ESP32 specific support in begin() - - see MCP_ADC.begin() - - SW_SPI is roughly equal in performance as HW SPI on ESP32. - investigate and reimplement the INPUT_PULLUP for pinMode() ? +- RP2040 support for SPI, setGPIOpins() etc + - See MCP_DAC +- AVR software SPI optimize 0.3.0 + - dao and clock - see fastShiftOut. #### Wont -- check address range in constructor. - diff --git a/examples/MCP23S17_digitalRead/MCP23S17_digitalRead.ino b/examples/MCP23S17_digitalRead/MCP23S17_digitalRead.ino index 0868dc9..a702f7c 100644 --- a/examples/MCP23S17_digitalRead/MCP23S17_digitalRead.ino +++ b/examples/MCP23S17_digitalRead/MCP23S17_digitalRead.ino @@ -1,8 +1,8 @@ // // FILE: MCP23S17_digitalRead.ino // AUTHOR: Rob Tillaart -// DATE: 2021-12-30 -// PUPROSE: test MCP23017 library +// PURPOSE: test MCP23S17 library +// URL: https://github.com/RobTillaart/MCP23S17 #include "MCP23S17.h" @@ -25,7 +25,7 @@ void setup() rv = MCP.begin(); Serial.println(rv ? "true" : "false"); - rv = MCP.pinMode8(0, 0xFF); // CHECK + rv = MCP.pinMode8(0, 0xFF); Serial.println(rv); rv = MCP.pinMode8(1, 0xFF); Serial.println(rv); @@ -62,5 +62,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/MCP23S17_digitalWrite/MCP23S17_digitalWrite.ino b/examples/MCP23S17_digitalWrite/MCP23S17_digitalWrite.ino index bb064e4..21bb76b 100644 --- a/examples/MCP23S17_digitalWrite/MCP23S17_digitalWrite.ino +++ b/examples/MCP23S17_digitalWrite/MCP23S17_digitalWrite.ino @@ -1,14 +1,14 @@ // // FILE: MCP23S17_digitalWrite.ino // AUTHOR: Rob Tillaart -// DATE: 2021-12-30 -// PUPROSE: test MCP23017 library +// PURPOSE: test MCP23S17 library +// URL: https://github.com/RobTillaart/MCP23S17 #include "MCP23S17.h" #include "SPI.h" -MCP23S17 MCP(10, 5, 6, 7); // SW SPI address 0x00 +MCP23S17 MCP(10, 5, 6, 7); // SW SPI address 0x00 void setup() @@ -24,14 +24,14 @@ void setup() Serial.println(b ? "true" : "false"); delay(100); - MCP.pinMode8(0, 0x00); // 0 = output , 1 = input + MCP.pinMode8(0, 0x00); // 0 = output , 1 = input MCP.pinMode8(1, 0x00); Serial.println("TEST digitalWrite(0)"); delay(100); for (int i = 0; i < 16; i++) { - MCP.digitalWrite(0, i % 2); // alternating HIGH/LOW + MCP.digitalWrite(0, i % 2); // alternating HIGH/LOW Serial.print(i % 2); Serial.print(' '); delay(100); @@ -43,7 +43,7 @@ void setup() delay(100); for (int pin = 0; pin < 16; pin++) { - MCP.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW + MCP.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW Serial.print(1 - pin % 2); Serial.print(' '); delay(100); @@ -70,5 +70,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/MCP23S17_performance/MCP23S17_performance.ino b/examples/MCP23S17_performance/MCP23S17_performance.ino index 2362cf4..73656f8 100644 --- a/examples/MCP23S17_performance/MCP23S17_performance.ino +++ b/examples/MCP23S17_performance/MCP23S17_performance.ino @@ -1,8 +1,8 @@ // // FILE: MCP23S17_performance.ino // AUTHOR: Rob Tillaart -// DATE: 2021-12-30 -// PUPROSE: test MCP23017 library +// PURPOSE: test MCP23S17 library +// URL: https://github.com/RobTillaart/MCP23S17 #include "MCP23S17.h" @@ -36,7 +36,7 @@ void setup() MCP.setSPIspeed(10000000); - MCP.pinMode8(0, 0x00); // 0 = output , 1 = input + MCP.pinMode8(0, 0x00); // 0 = output , 1 = input MCP.pinMode8(1, 0x00); Serial.println("\ntime in microseconds\n"); @@ -48,7 +48,7 @@ void setup() start = micros(); for (int i = 0; i < 16; i++) { - MCP.digitalWrite(0, i & 0x01); // alternating HIGH/LOW + MCP.digitalWrite(0, i & 0x01); // alternating HIGH/LOW } stop = micros(); Serial.println((stop - start) / 16.0); @@ -59,7 +59,7 @@ void setup() start = micros(); for (int pin = 0; pin < 16; pin++) { - MCP.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW + MCP.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW } stop = micros(); Serial.println((stop - start) / 16.0); @@ -79,13 +79,13 @@ void setup() ////////////////////////////////////////////////// // - // write8 read8 interface + // write8 read8 interface // Serial.print("TEST write8(port, mask):\t"); delay(100); start = micros(); - MCP.write8(0, 0xAA); // alternating HIGH/LOW - MCP.write8(1, 0xAA); // alternating HIGH/LOW + MCP.write8(0, 0xAA); // alternating HIGH/LOW + MCP.write8(1, 0xAA); // alternating HIGH/LOW stop = micros(); Serial.println((stop - start) / 2.0); @@ -102,12 +102,12 @@ void setup() ////////////////////////////////////////////////// // - // write16 read16 interface + // write16 read16 interface // Serial.print("TEST write16(mask):\t"); delay(100); start = micros(); - MCP.write16(0xAAAA); // alternating HIGH/LOW + MCP.write16(0xAAAA); // alternating HIGH/LOW stop = micros(); Serial.println((stop - start) / 2.0); @@ -120,7 +120,7 @@ void setup() Serial.println((stop - start) / 2.0); Serial.println(); - // keep compiler happy + // keep compiler happy Serial.print("VAL1:\t"); Serial.println(val1); Serial.print("VAL8:\t"); @@ -137,4 +137,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/MCP23S17_performance/performance_0.2.4.md b/examples/MCP23S17_performance/performance_0.2.4.md index 69a2a4b..b9f6eaa 100644 --- a/examples/MCP23S17_performance/performance_0.2.4.md +++ b/examples/MCP23S17_performance/performance_0.2.4.md @@ -10,16 +10,19 @@ Max clock frequency 10 MHz, ESP32 WROVER Module | Action | SW SPI | HW 1 MHz | HW 2 MHz | HW 4 MHz | HW 8 MHz | HW 10 MHz | notes | |:------------------------------|--------:|---------:|---------:|---------:|---------:|:---------:|:---------:| -| TEST digitalWrite(0, value) | 48.560 | 85.12 | 57.00 | 43.69 | 36.81 | 35.38 | -| TEST digitalWrite(pin, value) | 47.750 | 84.50 | 41.94 | 42.69 | 26.62 | 25.62 | -| TEST digitalRead(pin) | 31.940 | 43.75 | 29.31 | 22.37 | 18.75 | 18.06 | -| | | | | | | | -| TEST write8(port, mask) | 34.000 | 45.00 | 30.50 | 23.50 | 20.00 | 19.50 | -| TEST read8(port) | 34.500 | 46.00 | 31.50 | 24.50 | 21.00 | 20.50 | -| TEST write16(mask) | 32.500 | 44.00 | 29.50 | 22.50 | 19.00 | 18.00 | since 0.1.1 -| TEST read16() | 33.000 | 43.50 | 29.00 | 22.00 | 19.00 | 18.50 | since 0.1.1 +| TEST digitalWrite(0, value) | 48.560 | 85.12 | 57.00 | 43.69 | 36.81 | 35.38 | +| TEST digitalWrite(pin, value) | 47.750 | 84.50 | 41.94 | 42.69 | 26.62 | 25.62 | +| TEST digitalRead(pin) | 31.940 | 43.75 | 29.31 | 22.37 | 18.75 | 18.06 | +| | | | | | | | +| TEST write8(port, mask) | 34.000 | 45.00 | 30.50 | 23.50 | 20.00 | 19.50 | +| TEST read8(port) | 34.500 | 46.00 | 31.50 | 24.50 | 21.00 | 20.50 | +| TEST write16(mask) | 32.500 | 44.00 | 29.50 | 22.50 | 19.00 | 18.00 | since 0.1.1 +| TEST read16() | 33.000 | 43.50 | 29.00 | 22.00 | 19.00 | 18.50 | since 0.1.1 ### Notes For this test I used the ESP32 WROVER Module (ESP32-WROVER-IE-N16R8), ESP32-DEVKITS-R and MCP23S17T-E/ML (28QFN) Software SPI : D2, D4 and D5 + +See: https://github.com/RobTillaart/MCP23S17/pull/22 + diff --git a/examples/MCP23S17_test/MCP23S17_test.ino b/examples/MCP23S17_test/MCP23S17_test.ino index e6f6f33..e43af68 100644 --- a/examples/MCP23S17_test/MCP23S17_test.ino +++ b/examples/MCP23S17_test/MCP23S17_test.ino @@ -1,8 +1,8 @@ // // FILE: MCP23S17_test.ino // AUTHOR: Rob Tillaart -// DATE: 2022-06-28 -// PUPROSE: test MCP23017 library +// PURPOSE: test MCP23S17 library +// URL: https://github.com/RobTillaart/MCP23S17 #include "MCP23S17.h" @@ -26,7 +26,7 @@ void setup() bool b = MCP.begin(); Serial.println(b ? "true" : "false"); - MCP.pinMode8(0, 0x00); // 0 = output , 1 = input + MCP.pinMode8(0, 0x00); // 0 = output, 1 = input MCP.pinMode8(1, 0x00); @@ -148,4 +148,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- + diff --git a/examples/MCP23S17_test_connection/MCP23S17_test_connection.ino b/examples/MCP23S17_test_connection/MCP23S17_test_connection.ino index ce570e9..fb3ebf0 100644 --- a/examples/MCP23S17_test_connection/MCP23S17_test_connection.ino +++ b/examples/MCP23S17_test_connection/MCP23S17_test_connection.ino @@ -1,17 +1,20 @@ // // FILE: MCP23S17_test_connection.ino // AUTHOR: Rob Tillaart -// DATE: 2022-06-28 -// PUPROSE: test MCP23017 library +// PURPOSE: test MCP23S17 library +// URL: https://github.com/RobTillaart/MCP23S17 // // see issue #13 library + #include "MCP23S17.h" #include "SPI.h" + // MCP23S17 MCP(10, 12, 11, 13); // SW SPI address 0x00 MCP23S17 MCP(10, 4); // HW SPI address 4 + void setup() { Serial.begin(115200); @@ -24,7 +27,6 @@ void setup() SPI.begin(); MCP.begin(); - // test connected int c = testConnection(MCP); Serial.print("connection: "); Serial.println(c); @@ -40,18 +42,18 @@ int testConnection(MCP23S17 & mcp) { uint16_t magic_test_number = 0xABCD; - // Read the current polarity config to restore later + // Read the current polarity configuration to restore later uint16_t old_value; if (! mcp.getPolarity16(old_value)) return -1; - + // Write the magic number to polarity register if (! mcp.setPolarity16(magic_test_number)) return -2; // Read back the magic number from polarity register uint16_t temp; if (! mcp.getPolarity16(temp)) return -3; - - // Write old config to polarity register + + // Write old configuration to polarity register if (! mcp.setPolarity16(old_value)) return -4; // Check the magic connection test @@ -60,4 +62,5 @@ int testConnection(MCP23S17 & mcp) return 0; // OK } -// -- END OF FILE -- + +// -- END OF FILE -- diff --git a/examples/MCP23S17_two_ADDRESS/MCP23S17_two_ADDRESS.ino b/examples/MCP23S17_two_ADDRESS/MCP23S17_two_ADDRESS.ino new file mode 100644 index 0000000..3192932 --- /dev/null +++ b/examples/MCP23S17_two_ADDRESS/MCP23S17_two_ADDRESS.ino @@ -0,0 +1,100 @@ +// +// FILE: MCP23S17_two_ADDRESS.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo two MCP23S17 devices +// URL: https://github.com/RobTillaart/MCP23S17 +// +// #19, experimental, not tested wirth hardware yet + + +#include "MCP23S17.h" +#include "SPI.h" + + +MCP23S17 MCP_A(10, 12, 11, 13, 0); // SW SPI, address 0 +MCP23S17 MCP_B(10 , 12, 11, 13, 1); // SW SPI, address 1 + +// MCP23S17 MCP_A(10, 0); // HW SPI, address 0 +// MCP23S17 MCP_B(10, 1); // HW SPI, address 1 + + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.print("MCP23S17_LIB_VERSION: "); + Serial.println(MCP23S17_LIB_VERSION); + Serial.println(); + delay(100); + + SPI.begin(); + MCP_A.begin(); + MCP_B.begin(); + MCP_A.enableHardwareAddress(); + MCP_B.enableHardwareAddress(); + + // test connected + int c = testConnection(MCP_A); + Serial.print("Connect A: "); + Serial.println(c); + c = testConnection(MCP_B); + Serial.print("Connect B: "); + Serial.println(c); + + // all pins OUTPUT LOW + MCP_A.pinMode16(0); + MCP_A.write16(0x0000); + MCP_B.pinMode16(0); + MCP_B.write16(0x0000); +} + + +void loop() +{ + int x = random(32); + if (x < 16) + { + MCP_A.digitalWrite(x, HIGH); + delay(100); + MCP_A.digitalWrite(x, LOW); + } + else + { + x -= 16; + MCP_B.digitalWrite(x, HIGH); + delay(100); + MCP_B.digitalWrite(x, LOW); + } +} + + + +// +// the connection test tries to write a magic number to a register +// and read it back. If it is the same it is assumed to be connected +// +int testConnection(MCP23S17 & mcp) +{ + uint16_t magic_test_number = 0xABCD; + + // Read the current polarity config to restore later + uint16_t old_value; + if (! mcp.getPolarity16(old_value)) return -1; + + // Write the magic number to polarity register + if (! mcp.setPolarity16(magic_test_number)) return -2; + + // Read back the magic number from polarity register + uint16_t temp; + if (! mcp.getPolarity16(temp)) return -3; + + // Write old config to polarity register + if (! mcp.setPolarity16(old_value)) return -4; + + // Check the magic connection test + if (temp != magic_test_number) return -5; + + return 0; // OK +} + +// -- END OF FILE -- diff --git a/examples/MCP23S17_two_SELECT/MCP23S17_two_SELECT.ino b/examples/MCP23S17_two_SELECT/MCP23S17_two_SELECT.ino new file mode 100644 index 0000000..e9e3e2f --- /dev/null +++ b/examples/MCP23S17_two_SELECT/MCP23S17_two_SELECT.ino @@ -0,0 +1,98 @@ +// +// FILE: MCP23S17_two_SELECT.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo two MCP23S17 devices +// URL: https://github.com/RobTillaart/MCP23S17 +// +// #19, experimental, not tested wirth hardware yet + + +#include "MCP23S17.h" +#include "SPI.h" + + +MCP23S17 MCP_A(10, 12, 11, 13); // SW SPI +MCP23S17 MCP_B(9 , 12, 11, 13); // SW SPI, different select pin! + +// MCP23S17 MCP_A(10); // HW SPI +// MCP23S17 MCP_B(9); // HW SPI, different select pin! + + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.print("MCP23S17_LIB_VERSION: "); + Serial.println(MCP23S17_LIB_VERSION); + Serial.println(); + delay(100); + + SPI.begin(); + MCP_A.begin(); + MCP_B.begin(); + + // test connected + int c = testConnection(MCP_A); + Serial.print("Connect A: "); + Serial.println(c); + c = testConnection(MCP_B); + Serial.print("Connect B: "); + Serial.println(c); + + // all pins OUTPUT LOW + MCP_A.pinMode16(0); + MCP_A.write16(0x0000); + MCP_B.pinMode16(0); + MCP_B.write16(0x0000); +} + + +void loop() +{ + int x = random(32); + if (x < 16) + { + MCP_A.digitalWrite(x, HIGH); + delay(100); + MCP_A.digitalWrite(x, LOW); + } + else + { + x -= 16; + MCP_B.digitalWrite(x, HIGH); + delay(100); + MCP_B.digitalWrite(x, LOW); + } +} + + + +// +// the connection test tries to write a magic number to a register +// and read it back. If it is the same it is assumed to be connected +// +int testConnection(MCP23S17 & mcp) +{ + uint16_t magic_test_number = 0xABCD; + + // Read the current polarity config to restore later + uint16_t old_value; + if (! mcp.getPolarity16(old_value)) return -1; + + // Write the magic number to polarity register + if (! mcp.setPolarity16(magic_test_number)) return -2; + + // Read back the magic number from polarity register + uint16_t temp; + if (! mcp.getPolarity16(temp)) return -3; + + // Write old config to polarity register + if (! mcp.setPolarity16(old_value)) return -4; + + // Check the magic connection test + if (temp != magic_test_number) return -5; + + return 0; // OK +} + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..5f01ea2 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,69 @@ +# Syntax Colouring Map For MCP23S17 + +# Data types (KEYWORD1) +MCP23S17 KEYWORD1 + + +# Methods and Functions (KEYWORD2) +begin KEYWORD2 +isConnected KEYWORD2 +getAddress KEYWORD2 + +pinMode KEYWORD2 +digitalWrite KEYWORD2 +digitalRead KEYWORD2 + +setPolarity KEYWORD2 +getPolarity KEYWORD2 +setPullup KEYWORD2 +getPullup KEYWORD2 + +pinMode8 KEYWORD2 +write8 KEYWORD2 +read8 KEYWORD2 + +setPolarity8 KEYWORD2 +getPolarity8 KEYWORD2 +setPullup8 KEYWORD2 +getPullup8 KEYWORD2 + +pinMode16 KEYWORD2 +write16 KEYWORD2 +read16 KEYWORD2 + +setPolarity16 KEYWORD2 +getPolarity16 KEYWORD2 +setPullup16 KEYWORD2 +getPullup16 KEYWORD2 + +setSPIspeed KEYWORD2 +getSPIspeed KEYWORD2 +usesHWSPI KEYWORD2 +lastError KEYWORD2 + +enableControlRegister KEYWORD2 +disableControlRegister KEYWORD2 +enableHardwareAddress KEYWORD2 +disableHardwareAddress KEYWORD2 + +selectHSPI KEYWORD2 +selectVSPI KEYWORD2 +usesHSPI KEYWORD2 +usesVSPI KEYWORD2 +setGPIOpins KEYWORD2 + + +# Instances (KEYWORD2) + + +# Constants (LITERAL1) +MCP23S17_LIB_VERSION LITERAL1 + +MCP23S17_OK LITERAL1 +MCP23S17_PIN_ERROR LITERAL1 +MCP23S17_SPI_ERROR LITERAL1 +MCP23S17_VALUE_ERROR LITERAL1 +MCP23S17_PORT_ERROR LITERAL1 +MCP23S17_REGISTER_ERROR LITERAL1 +MCP23S17_INVALID_READ LITERAL1 + diff --git a/library.json b/library.json index 97925e0..c54dc62 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/MCP23S17.git" }, - "version": "0.2.4", + "version": "0.2.5", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 8601081..4ee9ec5 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=MCP23S17 -version=0.2.4 +version=0.2.5 author=Rob Tillaart maintainer=Rob Tillaart -sentence=Arduino library for SPI MCP23S17 16 channel port expander 16 IO-lines +sentence=Arduino library for SPI MCP23S17 16 channel port expander 16 IO-lines paragraph= setPolarity, setPullup category=Signal Input/Output url=https://github.com/RobTillaart/MCP23S17