The PCAL9555, PCAL9554 series is a low-voltage 16-bit/8-bit General Purpose Input/Output (GPIO) expander with interrupt. This conponent library is compatible to basic operation os GPIO expanders: 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.

Dependents:   PCAL9555_Hello OM13082-JoyStick OM13082_LED OM13082-test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCAL9555.h Source File

PCAL9555.h

00001 /** PCA(L)9555 16-bit I2C-bus GPIO expander
00002  *
00003  *  An operation sample of PCA(L)9555, PCA9535 and PCA9539.
00004  *  mbed accesses the PCAL9555 registers through I2C.
00005  *
00006  *  @class   PCAL9555
00007  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
00008  *  @version 0.6
00009  *  @date    19-Mar-2015
00010  *
00011  *  Released under the Apache 2 license
00012  *
00013  *  About PCAL9555:
00014  *    http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_general_purpose_i_o/series/PCAL9555A.html
00015  */
00016 
00017 #ifndef     MBED_PCAL9555
00018 #define     MBED_PCAL9555
00019 
00020 #include    "mbed.h"
00021 #include    "PCAL955x.h"
00022 #include    "CompGpioExpAPI.h"
00023 
00024 /** PCAL9555 class
00025  *
00026  *  This is a driver code for the Low-voltage 16-bit I2C-bus GPIO with Agile I/O.
00027  *  This class provides interface for PCAL9555 operation.
00028  *  Detail information is available on next URL.
00029  *    http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_general_purpose_i_o/series/PCAL9555A.html
00030  *  
00031  *  PCAL9555 library's basic IO operation is compatible to PCA9555, PCA9535 and PCA9539.
00032  *  This library can be used for those GPIO expander chips also.
00033  *  Next sample code shows operation based on low-level-API (operated by just device instane)
00034  *  
00035  *  Example:
00036  *  @code
00037  *  //  GPIO-expander operation sample using a device instance
00038  *  
00039  *  #include    "mbed.h"
00040  *  #include    "PCAL9555.h"
00041  *
00042  *  PCAL9555    gpio( p28, p27, 0xE8 );     //  using PCA9539
00043  *
00044  *  int main() {
00045  *      gpio.configure( 0xFFFF );           //  Set all pins: input
00046  *      printf( "  0x%04X\r\n", (int)gpio );//  Print pins state
00047  *
00048  *      gpio.configure( 0x0000 );           //  Set all pins: output
00049  *      int count   = 0;
00050  *      while(1) {
00051  *          gpio.write( count++ );
00052  *      }
00053  *  }
00054  *  @endcode
00055  *
00056  *  GpioDigitalInOut, GpioDigitalOut, GpioDigitalIn,
00057  *  GpioBusInOut, GpioBusOut and GpioBusIn API class are available also.
00058  *  For those high-level-API details, please find those class library page.
00059  *  The GpioDigital* and GpioBus* APIs can be used like next sample code.
00060  *
00061  *  @code
00062  *  //  GPIO-expander operation sample using high-level-API
00063  *  
00064  *  #include    "mbed.h"
00065  *  #include    "PCAL9555.h"
00066  *
00067  *  PCAL9555        gpio( p28, p27, 0xE8 );     //  using PCA9539
00068  *
00069  *  //  The GPIO pins are grouped in some groups and operated as bus I/O
00070  *  GpioBusIn       bus_in( gpio, X0_0, X0_1, X0_2, X0_3 );
00071  *  GpioBusOut      bus_out( gpio, X0_4, X0_5, X0_6 );
00072  *  GpioBusInOut    bus_io( gpio, X1_7, X1_6, X1_5, X1_4, X1_3, X1_2, X1_1, X1_0 );
00073  *  GpioDigitalOut  myled( gpio, X0_7 );
00074  *
00075  *  int main() {
00076  *      bus_io.input();
00077  *      printf( "I/O = 0x%02X\r\n", (int)bus_io );
00078  *      printf( "In  = 0x%01X\r\n", (int)bus_in );
00079  *
00080  *      bus_io.output();
00081  *
00082  *      int count   = 0;
00083  *      while(1) {
00084  *          bus_out = count;
00085  *          bus_io  = count;
00086  *          myled   = count & 0x1;
00087  *          count++;
00088  *          wait( 0.1 );
00089  *      }
00090  *  }
00091  *  @endcode
00092  */
00093 
00094 class PCAL9555 : public PCAL955x
00095 {
00096 public:
00097     /** Name of the PCAL9555 registers */
00098     enum command_reg {
00099         InputPort0              = 0x00, /**< InputPort0 register                */
00100         InputPort1,                     /**< InputPort1 register                */
00101         OutoutPort0,                    /**< OutoutPort0 register               */
00102         OutoutPort1,                    /**< OutoutPort1 register               */
00103         PolarityInversionPort0,         /**< PolarityInversionPort0 register    */
00104         PolarityInversionPort1,         /**< PolarityInversionPort1 register    */
00105         ConfigurationPort0,             /**< ConfigurationPort0 register        */
00106         ConfigurationPort1,             /**< ConfigurationPort1 register        */
00107         OutputDriveStrength0_0  = 0x40, /**< OutputDriveStrength0_0 register    */
00108         OutputDriveStrength0_1,         /**< OutputDriveStrength0_1 register    */
00109         OutputDriveStrength1_0,         /**< OutputDriveStrength1_0 register    */
00110         OutputDriveStrength1_1,         /**< OutputDriveStrength1_1 register    */
00111         InputLatch0,                    /**< InputLatch0 register               */
00112         InputLatch1,                    /**< InputLatch1 register               */
00113         PullUpPullDowmEnable0,          /**< PullUpPullDowmEnable0 register     */
00114         PullUpPullDowmEnable1,          /**< PullUpPullDowmEnable1 register     */
00115         PullUpPullDowmSelection0,       /**< PullUpPullDowmSelection0 register  */
00116         PullUpPullDowmSelection1,       /**< PullUpPullDowmSelection1 register  */
00117         InterruptMask0,                 /**< InterruptMask0 register            */
00118         InterruptMask1,                 /**< InterruptMask1 register            */
00119         InterruptStatus0,               /**< InterruptStatus0 register          */
00120         InterruptStatus1,               /**< InterruptStatus1 register          */
00121         OutputPortConfiguration = 0x4F, /**< OutputPortConfiguration register   */
00122     };
00123 
00124 #if DOXYGEN_ONLY
00125     /** GPIO-Expander pin names
00126      *    for when the high-level APIs 
00127      *    (GpioDigitalOut, GpioDigitalInOut, GpioDigitalIn, 
00128      *    GpioBusOut, GpioBusInOut are GpioBusIn) are used
00129      */
00130     typedef enum {
00131         X0_0,           /**< P0_0 pin */
00132         X0_1,           /**< P0_1 pin */
00133         X0_2,           /**< P0_2 pin */
00134         X0_3,           /**< P0_3 pin */
00135         X0_4,           /**< P0_4 pin */
00136         X0_5,           /**< P0_5 pin */
00137         X0_6,           /**< P0_6 pin */
00138         X0_7,           /**< P0_7 pin */
00139         X1_0,           /**< P1_0 pin */
00140         X1_1,           /**< P1_1 pin */
00141         X1_2,           /**< P1_2 pin */
00142         X1_3,           /**< P1_3 pin */
00143         X1_4,           /**< P1_4 pin */
00144         X1_5,           /**< P1_5 pin */
00145         X1_6,           /**< P1_6 pin */
00146         X1_7,           /**< P1_7 pin */
00147         X0  = X0_0,     /**< P0_0 pin */
00148         X1  = X0_1,     /**< P0_1 pin */
00149         X2  = X0_2,     /**< P0_2 pin */
00150         X3  = X0_3,     /**< P0_3 pin */
00151         X4  = X0_4,     /**< P0_4 pin */
00152         X5  = X0_5,     /**< P0_5 pin */
00153         X6  = X0_6,     /**< P0_6 pin */
00154         X7  = X0_7,     /**< P0_7 pin */
00155         X8  = X1_0,     /**< P1_0 pin */
00156         X9  = X1_1,     /**< P1_1 pin */
00157         X10 = X1_2,     /**< P1_2 pin */
00158         X11 = X1_3,     /**< P1_3 pin */
00159         X12 = X1_4,     /**< P1_4 pin */
00160         X13 = X1_5,     /**< P1_5 pin */
00161         X14 = X1_6,     /**< P1_6 pin */
00162         X15 = X1_7,     /**< P1_7 pin */
00163 
00164         X_NC = ~0x0L    /**< for when the pin is left no-connection */
00165     } GpioPinName;
00166 #endif
00167 
00168     /** Create a PCAL9555 instance connected to specified I2C pins with specified address
00169      *
00170      * @param i2c_sda       I2C-bus SDA pin
00171      * @param i2c_sda       I2C-bus SCL pin
00172      * @param i2c_address   I2C-bus address (default: 0x40)
00173      */
00174     PCAL9555( PinName i2c_sda, PinName i2c_scl, char i2c_address = PCAL955x::DEFAULT_I2C_ADDR );
00175 
00176     /** Create a PCAL9555 instance connected to specified I2C pins with specified address
00177      *
00178      * @param i2c_obj       I2C object (instance)
00179      * @param i2c_address   I2C-bus address (default: 0x40)
00180      */
00181     PCAL9555( I2C &i2c_obj, char i2c_address = PCAL955x::DEFAULT_I2C_ADDR );
00182 
00183     /** Destractor
00184      */
00185     virtual         ~PCAL9555();
00186 
00187     /** Returns the number of I/O pins
00188      *
00189      *  @returns
00190      *    The number of I/O pins
00191      */
00192     virtual int     number_of_pins( void );
00193 
00194 #if DOXYGEN_ONLY
00195 
00196     /** Set output port bits
00197      *
00198      *  @param bit_pattern  16-bit output pattern for port1 and port0.
00199      *
00200      *  @note
00201      *    The data for pins, given as integer.
00202      *    The 16-bit MSB goes to P1_7 pin and LSB goes to P0_0 pin.
00203      *    Data will not come out from the pin if it is configured as input.
00204      *
00205      *  @see configure()
00206      */
00207     void            write( int bit_pattern );
00208 
00209     /** Read pin states
00210      *
00211      *  @return
00212      *    16-bit pattern from port1 and port0.
00213      *
00214      *  @note
00215      *    The data from pins, given as integer.
00216      *    The 16-bit port data comes from IO pins, P1_7 as MSB, P0_0 as LSB.
00217      *    Data cannot be read if the port is configured as output.
00218      *
00219      *  @see configure()
00220      */
00221     int             read( void );
00222 
00223     /** Polarity setting
00224      *
00225      *  @param bit_pattern  16-bit polarity setting pattern for port1 and port0.
00226      *    If the bit is set to '1', the state will be inverted.
00227      *    (Default state is all '0')
00228      *
00229      *  @see configure()
00230      */
00231     void            polarity( int bit_pattern );
00232 
00233     /** Set IO congiguration
00234      *
00235      *  @param bit_pattern  16-bit IO direction setting pattern for port1 and port0.
00236      *
00237      *  @note
00238      *    The data for pins, given as integer.
00239      *    The 16-bit MSB goes to P1_7 pin and LSB goes to P0_0 pin.
00240      *    If the bit is set to '1', the pin will be input.
00241      *
00242      *  @see write()
00243      *  @see read()
00244      */
00245     void            configure( int bit_pattern );
00246 
00247     /** Set interrupt mask
00248      *
00249      *  @param bit_pattern  16-bit interrupt mask
00250      *
00251      *  @see interrupt_status()
00252      */
00253     void            interrupt_mask( int bit_pattern );
00254 
00255     /** Read interrupt status
00256      *
00257      *  @return
00258      *    16-bit data from interrupt status registers
00259      *
00260      *  @see interrupt_status()
00261      */
00262     int             interrupt_status( void );
00263 
00264     /** A shorthand for read()
00265      */
00266     operator int( void );
00267 
00268 #endif
00269 
00270     /** Write 16-bit data into registers
00271      *
00272      *  @param reg_index    Direst access to registers.
00273      *    The registers can be referenced by regiser index
00274      *  @param data  16-bit data. Data will be written into two 8-bit registers
00275      */
00276     virtual void    reg_index_write( char register_index, int data );
00277 
00278     /** Read 16-bit data from registers
00279      *
00280      *  @param reg_index    Direst access to registers.
00281      *    The registers can be referenced by regiser index
00282      *
00283      *  @return
00284      *    16-bit data from two registers.
00285      *    The register which has lower address will be upper 8-bit data.
00286      */
00287     virtual int     reg_index_read( char register_index );
00288 
00289     /** A shorthand for write()
00290      */
00291     PCAL9555&       operator= ( int bit_pattern );
00292     PCAL9555&       operator= ( PCAL9555& rhs );
00293 
00294 private:
00295     /** Register index name */
00296     enum RegisterIndex {
00297         InputPort               = InputPort0,
00298         OutoutPort              = OutoutPort0,
00299         PolarityInversionPort   = PolarityInversionPort0,
00300         ConfigurationPort       = ConfigurationPort0,
00301         OutputDriveStrength0    = OutputDriveStrength0_0,
00302         OutputDriveStrength1    = OutputDriveStrength1_0,
00303         InputLatch              = InputLatch0,
00304         PullUpPullDowmEnable    = PullUpPullDowmEnable0,
00305         PullUpPullDowmSelection = PullUpPullDowmSelection0,
00306         InterruptMask           = InterruptMask0,
00307         InterruptStatus         = InterruptStatus0
00308     };
00309 
00310     static const char   regmap[];
00311     const int           n_of_pins;
00312 }
00313 ;
00314 
00315 #endif  //  MBED_PCAL9555