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

What is this?

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.

How to use?

Wiring

/media/uploads/nxp_ip/gpio16.png
Wiring between mbed and 16-bit GPIO expander

/media/uploads/nxp_ip/gpio08.png
Wiring between mbed and 8-bit GPIO expander

Very basic register level I/O bit operation

PCAL9555 and PCAL9554 are class libraries for those compatible GPIO expander chips.
Those class libraries provides interface for bit operation of its I/O port.
For 16-bit GPIO expanders, the input/output access and its direction setting can be done by 16-bit data. For 8-bit GPIO expanders, those can be done by 8-bit data.

  #include    "mbed.h"
  #include    "PCAL9555.h"
 
  PCAL9555    gpio( p28, p27, 0xE8 );     //  using PCA9539
 
  int main() {
      gpio.configure( 0xFFFF );           //  Set all pins: input
      printf( "  0x%04X\r\n", (int)gpio );//  Print pins state
 
      gpio.configure( 0x0000 );           //  Set all pins: output
      int count   = 0;
      while(1) {
          gpio.write( count++ );
      }
  }


High level APIs

To use the GPIO expanders more simple, this library is including mbed-SDK-style APIs.
APIs that similar to DigitaiInOut, DigitalOut, DigitalIn, BusInOut, BusOut and BusIn are available.

GpioDigitalOut, GpioDigitaiInOut, GpioDigitalIn

Next code shows sample of DigitalOut equivalent API GpioDigitalOut usage.
A pin on the PCAL9555 is defined as pin and its state is changed by assignment.
(For single pin operation, GpioDigitaiInOut and GpioDigitalIn are available also.)

  #include "mbed.h"
  #include "PCAL9555.h"
  
  PCAL9555        gpio_exp( p28, p27, 0xE8 );    //  SDA, SCL, Slave_address(option)
  GpioDigitalOut  pin( gpio_exp, X0_0 );
   
  int main() {
      while( 1 ) {
          pin = 1;
          wait( 0.2 );
          pin = 0;
          wait( 0.2 );
      }
  }


GpioBusOut, GpioBusInOut, GpioBusIn

BusOut equivalent API GpioBusOut is available too.
In next code, pins are grouped as mypins to manage the output as bus output.
(Same as GpioDigitalInOut and GpioDigitalIn APIs, GpioBusInOut and GpioBusIn are available also.)

  #include "mbed.h"
  #include "PCAL9555.h"
  
  PCAL9555    gpio_exp( p28, p27, 0xE8 );    //  SDA, SCL, Slave_address(option)
  GpioBusOut  mypins( gpio_exp, X0_0, X0_1, X0_2, X0_3 );
   
  int main() {
      while( 1 ) {
          for( int i = 0; i < 16; i++ ) {
              mypins  = i;
              wait( 0.25 );
          }
      }
  }


The high level APIs can be used in combination

Those high level APIs can be used in combination.
Each pins can be managed by instances.

  #include    "mbed.h"
  #include    "PCAL9555.h"
 
  PCAL9555        gpio( p28, p27, 0xE8 );     //  using PCA9539
 
  //  The GPIO pins are grouped in some groups and operated as bus I/O
  GpioBusIn       bus_in( gpio, X0_0, X0_1, X0_2, X0_3 );
  GpioBusOut      bus_out( gpio, X0_4, X0_5, X0_6 );
  GpioBusInOut    bus_io( gpio, X1_7, X1_6, X1_5, X1_4, X1_3, X1_2, X1_1, X1_0 );
  GpioDigitalOut  myled( gpio, X0_7 );
 
  int main() {
      bus_io.input();
      printf( "I/O = 0x%02X\r\n", (int)bus_io );
      printf( "In  = 0x%01X\r\n", (int)bus_in );
 
      bus_io.output();
 
      int count   = 0;
      while(1) {
          bus_out = count;
          bus_io  = count;
          myled   = count & 0x1;
          count++;
          wait( 0.1 );
      }
  }
Committer:
nxp_ip
Date:
Thu Mar 19 02:21:57 2015 +0000
Revision:
1:fd7cfa821b6a
Parent:
0:6c9a51a50eea
API document update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:6c9a51a50eea 1 /** GpioBusInOut API for GPIO-expander component class
nxp_ip 0:6c9a51a50eea 2 *
nxp_ip 0:6c9a51a50eea 3 * @author Akifumi (Tedd) OKANO, NXP Semiconductors
nxp_ip 1:fd7cfa821b6a 4 * @version 0.6
nxp_ip 1:fd7cfa821b6a 5 * @date 19-Mar-2015
nxp_ip 0:6c9a51a50eea 6 *
nxp_ip 0:6c9a51a50eea 7 * Released under the Apache 2 license
nxp_ip 0:6c9a51a50eea 8 */
nxp_ip 0:6c9a51a50eea 9
nxp_ip 0:6c9a51a50eea 10 #ifndef MBED_GpioBusInOut
nxp_ip 0:6c9a51a50eea 11 #define MBED_GpioBusInOut
nxp_ip 0:6c9a51a50eea 12
nxp_ip 0:6c9a51a50eea 13 #include "mbed.h"
nxp_ip 0:6c9a51a50eea 14 #include "GpioDigitalInOut.h"
nxp_ip 0:6c9a51a50eea 15
nxp_ip 0:6c9a51a50eea 16 /** GpioBusInOut class
nxp_ip 0:6c9a51a50eea 17 *
nxp_ip 0:6c9a51a50eea 18 * @class GpioBusInOut
nxp_ip 0:6c9a51a50eea 19 *
nxp_ip 0:6c9a51a50eea 20 * "GpioBusInOut" class works like "BusInOut" class of mbed-SDK.
nxp_ip 0:6c9a51a50eea 21 * This class provides pin oriented API, abstracting the GPIO-expander chip.
nxp_ip 0:6c9a51a50eea 22 *
nxp_ip 0:6c9a51a50eea 23 * Example:
nxp_ip 0:6c9a51a50eea 24 * @code
nxp_ip 0:6c9a51a50eea 25 * #include "mbed.h"
nxp_ip 0:6c9a51a50eea 26 * #include "PCAL9555.h"
nxp_ip 0:6c9a51a50eea 27 *
nxp_ip 0:6c9a51a50eea 28 * PCAL9555 gpio_exp( p28, p27, 0xE8 ); // SDA, SCL, Slave_address(option)
nxp_ip 0:6c9a51a50eea 29 * GpioBusInOut pins( gpio_exp, X0_0, X0_1, X0_2 );
nxp_ip 0:6c9a51a50eea 30 *
nxp_ip 0:6c9a51a50eea 31 * int main() {
nxp_ip 0:6c9a51a50eea 32 * while(1) {
nxp_ip 0:6c9a51a50eea 33 * pins.output();
nxp_ip 0:6c9a51a50eea 34 * pins = 0x3;
nxp_ip 0:6c9a51a50eea 35 * wait( 1 );
nxp_ip 0:6c9a51a50eea 36 * pins.input();
nxp_ip 0:6c9a51a50eea 37 * wait( 1 );
nxp_ip 0:6c9a51a50eea 38 * if( pins == 0x6 ) {
nxp_ip 0:6c9a51a50eea 39 * printf( "Hello!\n" );
nxp_ip 0:6c9a51a50eea 40 * }
nxp_ip 0:6c9a51a50eea 41 * }
nxp_ip 0:6c9a51a50eea 42 * } * @endcode
nxp_ip 0:6c9a51a50eea 43 */
nxp_ip 0:6c9a51a50eea 44 class GpioBusInOut
nxp_ip 0:6c9a51a50eea 45 {
nxp_ip 0:6c9a51a50eea 46 public:
nxp_ip 0:6c9a51a50eea 47
nxp_ip 1:fd7cfa821b6a 48 #if DOXYGEN_ONLY
nxp_ip 1:fd7cfa821b6a 49 /** GPIO-Expander pin names */
nxp_ip 1:fd7cfa821b6a 50 typedef enum {
nxp_ip 1:fd7cfa821b6a 51 X0_0, /**< P0_0 pin */
nxp_ip 1:fd7cfa821b6a 52 X0_1, /**< P0_1 pin */
nxp_ip 1:fd7cfa821b6a 53 X0_2, /**< P0_2 pin */
nxp_ip 1:fd7cfa821b6a 54 X0_3, /**< P0_3 pin */
nxp_ip 1:fd7cfa821b6a 55 X0_4, /**< P0_4 pin */
nxp_ip 1:fd7cfa821b6a 56 X0_5, /**< P0_5 pin */
nxp_ip 1:fd7cfa821b6a 57 X0_6, /**< P0_6 pin */
nxp_ip 1:fd7cfa821b6a 58 X0_7, /**< P0_7 pin */
nxp_ip 1:fd7cfa821b6a 59 X1_0, /**< P1_0 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 60 X1_1, /**< P1_1 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 61 X1_2, /**< P1_2 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 62 X1_3, /**< P1_3 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 63 X1_4, /**< P1_4 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 64 X1_5, /**< P1_5 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 65 X1_6, /**< P1_6 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 66 X1_7, /**< P1_7 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 67 X0 = X0_0, /**< P0_0 pin */
nxp_ip 1:fd7cfa821b6a 68 X1 = X0_1, /**< P0_1 pin */
nxp_ip 1:fd7cfa821b6a 69 X2 = X0_2, /**< P0_2 pin */
nxp_ip 1:fd7cfa821b6a 70 X3 = X0_3, /**< P0_3 pin */
nxp_ip 1:fd7cfa821b6a 71 X4 = X0_4, /**< P0_4 pin */
nxp_ip 1:fd7cfa821b6a 72 X5 = X0_5, /**< P0_5 pin */
nxp_ip 1:fd7cfa821b6a 73 X6 = X0_6, /**< P0_6 pin */
nxp_ip 1:fd7cfa821b6a 74 X7 = X0_7, /**< P0_7 pin */
nxp_ip 1:fd7cfa821b6a 75 X8 = X1_0, /**< P1_0 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 76 X9 = X1_1, /**< P1_1 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 77 X10 = X1_2, /**< P1_2 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 78 X11 = X1_3, /**< P1_3 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 79 X12 = X1_4, /**< P1_4 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 80 X13 = X1_5, /**< P1_5 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 81 X14 = X1_6, /**< P1_6 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 82 X15 = X1_7, /**< P1_7 pin (for 16-bit GPIO device only) */
nxp_ip 1:fd7cfa821b6a 83
nxp_ip 1:fd7cfa821b6a 84 X_NC = ~0x0L /**< for when the pin is left no-connection */
nxp_ip 1:fd7cfa821b6a 85 } GpioPinName;
nxp_ip 1:fd7cfa821b6a 86 #endif
nxp_ip 1:fd7cfa821b6a 87
nxp_ip 0:6c9a51a50eea 88 /** Create an GpioBusInOut, connected to the specified pins
nxp_ip 0:6c9a51a50eea 89 *
nxp_ip 0:6c9a51a50eea 90 * @param gpiop Instance of GPIO expander device
nxp_ip 0:6c9a51a50eea 91 * @param p<n> DigitalInOut pin to connect to bus bit p<n> (GpioPinName)
nxp_ip 0:6c9a51a50eea 92 *
nxp_ip 0:6c9a51a50eea 93 * @note
nxp_ip 0:6c9a51a50eea 94 * It is only required to specify as many pin variables as is required
nxp_ip 0:6c9a51a50eea 95 * for the bus; the rest will default to NC (not connected)
nxp_ip 0:6c9a51a50eea 96 */
nxp_ip 0:6c9a51a50eea 97 GpioBusInOut( CompGpioExp &gpiop,
nxp_ip 0:6c9a51a50eea 98 GpioPinName p0, GpioPinName p1 = X_NC, GpioPinName p2 = X_NC, GpioPinName p3 = X_NC,
nxp_ip 0:6c9a51a50eea 99 GpioPinName p4 = X_NC, GpioPinName p5 = X_NC, GpioPinName p6 = X_NC, GpioPinName p7 = X_NC,
nxp_ip 0:6c9a51a50eea 100 GpioPinName p8 = X_NC, GpioPinName p9 = X_NC, GpioPinName p10 = X_NC, GpioPinName p11 = X_NC,
nxp_ip 0:6c9a51a50eea 101 GpioPinName p12 = X_NC, GpioPinName p13 = X_NC, GpioPinName p14 = X_NC, GpioPinName p15 = X_NC );
nxp_ip 0:6c9a51a50eea 102 GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[ 16 ] );
nxp_ip 0:6c9a51a50eea 103
nxp_ip 0:6c9a51a50eea 104 /**
nxp_ip 0:6c9a51a50eea 105 * Destractor
nxp_ip 0:6c9a51a50eea 106 */
nxp_ip 0:6c9a51a50eea 107 virtual ~GpioBusInOut();
nxp_ip 0:6c9a51a50eea 108
nxp_ip 0:6c9a51a50eea 109 /* Group: Access Methods */
nxp_ip 0:6c9a51a50eea 110
nxp_ip 0:6c9a51a50eea 111 /** Write the value to the output bus
nxp_ip 0:6c9a51a50eea 112 *
nxp_ip 0:6c9a51a50eea 113 * @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
nxp_ip 0:6c9a51a50eea 114 */
nxp_ip 0:6c9a51a50eea 115 void write( int value );
nxp_ip 0:6c9a51a50eea 116
nxp_ip 0:6c9a51a50eea 117 /** Read the value currently output on the bus
nxp_ip 0:6c9a51a50eea 118 *
nxp_ip 0:6c9a51a50eea 119 * @returns
nxp_ip 0:6c9a51a50eea 120 * An integer with each bit corresponding to associated DigitalInOut pin setting
nxp_ip 0:6c9a51a50eea 121 */
nxp_ip 0:6c9a51a50eea 122 int read();
nxp_ip 0:6c9a51a50eea 123
nxp_ip 0:6c9a51a50eea 124 /** Set as an output
nxp_ip 0:6c9a51a50eea 125 */
nxp_ip 0:6c9a51a50eea 126 void output();
nxp_ip 0:6c9a51a50eea 127
nxp_ip 0:6c9a51a50eea 128 /** Set as an input
nxp_ip 0:6c9a51a50eea 129 */
nxp_ip 0:6c9a51a50eea 130 void input();
nxp_ip 0:6c9a51a50eea 131
nxp_ip 0:6c9a51a50eea 132 /** A shorthand for write()
nxp_ip 0:6c9a51a50eea 133 */
nxp_ip 0:6c9a51a50eea 134 GpioBusInOut& operator= ( int rhs );
nxp_ip 0:6c9a51a50eea 135 GpioBusInOut& operator= ( GpioBusInOut& rhs );
nxp_ip 0:6c9a51a50eea 136
nxp_ip 0:6c9a51a50eea 137 /** A shorthand for read()
nxp_ip 0:6c9a51a50eea 138 */
nxp_ip 0:6c9a51a50eea 139 operator int( void );
nxp_ip 0:6c9a51a50eea 140
nxp_ip 0:6c9a51a50eea 141 protected:
nxp_ip 0:6c9a51a50eea 142 CompGpioExp *gpio_p;
nxp_ip 0:6c9a51a50eea 143 int pin_list[ 16 ];
nxp_ip 0:6c9a51a50eea 144 int mask_bits;
nxp_ip 0:6c9a51a50eea 145
nxp_ip 0:6c9a51a50eea 146 void init( CompGpioExp &gpiop, GpioPinName pins[16] );
nxp_ip 0:6c9a51a50eea 147 int make_bitpattern( int value );
nxp_ip 0:6c9a51a50eea 148 }
nxp_ip 0:6c9a51a50eea 149 ;
nxp_ip 0:6c9a51a50eea 150
nxp_ip 0:6c9a51a50eea 151 #endif // MBED_GpioBusInOut