/********************************************************************* * Function: XEE_RESULT XEEBeginRead(unsigned char control, * XEE_ADDR address) * * PreCondition: XEEInit() is already called. * * Input: control - EEPROM control and address code. * address - Address at which read is to be performed. * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Sets internal address counter to given address. * Puts EEPROM in sequential read mode. * * Note: This function does not release I2C bus. * User must call XEEEndRead() when read is not longer * needed; I2C bus will released after XEEEndRead() * is called. ********************************************************************/ XEE_RESULT XEEBeginRead(unsigned char control, XEE_ADDR address ) { unsigned char r; r = XEESetAddr(control, address); if ( r != XEE_SUCCESS ) return r; r = XEEClose(); if ( r != XEE_SUCCESS ) return r; IdleI2C(); StartI2C(); while( EEPROM_SPICON2bits.SEN ); if ( PIR2bits.BCLIF ) return XEE_BUS_COLLISION; if ( WriteI2C(control+1) ) return XEE_BUS_COLLISION; IdleI2C(); if ( !EEPROM_SPICON2bits.ACKSTAT ) return XEE_SUCCESS; return XEE_NAK; }
/********************************************************************* * Function: LDByteWriteI2C() * Input: Control Byte, 8 - bit address, data. * Overview: Write a byte to low density device at address LowAdd ********************************************************************/ unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode1; unsigned int ErrorCode2; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(ControlByte); //Write Control byte IdleI2C(); ErrorCode1 = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); ErrorCode2 = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition //EEAckPolling(ControlByte); //Perform ACK polling if(ErrorCode1 == 0) { printf("ACK 1 not recieved"); } if(ErrorCode2 == 0) { printf("ACK 2 not recieved"); } //return(ErrorCode); }
static int i2c_write(struct i2c_platform_data *adap, unsigned char *buf, unsigned int len) { int i; u32 data; pr_debug("i2c_write\n"); i = 0; while (i < len) { data = buf[i]; if (MasterWriteI2C(adap, data)) return -EIO; IdleI2C(adap); if (pic32_bus_readl(adap->base + PIC32_I2CxSTAT) & I2CSTAT_ACKSTAT) return -EIO; i++; } StopI2C(adap); IdleI2C(adap); return 0; }
// Configure and start temperature sensor void configStartTempSensor(void) { // Set configuration register StartI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xAC); // access configuration IdleI2C(); WriteI2C(0x00); // set config IdleI2C(); StopI2C(); // Need at least a 10msec delay (from data sheet) Delay10KTCYx(40); // Start temperature conversion StartI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xEE); // start conversion IdleI2C(); StopI2C(); // Another delay Delay10KTCYx(40); }
void internalChangeAddressLine(unsigned char oldAddress, unsigned char value) { IdleI2C(); StartI2C(); MasterWriteI2C(oldAddress); IdleI2C(); MasterWriteI2C(0); IdleI2C(); MasterWriteI2C(value); IdleI2C(); StopI2C(); IdleI2C(); }
// Configure and start temperature sensor void stopTempSensor(void) { StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0x22); // stop conversion IdleI2C(); StopI2C(); // Another delay Delay10KTCYx(40); }
XEE_RESULT XEEWrite(unsigned char val) { IdleI2C(); // ensure module is idle if ( WriteI2C( val ) ) // data byte for EEPROM return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); if ( !EEPROM_SPICON2bits.ACKSTAT ) return XEE_SUCCESS; return XEE_NAK; }
/********************************************************************* * Function: LDPageWriteI2C() * Input: ControlByte, LowAdd, *wrptr. * Overview: Write a page of data from array pointed to be wrptr * starting at LowAdd * Note: LowAdd must start on a page boundary ********************************************************************/ unsigned int LDPageWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char *wrptr) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start condition WriteI2C(ControlByte); //send controlbyte for a write IdleI2C(); //wait for bus Idle WriteI2C(LowAdd); //send low address IdleI2C(); //wait for bus Idle putstringI2C(wrptr); //send data IdleI2C(); //wait for bus Idle StopI2C(); //Generate Stop return(0); }
void writeSRF02Command(char addr, char reg, char cmd) { StartI2C(); // Wait till Start sequence is completed IdleI2C(); MasterWriteI2C(addr); IdleI2C(); MasterWriteI2C(reg); IdleI2C(); // command MasterWriteI2C(cmd); IdleI2C(); StopI2C(); }
/********************************************************************* * Function: XEE_RESULT XEEIsBusy(unsigned char control) * * PreCondition: XEEInit() is already called. * * Input: control - EEPROM control and address code. * * Output: XEE_READY if EEPROM is not busy * XEE_BUSY if EEPROM is busy * other value if failed. * * Side Effects: None * * Overview: Requests ack from EEPROM. * * Note: None ********************************************************************/ XEE_RESULT XEEIsBusy(unsigned char control) { XEE_RESULT r; IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( EEPROM_SPICON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCLIF ) // test for bus collision { return XEE_BUS_COLLISION; // return with Bus Collision error } else { if ( WriteI2C( control ) ) // write byte - R/W bit should be 0 return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); // ensure module is idle if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error if ( !EEPROM_SPICON2bits.ACKSTAT ) r = XEE_READY; else r = XEE_BUSY; #if 0 while ( EEPROM_SPICON2bits.ACKSTAT ) // test for ACK condition received { RestartI2C(); // initiate Restart condition while ( EEPROM_SPICON2bits.RSEN ); // wait until re-start condition is over if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error if ( WriteI2C( control ) ) // write byte - R/W bit should be 0 return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); // ensure module is idle } #endif } StopI2C(); // send STOP condition while ( EEPROM_SPICON2bits.PEN ); // wait until stop condition is over if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error return r; //return XEE_READY; // return with no error }
void setRelayStates(int relayStates) { StartI2C(); IdleI2C(); while(I2CCONbits.SEN); MasterWriteI2C(writeAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_RELAY_STATES); IdleI2C(); // send the value MasterWriteI2C(relayStates); IdleI2C(); StopI2C(); }
void sendRLY08Command(int command) { StartI2C(); IdleI2C(); while(I2CCONbits.SEN); MasterWriteI2C(writeAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_COMMAND); IdleI2C(); // send the value MasterWriteI2C(command); IdleI2C(); StopI2C(); }
int16_t ReadI2C (void) { int i; static int16_t retval = 0; if (IdleI2C () != 1) { return ERREUR_I2C_IDLE; } I2C1CONbits.RCEN = 1; for (i=TIMEOUT_I2C; i>0; i--) { if(I2C1STATbits.RBF) break; if ((i-1) <= 0) return ERREUR_I2C_READ; } retval = (int16_t)I2C1RCV; return retval; }
int16_t WriteI2C (uint8_t byte) { int i; if (IdleI2C () != 1) { return ERREUR_I2C_IDLE; } I2C1TRN = byte; for (i=TIMEOUT_I2C; i>0; i--) { if (!I2C1STATbits.TBF) { break; } else { if (i == 0) return ERREUR_I2C_WRITE; } } return 1; }
int16_t RestartI2C (void) { int i; if (IdleI2C () != 1) { return ERREUR_I2C_IDLE; } I2C1CONbits.RSEN = 1; for (i=TIMEOUT_I2C; i>0; i--) { if (!I2C1CONbits.RSEN) { break; } else { if (i == 0) return ERREUR_I2C_RESTART; } } return 1; }
int16_t StopI2C (void) { int i; if (IdleI2C () != 1) { return ERREUR_I2C_IDLE; } I2C1CONbits.PEN = 1; for (i=TIMEOUT_I2C; i>0; i--) { if (!I2C1CONbits.PEN) { break; } else { if (i == 0) return ERREUR_I2C_STOP; } } return 1; }
void SendChar(unsigned char data) { IdleI2C(); StartI2C(); IdleI2C(); putcI2C(OLED_address); //send address IdleI2C(); putcI2C(0x40); //send data incomming IdleI2C(); putcI2C(data); IdleI2C(); // WriteI2C(0x40); //WriteI2C(data); StopI2C(); }
/********************************************************************* * Function: LDSequentialReadI2C() * Input: ControlByte, address, *rdptr, length. * Overview: Performs a sequential read of length bytes starting at address * and places data in array pointed to by *rdptr ********************************************************************/ unsigned int LDSequentialReadI2C(unsigned char ControlByte, unsigned char address, unsigned char *rdptr, unsigned char length) { IdleI2C(); //Ensure Module is Idle StartI2C(); //Initiate start condition WriteI2C(ControlByte); //write 1 byte IdleI2C(); //Ensure module is Idle WriteI2C(address); //Write word address IdleI2C(); //Ensure module is idle RestartI2C(); //Generate I2C Restart Condition WriteI2C(ControlByte | 0x01); //Write 1 byte - R/W bit should be 1 for read IdleI2C(); //Ensure bus is idle getsI2C(rdptr, length); //Read in multiple bytes NotAckI2C(); //Send Not Ack StopI2C(); //Send stop condition return(0); }
char readEEPROMexterna (char endereco) { //while(!DataRdyI2C()); IdleI2C(); StartI2C(); IdleI2C(); WriteI2C(0xA0); IdleI2C(); WriteI2C(0x00); IdleI2C(); //HB WriteI2C(endereco); IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0xA1); IdleI2C(); value = ReadI2C(); IdleI2C(); //Dados NotAckI2C(); IdleI2C(); StopI2C();IdleI2C(); return value; }
/********************************************************************* * Function: XEE_RESULT XEEEndWrite(void) * * PreCondition: XEEInit() && XEEBeginWrite() are already called. * * Input: None * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Instructs EEPROM to begin write cycle. * * Note: Call this function after either page full of bytes * written or no more bytes are left to load. * This function initiates the write cycle. * User must call for XEEWait() to ensure that write * cycle is finished before calling any other * routine. ********************************************************************/ XEE_RESULT XEEEndWrite(void) { IdleI2C(); StopI2C(); while(EEPROM_SPICON2bits.PEN); return XEE_SUCCESS; }
/********************************************************************* * Function: LDByteReadI2C() * Input: Control Byte, Address, *Data, Length. * Overview: Performs a low density read of Length bytes and stores in *Data array * starting at Address. ********************************************************************/ unsigned int LDByteReadI2C(unsigned char ControlByte, unsigned char Address, unsigned char *Data, unsigned char Length) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C(ControlByte); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C(ControlByte | 0x01); //Write control byte for read IdleI2C(); //wait for bus Idle getsI2C(Data, Length); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop }
void EEPROM_read(BYTE devaddr, DWORD addr, BYTE *data, BYTE size) { // Dummy read to setup address IdleI2C(); StartI2C(); IdleI2C(); // Dev address WriteI2C( devaddr << 1 ); IdleI2C(); // Memory address WriteI2C( addr >> 8 ); IdleI2C(); WriteI2C( addr & 0xFF ); IdleI2C(); EEPROM_read_seq(devaddr, data, size); }
/********************************************************************* * Function: EEAckPolling() * Input: Control byte. * Output: Error state. * Overview: Polls the bus for an Acknowledge from device ********************************************************************/ unsigned int EEAckPolling(unsigned char control) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start condition if(I2C1STATbits.BCL) { return(-1); //Bus collision, return } else { if(WriteI2C(control)) { return(-3); //error return } IdleI2C(); //wait for bus idle if(I2C1STATbits.BCL) { return(-1); //error return } while(ACKStatus()) { RestartI2C(); //generate restart if(I2C1STATbits.BCL) { return(-1); //error return } if(WriteI2C(control)) { return(-3); } IdleI2C(); } } StopI2C(); //send stop condition if(I2C1STATbits.BCL) { return(-1); } return(0); }
/** * Gets the software revision of the software in the RLY08 */ unsigned int getRLY08SoftwareRevision() { IdleI2C(); StartI2C(); IdleI2C(); while(I2CCONbits.SEN); // send the address MasterWriteI2C(writeAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_SOFTWARE_REVISION); IdleI2C(); StopI2C(); IdleI2C(); StartI2C(); while(I2CCONbits.SEN); // send the address again with read bit MasterWriteI2C(readAddr); IdleI2C(); // read the data unsigned char data = MasterReadI2C(); IdleI2C(); StopI2C(); return data; }
XEE_RESULT XEEClose(void) { IdleI2C(); StopI2C(); while( EEPROM_SPICON2bits.PEN ); // wait until stop condition is over. if ( PIR2bits.BCLIF ) return XEE_BUS_COLLISION; return XEE_SUCCESS; }
unsigned char HDByteWriteI2C( unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char data ) { IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( ControlByte ); // write 1 byte - R/W bit should be 0 IdleI2C(); // ensure module is idle WriteI2C( HighAdd ); // write address byte to EEPROM IdleI2C(); // ensure module is idle WriteI2C( LowAdd ); // write address byte to EEPROM IdleI2C(); // ensure module is idle WriteI2C ( data ); // Write data byte to EEPROM IdleI2C(); // ensure module is idle StopI2C(); // send STOP condition while ( SSPCON2bits.PEN ); // wait until stop condition is over while (EEAckPolling(ControlByte)); //Wait for write cycle to complete return ( 0 ); // return with no error }
void Gyro_I2C_W(UINT8 LowAdd, UINT8 data) { //UINT8 ErrorCode; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(Gyro_MPU6050_Address); //Write Control byte IdleI2C(); //ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); //ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition }
/********************************************************************* * Function: XEE_RESULT XEESetAddr(unsigned char control, * XEE_ADDR address) * * PreCondition: XEEInit() is already called. * * Input: control - data EEPROM control code * address - address to be set * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Modifies internal address counter of EEPROM. * * Note: This function does not release the I2C bus. * User must close XEEClose() after this function * is called. ********************************************************************/ XEE_RESULT XEESetAddr(unsigned char control, XEE_ADDR address) { union { unsigned short int Val; struct { unsigned char LSB; unsigned char MSB; } bytes; } tempAddress; tempAddress.Val = address; IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( EEPROM_SPICON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error if ( WriteI2C( control ) ) // write 1 byte return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); // ensure module is idle if ( !EEPROM_SPICON2bits.ACKSTAT ) // test for ACK condition, if received { if ( WriteI2C( tempAddress.bytes.MSB ) ) // WRITE word address to EEPROM return XEE_BUS_COLLISION; // return with write collision error IdleI2C(); // ensure module is idle if ( WriteI2C( tempAddress.bytes.LSB ) ) // WRITE word address to EEPROM return XEE_BUS_COLLISION; // return with write collision error IdleI2C(); // ensure module is idle if ( !EEPROM_SPICON2bits.ACKSTAT ) // test for ACK condition received return XEE_SUCCESS; else return XEE_NAK; // return with Not Ack error } else return XEE_NAK; // return with Not Ack error }
/* * Gyro i2c interface, Read/Write */ UINT8 Gyro_I2C_R(UINT8 Address) { UINT8 Data; IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C(Gyro_MPU6050_Address); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C(Gyro_MPU6050_Address | 0x01); //Write control byte for read IdleI2C(); //wait for bus Idle Data = getI2C(); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop return Data; }
static int do_address(struct i2c_platform_data *adap, unsigned int addr, int rd) { pr_debug("doaddress\n"); IdleI2C(adap); StartI2C(adap); IdleI2C(adap); addr <<= 1; if (rd) addr |= 1; if (MasterWriteI2C(adap, addr)) return -EIO; IdleI2C(adap); if (pic32_bus_readl(adap->base + PIC32_I2CxSTAT) & I2CSTAT_ACKSTAT) return -EIO; return 0; }