This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender
AbstractGpioExpender.h@3:b902729a1675, 2015-01-13 (annotated)
- Committer:
- Yann
- Date:
- Tue Jan 13 10:20:02 2015 +0000
- Revision:
- 3:b902729a1675
- Parent:
- 2:3bea48e1505c
Remove DEBUG info
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 2:3bea48e1505c | 1 | /* mbed simplified interface for GPIO expender (e.g. Microchip MCP23x17) |
Yann | 2:3bea48e1505c | 2 | * Copyright (c) 2014-2015 ygarcia, MIT License |
Yann | 2:3bea48e1505c | 3 | * |
Yann | 2:3bea48e1505c | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Yann | 2:3bea48e1505c | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Yann | 2:3bea48e1505c | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Yann | 2:3bea48e1505c | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Yann | 2:3bea48e1505c | 8 | * furnished to do so, subject to the following conditions: |
Yann | 2:3bea48e1505c | 9 | * |
Yann | 2:3bea48e1505c | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
Yann | 2:3bea48e1505c | 11 | * substantial portions of the Software. |
Yann | 2:3bea48e1505c | 12 | * |
Yann | 2:3bea48e1505c | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Yann | 2:3bea48e1505c | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Yann | 2:3bea48e1505c | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Yann | 2:3bea48e1505c | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 2:3bea48e1505c | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Yann | 2:3bea48e1505c | 18 | */ |
Yann | 0:ebd3a7cc9b92 | 19 | #pragma once |
Yann | 0:ebd3a7cc9b92 | 20 | |
Yann | 0:ebd3a7cc9b92 | 21 | #include <list> |
Yann | 0:ebd3a7cc9b92 | 22 | |
Yann | 0:ebd3a7cc9b92 | 23 | #include <mbed.h> |
Yann | 0:ebd3a7cc9b92 | 24 | |
Yann | 0:ebd3a7cc9b92 | 25 | #define GPA0 0 |
Yann | 0:ebd3a7cc9b92 | 26 | #define GPA1 1 |
Yann | 0:ebd3a7cc9b92 | 27 | #define GPA2 2 |
Yann | 0:ebd3a7cc9b92 | 28 | #define GPA3 3 |
Yann | 0:ebd3a7cc9b92 | 29 | #define GPA4 4 |
Yann | 0:ebd3a7cc9b92 | 30 | #define GPA5 5 |
Yann | 0:ebd3a7cc9b92 | 31 | #define GPA6 6 |
Yann | 0:ebd3a7cc9b92 | 32 | #define GPA7 7 |
Yann | 0:ebd3a7cc9b92 | 33 | #define GPB0 8 |
Yann | 0:ebd3a7cc9b92 | 34 | #define GPB1 9 |
Yann | 0:ebd3a7cc9b92 | 35 | #define GPB2 10 |
Yann | 0:ebd3a7cc9b92 | 36 | #define GPB3 11 |
Yann | 0:ebd3a7cc9b92 | 37 | #define GPB4 12 |
Yann | 0:ebd3a7cc9b92 | 38 | #define GPB5 13 |
Yann | 0:ebd3a7cc9b92 | 39 | #define GPB6 14 |
Yann | 0:ebd3a7cc9b92 | 40 | #define GPB7 15 |
Yann | 0:ebd3a7cc9b92 | 41 | #define GPIO_MAX GPB7 |
Yann | 0:ebd3a7cc9b92 | 42 | #define GPIO_MED GPB0 |
Yann | 0:ebd3a7cc9b92 | 43 | #define GPIO_SIZE 8 |
Yann | 0:ebd3a7cc9b92 | 44 | |
Yann | 2:3bea48e1505c | 45 | /** This class describes a simplified interface for GPIO expender (e.g. Microchip MCP23x17) |
Yann | 2:3bea48e1505c | 46 | */ |
Yann | 0:ebd3a7cc9b92 | 47 | class AbstractGpioExpender { |
Yann | 0:ebd3a7cc9b92 | 48 | public: |
Yann | 0:ebd3a7cc9b92 | 49 | /** Interrupt modes |
Yann | 0:ebd3a7cc9b92 | 50 | */ |
Yann | 0:ebd3a7cc9b92 | 51 | enum InterruptModes { |
Yann | 0:ebd3a7cc9b92 | 52 | OnChange = 0x00, |
Yann | 0:ebd3a7cc9b92 | 53 | OnRising = 0x01, |
Yann | 0:ebd3a7cc9b92 | 54 | OnFalling = 0x03 |
Yann | 0:ebd3a7cc9b92 | 55 | }; |
Yann | 0:ebd3a7cc9b92 | 56 | |
Yann | 2:3bea48e1505c | 57 | /** Pull modes |
Yann | 2:3bea48e1505c | 58 | */ |
Yann | 2:3bea48e1505c | 59 | enum PullModes { |
Yann | 2:3bea48e1505c | 60 | PullOff = 0x00, |
Yann | 2:3bea48e1505c | 61 | PullUp = 0x01, |
Yann | 2:3bea48e1505c | 62 | PullDown = 0x03 |
Yann | 2:3bea48e1505c | 63 | }; |
Yann | 2:3bea48e1505c | 64 | |
Yann | 0:ebd3a7cc9b92 | 65 | /** Reset the device |
Yann | 0:ebd3a7cc9b92 | 66 | */ |
Yann | 0:ebd3a7cc9b92 | 67 | virtual void reset() = 0; |
Yann | 0:ebd3a7cc9b92 | 68 | |
Yann | 2:3bea48e1505c | 69 | /** Setup device interrupt mechanism |
Yann | 0:ebd3a7cc9b92 | 70 | * @param p_mirroring Set to 0x00 to disable INTA/B mirroring, 0x01 otherwise. Default: 0x00 |
Yann | 0:ebd3a7cc9b92 | 71 | * @param p_openDrain Set to 0x00 for active driver output, 0x01 for opn drain output. Default: 0x00 |
Yann | 0:ebd3a7cc9b92 | 72 | * @param p_polarity Set to 0x00 for interrupt active low, 0x01 otherwise. Default: 0x00 |
Yann | 0:ebd3a7cc9b92 | 73 | */ |
Yann | 0:ebd3a7cc9b92 | 74 | 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 | 75 | |
Yann | 2:3bea48e1505c | 76 | /** Setup interrupt mode for a specific IO port |
Yann | 0:ebd3a7cc9b92 | 77 | * @param p_gpioId The IO port identifier |
Yann | 0:ebd3a7cc9b92 | 78 | * @param p_mode The interrupt mode |
Yann | 2:3bea48e1505c | 79 | * @return 0 on success, -1 on wrong parameters and -2 otherwise |
Yann | 0:ebd3a7cc9b92 | 80 | */ |
Yann | 0:ebd3a7cc9b92 | 81 | virtual int setupInterruptPin(const unsigned char p_gpioId, const InterruptModes p_mode = OnRising) = 0; |
Yann | 0:ebd3a7cc9b92 | 82 | |
Yann | 2:3bea48e1505c | 83 | /** Setup pull mode for a specific IO port |
Yann | 2:3bea48e1505c | 84 | * @param p_gpioId The IO port identifier |
Yann | 2:3bea48e1505c | 85 | * @param p_mode The interrupt mode |
Yann | 2:3bea48e1505c | 86 | * @return 0 on success, -1 on wrong parameters and -2 otherwise |
Yann | 2:3bea48e1505c | 87 | */ |
Yann | 2:3bea48e1505c | 88 | virtual int setupPullPin(const unsigned char p_gpioId, const PullModes p_mode = PullOff) = 0; |
Yann | 2:3bea48e1505c | 89 | |
Yann | 0:ebd3a7cc9b92 | 90 | /** Get interrupt information and clear it |
Yann | 0:ebd3a7cc9b92 | 91 | * @param p_gpioId The IO port identifier where the interrupt occured |
Yann | 0:ebd3a7cc9b92 | 92 | * @param p_value The logic value on the pin port where the interrupt occured |
Yann | 2:3bea48e1505c | 93 | * @return 0 on success, -1 on wrong parameters and -2 otherwise |
Yann | 0:ebd3a7cc9b92 | 94 | */ |
Yann | 2:3bea48e1505c | 95 | virtual int getLastInterruptPinAndValue(unsigned char * p_gpioId, unsigned char * p_value) = 0; |
Yann | 0:ebd3a7cc9b92 | 96 | |
Yann | 0:ebd3a7cc9b92 | 97 | /** Attach a function to call when a interrupt occurs on the GPIOA input ports |
Yann | 2:3bea48e1505c | 98 | * @param p_fptr The pointer to the "C" callback function |
Yann | 0:ebd3a7cc9b92 | 99 | */ |
Yann | 0:ebd3a7cc9b92 | 100 | virtual void setIntrACallback(void (* p_fptr)(void)) = 0; |
Yann | 0:ebd3a7cc9b92 | 101 | |
Yann | 2:3bea48e1505c | 102 | /** Attach a function to call when a interrupt occurs on the GPIOB input ports |
Yann | 2:3bea48e1505c | 103 | * @param p_fptr The pointer to the "C" callback function |
Yann | 0:ebd3a7cc9b92 | 104 | */ |
Yann | 0:ebd3a7cc9b92 | 105 | virtual void setIntrBCallback(void (* p_fptr)(void)) = 0; |
Yann | 0:ebd3a7cc9b92 | 106 | |
Yann | 2:3bea48e1505c | 107 | /** Read the specific GPIO port pin |
Yann | 2:3bea48e1505c | 108 | * @param p_gpioId The GPIO port pin to be read |
Yann | 2:3bea48e1505c | 109 | * @param p_value The GPIO port pin value |
Yann | 2:3bea48e1505c | 110 | * @return 0 on success, -1 on wrong parameters and -2 otherwise |
Yann | 2:3bea48e1505c | 111 | */ |
Yann | 0:ebd3a7cc9b92 | 112 | virtual int read(const unsigned char p_gpioId, unsigned char * p_value) = 0; |
Yann | 2:3bea48e1505c | 113 | |
Yann | 2:3bea48e1505c | 114 | /** Write value to the specific GPIO port pin |
Yann | 2:3bea48e1505c | 115 | * @param p_gpioId The GPIO port pin to be written |
Yann | 2:3bea48e1505c | 116 | * @param p_value The GPIO port pin value |
Yann | 2:3bea48e1505c | 117 | * @return 0 on success, -1 on wrong parameters and -2 otherwise |
Yann | 2:3bea48e1505c | 118 | */ |
Yann | 0:ebd3a7cc9b92 | 119 | virtual int write(const unsigned char p_gpioId, const unsigned char p_value) = 0; |
Yann | 0:ebd3a7cc9b92 | 120 | |
Yann | 0:ebd3a7cc9b92 | 121 | virtual unsigned char createBus(const std::list<unsigned char> p_lines, const PinMode p_mode = PullNone) = 0; |
Yann | 0:ebd3a7cc9b92 | 122 | virtual void deleteBus(const unsigned char p_busId) = 0; |
Yann | 1:ec9e770173d5 | 123 | virtual int busRead(const unsigned char p_busId, unsigned short * p_value) = 0; |
Yann | 1:ec9e770173d5 | 124 | virtual int busWrite(const unsigned char p_busId, const unsigned short p_value) = 0; |
Yann | 0:ebd3a7cc9b92 | 125 | |
Yann | 0:ebd3a7cc9b92 | 126 | protected: |
Yann | 0:ebd3a7cc9b92 | 127 | virtual bool writeRegister(const unsigned char p_registerId, const unsigned char p_value) = 0; |
Yann | 0:ebd3a7cc9b92 | 128 | virtual bool readRegister(const unsigned char p_registerId, unsigned char * p_value) = 0; |
Yann | 0:ebd3a7cc9b92 | 129 | |
Yann | 1:ec9e770173d5 | 130 | }; // End of class AbstractGpioExpender |