Library test v 0.4
Revision 2:2e8fca65efaf, committed 2010-08-22
- Comitter:
- romilly
- Date:
- Sun Aug 22 15:39:32 2010 +0000
- Parent:
- 1:d7fe22a24841
- Child:
- 3:8f437a811ecf
- Commit message:
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Aug 21 16:51:33 2010 +0000
+++ b/main.cpp Sun Aug 22 15:39:32 2010 +0000
@@ -1,54 +1,165 @@
/* Test drive the MCP23S17 library
* Copyright (c) 2010 Romilly Cocking
* Released under the MIT License: http://mbed.org/license/mit
+*
* 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
+* which is assumed to work as specified
+* MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
+* version 0.2
*/
+
#include "mbed.h"
#include "MCP23S17.h"
SPI spi(p5, p6, p7);
-char writeOpcode = 0x40; // A0, A1, A2 are tied to ground on the breadboard.
-MCP23S17 chip = MCP23S17(spi, p20, 0x40);
-DigitalInOut chipA0(p12);
-DigitalInOut chipB0(p10);
+// A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
+// This is referred to as the opcode in the device datasheet
+char writeOpcode = 0x40;
+// mbed pin20 is connected to ~chipSelect of the MCP23S17
+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 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 checkEqual(int expected, int actual, char * text) {
+void reset() {
+ // I'm not sure we need the delays, but better safe than sorry.
+ chipNotReset = 0; // reset chip
+ wait_us(10);
+ chipNotReset = 1;
+ wait_us(10);
+}
+
+void checkEqual(int expected, int actual, char * description) {
if (expected != actual) {
- printf("%s **TEST FAILED** - expected %i but get %i\r\n", text, expected, actual);
+ printf("%s **TEST FAILED** - expected %i but get %i\r\n", description, expected, actual);
exit(-1);
}
}
-int main() {
+void testInputFromPortA() {
+ reset();
+ chipA0.output(); // prepare to output from the mbed
+ chip.directionA(0x01); // chip Port A bit 0 set to input
+ chipA0 = 0;
+ checkEqual(0, chip.inputA() && 0x1,"read from A0");
+ chipA0 = 1;
+ checkEqual(1, chip.inputA() && 0x1,"read from A0");
+}
+
+void testInputFromPortB() {
+ reset();
+ chipB0.output(); // output from the mbed
+ chip.directionB(0x01); // bit 0 set to input
+ chipB0 = 0;
+ checkEqual(0, chip.inputB() && 0x1,"read from B0");
+ chipB0 = 1;
+ checkEqual(1, chip.inputB() && 0x1,"read from B0");
+}
+
+void testOutputToPortA() {
+ reset();
+ chipA0.input(); // input to the mbed
+ chip.directionA(0xFE); // bit 0 set to output
+ chip.outputA(0x00);
+ checkEqual(0, int(chipA0),"write to A0");
+ chip.outputA(0x01);
+ checkEqual(1, int(chipA0),"write to A0");
+}
+
+void testOutputToPortB() {
+ reset();
+ chipB0.input(); // input to the mbed
+ chip.directionB(0xFE); // bit 0 set to output
+ chip.outputB(0x00);
+ checkEqual(0, int(chipB0),"write to B0");
+ chip.outputB(0x01);
+ checkEqual(1, int(chipB0),"write to B0");
+}
+
+void testInterruptEnableOnPortA() {
+// NB by default both int pins are Active-LOW
+ reset();
chipA0.output(); // output from the mbed
- chipB0.input(); // input to the mbed
- for (int i = 0; i < 100; i++) {
- chip.directionA(0xFF); // all 8 bits set to input
- chip.directionB(0xFE); // bit 0 set to output
- chipA0 = 1;
- // copy input bit 0 from A to output bit 0 on B
- chip.outputB(chip.inputA() && 1);
- checkEqual(1, int(chipB0),"copying 1 from A0 to B0");
- // copy input bit 0 from A to output bit 0 on B
- chipA0 = 0;
- chip.outputB(chip.inputA() && 1);
- checkEqual(0, int(chipB0), "copying 0 from A0 to B0");
- }
-
+ chip.directionA(0x01); // bit 0 set to input
+ chipA0 = 1;
+ checkEqual(1, int(chipIntA),"interrupts not yet enabled");
+ chipA0 = 0;
+ chip.interruptEnableA(0x01); // interupt enabled on pin 0
+ checkEqual(1, int(chipIntA), "value has not changed");
+ chipA0 = 1;
+ wait_us(1); // test fails without this - mbed is too darned fast!
+ checkEqual(0, int(chipIntA), "value has changed");
+}
+
+void testInterruptEnableOnPortB() {
+// NB by default both int pins are Active-LOW
+ reset();
+ chipB0.output(); // output from the mbed
+ chip.directionB(0x01); // bit 0 set to input
+ chipB0 = 1;
+ chip.interruptEnableB(0x01); // interupt enabled on pin 0
+ wait_us(1);
+ checkEqual(0, int(chipIntB), "interruptB");
+}
+
+void testMirrorInterrupts() {
+ reset();
chipB0.output(); // output from the mbed
- chipA0.input(); // input to the mbed
- for (int i = 0; i < 100; i++) {
- chip.directionB(0xFF); // all 8 bits set to input
- chip.directionA(0xFE); // bit 0 set to output
- chipB0 = 1;
- // copy input bit 0 from B to output bit 0 on A
- chip.outputA(chip.inputB() && 1);
- checkEqual(1, int(chipB0),"copying 1 from B0 to A0");
- // copy input bit 0 from B to output bit 0 on A
- chipB0 = 0;
- chip.outputA(chip.inputB() && 1);
- checkEqual(0, int(chipB0), "copying 0 from B0 to A0");
- }
-
+ chip.directionB(0x01); // bit 0 set to input
+ chipB0 = 1;
+ chip.interruptEnableB(0x01); // interupt enabled on pin 0
+ wait_us(1);
+ checkEqual(0, int(chipIntB), "interruptB");
+ checkEqual(1, int(chipIntA), "before mirroring"); // no interrupt A yet
+ chip.mirrorInterrupts(true);
+ wait_us(1);
+ checkEqual(0, int(chipIntA), "after mirroring");
+ chip.mirrorInterrupts(false);
+ wait_us(1);
+ checkEqual(1, int(chipIntA), "after mirroring turned off");
+}
+
+void testInterruptPolarity() {
+// NB by default both int pins are Active-LOW
+// interrupt off (so LHIGH) after POR
+ reset();
+ checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
+ chip.interruptPolarity(ACTIVE_HIGH);
+ wait_us(1);
+ checkEqual(0, int(chipIntA), "interrupt ACTIVE_HIGH");
+ chip.interruptPolarity(ACTIVE_LOW);
+ wait_us(1);
+ checkEqual(1, int(chipIntA), "interrupt ACTIVE_LOW");
}
+
+void testInterruptControlAndDefaultValueOnPortA() {
+ reset();
+ chipA0.output(); // output from the mbed
+ chip.directionA(0x01); // bit 0 set to input
+ chipA0 = 0;
+ checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
+ chip.interruptEnableA(0x01); // interupt enabled on pin 0
+ chip.defaultValueA(0x01); // default value != input value
+ checkEqual(1, int(chipIntA),"still no interrupt"); // interrupt control still set to interrupt on change
+ chip.interruptControlA(0x01);
+ wait_us(1);
+ checkEqual(0, int(chipIntA), "expecting interrupt as default != input");
+}
+
+int main() {
+ testInputFromPortA();
+ testInputFromPortB();
+ testOutputToPortA();
+ testOutputToPortB();
+ testInterruptEnableOnPortA();
+ testInterruptEnableOnPortB();
+ testInterruptPolarity();
+ testMirrorInterrupts();
+ testInterruptControlAndDefaultValueOnPortA();
+ printf("all tests OK\r\n");
+}