unsigned char beam_gpio_i2c_write( unsigned char ucAddress, unsigned char *pucData , int nLength ) { int i; unsigned int data_count=0; unsigned char bNack; unsigned char bRet=false; unsigned char ucBit; unsigned char ucData; ucAddress <<= 1; BEAM_SCL_OUTPUT(1); BEAM_SDA_OUTPUT(1); //--------------- // Start //--------------- BEAM_SDA_LOW(); BEAM_SCL_LOW(); //--------------- // Slave Address //--------------- for( ucBit=0x80;ucBit>0x01;ucBit>>=1 ) { if( ucAddress & ucBit ) { BEAM_SDA_HIGH(); } else { BEAM_SDA_LOW(); } CLK_ON(); } data_count++; //--------------- // Write bit //--------------- BEAM_SDA_LOW(); CLK_ON(); //--------------- // ACK //--------------- BEAM_SCL_HIGH(); BEAM_SDA_INPUT(); bNack = BEAM_SDA_IS_HIGH(); BEAM_SCL_LOW(); //CLK_ON(); if( bNack ) goto BEAM_I2C_WRITE_FINISH; //---------------------------- // Writing DATA //---------------------------- for( i=0; i<nLength; i++) { ucData = pucData[i]; //---------------------------- // First bit & Check SCL hold //---------------------------- if( ucData & 0x80 ) { BEAM_SDA_SET_OUTPUT(1); } else { BEAM_SDA_SET_OUTPUT(0); } #ifdef SCL_LOW_CHECK if( !BEAM_gpio_i2c_check_scl_hold() ) goto BEAM_I2C_WRITE_FINISH; #else BEAM_SCL_HIGH(); #endif BEAM_SCL_LOW(); //---------------------------- // Last 7 bits //---------------------------- for(ucBit=0x40;ucBit>0x00;ucBit>>=1) { if( ucData & ucBit) { BEAM_SDA_HIGH(); } else { BEAM_SDA_LOW(); } CLK_ON(); } data_count++; //--------------- // ACK //--------------- BEAM_SCL_HIGH(); BEAM_SDA_INPUT(); bNack = BEAM_SDA_IS_HIGH(); BEAM_SCL_LOW(); // CLK_ON(); #ifdef FORCE_DELAY if((data_count%4==0) && need_check){ if(data_count == 4) mdelay(110); else udelay(120); } #endif if( bNack && i != (nLength-1) ) goto BEAM_I2C_WRITE_FINISH; } //--------------- // STOP //--------------- BEAM_SDA_SET_OUTPUT(0); #ifdef SCL_LOW_CHECK if( ! beam_gpio_i2c_check_scl_hold() ) goto BEAM_I2C_WRITE_FINISH; #else BEAM_SCL_HIGH(); #endif BEAM_SDA_HIGH(); bRet = true; BEAM_I2C_WRITE_FINISH : if(!bRet) { BEAM_SCL_LOW(); BEAM_SDA_LOW(); BEAM_SCL_HIGH(); BEAM_SDA_HIGH(); } mdelay(1); return bRet; }
static void activation(ISO7816_SC* scard) { RST_LO(); PWR_ON(); CLK_ON(); }
unsigned char beam_gpio_i2c_read( unsigned char ucAddress, unsigned char *pucData, int nLength ) { int i; unsigned char bNack; unsigned char bRet=false; unsigned char ucBit; unsigned char ucData; ucAddress <<= 1; BEAM_SDA_OUTPUT(1); BEAM_SCL_OUTPUT(1); //--------------- // Start //--------------- BEAM_SDA_LOW(); BEAM_SCL_LOW(); //--------------- // Slave Address //--------------- for( ucBit=0x80;ucBit>0x01;ucBit>>=1 ) { if( ucAddress & ucBit ) { BEAM_SDA_HIGH(); } else { BEAM_SDA_LOW(); } CLK_ON(); } //--------------- // Read bit //--------------- BEAM_SDA_HIGH(); CLK_ON(); //--------------- // ACK //--------------- BEAM_SCL_HIGH(); BEAM_SDA_INPUT(); bNack = BEAM_SDA_IS_HIGH(); BEAM_SCL_LOW(); // CLK_ON(); udelay(20); if( bNack ) goto BEAM_I2C_READ_FINISH; //---------------------------- // Writing DATA //---------------------------- for( i=0; i<nLength; i++) { ucData = 0; if(i>0) BEAM_SDA_INPUT(); //---------------------------- // First bit & Check SCL hold //---------------------------- #ifdef SCL_LOW_CHECK if( !beam_gpio_i2c_check_scl_hold() ) goto BEAM_I2C_READ_FINISH; #else BEAM_SCL_HIGH(); #endif if( BEAM_SDA_IS_HIGH() ) ucData = 0x80; BEAM_SCL_LOW(); //---------------------------- // Last 7 bits //---------------------------- for(ucBit=0x40;ucBit>0x00;ucBit>>=1) { BEAM_SCL_HIGH(); if(BEAM_SDA_IS_HIGH()) ucData |= ucBit; BEAM_SCL_LOW(); } //--------------- // ACK //--------------- if( i == nLength-1 ) { BEAM_SDA_OUTPUT(1); } else { BEAM_SDA_OUTPUT(0); } CLK_ON(); BEAM_SDA_HIGH(); pucData[i] = ucData; } //--------------- // STOP //--------------- BEAM_SDA_LOW(); #ifdef SCL_LOW_CHECK if( !beam_gpio_i2c_check_scl_hold() ) goto BEAM_I2C_READ_FINISH; #else BEAM_SCL_HIGH(); #endif BEAM_SDA_HIGH(); bRet = true; BEAM_I2C_READ_FINISH : if( !bRet) { BEAM_SCL_LOW(); BEAM_SDA_LOW(); BEAM_SCL_HIGH(); BEAM_SDA_HIGH(); } mdelay(1); return bRet; }