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 PCAL9554.h Source File

PCAL9554.h

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