00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <compiler.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <memdebug.h>
00046 #include <sys/heap.h>
00047 #include <sys/event.h>
00048
00049 #include <cfg/os.h>
00050 #include <dev/twif.h>
00051
00052 #include <dev/gpio.h>
00053 #include <cfg/pca9555.h>
00054 #include <dev/pca9555.h>
00055
00056 #ifndef I2C_SLA_IOEXP
00057 #define I2C_SLA_IOEXP 0x23
00058 #endif
00059
00060 #define PCA_PINP 0
00061 #define PCA_POUT 2
00062 #define PCA_PINV 4
00063 #define PCA_CONF 6
00065 typedef struct __attribute__ ((packed))
00066 {
00067 uint8_t out[2];
00068 uint8_t pol[2];
00069 uint8_t con[2];
00070 } pca_regs_t;
00071
00072 pca_regs_t *pca_ctrl = NULL;
00073
00074
00075 int IOExpInit( void )
00076
00077 {
00078 pca_ctrl = malloc( sizeof( pca_regs_t));
00079 if( pca_ctrl == NULL) return -1;
00080
00081 memset( pca_ctrl, 0, sizeof( pca_regs_t));
00082 pca_ctrl->out[0] = 0xff;
00083 pca_ctrl->out[1] = 0xff;
00084 pca_ctrl->con[0] = 0xff;
00085 pca_ctrl->con[1] = 0xff;
00086
00087 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT, 1, (void*)pca_ctrl, sizeof(pca_regs_t), 50 ) == -1 )
00088 {
00089
00090 return -1;
00091 }
00092
00093 return 0;
00094 }
00095
00096
00097 int IOExpPinConfigSet( int bank, int bit, uint32_t flags)
00098
00099 {
00100 bank &= 0xf;
00101
00102 if( flags & GPIO_CFG_OUTPUT)
00103 pca_ctrl->con[bank] &= ~(1<<bit);
00104 if( flags & GPIO_CFG_PULLUP)
00105 pca_ctrl->out[bank] &= ~(1<<bit);
00106 if( flags == 0)
00107 pca_ctrl->con[bank] |= (1<<bit);
00108
00109 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50) == -1)
00110 return -1;
00111
00112 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_CONF+bank, 1, &pca_ctrl->con[bank], 1, 50) == -1)
00113 return -1;
00114
00115 return 0;
00116 }
00117
00118
00119 int IOExpRawWrite ( int bank, int value )
00120
00121 {
00122 bank &= 0x0f;
00123 if ( bank > 1 ) return -1;
00124
00125 pca_ctrl->out[bank] = value;
00126
00127 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
00128 return -1;
00129 return 0;
00130 }
00131
00132
00133 int IOExpRawRead ( int bank, int *value )
00134
00135 {
00136 bank &= 0x0f;
00137 if( bank > 1 ) return -1;
00138
00139 if( TwMasterRegRead( I2C_SLA_IOEXP, PCA_PINP+bank, 1, value, 1, 50) == -1)
00140 return-1;
00141 return 0;
00142 }
00143
00144
00145
00146 int IOExpGetBit ( int bank, int bit, int *value )
00147
00148 {
00149 int val;
00150
00151 bank &= 0x0f;
00152 if( bank > 1 ) return -1;
00153
00154 if( TwMasterRegRead( I2C_SLA_IOEXP, PCA_PINP+bank, 1, &val, 1, 50)==-1)
00155 return -1;
00156 else
00157 *value = (( val & ( 1 << bit )) == 0 ) ? 0 : 1;
00158
00159 return 0;
00160 }
00161
00162
00163 int IOExpSetBitHigh( int bank, int bit )
00164
00165 {
00166 bank &= 0x0f;
00167 if( bank > 1 ) return -1;
00168
00169 pca_ctrl->out[bank] |= ( 1 << bit );
00170
00171 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
00172 return -1;
00173 return 0;
00174 }
00175
00176
00177 int IOExpSetBitLow( int bank, int bit )
00178
00179 {
00180 bank &= 0x0f;
00181 if( bank > 1 ) return -1;
00182
00183 pca_ctrl->out[bank] &= ~( 1 << bit );
00184
00185 if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
00186 return -1;
00187 return 0;
00188 }
00189
00190
00191 int IOExpSetBit( int bank, int bit, int value )
00192
00193 {
00194 if( value)
00195 return IOExpSetBitHigh( bank, bit);
00196 else
00197 return IOExpSetBitLow( bank, bit);
00198 }
00199