mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.2 MCP23S17
This commit is contained in:
parent
3d573a8dcd
commit
19572c296e
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: MCP23S17.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.1
|
||||
// VERSION: 0.2.2
|
||||
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
|
||||
// DATE: 2021-12-30
|
||||
// URL: https://github.com/RobTillaart/MCP23S17
|
||||
@ -18,6 +18,7 @@
|
||||
// redo constructors.
|
||||
// add getAddress() + optimized (_address << 1)
|
||||
// update readme.md
|
||||
// 0.2.2 2022-09-28 optimize digitalWrite - most used one only.
|
||||
|
||||
|
||||
|
||||
@ -209,6 +210,7 @@ bool MCP23S17::digitalWrite(uint8_t pin, uint8_t value)
|
||||
}
|
||||
|
||||
uint8_t val = readReg(IOR);
|
||||
uint8_t pre = val;
|
||||
if (_error != MCP23S17_OK)
|
||||
{
|
||||
return false;
|
||||
@ -223,11 +225,14 @@ bool MCP23S17::digitalWrite(uint8_t pin, uint8_t value)
|
||||
{
|
||||
val &= ~mask;
|
||||
}
|
||||
if (pre != val)
|
||||
{
|
||||
writeReg(IOR, val);
|
||||
if (_error != MCP23S17_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -390,6 +395,7 @@ void MCP23S17::setSPIspeed(uint32_t speed)
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 8 pins interface
|
||||
// whole register at once
|
||||
// port = 0..1
|
||||
@ -408,7 +414,8 @@ bool MCP23S17::pinMode8(uint8_t port, uint8_t value)
|
||||
}
|
||||
|
||||
|
||||
bool MCP23S17::write8(uint8_t port, uint8_t value) // port = 0..1
|
||||
// port = 0..1
|
||||
bool MCP23S17::write8(uint8_t port, uint8_t value)
|
||||
{
|
||||
if (port > 1)
|
||||
{
|
||||
@ -508,9 +515,10 @@ bool MCP23S17::getPullup8(uint8_t port, uint8_t &mask)
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 16 pins interface
|
||||
// two register at once
|
||||
// value = 0..0xFFFF bit pattern
|
||||
// value = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::pinMode16(uint16_t value)
|
||||
{
|
||||
writeReg(MCP23S17_DDR_A, value >> 8);
|
||||
@ -520,7 +528,7 @@ bool MCP23S17::pinMode16(uint16_t value)
|
||||
}
|
||||
|
||||
|
||||
// value = 0..0xFFFF bit pattern
|
||||
// value = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::write16(uint16_t value)
|
||||
{
|
||||
writeReg(MCP23S17_GPIO_A, value >> 8);
|
||||
@ -530,7 +538,7 @@ bool MCP23S17::write16(uint16_t value)
|
||||
}
|
||||
|
||||
|
||||
// return = 0..0xFFFF bit pattern
|
||||
// return = 0x0000..0xFFFF bit pattern
|
||||
uint16_t MCP23S17::read16()
|
||||
{
|
||||
_error = MCP23S17_OK;
|
||||
@ -541,7 +549,7 @@ uint16_t MCP23S17::read16()
|
||||
}
|
||||
|
||||
|
||||
// mask = 0..0xFFFF bit pattern
|
||||
// mask = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::setPolarity16(uint16_t mask)
|
||||
{
|
||||
writeReg(MCP23S17_POL_A, mask >> 8);
|
||||
@ -554,7 +562,7 @@ bool MCP23S17::setPolarity16(uint16_t mask)
|
||||
}
|
||||
|
||||
|
||||
// mask = 0..0xFFFF bit pattern
|
||||
// mask = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::getPolarity16(uint16_t &mask)
|
||||
{
|
||||
mask = readReg(MCP23S17_POL_A);
|
||||
@ -568,7 +576,7 @@ bool MCP23S17::getPolarity16(uint16_t &mask)
|
||||
}
|
||||
|
||||
|
||||
// mask = 0..0xFFFF bit pattern
|
||||
// mask = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::setPullup16(uint16_t mask)
|
||||
{
|
||||
writeReg(MCP23S17_PUR_A, mask >> 8);
|
||||
@ -581,7 +589,7 @@ bool MCP23S17::setPullup16(uint16_t mask)
|
||||
}
|
||||
|
||||
|
||||
// mask = 0..0xFFFF bit pattern
|
||||
// mask = 0x0000..0xFFFF bit pattern
|
||||
bool MCP23S17::getPullup16(uint16_t &mask)
|
||||
{
|
||||
mask = readReg(MCP23S17_PUR_A);
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: MCP23S17.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.1
|
||||
// VERSION: 0.2.2
|
||||
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
|
||||
// DATE: 2021-12-30
|
||||
// URL: https://github.com/RobTillaart/MCP23S17
|
||||
@ -12,7 +12,7 @@
|
||||
#include "SPI.h"
|
||||
|
||||
|
||||
#define MCP23S17_LIB_VERSION (F("0.2.1"))
|
||||
#define MCP23S17_LIB_VERSION (F("0.2.2"))
|
||||
|
||||
#define MCP23S17_OK 0x00
|
||||
#define MCP23S17_PIN_ERROR 0x81
|
||||
|
@ -51,39 +51,51 @@ I assume that this is less used and IMHO not recommended.
|
||||
|
||||
### Single pin interface
|
||||
|
||||
- **bool pinMode(uint8_t pin, uint8_t mode)** pin = 0..15, mode = INPUT, OUTPUT, returns true if successful.
|
||||
- **bool digitalWrite(uint8_t pin, uint8_t value)** pin = 0..15, value = LOW(0) HIGH (!0), returns true if successful.
|
||||
- **bool pinMode(uint8_t pin, uint8_t mode)** pin = 0..15, mode = INPUT, OUTPUT. Returns true if successful.
|
||||
- **bool digitalWrite(uint8_t pin, uint8_t value)** pin = 0..15, value = LOW(0) HIGH (!0). Returns true if successful.
|
||||
- **uint8_t digitalRead(uint8_t pin)** pin = 0..15, returns LOW or HIGH, might set the lastError();
|
||||
- **bool setPolarity(uint8_t pin, bool reversed)** pin = 0..15, set reversed flag, returns true if successful.
|
||||
- **bool getPolarity(uint8_t pin, bool &reversed)** pin = 0..15, reads reversed flag, returns true if successful.
|
||||
- **bool setPullup(uint8_t pin, bool pullup)** pin = 0..15, set pull-up flag, returns true if successful.
|
||||
- **bool getPullup(uint8_t pin, bool &pullup)** pin = 0..15, reads pull-up flag, returns true if successful.
|
||||
- **bool setPolarity(uint8_t pin, bool reversed)** pin = 0..15, set reversed flag. Returns true if successful.
|
||||
- **bool getPolarity(uint8_t pin, bool &reversed)** pin = 0..15, reads reversed flag. Returns true if successful.
|
||||
- **bool setPullup(uint8_t pin, bool pullup)** pin = 0..15, set pull-up flag. Returns true if successful.
|
||||
- **bool getPullup(uint8_t pin, bool &pullup)** pin = 0..15, reads pull-up flag. Returns true if successful.
|
||||
|
||||
|
||||
### 8 pins interface
|
||||
|
||||
- **bool pinMode8(uint8_t port, uint8_t value)** port = 0..1, value = 0..255, returns true if successful.
|
||||
- **bool write8(uint8_t port, uint8_t value)** port = 0..1, value = 0..255, returns true if successful.
|
||||
- **bool pinMode8(uint8_t port, uint8_t value)** port = 0..1, value = 0..255. Returns true if successful.
|
||||
- **bool write8(uint8_t port, uint8_t value)** port = 0..1, value = 0..255. Returns true if successful.
|
||||
- **uint8_t read8(uint8_t port)** port = 0..1, reads 8 pins into one byte.
|
||||
- **bool setPolarity8(uint8_t port, uint8_t mask)** port = 0..1, sets polarity for 8 channels at once.
|
||||
Returns true if successful.
|
||||
- **bool getPolarity8(uint8_t port, uint8_t &mask)** port = 0..1, reads polarity of 8 channels at once.
|
||||
Returns true if successful.
|
||||
- **bool setPullup8(uint8_t port, uint8_t mask)** port = 0..1, sets pull-up for 8 channels at once.
|
||||
Returns true if successful.
|
||||
- **bool getPullup8(uint8_t port, uint8_t &mask)** port = 0..1, reads pull-up for 8 channels at once.
|
||||
Returns true if successful.
|
||||
|
||||
|
||||
### 16 pins interface
|
||||
|
||||
- **bool pinMode16(uint16_t value)** value = 0..0xFFFF, returns true if successful.
|
||||
Returns true if successful.
|
||||
- **bool write16(uint16_t value)** value = 0..0xFFFF, returns true if successful.
|
||||
Returns true if successful.
|
||||
- **uint16_t read16()** reads 16 pins into an uint16_t.
|
||||
- **bool setPolarity16(uint16_t mask)** sets polarity for 16 channels.
|
||||
Returns true if successful.
|
||||
- **bool getPolarity16(uint16_t &mask)** reads polarity of 16 channels.
|
||||
Returns true if successful.
|
||||
- **bool setPullup16(uint16_t mask)** sets pull-up for 16 channels.
|
||||
Returns true if successful.
|
||||
- **bool getPullup16(uint16_t &mask)** reads pull-up for 16 channels.
|
||||
Returns true if successful.
|
||||
|
||||
|
||||
### Error codes
|
||||
|
||||
If one of the above functions return false, there might be an error.
|
||||
|
||||
- **int lastError()** Above functions set an error flag that can be read with this function.
|
||||
Reading it will reset the flag to **MCP23S17_OK**.
|
||||
|
||||
|
@ -34,6 +34,8 @@ void setup()
|
||||
Serial.print("HWSPI: ");
|
||||
Serial.println(MCP.usesHWSPI());
|
||||
|
||||
MCP.setSPIspeed(10000000);
|
||||
|
||||
MCP.pinMode8(0, 0x00); // 0 = output , 1 = input
|
||||
MCP.pinMode8(1, 0x00);
|
||||
|
||||
|
@ -0,0 +1,35 @@
|
||||
|
||||
|
||||
# Performance test
|
||||
|
||||
Test sketch MCP23S17_performance.ino
|
||||
|
||||
Max clock frequency 10 MHz, for an UNO this is 8 MHz (divider of CPU clock)
|
||||
|
||||
### Library version: 0.2.2
|
||||
|
||||
| Action | SW SPI | HW 1 MHz | HW 2 MHz | HW 4 MHz | HW 8 MHz | notes |
|
||||
|:------------------------------|--------:|---------:|---------:|---------:|---------:|:---------:|
|
||||
| TEST digitalWrite(0, value) | 676.75 | 67.00 | 48.75 | 39.75 | 35.25 | Optimized!
|
||||
| TEST digitalWrite(pin, value) | 678.00 | 68.25 | 50.00 | 41.25 | 36.50 | Optimized!
|
||||
| TEST digitalRead(pin) | 452.00 | 45.00 | 32.50 | 27.25 | 23.75 |
|
||||
| | | | | | |
|
||||
| TEST write8(port, mask) | 450.00 | 44.00 | 32.00 | 26.00 | 24.00 |
|
||||
| TEST read8(port) | 450.00 | 44.00 | 32.00 | 26.00 | 24.00 |
|
||||
| TEST write16(mask) | 452.00 | 48.00 | 32.00 | 26.00 | 22.00 | since 0.1.1
|
||||
| TEST read16() | 452.00 | 44.00 | 32.00 | 26.00 | 22.00 | since 0.1.1
|
||||
|
||||
|
||||
### Notes
|
||||
|
||||
Performance as expected.
|
||||
|
||||
0.2.2 has optimized digitalWrite(pin, value) when value is same as in IO register.
|
||||
|
||||
|
||||
### Future
|
||||
|
||||
- test ESP32 and other platforms
|
||||
- register based IO version for the SW SPI on AVR ?
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/MCP23S17.git"
|
||||
},
|
||||
"version": "0.2.1",
|
||||
"version": "0.2.2",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=MCP23S17
|
||||
version=0.2.1
|
||||
version=0.2.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for SPI MCP23S17 16 channel port expander 16 IO-lines
|
||||
|
Loading…
Reference in New Issue
Block a user