Added support for banked registers

Dependents:   Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more

Revision:
0:1a9288cc0630
Child:
1:e2edbd61f4d0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23017.h	Sat Dec 18 18:49:19 2010 +0000
@@ -0,0 +1,72 @@
+/* MCP23017 - drive the Microchip MCP23017 16-bit Port Extender using I2C
+* Copyright (c) 2010 Wim Huiskamp, Romilly Cocking (original version for SPI)
+*
+* Released under the MIT License: http://mbed.org/license/mit
+*
+* version 0.2
+*/
+#include "mbed.h"
+
+#ifndef  MCP23017_H
+#define  MCP23017_H
+
+// All register addresses assume IOCON.BANK = 0 (POR default)
+#define IODIRA   0x00
+#define IODIRB   0x01
+#define GPINTENA 0x04
+#define GPINTENB 0x05
+#define DEFVALA  0x06
+#define DEFVALB  0x07
+#define INTCONA  0x08
+#define INTCONB  0x09
+#define IOCON    0x0A
+//#define IOCON    0x0B
+#define GPPUA    0x0C
+#define GPPUB    0x0D
+#define INTFA    0x0E
+#define INTFB    0x0F
+#define INTCAPA  0x10
+#define INTCAPB  0x11
+#define GPIOA    0x12
+#define GPIOB    0x13
+#define OLATA    0x14
+#define OLATB    0x15
+
+// Control settings
+#define IOCON_BANK      0x80 // Banked registers for Port A and B
+#define IOCON_BYTE_MODE 0x20 // Disables sequential operation, Address Ptr does not increment
+                             //   If Disabled and Bank = 0, operations toggle between Port A and B registers
+#define IOCON_HAEN      0x08 // Hardware address enable
+
+#define INTERRUPT_POLARITY_BIT 0x02
+#define INTERRUPT_MIRROR_BIT   0x40
+
+#define PORT_DIR_OUT   0x00
+#define PORT_DIR_IN    0xFF
+
+enum Polarity { ACTIVE_LOW , ACTIVE_HIGH };
+enum Port { PORT_A, PORT_B };
+
+class MCP23017 {
+public:
+    MCP23017(I2C &i2c, char deviceAddress);
+    void direction(Port port, char direction);
+    void configurePullUps(Port port, char offOrOn);
+    void interruptEnable(Port port, char interruptsEnabledMask);
+    void interruptPolarity(Polarity polarity);
+    void mirrorInterrupts(bool mirror);
+    void defaultValue(Port port, char valuesToCompare);
+    void interruptControl(Port port, char interruptControlBits);
+    char read(Port port);
+    void write(Port port, char byte);
+    void databus_write(char data, char ctrl_1, char ctrl_2, char ctrl_3, char ctrl_4);
+protected:
+    I2C &_i2c;
+    void _init();
+    void _write(char address, char byte);
+    char _read(char address);
+    char _readOpcode;
+    char _writeOpcode;
+};
+
+#endif
\ No newline at end of file