void ConfigureCodec() { I2CConfigure( EEPROM_I2C_BUS, 0 ); I2CSetFrequency( EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ ); I2CEnable( EEPROM_I2C_BUS, TRUE ); // UINT8 i2cData[10]; I2C_7_BIT_ADDRESS SlaveAddress; // Initialize the data buffer I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, 0x46, I2C_WRITE); i2cData[0] = SlaveAddress.byte; i2cData[1] = 0x40; // register 64 i2cData[2] = 0xC0; // turn off power save SendPacket( i2cData, 3 ); i2cData[1] = 73; // register 73 i2cData[2] = 0x0C; // inverted phase, no HPF SendPacket( i2cData, 3 ); // I2CEnable( EEPROM_I2C_BUS, FALSE ); }
BOOL I2Csend(UINT8 address, int length, const UINT8* buffer) { BOOL success = TRUE; // Start the transfer for writing data to the LCD. if (StartTransfer(FALSE)) { I2C_7_BIT_ADDRESS SlaveAddress; I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, address, I2C_WRITE); if (TransmitOneByte(SlaveAddress.byte)) { const char* cp; // Call TransmitOneByte() for each non-null character in the string. // Stop with success set to false if TransmitOneByte() returns failure. int index = 0; while (index < length) { success = TransmitOneByte(*buffer); index++; buffer++; } if (success) { StopTransfer(); } } else { success = FALSE; } } else { success = FALSE; } return success; }
int main(void) { UINT8 i2cData[10]; I2C_7_BIT_ADDRESS SlaveAddress; int Index; int DataSz; UINT32 actualClock; BOOL Acknowledged; BOOL Success = TRUE; UINT8 i2cbyte; // Initialize debug messages (when supported) DBINIT(); // Set the I2C baudrate actualClock = I2CSetFrequency(EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ); if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 ) { DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock); } // Enable the I2C bus I2CEnable(EEPROM_I2C_BUS, TRUE); // // Send the data to EEPROM to program one location // // Initialize the data buffer I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_WRITE); i2cData[0] = SlaveAddress.byte; i2cData[1] = 0x05; // EEPROM location to program (high address byte) i2cData[2] = 0x40; // EEPROM location to program (low address byte) i2cData[3] = 0xAA; // Data to write DataSz = 4; // Start the transfer to write data to the EEPROM if( !StartTransfer(FALSE) ) { while(1); } // Transmit all data Index = 0; while( Success && (Index < DataSz) ) { // Transmit a byte if (TransmitOneByte(i2cData[Index])) { // Advance to the next byte Index++; // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS)) { DBPRINTF("Error: Sent byte was not acknowledged\n"); Success = FALSE; } } else { Success = FALSE; } } // End the transfer (hang here if an error occured) StopTransfer(); if(!Success) { while(1); } // Wait for EEPROM to complete write process, by polling the ack status. Acknowledged = FALSE; do { // Start the transfer to address the EEPROM if( !StartTransfer(FALSE) ) { while(1); } // Transmit just the EEPROM's address if (TransmitOneByte(SlaveAddress.byte)) { // Check to see if the byte was acknowledged Acknowledged = I2CByteWasAcknowledged(EEPROM_I2C_BUS); } else { Success = FALSE; } // End the transfer (stop here if an error occured) StopTransfer(); if(!Success) { while(1); } } while (Acknowledged != TRUE); // // Read the data back from the EEPROM. // // Initialize the data buffer I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_WRITE); i2cData[0] = SlaveAddress.byte; i2cData[1] = 0x05; // EEPROM location to read (high address byte) i2cData[2] = 0x40; // EEPROM location to read (low address byte) DataSz = 3; // Start the transfer to read the EEPROM. if( !StartTransfer(FALSE) ) { while(1); } // Address the EEPROM. Index = 0; while( Success & (Index < DataSz) ) { // Transmit a byte if (TransmitOneByte(i2cData[Index])) { // Advance to the next byte Index++; } else { Success = FALSE; } // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS)) { DBPRINTF("Error: Sent byte was not acknowledged\n"); Success = FALSE; } } // Restart and send the EEPROM's internal address to switch to a read transfer if(Success) { // Send a Repeated Started condition if( !StartTransfer(TRUE) ) { while(1); } // Transmit the address with the READ bit set I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_READ); if (TransmitOneByte(SlaveAddress.byte)) { // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS)) { DBPRINTF("Error: Sent byte was not acknowledged\n"); Success = FALSE; } } else { Success = FALSE; } } // Read the data from the desired address if(Success) { if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW) { DBPRINTF("Error: I2C Receive Overflow\n"); Success = FALSE; } else { while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS)); i2cbyte = I2CGetByte(EEPROM_I2C_BUS); } } // End the transfer (stop here if an error occured) StopTransfer(); if(!Success) { while(1); } // Validate the data read if( i2cbyte != 0xAA ) { DBPRINTF("Error: Verify failed\n"); } else { DBPRINTF("Success\n"); } // Example complete while(1); }
BOOL MPU6050::writeReg(UINT8 regAddress, UINT8 data) { UINT8 i2cData[10]; I2C_7_BIT_ADDRESS SlaveAddress; int Index; int DataSz; BOOL Acknowledged = FALSE; BOOL Success = TRUE; //sprintf(filename, "Starting writeReg().\n"); //putsUART1( filename ); // Initialize the data buffer I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, this->deviceAddress, I2C_WRITE); i2cData[0] = SlaveAddress.byte; i2cData[1] = regAddress; // Register Address to write i2cData[2] = data; // Data to write DataSz = 3; // Start the transfer if( !StartTransfer(FALSE) ) { Success = FALSE; sprintf( filename, "Error in #1 StartTransfer(): when writing address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } // Transmit all data Index = 0; while( Success && (Index < DataSz) ) { // Transmit a byte if (TransmitOneByte(i2cData[Index++])) { // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged( this->i2cBusId )) { Success = FALSE; sprintf( filename, "Error in #1 I2CByteWasAcknowledged(): when writing address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } else { Success = FALSE; sprintf( filename, "Error in #1 TransmitOneByte(): when writing address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } //sprintf(filename, "Before StopTransfer()\n"); //putsUART1( filename ); // End the transfer StopTransfer(); //sprintf(filename, "After StopTransfer()\n"); //putsUART1( filename ); // Wait for device to complete write process, by polling the ack status. while(Acknowledged != TRUE && Success != FALSE) { //sprintf(filename, "Inside the loop\n"); //putsUART1( filename ); // Start the transfer if( StartTransfer(FALSE) ) { // Transmit just the device's address if (TransmitOneByte(SlaveAddress.byte)) { // Check to see if the byte was acknowledged Acknowledged = I2CByteWasAcknowledged( this->i2cBusId ); /*if( !Acknowledged ) { sprintf( filename, "!Acknowledged %u.\n", (unsigned)regAddress ); putsUART1( filename ); }*/ } else { Success = FALSE; sprintf( filename, "Error in #2 TransmitOneByte() - !starttranfer : when writing address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } // End the transfer StopTransfer(); } else { Success = FALSE; sprintf( filename, "Error in #2 StartTransfer(): when writing address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } //sprintf(filename, "After the loop\n"); //putsUART1( filename ); if( !Success ) { sprintf( filename, "Error in writeReg(): when writing to address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } return Success; }
BOOL MPU6050::readReg( UINT8 regAddress, UINT8 &data ) { // Variable declarations UINT8 i2cData[10]; I2C_7_BIT_ADDRESS SlaveAddress; int Index; int DataSz; BOOL Acknowledged; BOOL Success = TRUE; UINT8 i2cbyte; // Initialize the data buffer I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, this->deviceAddress, I2C_WRITE); i2cData[0] = SlaveAddress.byte; i2cData[1] = regAddress; // MPU6050 data address to read (0x75 = WHO_AM_I which contains 0x68) DataSz = 2; // Start the transfer if( !StartTransfer(FALSE) ) { //while(1); Success = FALSE; sprintf( filename, "Error in #1 StartTransfer(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } // Address the device. Index = 0; while( Success & (Index < DataSz) ) { // Transmit a byte if (TransmitOneByte(i2cData[Index])) Index++; else { Success = FALSE; sprintf( filename, "Error in #1 TransmitOneByte(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged( this->i2cBusId )) { //DBPRINTF("Error: Sent byte was not acknowledged\n"); Success = FALSE; sprintf( filename, "Error in #1 I2CByteWasAcknowledged(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } // Restart and send the device's internal address to switch to a read transfer if(Success) { // Send a Repeated Started condition if( !StartTransfer(TRUE) ) { //while(1); Success = FALSE; sprintf( filename, "Error in #2 StartTransfer(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } // Transmit the address with the READ bit set I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, this->deviceAddress, I2C_READ); if (TransmitOneByte(SlaveAddress.byte)) { // Verify that the byte was acknowledged if(!I2CByteWasAcknowledged( this->i2cBusId )) { //DBPRINTF("Error: Sent byte was not acknowledged\n"); Success = FALSE; sprintf( filename, "Error in #2 I2CByteWasAcknowledged(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } else { Success = FALSE; sprintf( filename, "Error in #2 TransmitOneByte(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } } //i2cbyte = 9; // Read the data from the desired address if(Success) { if(I2CReceiverEnable( this->i2cBusId , TRUE) == I2C_RECEIVE_OVERFLOW) { //DBPRINTF("Error: I2C Receive Overflow\n"); Success = FALSE; sprintf( filename, "Error I2CReceiverEnable(): when reading address %u. I2C Receive Overflow.\n", (unsigned)regAddress ); putsUART1( filename ); } else { while(!I2CReceivedDataIsAvailable( this->i2cBusId )); i2cbyte = I2CGetByte( this->i2cBusId ); } } // End the transfer StopTransfer(); data = i2cbyte; if(!Success) { //mPORTBSetBits(BIT_2); //mPORTBClearBits(BIT_3); sprintf( filename, "Error in readReg(): when reading address %u.\n", (unsigned)regAddress ); putsUART1( filename ); } return Success; }