Skip to content

Commit ee545d9

Browse files
authored
fix #47, enable interrupts. (#49)
- fix #47, interrupt handling. Kudos to GlibSkunk! - update readme.md - minor edits.
1 parent 3d03c7d commit ee545d9

File tree

6 files changed

+92
-90
lines changed

6 files changed

+92
-90
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.6.0] - 2024-11-19
10+
- fix #47, interrupt handling. Kudos to GlibSkunk!
11+
- update readme.md
12+
- minor edits.
13+
14+
----
15+
916
## [0.5.4] - 2024-07-04
1017
- fix #45, documentation bug
1118

MCP23S17.cpp

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MCP23S17.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.5.4
4+
// VERSION: 0.6.0
55
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
66
// DATE: 2021-12-30
77
// URL: https://github.com/RobTillaart/MCP23S17
@@ -570,114 +570,108 @@ bool MCP23S17::enableInterrupt(uint8_t pin, uint8_t mode)
570570
_error = MCP23S17_PIN_ERROR;
571571
return false;
572572
}
573-
573+
574574
uint8_t INTCONREG = MCP23x17_INTCON_A;
575-
uint8_t DEFVALREG = MCP23x17_DEFVAL_A;
576-
uint8_t GPINTENREG = MCP23x17_GPINTEN_A;
575+
uint8_t DEFVALREG = MCP23x17_DEFVAL_A;
576+
uint8_t GPINTENREG = MCP23x17_GPINTEN_A;
577577
if (pin > 7)
578578
{
579579
INTCONREG = MCP23x17_INTCON_B;
580-
DEFVALREG = MCP23x17_DEFVAL_B;
581-
GPINTENREG = MCP23x17_GPINTEN_B;
580+
DEFVALREG = MCP23x17_DEFVAL_B;
581+
GPINTENREG = MCP23x17_GPINTEN_B;
582582
pin -= 8;
583583
}
584-
uint8_t mask = 1 << pin;
584+
uint8_t mask = 1 << pin;
585585

586586
uint8_t intcon = readReg(INTCONREG);
587-
uint8_t pre_intcon = intcon;
587+
uint8_t pre_intcon = intcon;
588588
if (_error != MCP23S17_OK)
589589
{
590590
return false;
591591
}
592-
593-
if (mode == CHANGE)
594-
{
595-
// Compare to previous value
596-
intcon &= ~mask;
597-
}
598-
else
599-
{
600-
// Compare to DEFVALREG
601-
intcon |= mask;
602-
603-
// Get and Modify Pin's Value in DEFVALREG
604-
uint8_t defval = readReg(DEFVALREG);
605-
uint8_t pre_defval = defval;
592+
593+
if (mode == CHANGE)
594+
{
595+
// Compare to previous value
596+
intcon &= ~mask;
597+
}
598+
else
599+
{
600+
// Compare to DEFVALREG
601+
intcon |= mask;
602+
603+
// Get and Modify Pin's Value in DEFVALREG
604+
uint8_t defval = readReg(DEFVALREG);
605+
uint8_t pre_defval = defval;
606606
if (mode == RISING)
607607
{
608-
defval &= ~mask; // RISING == compare to 0
608+
defval &= ~mask; // RISING == compare to 0
609609
}
610610
else if (mode == FALLING)
611611
{
612-
defval |= mask; // FALLING == compare to 1
613-
}
614-
// only write when changed.
615-
if (pre_defval != defval)
616-
{
617-
writeReg(DEFVALREG, defval);
618-
if (_error != MCP23S17_OK)
619-
{
620-
return false;
621-
}
622-
}
623-
}
624-
// only write when changed.
625-
if (pre_intcon != intcon)
626-
{
627-
writeReg(INTCONREG, intcon);
628-
if (_error != MCP23S17_OK)
629-
{
630-
return false;
631-
}
632-
}
633-
612+
defval |= mask; // FALLING == compare to 1
613+
}
614+
// only write when changed.
615+
if (pre_defval != defval)
616+
{
617+
writeReg(DEFVALREG, defval);
618+
if (_error != MCP23S17_OK)
619+
{
620+
return false;
621+
}
622+
}
623+
}
624+
// only write when changed.
625+
if (pre_intcon != intcon)
626+
{
627+
writeReg(INTCONREG, intcon);
628+
if (_error != MCP23S17_OK)
629+
{
630+
return false;
631+
}
632+
}
633+
634634
// enable interrupt
635-
uint8_t gpinten = readReg(GPINTENREG);
636-
uint8_t pre_gpinten = gpinten;
637-
gpinten |= mask;
638-
if (pre_gpinten != gpinten)
639-
{
640-
return writeReg(GPINTENREG, gpinten);
641-
}
642-
else
643-
{
644-
return true; // Already enabled for pin
645-
}
635+
uint8_t gpinten = readReg(GPINTENREG);
636+
uint8_t pre_gpinten = gpinten;
637+
gpinten |= mask;
638+
if (pre_gpinten != gpinten)
639+
{
640+
return writeReg(GPINTENREG, gpinten);
641+
}
642+
return true; // Already enabled for pin
646643
}
647644

645+
648646
bool MCP23S17::disableInterrupt(uint8_t pin)
649647
{
650648
if (pin > 15)
651649
{
652650
_error = MCP23S17_PIN_ERROR;
653651
return false;
654652
}
655-
656-
uint8_t GPINTENREG = MCP23x17_GPINTEN_A;
653+
654+
uint8_t GPINTENREG = MCP23x17_GPINTEN_A;
657655
if (pin > 7)
658656
{
659-
GPINTENREG = MCP23x17_GPINTEN_B;
657+
GPINTENREG = MCP23x17_GPINTEN_B;
660658
pin -= 8;
661659
}
662-
uint8_t mask = 1 << pin;
663-
660+
uint8_t mask = 1 << pin;
664661

665-
// disable interrupt
666-
uint8_t gpinten = readReg(GPINTENREG);
667-
uint8_t pre_gpinten = gpinten;
668-
if (_error != MCP23S17_OK)
662+
// disable interrupt
663+
uint8_t gpinten = readReg(GPINTENREG);
664+
uint8_t pre_gpinten = gpinten;
665+
if (_error != MCP23S17_OK)
669666
{
670667
return false;
671668
}
672-
gpinten &= ~mask;
673-
if (pre_gpinten != gpinten)
674-
{
675-
return writeReg(GPINTENREG, gpinten);
676-
}
677-
else
678-
{
679-
return true; // Already disabled for pin
680-
}
669+
gpinten &= ~mask;
670+
if (pre_gpinten != gpinten)
671+
{
672+
return writeReg(GPINTENREG, gpinten);
673+
}
674+
return true; // Already disabled for pin
681675
}
682676

683677

@@ -692,15 +686,14 @@ bool MCP23S17::enableInterrupt16(uint16_t mask, uint8_t mode)
692686
}
693687
else
694688
{
689+
intcon = mask;
695690
if (mode == RISING)
696691
{
697-
intcon = mask;
698692
defval = ~mask; // RISING == compare to 0
699693
}
700694
else if (mode == FALLING)
701695
{
702-
intcon = mask;
703-
defval = mask; // FALLING == compare to 1
696+
defval = mask; // FALLING == compare to 1
704697
}
705698
writeReg16(MCP23x17_DEFVAL_A, defval);
706699
}

MCP23S17.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: MCP23S17.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.5.4
5+
// VERSION: 0.6.0
66
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
77
// DATE: 2021-12-30
88
// URL: https://github.com/RobTillaart/MCP23S17
@@ -13,7 +13,7 @@
1313
#include "MCP23x17_registers.h"
1414

1515

16-
#define MCP23S17_LIB_VERSION (F("0.5.4"))
16+
#define MCP23S17_LIB_VERSION (F("0.6.0"))
1717

1818
// ERROR CODES
1919
#define MCP23S17_OK 0x00
@@ -70,7 +70,7 @@ class MCP23S17
7070

7171
// 8 pins interface
7272
// port = 0..1
73-
// mask = 0x00..0xFF bit pattern,
73+
// mask = 0x00..0xFF bit pattern,
7474
// bit 0 = output mode, bit 1 = input mode
7575
// value = bit pattern.
7676
bool pinMode8(uint8_t port, uint8_t mask);
@@ -101,7 +101,7 @@ class MCP23S17
101101
// pin = 0..15, mode = { RISING, FALLING, CHANGE }
102102
bool enableInterrupt(uint8_t pin, uint8_t mode);
103103
bool disableInterrupt(uint8_t pin);
104-
104+
105105
// mask = 0x0000..0xFFFF (overrides all earlier settings.
106106
bool enableInterrupt16(uint16_t mask, uint8_t mode);
107107
bool disableInterrupt16(uint16_t mask);

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ Programming Interface is kept the same as much as possible.
2424
The **write1(pin, value)** is optimized.
2525
If a pin is not changed it will not be written again to save time.
2626

27+
### 0.6.0 Breaking change
2728

28-
#### 0.5.0 Breaking change
29+
Fix for #47, bug in enable interrupt handling.
30+
Pre 0.6.0 versions are obsolete.
31+
32+
### 0.5.0 Breaking change
2933

3034
Version 0.5.0 introduced a breaking change to improve handling the SPI dependency.
3135
The user has to call **SPI.begin()** or equivalent before calling **MCP.begin()**.
3236
Optionally the user can provide parameters to the **SPI.begin(...)**
3337

34-
35-
#### 0.4.0 Breaking change
38+
### 0.4.0 Breaking change
3639

3740
The version 0.4.0 has breaking changes in the interface.
3841
The rationale is that the programming environment of the **Arduino ESP32 S3**
@@ -54,15 +57,15 @@ The following library functions have been renamed:
5457
| digitalWrite() | write1() |
5558

5659

57-
#### 0.3.0 Breaking change
60+
### 0.3.0 Breaking change
5861

5962
The version 0.3.0 has breaking changes in the interface.
6063
The essence is removal of ESP32 specific code from the library.
6164
This makes it possible to support the ESP32-S3 and other processors in the future.
6265
Also it makes the library a bit simpler to maintain.
6366

6467

65-
#### Related
68+
### Related
6669

6770
16 bit port expanders
6871

@@ -111,7 +114,7 @@ The two hardware constructors allow to call 4 different constructors.
111114
```
112115

113116

114-
#### Sharing SELECT lines
117+
### Sharing SELECT lines
115118

116119
(verified in #19)
117120
Technically two chips could use the same SELECT pin and a different address.
@@ -184,7 +187,6 @@ If there are problems please open an issue.
184187
### Interrupts (experimental 0.5.2)
185188

186189
Read the datasheet for the details, page 24,25.
187-
Note: Error handling is limited.
188190

189191
pin = 0..15
190192
mode = { RISING, FALLING, CHANGE }

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MCP23S17.git"
1717
},
18-
"version": "0.5.4",
18+
"version": "0.6.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP23S17
2-
version=0.5.4
2+
version=0.6.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for SPI MCP23S17 16 channel port expander 16 IO-lines

0 commit comments

Comments
 (0)