This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender
Diff: AbstractGpioExpender.h
- Revision:
- 2:3bea48e1505c
- Parent:
- 1:ec9e770173d5
--- a/AbstractGpioExpender.h Fri Jan 09 15:35:40 2015 +0000
+++ b/AbstractGpioExpender.h Tue Jan 13 10:09:01 2015 +0000
@@ -1,3 +1,21 @@
+/* mbed simplified interface for GPIO expender (e.g. Microchip MCP23x17)
+ * Copyright (c) 2014-2015 ygarcia, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
#pragma once
#include <list>
@@ -24,6 +42,8 @@
#define GPIO_MED GPB0
#define GPIO_SIZE 8
+/** This class describes a simplified interface for GPIO expender (e.g. Microchip MCP23x17)
+ */
class AbstractGpioExpender {
public:
/** Interrupt modes
@@ -34,40 +54,68 @@
OnFalling = 0x03
};
+ /** Pull modes
+ */
+ enum PullModes {
+ PullOff = 0x00,
+ PullUp = 0x01,
+ PullDown = 0x03
+ };
+
/** Reset the device
*/
virtual void reset() = 0;
- /** Setup interrupt mechanism for all GPIO ports
+ /** Setup device interrupt mechanism
* @param p_mirroring Set to 0x00 to disable INTA/B mirroring, 0x01 otherwise. Default: 0x00
* @param p_openDrain Set to 0x00 for active driver output, 0x01 for opn drain output. Default: 0x00
* @param p_polarity Set to 0x00 for interrupt active low, 0x01 otherwise. Default: 0x00
*/
virtual void setupInterrupts(const unsigned char p_mirroring = 0x00, const unsigned char p_openDrain = 0x00, const unsigned char p_polarity = 0x00) = 0;
- /** Setup interrupt for a specific IO port
+ /** Setup interrupt mode for a specific IO port
* @param p_gpioId The IO port identifier
* @param p_mode The interrupt mode
- * @return 0 on success, negative value otherwise
+ * @return 0 on success, -1 on wrong parameters and -2 otherwise
*/
virtual int setupInterruptPin(const unsigned char p_gpioId, const InterruptModes p_mode = OnRising) = 0;
+ /** Setup pull mode for a specific IO port
+ * @param p_gpioId The IO port identifier
+ * @param p_mode The interrupt mode
+ * @return 0 on success, -1 on wrong parameters and -2 otherwise
+ */
+ virtual int setupPullPin(const unsigned char p_gpioId, const PullModes p_mode = PullOff) = 0;
+
/** Get interrupt information and clear it
* @param p_gpioId The IO port identifier where the interrupt occured
* @param p_value The logic value on the pin port where the interrupt occured
- * @return 0 on success, negative value otherwise
+ * @return 0 on success, -1 on wrong parameters and -2 otherwise
*/
- virtual int getLastInterruptPin(unsigned char * p_gpioId, unsigned char * p_value) = 0;
+ virtual int getLastInterruptPinAndValue(unsigned char * p_gpioId, unsigned char * p_value) = 0;
/** Attach a function to call when a interrupt occurs on the GPIOA input ports
+ * @param p_fptr The pointer to the "C" callback function
*/
virtual void setIntrACallback(void (* p_fptr)(void)) = 0;
- /** Attach a function to call when a rising edge occurs on the input
+ /** Attach a function to call when a interrupt occurs on the GPIOB input ports
+ * @param p_fptr The pointer to the "C" callback function
*/
virtual void setIntrBCallback(void (* p_fptr)(void)) = 0;
+ /** Read the specific GPIO port pin
+ * @param p_gpioId The GPIO port pin to be read
+ * @param p_value The GPIO port pin value
+ * @return 0 on success, -1 on wrong parameters and -2 otherwise
+ */
virtual int read(const unsigned char p_gpioId, unsigned char * p_value) = 0;
+
+ /** Write value to the specific GPIO port pin
+ * @param p_gpioId The GPIO port pin to be written
+ * @param p_value The GPIO port pin value
+ * @return 0 on success, -1 on wrong parameters and -2 otherwise
+ */
virtual int write(const unsigned char p_gpioId, const unsigned char p_value) = 0;
virtual unsigned char createBus(const std::list<unsigned char> p_lines, const PinMode p_mode = PullNone) = 0;
Yann Garcia