The PCAL6416A is a low-voltage 16-bit general purpose I/O (GPIO) expander with interrupt. This component library is compatible to basic operation as GPIO expanders: PCAL6416A, 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.
This is a copy of the PCA9555 library by Akifumi "Tedd" OKANO, which is compatible with PCAL6416A chip.
Revision 0:035111d3d631, committed 2017-02-15
- Comitter:
- andriym
- Date:
- Wed Feb 15 10:07:59 2017 +0000
- Commit message:
- A change of name for the PCAL955x library to make it searchable for people needing PCAL6416A library.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCAL6416/PCAL6416.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,69 @@ +#include "mbed.h" +#include "PCAL6416.h" + + +PCAL6416::PCAL6416( PinName i2c_sda, PinName i2c_scl, char i2c_address ) + : PCAL955x( i2c_sda, i2c_scl, i2c_address ), n_of_pins( 16 ) +{ +} + +PCAL6416::PCAL6416( I2C &i2c_obj, char i2c_address ) + : PCAL955x( i2c_obj, i2c_address ), n_of_pins( 16 ) +{ +} + +PCAL6416::~PCAL6416() +{ +} + +int PCAL6416::number_of_pins( void ) +{ + return ( n_of_pins ); +} + +void PCAL6416::reg_index_write( char reg_index, int data ) +{ + char a[ 3 ]; + + a[ 0 ] = regmap[ reg_index ]; + a[ 1 ] = data; + a[ 2 ] = data >> 8; + + bus_write( a, sizeof( a ) ); +} + +int PCAL6416::reg_index_read( char reg_index ) +{ + char a[ 2 ]; + + bus_read( regmap[ reg_index ], a, sizeof( a ) ); + + return ( ((int)(a[ 1 ]) << 8) | a[ 0 ] ); +} + +PCAL6416& PCAL6416::operator= ( int bit_pattern ) +{ + write( bit_pattern ); + return ( *this ); +} + +PCAL6416& PCAL6416::operator= ( PCAL6416& rhs ) +{ + write( rhs.read() ); + return *this; +} + +const char PCAL6416::regmap[] = { + InputPort, + OutoutPort, + PolarityInversionPort, + ConfigurationPort, + OutputDriveStrength0, + OutputDriveStrength1, + InputLatch, + PullUpPullDowmEnable, + PullUpPullDowmSelection, + InterruptMask, + InterruptStatus, + OutputPortConfiguration +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCAL6416/PCAL6416.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,317 @@ +/** PCAL6416A 16-bit I2C-bus GPIO expander + * + * An operation sample of PCA(L)9555, PCA9535, PCA9539 and PCAL6416. + * mbed accesses the PCAL6416 registers through I2C. + * + * @class PCAL6416 + * @author Akifumi (Tedd) OKANO, NXP Semiconductors + * @version 0.6.1 + * @date 19-Mar-2015 + * @modified 15-Feb-2017, by Andriy Makukha, Shenzhen MZJ Technology Co. + * + * Released under the Apache 2 license + * + * About PCAL6416: + * http://www.nxp.com/documents/data_sheet/PCAL6416A.pdf + */ + +#ifndef MBED_PCAL6416 +#define MBED_PCAL6416 + +#include "mbed.h" +#include "PCAL955x.h" +#include "CompGpioExpAPI.h" + +/** PCAL6416 class + * + * This is a driver code for the Low-voltage 16-bit I2C-bus GPIO with Agile I/O. + * This class provides interface for PCAL6416 operation. + * Detail information is available on next URL. + * http://www.nxp.com/documents/data_sheet/PCAL6416A.pdf + * + * PCAL9555 library's basic IO operation is compatible to PCA9555, PCA9535, PCAL6416A and PCA9539. + * This library can be used for those GPIO expander chips also. + * Next sample code shows operation based on low-level-API (operated by just device instane) + * + * Example: + * @code + * // GPIO-expander operation sample using a device instance + * + * #include "mbed.h" + * #include "PCAL6416.h" + * + * PCAL6416 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++ ); + * } + * } + * @endcode + * + * GpioDigitalInOut, GpioDigitalOut, GpioDigitalIn, + * GpioBusInOut, GpioBusOut and GpioBusIn API class are available also. + * For those high-level-API details, please find those class library page. + * The GpioDigital* and GpioBus* APIs can be used like next sample code. + * + * @code + * // GPIO-expander operation sample using high-level-API + * + * #include "mbed.h" + * #include "PCAL6416.h" + * + * PCAL6416 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 ); + * } + * } + * @endcode + */ + +class PCAL6416 : public PCAL955x +{ +public: + /** Name of the PCAL6416 registers */ + enum command_reg { + InputPort0 = 0x00, /**< InputPort0 register */ + InputPort1, /**< InputPort1 register */ + OutoutPort0, /**< OutoutPort0 register */ + OutoutPort1, /**< OutoutPort1 register */ + PolarityInversionPort0, /**< PolarityInversionPort0 register */ + PolarityInversionPort1, /**< PolarityInversionPort1 register */ + ConfigurationPort0, /**< ConfigurationPort0 register */ + ConfigurationPort1, /**< ConfigurationPort1 register */ + OutputDriveStrength0_0 = 0x40, /**< OutputDriveStrength0_0 register */ + OutputDriveStrength0_1, /**< OutputDriveStrength0_1 register */ + OutputDriveStrength1_0, /**< OutputDriveStrength1_0 register */ + OutputDriveStrength1_1, /**< OutputDriveStrength1_1 register */ + InputLatch0, /**< InputLatch0 register */ + InputLatch1, /**< InputLatch1 register */ + PullUpPullDowmEnable0, /**< PullUpPullDowmEnable0 register */ + PullUpPullDowmEnable1, /**< PullUpPullDowmEnable1 register */ + PullUpPullDowmSelection0, /**< PullUpPullDowmSelection0 register */ + PullUpPullDowmSelection1, /**< PullUpPullDowmSelection1 register */ + InterruptMask0, /**< InterruptMask0 register */ + InterruptMask1, /**< InterruptMask1 register */ + InterruptStatus0, /**< InterruptStatus0 register */ + InterruptStatus1, /**< InterruptStatus1 register */ + OutputPortConfiguration = 0x4F, /**< OutputPortConfiguration register */ + }; + +#if DOXYGEN_ONLY + /** GPIO-Expander pin names + * for when the high-level APIs + * (GpioDigitalOut, GpioDigitalInOut, GpioDigitalIn, + * GpioBusOut, GpioBusInOut are GpioBusIn) are used + */ + 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 */ + X1_1, /**< P1_1 pin */ + X1_2, /**< P1_2 pin */ + X1_3, /**< P1_3 pin */ + X1_4, /**< P1_4 pin */ + X1_5, /**< P1_5 pin */ + X1_6, /**< P1_6 pin */ + X1_7, /**< P1_7 pin */ + 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 */ + X9 = X1_1, /**< P1_1 pin */ + X10 = X1_2, /**< P1_2 pin */ + X11 = X1_3, /**< P1_3 pin */ + X12 = X1_4, /**< P1_4 pin */ + X13 = X1_5, /**< P1_5 pin */ + X14 = X1_6, /**< P1_6 pin */ + X15 = X1_7, /**< P1_7 pin */ + + X_NC = ~0x0L /**< for when the pin is left no-connection */ + } GpioPinName; +#endif + + /** Create a PCAL6416 instance connected to specified I2C pins with specified address + * + * @param i2c_sda I2C-bus SDA pin + * @param i2c_sda I2C-bus SCL pin + * @param i2c_address I2C-bus address (default: 0x40) + */ + PCAL6416( PinName i2c_sda, PinName i2c_scl, char i2c_address = PCAL955x::DEFAULT_I2C_ADDR ); + + /** Create a PCAL6416 instance connected to specified I2C pins with specified address + * + * @param i2c_obj I2C object (instance) + * @param i2c_address I2C-bus address (default: 0x40) + */ + PCAL6416( I2C &i2c_obj, char i2c_address = PCAL955x::DEFAULT_I2C_ADDR ); + + /** Destractor + */ + virtual ~PCAL6416(); + + /** Returns the number of I/O pins + * + * @returns + * The number of I/O pins + */ + virtual int number_of_pins( void ); + +#if DOXYGEN_ONLY + + /** Set output port bits + * + * @param bit_pattern 16-bit output pattern for port1 and port0. + * + * @note + * The data for pins, given as integer. + * The 16-bit MSB goes to P1_7 pin and LSB goes to P0_0 pin. + * Data will not come out from the pin if it is configured as input. + * + * @see configure() + */ + void write( int bit_pattern ); + + /** Read pin states + * + * @return + * 16-bit pattern from port1 and port0. + * + * @note + * The data from pins, given as integer. + * The 16-bit port data comes from IO pins, P1_7 as MSB, P0_0 as LSB. + * Data cannot be read if the port is configured as output. + * + * @see configure() + */ + int read( void ); + + /** Polarity setting + * + * @param bit_pattern 16-bit polarity setting pattern for port1 and port0. + * If the bit is set to '1', the state will be inverted. + * (Default state is all '0') + * + * @see configure() + */ + void polarity( int bit_pattern ); + + /** Set IO congiguration + * + * @param bit_pattern 16-bit IO direction setting pattern for port1 and port0. + * + * @note + * The data for pins, given as integer. + * The 16-bit MSB goes to P1_7 pin and LSB goes to P0_0 pin. + * If the bit is set to '1', the pin will be input. + * + * @see write() + * @see read() + */ + void configure( int bit_pattern ); + + /** Set interrupt mask + * + * @param bit_pattern 16-bit interrupt mask + * + * @see interrupt_status() + */ + void interrupt_mask( int bit_pattern ); + + /** Read interrupt status + * + * @return + * 16-bit data from interrupt status registers + * + * @see interrupt_status() + */ + int interrupt_status( void ); + + /** A shorthand for read() + */ + operator int( void ); + +#endif + + /** Write 16-bit data into registers + * + * @param reg_index + * RegisterIndex (like InputPort, OutoutPort, ConfigurationPort, etc.) + * @param data 16-bit data. Data will be written into the pair of 8-bit + * registers. + */ + virtual void reg_index_write( char register_index, int data ); + + /** Read 16-bit data from registers + * + * @param reg_index + * RegisterIndex (like InputPort, OutoutPort, ConfigurationPort, etc.) + * + * @return + * 16-bit data from each pair of registers. + * The register which has lower address will be upper 8-bit data. + */ + virtual int reg_index_read( char register_index ); + + /** A shorthand for write() + */ + PCAL6416& operator= ( int bit_pattern ); + PCAL6416& operator= ( PCAL6416& rhs ); + +private: + /** Register index name */ + enum RegisterIndex { + InputPort = InputPort0, + OutoutPort = OutoutPort0, + PolarityInversionPort = PolarityInversionPort0, + ConfigurationPort = ConfigurationPort0, + OutputDriveStrength0 = OutputDriveStrength0_0, + OutputDriveStrength1 = OutputDriveStrength1_0, + InputLatch = InputLatch0, + PullUpPullDowmEnable = PullUpPullDowmEnable0, + PullUpPullDowmSelection = PullUpPullDowmSelection0, + InterruptMask = InterruptMask0, + InterruptStatus = InterruptStatus0 + }; + + static const char regmap[]; + const int n_of_pins; +} +; + +#endif // MBED_PCAL6416
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/CompGpioExp.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,5 @@ +#include "mbed.h" +#include "CompGpioExp.h" + +CompGpioExp::CompGpioExp() {} +CompGpioExp::~CompGpioExp() {}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/CompGpioExp.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,76 @@ +/** GPIO expander abstraction class + * + * No instance can be made from this class + * + * @class CompGpioExp + * @author Akifumi (Tedd) OKANO, NXP Semiconductors + * @version 0.6 + * @date 19-Mar-2015 + * + * Released under the Apache 2 license + */ + +#ifndef MBED_CompGpioExp +#define MBED_CompGpioExp + +#include "mbed.h" + +typedef enum { + // GPIO Expander pin names + 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; + +/** Abstract class for GPIO expander devices + * + * No instance can be made from this class + */ +class CompGpioExp +{ +public: + CompGpioExp(); + virtual ~CompGpioExp(); + + virtual void write( int pin, int value ) = 0; + virtual int read( int pin ) = 0; + virtual void configure( int pin, int value ) = 0; + virtual int read( void ) = 0; + virtual void write_with_mask( int bitpattern, int mask_bits ) = 0; + virtual void configure_with_mask( int bitpattern, int mask_bits ) = 0; +} +; + +#endif // MBED_CompGpioExp + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/CompGpioExpAPI.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,6 @@ +#include "GpioDigitalInOut.h" +#include "GpioDigitalOut.h" +#include "GpioDigitalIn.h" +#include "GpioBusInOut.h" +#include "GpioBusOut.h" +#include "GpioBusIn.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusIn.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,29 @@ +#include "mbed.h" +#include "GpioBusIn.h" + +GpioBusIn::GpioBusIn( + CompGpioExp &gpiop, + GpioPinName p0, GpioPinName p1, GpioPinName p2, GpioPinName p3, + GpioPinName p4, GpioPinName p5, GpioPinName p6, GpioPinName p7, + GpioPinName p8, GpioPinName p9, GpioPinName p10, GpioPinName p11, + GpioPinName p12, GpioPinName p13, GpioPinName p14, GpioPinName p15 ) +: +GpioBusInOut( gpiop, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 ) +{ + this->input(); +} + +GpioBusIn::GpioBusIn( CompGpioExp &gpiop, GpioPinName pins[ 16 ] ) + : GpioBusInOut( gpiop, pins ) +{ + this->input(); +} + +GpioBusIn::~GpioBusIn() +{ +} + +GpioBusIn::operator int( void ) +{ + return( read() ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusIn.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,116 @@ +/** GpioBusIn 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_GpioBusIn +#define MBED_GpioBusIn + +#include "mbed.h" +#include "GpioBusInOut.h" + +/** GpioBusIn class + * + * @class GpioBusIn + * + * "GpioBusIn" class works like "BusIn" 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) + * GpioBusIn nibble( gpio_exp, X0_0, X0_1, X0_2, X0_3 ); + * + * int main() { + * while ( 1 ) { + * switch ( nibble ) { + * case 0x3: printf( "Hello!\n" ); break; // X0_0 and X0_1 are 1 + * case 0x8: printf( "World!\n" ); break; // X0_3 is 1 + * } + * } + * } + * @endcode + */ +class GpioBusIn : public 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 GpioBusIn, 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) + */ + GpioBusIn( 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 ); + GpioBusIn( CompGpioExp &gpiop, GpioPinName pins[ 16 ] ); + + /** + * Destractor + */ + virtual ~GpioBusIn(); + + /** A shorthand for read() + */ + operator int( void ); + +private: + GpioBusIn& operator= ( int rhs ); + GpioBusIn& operator= ( GpioBusIn& rhs ); +} +; + +#endif // MBED_GpioBusIn
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusInOut.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,89 @@ +#include "GpioBusInOut.h" + +GpioBusInOut::GpioBusInOut( + CompGpioExp &gpiop, + GpioPinName p0, GpioPinName p1, GpioPinName p2, GpioPinName p3, + GpioPinName p4, GpioPinName p5, GpioPinName p6, GpioPinName p7, + GpioPinName p8, GpioPinName p9, GpioPinName p10, GpioPinName p11, + GpioPinName p12, GpioPinName p13, GpioPinName p14, GpioPinName p15 ) + : gpio_p( &gpiop ) +{ + GpioPinName pins[16] = { p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 }; + init( gpiop, pins ); +} + +GpioBusInOut::GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[16] ) +{ + init( gpiop, pins ); +} + +void GpioBusInOut::init( CompGpioExp &gpiop, GpioPinName pins[16] ) +{ + for ( int i = 0; i < 16; i++ ) + pin_list[ i ] = pins[ i ]; + + mask_bits = ~(make_bitpattern( 0xFFFF )); +} + +GpioBusInOut::~GpioBusInOut() +{ +} + +void GpioBusInOut::write( int value ) +{ + int bitpattern; + + bitpattern = make_bitpattern( value ); + gpio_p->write_with_mask( bitpattern, mask_bits ); +} + +int GpioBusInOut::make_bitpattern( int value ) +{ + int bit_pattern = 0; + + for ( int i = 0; i < 16; i++ ) + bit_pattern |= (pin_list[ i ] != X_NC) ? ((value >> i) & 0x1) << pin_list[ i ] : 0x0; + + return ( bit_pattern ); +} + +int GpioBusInOut::read( void ) +{ + int r; + int v = 0; + + r = gpio_p->read(); + + for ( int i = 0; i < 16; i++ ) + v |= (pin_list[ i ] != X_NC) ? ((r >> pin_list[ i ]) & 0x1) << i : 0x0; + + return v; +} + +void GpioBusInOut::output( void ) +{ + gpio_p->configure_with_mask( 0x0, mask_bits ); +} + +void GpioBusInOut::input( void ) +{ + gpio_p->configure_with_mask( ~0x0, mask_bits ); +} + +GpioBusInOut& GpioBusInOut::operator=( int rhs ) +{ + write( rhs ); + return *this; +} + +GpioBusInOut& GpioBusInOut::operator=( GpioBusInOut& rhs ) +{ + write(rhs.read()); + return *this; +} + + +GpioBusInOut::operator int( void ) +{ + return( read() ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusInOut.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,151 @@ +/** 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 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusOut.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,42 @@ +#include "mbed.h" +#include "GpioBusOut.h" + +GpioBusOut::GpioBusOut( + CompGpioExp &gpiop, + GpioPinName p0, GpioPinName p1, GpioPinName p2, GpioPinName p3, + GpioPinName p4, GpioPinName p5, GpioPinName p6, GpioPinName p7, + GpioPinName p8, GpioPinName p9, GpioPinName p10, GpioPinName p11, + GpioPinName p12, GpioPinName p13, GpioPinName p14, GpioPinName p15 ) +: +GpioBusInOut( gpiop, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 ) +{ + init(); +} + +GpioBusOut::GpioBusOut( CompGpioExp &gpiop, GpioPinName pins[ 16 ] ) + : GpioBusInOut( gpiop, pins ) +{ + init(); +} + +GpioBusOut::~GpioBusOut() +{ +} + +GpioBusOut& GpioBusOut::operator=( int rhs ) +{ + write( rhs ); + return ( *this ); +} + +GpioBusOut& GpioBusOut::operator=( GpioBusOut& rhs ) +{ + write( rhs.read() ); + return *this; +} + +void GpioBusOut::init( void ) +{ + write( 0x0 ); + this->output(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioBus/GpioBusOut.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,116 @@ +/** GpioBusOut 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_GpioBusOut +#define MBED_GpioBusOut + +#include "mbed.h" +#include "GpioBusInOut.h" + +/** GpioBusOut class + * + * @class GpioBusOut + * + * "GpioBusOut" class works like "BusOut" 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) + * 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 ); + * } + * } + * } + * @endcode + */ +class GpioBusOut : public 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 GpioBusOut, 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) + */ + GpioBusOut( 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 ); + GpioBusOut( CompGpioExp &gpiop, GpioPinName pins[ 16 ] ); + + /** + * Destractor + */ + virtual ~GpioBusOut(); + + /** A shorthand for write() + */ + GpioBusOut& operator= ( int rhs ); + GpioBusOut& operator= ( GpioBusOut& rhs ); + +private: + void init( void ); +} +; + +#endif // MBED_GpioBusOut
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalIn.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,12 @@ +#include "mbed.h" +#include "GpioDigitalIn.h" + +GpioDigitalIn::GpioDigitalIn( CompGpioExp &gpiop, GpioPinName pin_name ) + : GpioDigitalInOut( gpiop, pin_name ) +{ + this->input(); +} + +GpioDigitalIn::~GpioDigitalIn() +{ +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalIn.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,106 @@ +/** GpioDigitalIn 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_GpioDigitalIn +#define MBED_GpioDigitalIn + +#include "mbed.h" +#include "GpioDigitalInOut.h" + +/** GpioDigitalIn class + * + * @class GpioDigitalIn + * + * "GpioDigitalIn" class works like "DigitalIn" 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) + * GpioDigitalOut pin( gpio_exp, X0_0 ); + * + * GpioDigitalIn enable( gpio_exp, X0_0 ); + * DigitalOut led( LED1 ); + * + * int main() { + * while(1) { + * if( enable ) { + * led = !led; + * } + * wait(0.25); + * } + * } + * @endcode + */ +class GpioDigitalIn : public GpioDigitalInOut +{ +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 a GpioDigitalInOut connected to the specified pin + * + * @param gpiop Instance of GPIO expander device + * @param pin_name DigitalInOut pin to connect to + */ + GpioDigitalIn( CompGpioExp &gpiop, GpioPinName pin_name ); + + /** + * Destractor + */ + virtual ~GpioDigitalIn(); + +private: + GpioDigitalIn& operator= ( int rhs ); + GpioDigitalIn& operator= ( GpioDigitalIn& rhs ); +} +; + +#endif // MBED_GpioDigitalIn
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalInOut.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "GpioDigitalInOut.h" + +GpioDigitalInOut::GpioDigitalInOut( CompGpioExp &gpiop, GpioPinName pin_name ) + : gpio_p( &gpiop ), pin( pin_name ), output_state( 1 ), config_state( 1 ) +{ + this->input(); +} + +GpioDigitalInOut::~GpioDigitalInOut() +{ +} + +void GpioDigitalInOut::write( int v ) +{ + output_state = v; + gpio_p->write( pin, output_state ); +} + +void GpioDigitalInOut::configure( int pin, int v ) +{ + config_state = v; + gpio_p->configure( pin, config_state ); +} + +int GpioDigitalInOut::read( void ) +{ + if ( config_state ) + return ( gpio_p->read( pin ) ); + else + return ( output_state ); +} + +void GpioDigitalInOut::input( void ) +{ + config_state = 1; + gpio_p->configure( pin, config_state ); +} + +void GpioDigitalInOut::output( void ) +{ + config_state = 0; + gpio_p->configure( pin, config_state ); +} + +GpioDigitalInOut& GpioDigitalInOut::operator=( int rhs ) +{ + write( rhs ); + return ( *this ); +} + +GpioDigitalInOut& GpioDigitalInOut::operator=( GpioDigitalInOut& rhs ) +{ + write( rhs.read() ); + return *this; +} + +GpioDigitalInOut::operator int( void ) +{ + return( read() ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalInOut.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,141 @@ +/** GpioDigitalInOut 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_GpioDigitalInOut +#define MBED_GpioDigitalInOut + +#include "mbed.h" +#include "CompGpioExp.h" +#include "GpioDigitalInOut.h" + +/** GpioDigitalInOut class + * + * @class GpioDigitalInOut + * + * "GpioDigitalInOut" class works like "DigitalInOut" 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, 0x40 ); // SDA, SCL, Slave_address(option) + * GpioDigitalInOut pin( gpio_exp, X0_0 ); + * + * int main() + * { + * pin.output(); + * pin = 0; + * wait_us( 500 ); + * pin.input(); + * wait_us( 500 ); + * } + * @endcode + */ +class GpioDigitalInOut +{ +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 a GpioDigitalInOut connected to the specified pin + * + * @param gpiop Instance of GPIO expander device + * @param pin_name DigitalInOut pin to connect to + */ + GpioDigitalInOut( CompGpioExp &gpiop, GpioPinName pin_name ); + + /** + * Destractor + */ + virtual ~GpioDigitalInOut(); + + /** Set the output, specified as 0 or 1 (int) + * + * @param v An integer specifying the pin output value, + * 0 for logical 0, 1 (or any other non-zero value) for logical 1 + */ + virtual void write( int v ); + + /** Return the output setting, represented as 0 or 1 (int) + * + * @returns + * an integer representing the output setting of the pin if it is an output, + * or read the input if set as an input + */ + virtual int read( void ); + + /** Set as an output + */ + void output( void ); + + /** Set as an input + */ + void input( void ); + + /** A shorthand for write() + */ + GpioDigitalInOut& operator= ( int rhs ); + GpioDigitalInOut& operator= ( GpioDigitalInOut& rhs ); + + /** A shorthand for read() + */ + virtual operator int( void ); + +private: + CompGpioExp *gpio_p; + GpioPinName pin; + int output_state; + int config_state; + + void write( int pin, int value ); + void configure( int pin, int value ); +} +; + +#endif // MBED_GpioDigitalInOut
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalOut.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,25 @@ +#include "mbed.h" +#include "GpioDigitalOut.h" + +GpioDigitalOut::GpioDigitalOut( CompGpioExp &gpiop, GpioPinName pin_name ) + : GpioDigitalInOut( gpiop, pin_name ) +{ + write( 0 ); + this->output(); +} + +GpioDigitalOut::~GpioDigitalOut() +{ +} + +GpioDigitalOut& GpioDigitalOut::operator=( int rhs ) +{ + write( rhs ); + return ( *this ); +} + +GpioDigitalOut& GpioDigitalOut::operator=( GpioDigitalOut& rhs ) +{ + write( rhs.read() ); + return *this; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/CompGpioExp/GpioDigital/GpioDigitalOut.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,104 @@ +/** GpioDigitalInOut 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_GpioDigitalOut +#define MBED_GpioDigitalOut + +#include "mbed.h" +#include "GpioDigitalInOut.h" + +/** GpioDigitalOut class + * + * @class GpioDigitalOut + * + * "GpioDigitalOut" class works like "DigitalOut" 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) + * GpioDigitalOut pin( gpio_exp, X0_0 ); + * + * int main() { + * while( 1 ) { + * pin = 1; + * wait( 0.2 ); + * pin = 0; + * wait( 0.2 ); + * } + * } + * @endcode + */ + class GpioDigitalOut : public GpioDigitalInOut +{ +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 a GpioDigitalOut connected to the specified pin + * + * @param gpiop Instance of GPIO expander device + * @param pin_name DigitalInOut pin to connect to + */ + GpioDigitalOut( CompGpioExp &gpiop, GpioPinName pin_name ); + + /** + * Destractor + */ + virtual ~GpioDigitalOut(); + + /** A shorthand for write() + */ + GpioDigitalOut& operator= ( int rhs ); + GpioDigitalOut& operator= ( GpioDigitalOut& rhs ); +} +; + +#endif // MBED_GpioDigitalOut
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/PCAL955x.cpp Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,135 @@ +#include "mbed.h" +#include "PCAL955x.h" + +PCAL955x::PCAL955x( PinName i2c_sda, PinName i2c_scl, char i2c_address ) + : i2c_p( new I2C( i2c_sda, i2c_scl ) ), + i2c( *i2c_p ), + address( i2c_address ), + pin_state( ~0x0 ), + pin_direction( ~0x0 ) +{ +} + +PCAL955x::PCAL955x( I2C &i2c_, char i2c_address ) + : i2c_p( NULL ), + i2c( i2c_ ), + address( i2c_address ), + pin_state( ~0x0 ), + pin_direction( ~0x0 ) +{ +} + +PCAL955x::~PCAL955x() +{ + if ( NULL != i2c_p ) + delete i2c_p; +} + +void PCAL955x::write( int bit_pattern ) +{ + pin_state = bit_pattern; + reg_index_write( OUTPUT, pin_state ); +} + +int PCAL955x::read( void ) +{ + return ( reg_index_read( INPUT ) ); +} + +void PCAL955x::polarity( int bit_pattern ) +{ + reg_index_write( POLARITY, pin_direction ); +} + +void PCAL955x::configure( int bit_pattern ) +{ + pin_direction = bit_pattern; + reg_index_write( CONFIG, pin_direction ); +} + +void PCAL955x::write_with_mask( int bitpattern, int mask_bits ) +{ + pin_state &= (bitpattern | mask_bits); + pin_state |= (bitpattern & ~mask_bits); + + write( pin_state ); +} + +void PCAL955x::configure_with_mask( int bitpattern, int mask_bits ) +{ + pin_direction &= (bitpattern | mask_bits); + pin_direction |= (bitpattern & ~mask_bits); + + configure( pin_direction ); +} + +void PCAL955x::interrupt_mask( int bit_pattern ) +{ + int new_config; + + new_config = (~bit_pattern) | reg_index_read( CONFIG ); + reg_index_write( CONFIG, new_config ); + reg_index_write( INT_MASK, bit_pattern ); +} + +int PCAL955x::interrupt_status( void ) +{ + return ( reg_index_read( INT_STAT ) ); +} + + +void PCAL955x::bus_write( char *dp, int length ) +{ +#if 0 + printf( "bus_write: 0x%02X - ", address ); + for ( int i = 0; i < length; i++ ) + printf( " 0x%02X", *(dp + i) ); + printf( "\r\n" ); +#endif + + i2c.write( address, dp, length ); +} + +void PCAL955x::bus_read( char reg_addr, char *dp, int length ) +{ + i2c.write( address, (char *)(®_addr), 1, true ); + i2c.read( address, dp, length ); +} + +void PCAL955x::write( int pin, int v ) +{ + if ( pin < number_of_pins() ) { + if ( v ) + pin_state |= 0x01 << pin; + else + pin_state &= ~(0x01 << pin); + } else { + pin_state = v ? ~0x0 : 0x0; + } + + write( pin_state ); +} + +int PCAL955x::read( int pin ) +{ + return ( (reg_index_read( INPUT ) >> pin) & 0x1 ); +} + +void PCAL955x::configure( int pin, int v ) +{ + if ( pin < number_of_pins() ) { + if ( v ) + pin_direction |= 0x01 << pin; + else + pin_direction &= ~(0x01 << pin); + } else { + pin_direction = v ? ~0x0 : 0x0; + } + + configure( pin_direction ); +} + +PCAL955x::operator int( void ) +{ + return( read() ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base_classes/PCAL955x.h Wed Feb 15 10:07:59 2017 +0000 @@ -0,0 +1,83 @@ +/** PCAL955x GPIO with Agile I/O, interrupt and weak pull-up family + * + * Abstract class for PCAL955x/PCAL6416 family + * No instance can be made from this class + * + * @class PCAL955x + * @author Akifumi (Tedd) OKANO, NXP Semiconductors + * @version 0.5.1 + * @date 07-Mar-2015 + * @modified 15-Feb-2017, by Andriy Makukha, Shenzhen MZJ Technology Co. + * + * Released under the Apache 2 license + */ + +#ifndef MBED_PCAL955x +#define MBED_PCAL955x + +#include "mbed.h" +#include "CompGpioExp.h" + +/** Abstract class for PCAL955x/PCAL6416 family + * + * No instance can be made from this class + */ +class PCAL955x : public CompGpioExp +{ +public: + enum register_index { + INPUT, + OUTPUT, + POLARITY, + CONFIG, + ODS0, + ODS1, + IN_LATCH, + PULL_EN, + PULL_SEL, + INT_MASK, + INT_STAT, + OPC + }; + + PCAL955x( PinName i2c_sda, PinName i2c_scl, char i2c_address = DEFAULT_I2C_ADDR ); + PCAL955x( I2C &i2c_obj, char i2c_address = DEFAULT_I2C_ADDR ); + virtual ~PCAL955x(); + + virtual void write( int pin, int value ); + virtual int read( int pin ); + virtual void configure( int pin, int value ); + virtual int read( void ); + virtual void write_with_mask( int bitpattern, int mask_bits ); + virtual void configure_with_mask( int bitpattern, int mask_bits ); + + void write( int bit_pattern ); + void polarity( int bit_pattern ); + void configure( int bit_pattern ); + + void interrupt_mask( int bit_pattern ); + int interrupt_status( void ); + + virtual int number_of_pins( void ) = 0; + virtual void reg_index_write( char reg_addr, int data ) = 0; + virtual int reg_index_read( char reg_addr ) = 0; + operator int( void ); + +protected: + enum { + DEFAULT_I2C_ADDR = 0x40, + }; + + void bus_write( char *dp, int length ); + void bus_read( char reg_addr, char *dp, int length ); + +private: + I2C *i2c_p; + I2C &i2c; + char address; // I2C slave address + int pin_state; + int pin_direction; +} +; + +#endif // MBED_PCAL955x \ No newline at end of file