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:
Sat Mar 07 12:56:05 2015 +0000
Revision:
0:6c9a51a50eea
Child:
1:fd7cfa821b6a
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:6c9a51a50eea 1 /** GpioDigitalInOut API for GPIO-expander component class
nxp_ip 0:6c9a51a50eea 2 *
nxp_ip 0:6c9a51a50eea 3 * @author Akifumi (Tedd) OKANO, NXP Semiconductors
nxp_ip 0:6c9a51a50eea 4 * @version 0.5
nxp_ip 0:6c9a51a50eea 5 * @date 07-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_GpioDigitalInOut
nxp_ip 0:6c9a51a50eea 11 #define MBED_GpioDigitalInOut
nxp_ip 0:6c9a51a50eea 12
nxp_ip 0:6c9a51a50eea 13 #include "mbed.h"
nxp_ip 0:6c9a51a50eea 14 #include "CompGpioExp.h"
nxp_ip 0:6c9a51a50eea 15 #include "GpioDigitalInOut.h"
nxp_ip 0:6c9a51a50eea 16
nxp_ip 0:6c9a51a50eea 17 /** GpioDigitalInOut class
nxp_ip 0:6c9a51a50eea 18 *
nxp_ip 0:6c9a51a50eea 19 * @class GpioDigitalInOut
nxp_ip 0:6c9a51a50eea 20 *
nxp_ip 0:6c9a51a50eea 21 * "GpioDigitalInOut" class works like "DigitalInOut" class of mbed-SDK.
nxp_ip 0:6c9a51a50eea 22 * This class provides pin oriented API, abstracting the GPIO-expander chip.
nxp_ip 0:6c9a51a50eea 23 *
nxp_ip 0:6c9a51a50eea 24 * Example:
nxp_ip 0:6c9a51a50eea 25 * @code
nxp_ip 0:6c9a51a50eea 26 * #include "mbed.h"
nxp_ip 0:6c9a51a50eea 27 * #include "PCAL9555.h"
nxp_ip 0:6c9a51a50eea 28 *
nxp_ip 0:6c9a51a50eea 29 * PCAL9555 gpio_exp( p28, p27, 0x40 ); // SDA, SCL, Slave_address(option)
nxp_ip 0:6c9a51a50eea 30 * GpioDigitalInOut pin( gpio_exp, X0_0 );
nxp_ip 0:6c9a51a50eea 31 *
nxp_ip 0:6c9a51a50eea 32 * int main()
nxp_ip 0:6c9a51a50eea 33 * {
nxp_ip 0:6c9a51a50eea 34 * pin.output();
nxp_ip 0:6c9a51a50eea 35 * pin = 0;
nxp_ip 0:6c9a51a50eea 36 * wait_us( 500 );
nxp_ip 0:6c9a51a50eea 37 * pin.input();
nxp_ip 0:6c9a51a50eea 38 * wait_us( 500 );
nxp_ip 0:6c9a51a50eea 39 * }
nxp_ip 0:6c9a51a50eea 40 * @endcode
nxp_ip 0:6c9a51a50eea 41 */
nxp_ip 0:6c9a51a50eea 42 class GpioDigitalInOut
nxp_ip 0:6c9a51a50eea 43 {
nxp_ip 0:6c9a51a50eea 44 public:
nxp_ip 0:6c9a51a50eea 45
nxp_ip 0:6c9a51a50eea 46 /** Create a GpioDigitalInOut connected to the specified pin
nxp_ip 0:6c9a51a50eea 47 *
nxp_ip 0:6c9a51a50eea 48 * @param gpiop Instance of GPIO expander device
nxp_ip 0:6c9a51a50eea 49 * @param pin_name DigitalInOut pin to connect to
nxp_ip 0:6c9a51a50eea 50 */
nxp_ip 0:6c9a51a50eea 51 GpioDigitalInOut( CompGpioExp &gpiop, GpioPinName pin_name );
nxp_ip 0:6c9a51a50eea 52
nxp_ip 0:6c9a51a50eea 53 /**
nxp_ip 0:6c9a51a50eea 54 * Destractor
nxp_ip 0:6c9a51a50eea 55 */
nxp_ip 0:6c9a51a50eea 56 virtual ~GpioDigitalInOut();
nxp_ip 0:6c9a51a50eea 57
nxp_ip 0:6c9a51a50eea 58 /** Set the output, specified as 0 or 1 (int)
nxp_ip 0:6c9a51a50eea 59 *
nxp_ip 0:6c9a51a50eea 60 * @param v An integer specifying the pin output value,
nxp_ip 0:6c9a51a50eea 61 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
nxp_ip 0:6c9a51a50eea 62 */
nxp_ip 0:6c9a51a50eea 63 virtual void write( int v );
nxp_ip 0:6c9a51a50eea 64
nxp_ip 0:6c9a51a50eea 65 /** Return the output setting, represented as 0 or 1 (int)
nxp_ip 0:6c9a51a50eea 66 *
nxp_ip 0:6c9a51a50eea 67 * @returns
nxp_ip 0:6c9a51a50eea 68 * an integer representing the output setting of the pin if it is an output,
nxp_ip 0:6c9a51a50eea 69 * or read the input if set as an input
nxp_ip 0:6c9a51a50eea 70 */
nxp_ip 0:6c9a51a50eea 71 virtual int read( void );
nxp_ip 0:6c9a51a50eea 72
nxp_ip 0:6c9a51a50eea 73 /** Set as an output
nxp_ip 0:6c9a51a50eea 74 */
nxp_ip 0:6c9a51a50eea 75 void output( void );
nxp_ip 0:6c9a51a50eea 76
nxp_ip 0:6c9a51a50eea 77 /** Set as an input
nxp_ip 0:6c9a51a50eea 78 */
nxp_ip 0:6c9a51a50eea 79 void input( void );
nxp_ip 0:6c9a51a50eea 80
nxp_ip 0:6c9a51a50eea 81 /** A shorthand for write()
nxp_ip 0:6c9a51a50eea 82 */
nxp_ip 0:6c9a51a50eea 83 GpioDigitalInOut& operator= ( int rhs );
nxp_ip 0:6c9a51a50eea 84 GpioDigitalInOut& operator= ( GpioDigitalInOut& rhs );
nxp_ip 0:6c9a51a50eea 85
nxp_ip 0:6c9a51a50eea 86 /** A shorthand for read()
nxp_ip 0:6c9a51a50eea 87 */
nxp_ip 0:6c9a51a50eea 88 virtual operator int( void );
nxp_ip 0:6c9a51a50eea 89
nxp_ip 0:6c9a51a50eea 90 private:
nxp_ip 0:6c9a51a50eea 91 CompGpioExp *gpio_p;
nxp_ip 0:6c9a51a50eea 92 GpioPinName pin;
nxp_ip 0:6c9a51a50eea 93 int output_state;
nxp_ip 0:6c9a51a50eea 94 int config_state;
nxp_ip 0:6c9a51a50eea 95
nxp_ip 0:6c9a51a50eea 96 void write( int pin, int value );
nxp_ip 0:6c9a51a50eea 97 void configure( int pin, int value );
nxp_ip 0:6c9a51a50eea 98 }
nxp_ip 0:6c9a51a50eea 99 ;
nxp_ip 0:6c9a51a50eea 100
nxp_ip 0:6c9a51a50eea 101 #endif // MBED_GpioDigitalInOut