This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender

Dependents:   MCP23017App

Committer:
Yann
Date:
Fri Jan 09 14:37:42 2015 +0000
Revision:
0:ebd3a7cc9b92
Child:
1:ec9e770173d5
Add support of interruptions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:ebd3a7cc9b92 1 #pragma once
Yann 0:ebd3a7cc9b92 2
Yann 0:ebd3a7cc9b92 3 #include <list>
Yann 0:ebd3a7cc9b92 4
Yann 0:ebd3a7cc9b92 5 #include <mbed.h>
Yann 0:ebd3a7cc9b92 6
Yann 0:ebd3a7cc9b92 7 #define GPA0 0
Yann 0:ebd3a7cc9b92 8 #define GPA1 1
Yann 0:ebd3a7cc9b92 9 #define GPA2 2
Yann 0:ebd3a7cc9b92 10 #define GPA3 3
Yann 0:ebd3a7cc9b92 11 #define GPA4 4
Yann 0:ebd3a7cc9b92 12 #define GPA5 5
Yann 0:ebd3a7cc9b92 13 #define GPA6 6
Yann 0:ebd3a7cc9b92 14 #define GPA7 7
Yann 0:ebd3a7cc9b92 15 #define GPB0 8
Yann 0:ebd3a7cc9b92 16 #define GPB1 9
Yann 0:ebd3a7cc9b92 17 #define GPB2 10
Yann 0:ebd3a7cc9b92 18 #define GPB3 11
Yann 0:ebd3a7cc9b92 19 #define GPB4 12
Yann 0:ebd3a7cc9b92 20 #define GPB5 13
Yann 0:ebd3a7cc9b92 21 #define GPB6 14
Yann 0:ebd3a7cc9b92 22 #define GPB7 15
Yann 0:ebd3a7cc9b92 23 #define GPIO_MAX GPB7
Yann 0:ebd3a7cc9b92 24 #define GPIO_MED GPB0
Yann 0:ebd3a7cc9b92 25 #define GPIO_SIZE 8
Yann 0:ebd3a7cc9b92 26
Yann 0:ebd3a7cc9b92 27 class AbstractGpioExpender {
Yann 0:ebd3a7cc9b92 28 public:
Yann 0:ebd3a7cc9b92 29 /** Interrupt modes
Yann 0:ebd3a7cc9b92 30 */
Yann 0:ebd3a7cc9b92 31 enum InterruptModes {
Yann 0:ebd3a7cc9b92 32 OnChange = 0x00,
Yann 0:ebd3a7cc9b92 33 OnRising = 0x01,
Yann 0:ebd3a7cc9b92 34 OnFalling = 0x03
Yann 0:ebd3a7cc9b92 35 };
Yann 0:ebd3a7cc9b92 36
Yann 0:ebd3a7cc9b92 37 /** Reset the device
Yann 0:ebd3a7cc9b92 38 */
Yann 0:ebd3a7cc9b92 39 virtual void reset() = 0;
Yann 0:ebd3a7cc9b92 40
Yann 0:ebd3a7cc9b92 41 /** Setup interrupt mechanism for all GPIO ports
Yann 0:ebd3a7cc9b92 42 * @param p_mirroring Set to 0x00 to disable INTA/B mirroring, 0x01 otherwise. Default: 0x00
Yann 0:ebd3a7cc9b92 43 * @param p_openDrain Set to 0x00 for active driver output, 0x01 for opn drain output. Default: 0x00
Yann 0:ebd3a7cc9b92 44 * @param p_polarity Set to 0x00 for interrupt active low, 0x01 otherwise. Default: 0x00
Yann 0:ebd3a7cc9b92 45 */
Yann 0:ebd3a7cc9b92 46 virtual void setupInterrupts(const unsigned char p_mirroring = 0x00, const unsigned char p_openDrain = 0x00, const unsigned char p_polarity = 0x00) = 0;
Yann 0:ebd3a7cc9b92 47
Yann 0:ebd3a7cc9b92 48 /** Setup interrupt for a specific IO port
Yann 0:ebd3a7cc9b92 49 * @param p_gpioId The IO port identifier
Yann 0:ebd3a7cc9b92 50 * @param p_mode The interrupt mode
Yann 0:ebd3a7cc9b92 51 * @return 0 on success, negative value otherwise
Yann 0:ebd3a7cc9b92 52 */
Yann 0:ebd3a7cc9b92 53 virtual int setupInterruptPin(const unsigned char p_gpioId, const InterruptModes p_mode = OnRising) = 0;
Yann 0:ebd3a7cc9b92 54
Yann 0:ebd3a7cc9b92 55 /** Get interrupt information and clear it
Yann 0:ebd3a7cc9b92 56 * @param p_gpioId The IO port identifier where the interrupt occured
Yann 0:ebd3a7cc9b92 57 * @param p_value The logic value on the pin port where the interrupt occured
Yann 0:ebd3a7cc9b92 58 * @return 0 on success, negative value otherwise
Yann 0:ebd3a7cc9b92 59 */
Yann 0:ebd3a7cc9b92 60 virtual int getLastInterruptPin(unsigned char * p_gpioId, unsigned char * p_value) = 0;
Yann 0:ebd3a7cc9b92 61
Yann 0:ebd3a7cc9b92 62 /** Attach a function to call when a interrupt occurs on the GPIOA input ports
Yann 0:ebd3a7cc9b92 63 */
Yann 0:ebd3a7cc9b92 64 virtual void setIntrACallback(void (* p_fptr)(void)) = 0;
Yann 0:ebd3a7cc9b92 65
Yann 0:ebd3a7cc9b92 66 /** Attach a function to call when a rising edge occurs on the input
Yann 0:ebd3a7cc9b92 67 */
Yann 0:ebd3a7cc9b92 68 virtual void setIntrBCallback(void (* p_fptr)(void)) = 0;
Yann 0:ebd3a7cc9b92 69
Yann 0:ebd3a7cc9b92 70 virtual int read(const unsigned char p_gpioId, unsigned char * p_value) = 0;
Yann 0:ebd3a7cc9b92 71 virtual int write(const unsigned char p_gpioId, const unsigned char p_value) = 0;
Yann 0:ebd3a7cc9b92 72
Yann 0:ebd3a7cc9b92 73 virtual unsigned char createBus(const std::list<unsigned char> p_lines, const PinMode p_mode = PullNone) = 0;
Yann 0:ebd3a7cc9b92 74 virtual void deleteBus(const unsigned char p_busId) = 0;
Yann 0:ebd3a7cc9b92 75 virtual int busRead(const unsigned char p_busId) = 0;
Yann 0:ebd3a7cc9b92 76 virtual int busWrite(const unsigned char p_busId, const unsigned char p_value) = 0;
Yann 0:ebd3a7cc9b92 77
Yann 0:ebd3a7cc9b92 78 protected:
Yann 0:ebd3a7cc9b92 79 virtual bool writeRegister(const unsigned char p_registerId, const unsigned char p_value) = 0;
Yann 0:ebd3a7cc9b92 80 virtual bool readRegister(const unsigned char p_registerId, unsigned char * p_value) = 0;
Yann 0:ebd3a7cc9b92 81
Yann 0:ebd3a7cc9b92 82 }; // End of class AbstractGpioExpender