Library test v 0.4
Diff: main.cpp
- Revision:
- 4:15297eea34f6
- Parent:
- 3:8f437a811ecf
--- a/main.cpp Mon Aug 23 10:59:22 2010 +0000
+++ b/main.cpp Sat Aug 28 09:51:44 2010 +0000
@@ -4,10 +4,10 @@
*
* See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
*
-* NB this code is inteded to test the driver library, not the chip
+* NB this code is intended to test the driver library, not the chip
* which is assumed to work as specified
* MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
-* version 0.3
+* version 0.4
*/
#include "mbed.h"
@@ -22,12 +22,17 @@
MCP23S17 chip = MCP23S17(spi, p20, writeOpcode); // create MCP23S17
DigitalOut chipNotReset(p14); // connected to ~reset of the MCP23S17
DigitalInOut chipA0(p12); // connected to Port A bit 0 of the MCP23S17
+DigitalInOut chipA1(p30); // connected to Port A bit 1 via 100k resistor
DigitalInOut chipB0(p10); // connected to Port B bit 0 of the MCP23S17
DigitalIn chipIntA(p16); // connected to INTA on the MCP23S17
DigitalIn chipIntB(p18); // connected to INTB on the MCP23S17
void reset() {
// I'm not sure we need the delays, but better safe than sorry.
+ // Ensure mbed bi-directional pins set to input
+ chipA0.input();
+ chipA1.input();
+ chipB0.input();
chipNotReset = 0; // reset chip
wait_us(10);
chipNotReset = 1;
@@ -103,7 +108,7 @@
chip.direction(PORT_B, 0x01); // bit 0 set to input
chipB0 = 1;
chip.interruptEnable(PORT_B, 0x01); // interupt enabled on pin 0
- wait_us(1);
+ wait_us(1);
checkEqual(0, int(chipIntB), "interruptB");
}
@@ -113,14 +118,14 @@
chip.direction(PORT_B, 0x01); // bit 0 set to input
chipB0 = 1;
chip.interruptEnable(PORT_B, 0x01); // interupt enabled on pin 0
- wait_us(1);
+ wait_us(1);
checkEqual(0, int(chipIntB), "interruptB");
checkEqual(1, int(chipIntA), "before mirroring"); // no interrupt A yet
chip.mirrorInterrupts(true);
- wait_us(1);
+ wait_us(1);
checkEqual(0, int(chipIntA), "after mirroring");
chip.mirrorInterrupts(false);
- wait_us(1);
+ wait_us(1);
checkEqual(1, int(chipIntA), "after mirroring turned off");
}
@@ -138,17 +143,25 @@
}
void testInterruptControlAndDefaultValueOnPortA() {
- reset();
- chipA0.output(); // output from the mbed
- chip.direction(PORT_A, 0x01); // bit 0 set to input
- chipA0 = 0;
- checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
- chip.interruptEnable(PORT_A, 0x01); // interupt enabled on pin 0
- chip.defaultValue(PORT_A, 0x01); // default value != input value
- checkEqual(1, int(chipIntA),"still no interrupt"); // interrupt control still set to interrupt on change
- chip.interruptControl(PORT_A, 0x01);
- wait_us(1);
- checkEqual(0, int(chipIntA), "expecting interrupt as default != input");
+ reset();
+ chipA0.output(); // output from the mbed
+ chip.direction(PORT_A, 0x01); // bit 0 set to input
+ chipA0 = 0;
+ checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
+ chip.interruptEnable(PORT_A, 0x01); // interupt enabled on pin 0
+ chip.defaultValue(PORT_A, 0x01); // default value != input value
+ checkEqual(1, int(chipIntA),"still no interrupt"); // interrupt control still set to interrupt on change
+ chip.interruptControl(PORT_A, 0x01);
+ wait_us(1);
+ checkEqual(0, int(chipIntA), "expecting interrupt as default != input");
+}
+
+void testConfigurePullUpsOnPortA() {
+ reset();
+ chipA1.output();
+ checkEqual(0, chip.read(PORT_A), "without pull-up input should be 0");
+ chip.configurePullUps(PORT_A, 0x02); // pin A1 pull-up enabled
+ checkEqual(2, chip.read(PORT_A), "pull-up should raise chip input to 1");
}
int main() {
@@ -161,6 +174,7 @@
testInterruptPolarity();
testMirrorInterrupts();
testInterruptControlAndDefaultValueOnPortA();
+ testConfigurePullUpsOnPortA();
// testInterruptControlAndDefaultValueOnPortB();
// testInterruptCaptureA();
// testInterruptCaptureB();