The PCAL6416A is a low-voltage 16-bit general purpose I/O (GPIO) expander with interrupt. This component library is compatible to basic operation as GPIO expanders: PCAL6416A, PCAL9555, PCA9555, PCA9535, PCA9539, PCAL9554, PCA9554 and PCA9538. On addition to this, this library is including mbed-SDK-style APIs. APIs that similar to DigitaiInOut, DigitalOut, DigitalIn, BusInOUt, BusOut and BusIn are available.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GpioBusInOut.h Source File

GpioBusInOut.h

00001 /** GpioBusInOut API for GPIO-expander component class 
00002  *
00003  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
00004  *  @version 0.6
00005  *  @date    19-Mar-2015
00006  *
00007  *  Released under the Apache 2 license
00008  */
00009 
00010 #ifndef     MBED_GpioBusInOut
00011 #define     MBED_GpioBusInOut
00012 
00013 #include    "mbed.h"
00014 #include    "GpioDigitalInOut.h"
00015 
00016 /** GpioBusInOut class
00017  *
00018  *  @class GpioBusInOut
00019  *
00020  *  "GpioBusInOut" class works like "BusInOut" class of mbed-SDK. 
00021  *  This class provides pin oriented API, abstracting the GPIO-expander chip. 
00022  *
00023  *  Example:
00024  *  @code
00025  *  #include "mbed.h"
00026  *  #include "PCAL9555.h"
00027  *  
00028  *  PCAL9555        gpio_exp( p28, p27, 0xE8 );    //  SDA, SCL, Slave_address(option)
00029  *  GpioBusInOut    pins( gpio_exp, X0_0, X0_1, X0_2 );
00030  *   
00031  *  int main() {
00032  *      while(1) {
00033  *          pins.output();
00034  *          pins    = 0x3;
00035  *          wait( 1 );
00036  *          pins.input();
00037  *          wait( 1 );
00038  *          if( pins == 0x6 ) {
00039  *              printf( "Hello!\n" );
00040  *          }
00041  *      }
00042  *  } *  @endcode
00043  */
00044 class GpioBusInOut
00045 {
00046 public:
00047 
00048 #if DOXYGEN_ONLY
00049     /** GPIO-Expander pin names */
00050     typedef enum {
00051     X0_0,           /**< P0_0 pin                                   */
00052     X0_1,           /**< P0_1 pin                                   */
00053     X0_2,           /**< P0_2 pin                                   */
00054     X0_3,           /**< P0_3 pin                                   */
00055     X0_4,           /**< P0_4 pin                                   */
00056     X0_5,           /**< P0_5 pin                                   */
00057     X0_6,           /**< P0_6 pin                                   */
00058     X0_7,           /**< P0_7 pin                                   */
00059     X1_0,           /**< P1_0 pin (for 16-bit GPIO device only)     */
00060     X1_1,           /**< P1_1 pin (for 16-bit GPIO device only)     */
00061     X1_2,           /**< P1_2 pin (for 16-bit GPIO device only)     */
00062     X1_3,           /**< P1_3 pin (for 16-bit GPIO device only)     */
00063     X1_4,           /**< P1_4 pin (for 16-bit GPIO device only)     */
00064     X1_5,           /**< P1_5 pin (for 16-bit GPIO device only)     */
00065     X1_6,           /**< P1_6 pin (for 16-bit GPIO device only)     */
00066     X1_7,           /**< P1_7 pin (for 16-bit GPIO device only)     */
00067     X0  = X0_0,     /**< P0_0 pin                                   */
00068     X1  = X0_1,     /**< P0_1 pin                                   */
00069     X2  = X0_2,     /**< P0_2 pin                                   */
00070     X3  = X0_3,     /**< P0_3 pin                                   */
00071     X4  = X0_4,     /**< P0_4 pin                                   */
00072     X5  = X0_5,     /**< P0_5 pin                                   */
00073     X6  = X0_6,     /**< P0_6 pin                                   */
00074     X7  = X0_7,     /**< P0_7 pin                                   */
00075     X8  = X1_0,     /**< P1_0 pin (for 16-bit GPIO device only)     */
00076     X9  = X1_1,     /**< P1_1 pin (for 16-bit GPIO device only)     */
00077     X10 = X1_2,     /**< P1_2 pin (for 16-bit GPIO device only)     */
00078     X11 = X1_3,     /**< P1_3 pin (for 16-bit GPIO device only)     */
00079     X12 = X1_4,     /**< P1_4 pin (for 16-bit GPIO device only)     */
00080     X13 = X1_5,     /**< P1_5 pin (for 16-bit GPIO device only)     */
00081     X14 = X1_6,     /**< P1_6 pin (for 16-bit GPIO device only)     */
00082     X15 = X1_7,     /**< P1_7 pin (for 16-bit GPIO device only)     */
00083     
00084     X_NC = ~0x0L    /**< for when the pin is left no-connection     */
00085     } GpioPinName;
00086 #endif
00087 
00088     /** Create an GpioBusInOut, connected to the specified pins
00089      *
00090      *  @param gpiop    Instance of GPIO expander device
00091      *  @param p<n>     DigitalInOut pin to connect to bus bit p<n> (GpioPinName)
00092      *
00093      *  @note
00094      *    It is only required to specify as many pin variables as is required
00095      *    for the bus; the rest will default to NC (not connected)
00096      */
00097     GpioBusInOut( CompGpioExp &gpiop,
00098                   GpioPinName p0,         GpioPinName p1  = X_NC, GpioPinName p2  = X_NC, GpioPinName p3  = X_NC,
00099                   GpioPinName p4  = X_NC, GpioPinName p5  = X_NC, GpioPinName p6  = X_NC, GpioPinName p7  = X_NC,
00100                   GpioPinName p8  = X_NC, GpioPinName p9  = X_NC, GpioPinName p10 = X_NC, GpioPinName p11 = X_NC,
00101                   GpioPinName p12 = X_NC, GpioPinName p13 = X_NC, GpioPinName p14 = X_NC, GpioPinName p15 = X_NC );
00102     GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[ 16 ] );
00103     
00104     /**
00105      *  Destractor
00106      */
00107     virtual ~GpioBusInOut();
00108 
00109     /* Group: Access Methods */
00110 
00111     /** Write the value to the output bus
00112      *
00113      *  @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
00114      */
00115     void write( int value );
00116 
00117     /** Read the value currently output on the bus
00118      *
00119      *  @returns
00120      *    An integer with each bit corresponding to associated DigitalInOut pin setting
00121      */
00122     int read();
00123 
00124     /** Set as an output
00125      */
00126     void output();
00127 
00128     /** Set as an input
00129      */
00130     void input();
00131 
00132     /** A shorthand for write()
00133      */
00134     GpioBusInOut&   operator= ( int rhs );
00135     GpioBusInOut&   operator= ( GpioBusInOut& rhs );
00136 
00137     /** A shorthand for read()
00138      */
00139     operator int( void );
00140 
00141 protected:
00142     CompGpioExp     *gpio_p;
00143     int             pin_list[ 16 ];
00144     int             mask_bits;
00145 
00146     void    init( CompGpioExp &gpiop, GpioPinName pins[16] );
00147     int     make_bitpattern( int value );
00148 }
00149 ;
00150 
00151 #endif  //  MBED_GpioBusInOut