Skip to content

Commit

Permalink
fix #6 Compilation Nano33 BLE (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Apr 14, 2022
1 parent bf5084f commit daa876a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
26 changes: 20 additions & 6 deletions MCP23S17.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: MCP23S17.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
// DATE: 2021-12-30
// URL: https://github.com/RobTillaart/MCP23S17
Expand All @@ -11,6 +11,7 @@
// 0.1.0 2021-12-30 initial version (a 2019 version did not make it)
// 0.1.1 2022-01-10 add 16 bit interface
// 0.1.2 2022-01-12 change the URL for library manager
// 0.1.3 2022-04-13 fix compiling for NANO33 BLE


#include "Arduino.h"
Expand Down Expand Up @@ -41,6 +42,15 @@
#define MCP23S17_OLAT_A 0x14 // NOT USED output latch P24
#define MCP23S17_OLAT_B 0x15 // NOT USED

// IOCR bit masks (details datasheet P20)
#define MCP23S17_IOCR_BANK 0x80 // Controls how the registers are addressed.
#define MCP23S17_IOCR_MIRROR 0x40 // INT Pins Mirror bit.
#define MCP23S17_IOCR_SEQOP 0x20 // Sequential Operation mode bit.
#define MCP23S17_IOCR_DISSLW 0x10 // Slew Rate control bit for SDA output.
#define MCP23S17_IOCR_HAEN 0x08 // Hardware Address Enable bit (MCP23S17 only).
#define MCP23S17_IOCR_ODR 0x04 // Configures the INT pin as an open-drain output.
#define MCP23S17_IOCR_INTPOL 0x02 // This bit sets the polarity of the INT output pin.
#define MCP23S17_IOCR_NI 0x01 // Not implemented.

// low level read / write masks
#define MCP23S17_WRITE_REG 0x40
Expand Down Expand Up @@ -100,11 +110,15 @@ bool MCP23S17::begin()
// check connected
if (! isConnected()) return false;

// disable address increment (datasheet)
if (! writeReg(MCP23S17_IOCR, 0b00100000)) return false; // TODO MAGIC NR
// disable address increment (datasheet P20
// SEQOP: Sequential Operation mode bit
// 1 = Sequential operation disabled, address pointer does not increment.
// 0 = Sequential operation enabled, address pointer increments.
if (! writeReg(MCP23S17_IOCR, MCP23S17_IOCR_SEQOP)) return false;

// Force INPUT_PULLUP
if (! writeReg(MCP23S17_PUR_A, 0xFF)) return false;
if (! writeReg(MCP23S17_PUR_B, 0xFF)) return false;
if (! writeReg(MCP23S17_PUR_A, 0xFF)) return false; // 0xFF == all UP
if (! writeReg(MCP23S17_PUR_B, 0xFF)) return false; // 0xFF == all UP
return true;
}

Expand Down Expand Up @@ -649,7 +663,7 @@ uint8_t MCP23S17::swSPI_transfer(uint8_t val)
uint8_t rv = 0;
for (uint8_t mask = 0x80; mask > 0; mask >>= 1)
{
::digitalWrite(dao, (val & mask));
::digitalWrite(dao, (val & mask) ? HIGH : LOW);
::digitalWrite(clk, HIGH);
if (::digitalRead(dai) == HIGH) rv |= mask;
::digitalWrite(clk, LOW);
Expand Down
4 changes: 2 additions & 2 deletions MCP23S17.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: MCP23S17.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
// DATE: 2021-12-30
// URL: https://github.com/RobTillaart/MCP23S17
Expand All @@ -12,7 +12,7 @@
#include "SPI.h"


#define MCP23S17_LIB_VERSION (F("0.1.2"))
#define MCP23S17_LIB_VERSION (F("0.1.3"))

#define MCP23S17_OK 0x00
#define MCP23S17_PIN_ERROR 0x81
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ Programming Interface is kept the same as much as possible.
- **int lastError()** Above functions set an error flag that can be read with this function.
Reading it will reset the flag to **MCP23S17_OK**.

| DESCRIPTION | VALUE |
|:----------------------|:-----:|
| MCP23S17_OK | 0x00 |
| NAME | VALUE | DESCRIPTION |
|:----------------------|:-----:|:------------|
| MCP23S17_OK | 0x00 | No error |
| MCP23S17_PIN_ERROR | 0x81 |
| MCP23S17_I2C_ERROR | 0x82 |
| MCP23S17_VALUE_ERROR | 0x83 |
Expand All @@ -85,3 +85,6 @@ See examples.

- keep functional in sync with MCP23017_RT
- **isConnected()** is not really needed
- implement ESP32 specific support - see MCP_ADC.begin()
- replace magic numbers with a defined constant

2 changes: 1 addition & 1 deletion examples/MCP23S17_digitalRead/MCP23S17_digitalRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void setup()

SPI.begin();
rv = MCP.begin();
Serial.println(rv);
Serial.println(rv ? "true" : "false");

rv = MCP.pinMode8(0, 0xFF); // CHECK
Serial.println(rv);
Expand Down
2 changes: 1 addition & 1 deletion examples/MCP23S17_digitalWrite/MCP23S17_digitalWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void setup()

SPI.begin();
bool b = MCP.begin();
// Serial.println(b ? "true" : "false");
Serial.println(b ? "true" : "false");
delay(100);

MCP.pinMode8(0, 0x00); // 0 = output , 1 = input
Expand Down
19 changes: 16 additions & 3 deletions examples/MCP23S17_performance/MCP23S17_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ MCP23S17 MCP(10); // HW SPI address 0x00

uint32_t start, stop;

volatile int val1;
volatile int val8;
volatile uint16_t val16;


void setup()
{
Expand All @@ -25,6 +29,7 @@ void setup()

SPI.begin();
bool b = MCP.begin();
Serial.println(b ? "true" : "false");

Serial.print("HWSPI: ");
Serial.println(MCP.usesHWSPI());
Expand Down Expand Up @@ -63,7 +68,7 @@ void setup()
start = micros();
for (int pin = 0; pin < 16; pin++)
{
volatile int val = MCP.digitalRead(pin);
val1 = MCP.digitalRead(pin);
}
stop = micros();
Serial.println((stop - start) / 16.0);
Expand All @@ -86,7 +91,7 @@ void setup()
Serial.print("TEST read8(port):\t");
delay(100);
start = micros();
volatile int val8 = MCP.read8(0);
val8 = MCP.read8(0);
val8 = MCP.read8(1);
stop = micros();
Serial.println((stop - start) / 2.0);
Expand All @@ -108,11 +113,19 @@ void setup()
Serial.print("TEST read16():\t");
delay(100);
start = micros();
volatile uint16_t val16 = MCP.read16();
val16 = MCP.read16();
stop = micros();
Serial.println((stop - start) / 2.0);
Serial.println();

// keep compiler happy
Serial.print("VAL1:\t");
Serial.println(val1);
Serial.print("VAL8:\t");
Serial.println(val8);
Serial.print("VAL16:\t");
Serial.println(val16);

Serial.println("\ndone...");
}

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MCP23S17.git"
},
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MCP23S17
version=0.1.2
version=0.1.3
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
Expand Down

0 comments on commit daa876a

Please sign in to comment.