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
Wiring between mbed and 16-bit GPIO expander
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 ); } }
base_classes/CompGpioExp/GpioBus/GpioBusInOut.h
- Committer:
- nxp_ip
- Date:
- 2015-03-19
- Revision:
- 1:fd7cfa821b6a
- Parent:
- 0:6c9a51a50eea
File content as of revision 1:fd7cfa821b6a:
/** GpioBusInOut API for GPIO-expander component class * * @author Akifumi (Tedd) OKANO, NXP Semiconductors * @version 0.6 * @date 19-Mar-2015 * * Released under the Apache 2 license */ #ifndef MBED_GpioBusInOut #define MBED_GpioBusInOut #include "mbed.h" #include "GpioDigitalInOut.h" /** GpioBusInOut class * * @class GpioBusInOut * * "GpioBusInOut" class works like "BusInOut" class of mbed-SDK. * This class provides pin oriented API, abstracting the GPIO-expander chip. * * Example: * @code * #include "mbed.h" * #include "PCAL9555.h" * * PCAL9555 gpio_exp( p28, p27, 0xE8 ); // SDA, SCL, Slave_address(option) * GpioBusInOut pins( gpio_exp, X0_0, X0_1, X0_2 ); * * int main() { * while(1) { * pins.output(); * pins = 0x3; * wait( 1 ); * pins.input(); * wait( 1 ); * if( pins == 0x6 ) { * printf( "Hello!\n" ); * } * } * } * @endcode */ class GpioBusInOut { public: #if DOXYGEN_ONLY /** GPIO-Expander pin names */ typedef enum { X0_0, /**< P0_0 pin */ X0_1, /**< P0_1 pin */ X0_2, /**< P0_2 pin */ X0_3, /**< P0_3 pin */ X0_4, /**< P0_4 pin */ X0_5, /**< P0_5 pin */ X0_6, /**< P0_6 pin */ X0_7, /**< P0_7 pin */ X1_0, /**< P1_0 pin (for 16-bit GPIO device only) */ X1_1, /**< P1_1 pin (for 16-bit GPIO device only) */ X1_2, /**< P1_2 pin (for 16-bit GPIO device only) */ X1_3, /**< P1_3 pin (for 16-bit GPIO device only) */ X1_4, /**< P1_4 pin (for 16-bit GPIO device only) */ X1_5, /**< P1_5 pin (for 16-bit GPIO device only) */ X1_6, /**< P1_6 pin (for 16-bit GPIO device only) */ X1_7, /**< P1_7 pin (for 16-bit GPIO device only) */ X0 = X0_0, /**< P0_0 pin */ X1 = X0_1, /**< P0_1 pin */ X2 = X0_2, /**< P0_2 pin */ X3 = X0_3, /**< P0_3 pin */ X4 = X0_4, /**< P0_4 pin */ X5 = X0_5, /**< P0_5 pin */ X6 = X0_6, /**< P0_6 pin */ X7 = X0_7, /**< P0_7 pin */ X8 = X1_0, /**< P1_0 pin (for 16-bit GPIO device only) */ X9 = X1_1, /**< P1_1 pin (for 16-bit GPIO device only) */ X10 = X1_2, /**< P1_2 pin (for 16-bit GPIO device only) */ X11 = X1_3, /**< P1_3 pin (for 16-bit GPIO device only) */ X12 = X1_4, /**< P1_4 pin (for 16-bit GPIO device only) */ X13 = X1_5, /**< P1_5 pin (for 16-bit GPIO device only) */ X14 = X1_6, /**< P1_6 pin (for 16-bit GPIO device only) */ X15 = X1_7, /**< P1_7 pin (for 16-bit GPIO device only) */ X_NC = ~0x0L /**< for when the pin is left no-connection */ } GpioPinName; #endif /** Create an GpioBusInOut, connected to the specified pins * * @param gpiop Instance of GPIO expander device * @param p<n> DigitalInOut pin to connect to bus bit p<n> (GpioPinName) * * @note * It is only required to specify as many pin variables as is required * for the bus; the rest will default to NC (not connected) */ GpioBusInOut( CompGpioExp &gpiop, GpioPinName p0, GpioPinName p1 = X_NC, GpioPinName p2 = X_NC, GpioPinName p3 = X_NC, GpioPinName p4 = X_NC, GpioPinName p5 = X_NC, GpioPinName p6 = X_NC, GpioPinName p7 = X_NC, GpioPinName p8 = X_NC, GpioPinName p9 = X_NC, GpioPinName p10 = X_NC, GpioPinName p11 = X_NC, GpioPinName p12 = X_NC, GpioPinName p13 = X_NC, GpioPinName p14 = X_NC, GpioPinName p15 = X_NC ); GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[ 16 ] ); /** * Destractor */ virtual ~GpioBusInOut(); /* Group: Access Methods */ /** Write the value to the output bus * * @param value An integer specifying a bit to write for every corresponding DigitalInOut pin */ void write( int value ); /** Read the value currently output on the bus * * @returns * An integer with each bit corresponding to associated DigitalInOut pin setting */ int read(); /** Set as an output */ void output(); /** Set as an input */ void input(); /** A shorthand for write() */ GpioBusInOut& operator= ( int rhs ); GpioBusInOut& operator= ( GpioBusInOut& rhs ); /** A shorthand for read() */ operator int( void ); protected: CompGpioExp *gpio_p; int pin_list[ 16 ]; int mask_bits; void init( CompGpioExp &gpiop, GpioPinName pins[16] ); int make_bitpattern( int value ); } ; #endif // MBED_GpioBusInOut