Romilly Cocking / Mbed 2 deprecated MCP23S17Test

Dependencies:   mbed MCP23S17

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Test drive the MCP23S17 library
00002 * Copyright (c) 2010 Romilly Cocking
00003 * Released under the MIT License: http://mbed.org/license/mit
00004 *
00005 * See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
00006 *
00007 * NB this code is intended to test the driver library, not the chip
00008 * which is assumed to work as specified
00009 * MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
00010 * version 0.4
00011 */
00012 
00013 #include "mbed.h"
00014 #include "MCP23S17.h"
00015 
00016 SPI spi(p5, p6, p7);
00017 
00018 // A0, A1, A2 of the MCP23S17  are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
00019 // This is referred to as the opcode in the device datasheet
00020 char writeOpcode = 0x40;
00021 // mbed pin20 is connected to ~chipSelect of the MCP23S17
00022 MCP23S17 chip = MCP23S17(spi, p20, writeOpcode); // create MCP23S17
00023 DigitalOut    chipNotReset(p14); // connected to ~reset of the MCP23S17
00024 DigitalInOut chipA0(p12);  // connected to Port A bit 0 of the MCP23S17
00025 DigitalInOut chipA1(p30);     // connected to Port A bit 1 via 100k resistor
00026 DigitalInOut  chipB0(p10); // connected to Port B bit 0 of the MCP23S17
00027 DigitalIn   chipIntA(p16); // connected to INTA on the MCP23S17
00028 DigitalIn   chipIntB(p18); // connected to INTB on the MCP23S17
00029 
00030 void reset() {
00031     // I'm not sure we need the delays, but better safe than sorry.
00032     // Ensure mbed bi-directional pins set to input
00033     chipA0.input();
00034     chipA1.input();
00035     chipB0.input();
00036     chipNotReset = 0;  // reset chip
00037     wait_us(10);
00038     chipNotReset = 1;
00039     wait_us(10);
00040 }
00041 
00042 void checkEqual(int expected, int actual, char * description) {
00043     if (expected != actual) {
00044         printf("%s **TEST FAILED** - expected %i but get %i\r\n", description, expected, actual);
00045         exit(-1);
00046     }
00047 }
00048 
00049 void testInputFromPortA() {
00050     reset();
00051     chipA0.output(); // prepare to output from the mbed
00052     chip.direction(PORT_A,0x01); // chip Port A bit 0 set to input
00053     chipA0 = 0;
00054     checkEqual(0, chip.read(PORT_A) && 0x1,"read from A0");
00055     chipA0 = 1;
00056     checkEqual(1, chip.read(PORT_A) && 0x1,"read from A0");
00057 }
00058 
00059 void testInputFromPortB() {
00060     reset();
00061     chipB0.output(); // output from the mbed
00062     chip.direction(PORT_B,0x01); //  bit 0 set to input
00063     chipB0 = 0;
00064     checkEqual(0, chip.read(PORT_B) && 0x1,"read from B0");
00065     chipB0 = 1;
00066     checkEqual(1, chip.read(PORT_B) && 0x1,"read from B0");
00067 }
00068 
00069 void testOutputToPortA() {
00070     reset();
00071     chipA0.input(); // input to the mbed
00072     chip.direction(PORT_A, 0xFE); //  bit 0 set to output
00073     chip.write(PORT_A,0x00);
00074     checkEqual(0, int(chipA0),"write to A0");
00075     chip.write(PORT_A,0x01);
00076     checkEqual(1, int(chipA0),"write to A0");
00077 }
00078 
00079 void testOutputToPortB() {
00080     reset();
00081     chipB0.input(); // input to the mbed
00082     chip.direction(PORT_B, 0xFE); //  bit 0 set to output
00083     chip.write(PORT_B,0x00);
00084     checkEqual(0, int(chipB0),"write to B0");
00085     chip.write(PORT_B,0x01);
00086     checkEqual(1, int(chipB0),"write to B0");
00087 }
00088 
00089 void testInterruptEnableOnPortA() {
00090 // NB by default both int pins are Active-LOW
00091     reset();
00092     chipA0.output(); // output from the mbed
00093     chip.direction(PORT_A, 0x01); //  bit 0 set to input
00094     chipA0 = 1;
00095     checkEqual(1, int(chipIntA),"interrupts not yet enabled");
00096     chipA0 = 0;
00097     chip.interruptEnable(PORT_A, 0x01); // interupt enabled on pin A0
00098     checkEqual(1, int(chipIntA), "value has not changed");
00099     chipA0 = 1;
00100     wait_us(1); // test fails without this - mbed is too darned fast!
00101     checkEqual(0, int(chipIntA), "value has changed");
00102 }
00103 
00104 void testInterruptEnableOnPortB() {
00105 // NB by default both int pins are Active-LOW
00106     reset();
00107     chipB0.output(); // output from the mbed
00108     chip.direction(PORT_B, 0x01); //  bit 0 set to input
00109     chipB0 = 1;
00110     chip.interruptEnable(PORT_B, 0x01); // interupt enabled on pin 0
00111     wait_us(1);
00112     checkEqual(0, int(chipIntB), "interruptB");
00113 }
00114 
00115 void testMirrorInterrupts() {
00116     reset();
00117     chipB0.output(); // output from the mbed
00118     chip.direction(PORT_B, 0x01); //  bit 0 set to input
00119     chipB0 = 1;
00120     chip.interruptEnable(PORT_B, 0x01); // interupt enabled on pin 0
00121     wait_us(1);
00122     checkEqual(0, int(chipIntB), "interruptB");
00123     checkEqual(1, int(chipIntA), "before mirroring"); // no interrupt A yet
00124     chip.mirrorInterrupts(true);
00125     wait_us(1);
00126     checkEqual(0, int(chipIntA), "after mirroring");
00127     chip.mirrorInterrupts(false);
00128     wait_us(1);
00129     checkEqual(1, int(chipIntA), "after mirroring turned off");
00130 }
00131 
00132 void testInterruptPolarity() {
00133 // NB by default both int pins are Active-LOW
00134 // interrupt off (so HIGH) after POR
00135     reset();
00136     checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
00137     chip.interruptPolarity(ACTIVE_HIGH);
00138     wait_us(1);
00139     checkEqual(0, int(chipIntA), "interrupt ACTIVE_HIGH");
00140     chip.interruptPolarity(ACTIVE_LOW);
00141     wait_us(1);
00142     checkEqual(1, int(chipIntA), "interrupt ACTIVE_LOW");
00143 }
00144 
00145 void testInterruptControlAndDefaultValueOnPortA() {
00146     reset();
00147     chipA0.output(); // output from the mbed
00148     chip.direction(PORT_A, 0x01); //  bit 0 set to input
00149     chipA0 = 0;
00150     checkEqual(1, int(chipIntA),"interrupt ACTIVE_LOW by default");
00151     chip.interruptEnable(PORT_A, 0x01); // interupt enabled on pin 0
00152     chip.defaultValue(PORT_A, 0x01); // default value != input value
00153     checkEqual(1, int(chipIntA),"still no interrupt"); // interrupt control still set to interrupt on change
00154     chip.interruptControl(PORT_A, 0x01);
00155     wait_us(1);
00156     checkEqual(0, int(chipIntA), "expecting interrupt as default != input");
00157 }
00158 
00159 void testConfigurePullUpsOnPortA() {
00160     reset();
00161     chipA1.output();
00162     checkEqual(0, chip.read(PORT_A), "without pull-up input should be 0");
00163     chip.configurePullUps(PORT_A, 0x02); // pin A1 pull-up enabled
00164     checkEqual(2, chip.read(PORT_A), "pull-up should raise chip input to 1");
00165 }
00166 
00167 int main() {
00168     testInputFromPortA();
00169     testInputFromPortB();
00170     testOutputToPortA();
00171     testOutputToPortB();
00172     testInterruptEnableOnPortA();
00173     testInterruptEnableOnPortB();
00174     testInterruptPolarity();
00175     testMirrorInterrupts();
00176     testInterruptControlAndDefaultValueOnPortA();
00177     testConfigurePullUpsOnPortA();
00178     // testInterruptControlAndDefaultValueOnPortB();
00179     // testInterruptCaptureA();
00180     // testInterruptCaptureB();
00181     printf("all tests OK\r\n");
00182 }