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 #include "GpioBusInOut.h"
nxp_ip 0:6c9a51a50eea 2
nxp_ip 0:6c9a51a50eea 3 GpioBusInOut::GpioBusInOut(
nxp_ip 0:6c9a51a50eea 4 CompGpioExp &gpiop,
nxp_ip 0:6c9a51a50eea 5 GpioPinName p0, GpioPinName p1, GpioPinName p2, GpioPinName p3,
nxp_ip 0:6c9a51a50eea 6 GpioPinName p4, GpioPinName p5, GpioPinName p6, GpioPinName p7,
nxp_ip 0:6c9a51a50eea 7 GpioPinName p8, GpioPinName p9, GpioPinName p10, GpioPinName p11,
nxp_ip 0:6c9a51a50eea 8 GpioPinName p12, GpioPinName p13, GpioPinName p14, GpioPinName p15 )
nxp_ip 0:6c9a51a50eea 9 : gpio_p( &gpiop )
nxp_ip 0:6c9a51a50eea 10 {
nxp_ip 0:6c9a51a50eea 11 GpioPinName pins[16] = { p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 };
nxp_ip 0:6c9a51a50eea 12 init( gpiop, pins );
nxp_ip 0:6c9a51a50eea 13 }
nxp_ip 0:6c9a51a50eea 14
nxp_ip 0:6c9a51a50eea 15 GpioBusInOut::GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[16] )
nxp_ip 0:6c9a51a50eea 16 {
nxp_ip 0:6c9a51a50eea 17 init( gpiop, pins );
nxp_ip 0:6c9a51a50eea 18 }
nxp_ip 0:6c9a51a50eea 19
nxp_ip 0:6c9a51a50eea 20 void GpioBusInOut::init( CompGpioExp &gpiop, GpioPinName pins[16] )
nxp_ip 0:6c9a51a50eea 21 {
nxp_ip 0:6c9a51a50eea 22 for ( int i = 0; i < 16; i++ )
nxp_ip 0:6c9a51a50eea 23 pin_list[ i ] = pins[ i ];
nxp_ip 0:6c9a51a50eea 24
nxp_ip 0:6c9a51a50eea 25 mask_bits = ~(make_bitpattern( 0xFFFF ));
nxp_ip 0:6c9a51a50eea 26 }
nxp_ip 0:6c9a51a50eea 27
nxp_ip 0:6c9a51a50eea 28 GpioBusInOut::~GpioBusInOut()
nxp_ip 0:6c9a51a50eea 29 {
nxp_ip 0:6c9a51a50eea 30 }
nxp_ip 0:6c9a51a50eea 31
nxp_ip 0:6c9a51a50eea 32 void GpioBusInOut::write( int value )
nxp_ip 0:6c9a51a50eea 33 {
nxp_ip 0:6c9a51a50eea 34 int bitpattern;
nxp_ip 0:6c9a51a50eea 35
nxp_ip 0:6c9a51a50eea 36 bitpattern = make_bitpattern( value );
nxp_ip 0:6c9a51a50eea 37 gpio_p->write_with_mask( bitpattern, mask_bits );
nxp_ip 0:6c9a51a50eea 38 }
nxp_ip 0:6c9a51a50eea 39
nxp_ip 0:6c9a51a50eea 40 int GpioBusInOut::make_bitpattern( int value )
nxp_ip 0:6c9a51a50eea 41 {
nxp_ip 0:6c9a51a50eea 42 int bit_pattern = 0;
nxp_ip 0:6c9a51a50eea 43
nxp_ip 0:6c9a51a50eea 44 for ( int i = 0; i < 16; i++ )
nxp_ip 0:6c9a51a50eea 45 bit_pattern |= (pin_list[ i ] != X_NC) ? ((value >> i) & 0x1) << pin_list[ i ] : 0x0;
nxp_ip 0:6c9a51a50eea 46
nxp_ip 0:6c9a51a50eea 47 return ( bit_pattern );
nxp_ip 0:6c9a51a50eea 48 }
nxp_ip 0:6c9a51a50eea 49
nxp_ip 0:6c9a51a50eea 50 int GpioBusInOut::read( void )
nxp_ip 0:6c9a51a50eea 51 {
nxp_ip 0:6c9a51a50eea 52 int r;
nxp_ip 0:6c9a51a50eea 53 int v = 0;
nxp_ip 0:6c9a51a50eea 54
nxp_ip 0:6c9a51a50eea 55 r = gpio_p->read();
nxp_ip 0:6c9a51a50eea 56
nxp_ip 0:6c9a51a50eea 57 for ( int i = 0; i < 16; i++ )
nxp_ip 0:6c9a51a50eea 58 v |= (pin_list[ i ] != X_NC) ? ((r >> pin_list[ i ]) & 0x1) << i : 0x0;
nxp_ip 0:6c9a51a50eea 59
nxp_ip 0:6c9a51a50eea 60 return v;
nxp_ip 0:6c9a51a50eea 61 }
nxp_ip 0:6c9a51a50eea 62
nxp_ip 0:6c9a51a50eea 63 void GpioBusInOut::output( void )
nxp_ip 0:6c9a51a50eea 64 {
nxp_ip 0:6c9a51a50eea 65 gpio_p->configure_with_mask( 0x0, mask_bits );
nxp_ip 0:6c9a51a50eea 66 }
nxp_ip 0:6c9a51a50eea 67
nxp_ip 0:6c9a51a50eea 68 void GpioBusInOut::input( void )
nxp_ip 0:6c9a51a50eea 69 {
nxp_ip 0:6c9a51a50eea 70 gpio_p->configure_with_mask( ~0x0, mask_bits );
nxp_ip 0:6c9a51a50eea 71 }
nxp_ip 0:6c9a51a50eea 72
nxp_ip 0:6c9a51a50eea 73 GpioBusInOut& GpioBusInOut::operator=( int rhs )
nxp_ip 0:6c9a51a50eea 74 {
nxp_ip 0:6c9a51a50eea 75 write( rhs );
nxp_ip 0:6c9a51a50eea 76 return *this;
nxp_ip 0:6c9a51a50eea 77 }
nxp_ip 0:6c9a51a50eea 78
nxp_ip 0:6c9a51a50eea 79 GpioBusInOut& GpioBusInOut::operator=( GpioBusInOut& rhs )
nxp_ip 0:6c9a51a50eea 80 {
nxp_ip 0:6c9a51a50eea 81 write(rhs.read());
nxp_ip 0:6c9a51a50eea 82 return *this;
nxp_ip 0:6c9a51a50eea 83 }
nxp_ip 0:6c9a51a50eea 84
nxp_ip 0:6c9a51a50eea 85
nxp_ip 0:6c9a51a50eea 86 GpioBusInOut::operator int( void )
nxp_ip 0:6c9a51a50eea 87 {
nxp_ip 0:6c9a51a50eea 88 return( read() );
nxp_ip 0:6c9a51a50eea 89 }