Exemplo n.º 1
0
void readChipTemp(void){
    BOOL Success = TRUE;
    UINT8 tempMSB, tempLSB;
    if(!I2C_startTransfer(THERMAL_I2C_ID, FALSE)){
        printf("FAILED initial transfer!\n");
        Success = FALSE;
    }
// Transmit the slave's address to notify it
    if(!I2C_sendData(THERMAL_I2C_ID,CAMERA_WRITE_ADDRESS)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,CAMERA_READ_COMMAND)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x90)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x00)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x01)){
        Success = FALSE;
    }
    if(Success){
    // Send a Repeated Started condition
        if(!I2C_startTransfer(THERMAL_I2C_ID,TRUE)){
            printf("FAILED Repeated start!\n");
            Success = FALSE;
        }
    // Transmit the address with the READ bit set
        if (!I2C_sendData(THERMAL_I2C_ID,CAMERA_READ_ADDRESS)){
            Success = FALSE;
        }
    }
    if(Success){
    // Read the I2C bus most significan byte and send an acknowledge bit
        tempLSB = I2C_getData(THERMAL_I2C_ID);
        I2CAcknowledgeByte(THERMAL_I2C_ID, TRUE);
        while(!I2CAcknowledgeHasCompleted(THERMAL_I2C_ID));
        tempMSB = I2C_getData(THERMAL_I2C_ID);
        I2CAcknowledgeByte(THERMAL_I2C_ID, TRUE);
        while(!I2CAcknowledgeHasCompleted(THERMAL_I2C_ID));
        }
    I2C_stopTransfer(THERMAL_I2C_ID);
        rawTemp = (tempMSB << 8) + tempLSB;
}
Exemplo n.º 2
0
void readEeprom(void){
    BOOL success = FALSE;
    int index;

    do {
        if(!I2C_startTransfer(THERMAL_I2C_ID, FALSE)){
            #ifdef DEBUG
            printf("FAILED initial transfer!\n");
            #endif
            break;
        }
        // Transmit the slave's address to notify it
        if (!I2C_sendData(THERMAL_I2C_ID,EEPROM_WRITE_ADDRESS))
            break;

        // Tranmit the read address module
        if(!I2C_sendData(THERMAL_I2C_ID,EEPROM_READ_COMMAND)){
            #ifdef DEBUG
            printf("Error: Sent byte was not acknowledged\n");
            #endif
            break;
        }

        // Send a Repeated Started condition
        if(!I2C_startTransfer(THERMAL_I2C_ID,TRUE)){
            #ifdef DEBUG
                printf("FAILED Repeated start!\n");
            #endif
            break;
        }
        // Transmit the address with the READ bit set
        if (!I2C_sendData(THERMAL_I2C_ID,EEPROM_READ_ADDRESS))
            break;

        // Read the I2C bus most significant byte and send an acknowledge bit
        for(index = 0; index <=0xFF; index++){
            eepromData[index] = I2C_getData(THERMAL_I2C_ID);
            if(index < 0xFF)
                I2CAcknowledgeByte(I2C1, TRUE);
            else
                I2CAcknowledgeByte(I2C1, FALSE);
            while(!I2CAcknowledgeHasCompleted(I2C1));
        }
        success = TRUE;
    } while(0);
    // Send the stop bit to finish the transfer
    I2C_stopTransfer(THERMAL_I2C_ID);
    if(!success){
        printf("Data transfer unsuccessful.\n");
        return;
    }
    // Stop transfer twice?
    I2C_stopTransfer(THERMAL_I2C_ID);
    int Index;
    for(Index = 0; Index <=255; Index++){
        while(!Serial_isTransmitEmpty());
        //printf("EEPROM %x / %d: %x\n", Index, Index, eepromData[Index]);
    }
     
}
Exemplo n.º 3
0
BYTE i2cRecieveByte(I2C_MODULE id, BOOL ack){
    I2CReceiverEnable(id, TRUE);
    while(!I2CReceivedDataIsAvailable(id));
    I2CAcknowledgeByte(id, ack);
    while(!I2CAcknowledgeHasCompleted(id)) ;
    BYTE result = I2CGetByte(id);
    return result;
}
Exemplo n.º 4
0
/*
 Reads a value from a ADXL345 register and returns it
 */
unsigned int accelReadReg(const Accel *a, int reg){
    int I2C = a->I2C;
    sendStart(I2C);                 // start transaction
    sendByte(I2C, a->write);        // write accel device
    sendByte(I2C, reg);             // specify device register
    repeatStart(I2C);
    sendByte(I2C, a->read);         // read accel device
    unsigned int d = readByte(I2C);          // grab data
    I2CAcknowledgeByte(I2C, 0);     // Send nack
    while(!I2CAcknowledgeHasCompleted(I2C));
    I2CStop(I2C);                   // end transaction
    return d;
}
Exemplo n.º 5
0
void readConfigReg(void){
    BOOL Success = TRUE;
    UINT8 configMSB, configLSB;
    if(!I2C_startTransfer(THERMAL_I2C_ID, FALSE)){
        printf("FAILED initial transfer!\n");
        Success = FALSE;
    }
// Transmit the slave's address to notify it
    if(!I2C_sendData(THERMAL_I2C_ID,CAMERA_WRITE_ADDRESS)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,CAMERA_READ_COMMAND)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x92)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x00)){
        Success = FALSE;
    }
// Tranmit the read address module
    if(!I2C_sendData(THERMAL_I2C_ID,0x01)){
        Success = FALSE;
    }
    if(Success){
    // Send a Repeated Started condition
        if(!I2C_startTransfer(THERMAL_I2C_ID,TRUE)){
            printf("FAILED Repeated start!\n");
            Success = FALSE;
        }
    // Transmit the address with the READ bit set
        if (!I2C_sendData(THERMAL_I2C_ID,CAMERA_READ_ADDRESS)){
            Success = FALSE;
        }
    }
    if(Success){
    // Read the I2C bus most significan byte and send an acknowledge bit
        configLSB = I2C_getData(THERMAL_I2C_ID);
        I2CAcknowledgeByte(I2C1, TRUE);
        while(!I2CAcknowledgeHasCompleted(THERMAL_I2C_ID));
        configMSB = I2C_getData(THERMAL_I2C_ID);
        }
    I2C_stopTransfer(THERMAL_I2C_ID);
        //while(!IsTransmitEmpty());
        //printf("Config Data %x %x\n", configMSB, configLSB);
}
Exemplo n.º 6
0
BOOL ReceiveOneByte(UINT8* data, BOOL ack)
{
	if(I2CReceiverEnable(LCD_I2C_BUS, TRUE)==I2C_SUCCESS)
	{
		while (!(I2CReceivedDataIsAvailable(LCD_I2C_BUS)));		
		*data = I2CGetByte(LCD_I2C_BUS);
		I2CAcknowledgeByte(LCD_I2C_BUS, ack);
		while (!(I2CAcknowledgeHasCompleted(LCD_I2C_BUS))); 
		return TRUE;
	}
	else 
	{
	return FALSE;
	}
}
static uint8_t ReceiveOneByte(I2C_MODULE i2c_id, bool ack)
{
    uint8_t data;
    
    // Enable I2C receive
    I2CReceiverEnable(i2c_id, TRUE);
    
    // Wait until 1-byte is fully received
    while (!I2CReceivedDataIsAvailable(i2c_id));
    
    // Save the byte received
    data = I2CGetByte(i2c_id);
    
    // Perform acknowledgement sequence
    I2CAcknowledgeByte(i2c_id, ack);
    
    // Wait until acknowledgement is successfully sent 
    while (!I2CAcknowledgeHasCompleted(i2c_id));
    
    return data;
}
Exemplo n.º 8
0
/************************************************************************************************** 
  Function:
    BOOL I2CShared_ReadMultipleBytes(const I2C_MODULE i2c, const UINT8 i2c_addr,
      const UINT8 i2c_register_start, const int nbytes, UINT8 *buffer)

  Author(s):
    mkobit

  Summary:
    Reads (nbytes) from the (i2c) module starting from (i2c_read_addr)  and places in (buffer)

  Description:
    Waits until (i2c) bus is idle and then uses I2C protocol to start a transaction, write to the device and its register, restart with the read address
    and read the data. Sends a NACK at the end to stop the transmission and then stops the transaction

  Preconditions:
    I2C module configured

  Parameters:
    const I2C_MODULE i2c - I2C module to be used for this transaction
    const UINT8 i2c_addr - address of I2C device
    const UINT8 i2c_register_start - I2C register to begin reading from
    const int nbytes - how many bytes to be read
    UINT8 *const buffer - buffer to place read bytes

  Returns:
    TRUE - reads were successful
    FALSE - at least 1 read was not successful

  Example:
    <code>
    // example from gyroscope, reading_rainbow buffer size 8
    I2CShared_ReadMultipleBytes(I2C_MODULE i2c, GYRO_WRITE, GYRO_READ, startReadI2CReg, nDataToRead, reading_rainbow)
    </code>

  Conditions at Exit:
    Buffer has n bytes of data in it read from (i2c)
    I2C bus idle

**************************************************************************************************/
BOOL I2CShared_ReadMultipleBytes(const I2C_MODULE i2c, const UINT8 i2c_addr, 
    const UINT8 i2c_register_start, const int nbytes, UINT8 *buffer) {
  int i;
  int fault_count = 0;
  UINT8 temp;
  
  UINT8 i2c_write_addr = i2c_addr | I2C_WRITE;
  UINT8 i2c_read_addr = i2c_addr | I2C_READ;

  // START TRANSACTION
  if(!I2CShared_StartTransfer(i2c, FALSE)) {
    //printf("I2CShared_ReadMultipleBytes: Error, bus collision during transfer start to I2C=%d\n", i2c);
    return FALSE;
  }
    
  // SEND ADDRESS
  if (!I2CShared_TransmitOneByte(i2c, i2c_write_addr)) {
    //printf("I2CShared_ReadMultipleBytes: Error, could not send i2c_address 0x%x to I2C=%d\n", i2c_write_addr, i2c);
    return FALSE;
  }
  
  // SEND INTERNAL REGISTER
  if (!I2CShared_TransmitOneByte(i2c, i2c_register_start)) {
    //printf("I2CShared_ReadMultipleBytes: Error, could not send i2c_register 0x%x to I2C=%d\n", (unsigned char) i2c_register_start, i2c);
    return FALSE;
  }
  
  // SEND START AGAIN FOR READ
  // Send read address
  if(!I2CShared_StartTransfer(i2c, TRUE)) {
    //printf("I2CShared_ReadMultipleBytes: Error, bus collision during transfer Restart to I2C=%d\n", i2c);
    return FALSE;
  }

  if (!I2CShared_TransmitOneByte(i2c, i2c_read_addr)) {
    //printf("I2CShared_ReadMultipleBytes: Error, could not send i2c_address 0x%x to I2C=%d\n", (unsigned char) i2c_read_addr, i2c);
    return FALSE;
  }
  
  // START READING
  // read all the data bytes and place them into the buffer
  for (i = 0; i < nbytes - 1; i++) {
    // configure i2c module to receive
    if (I2CReceiverEnable(i2c, TRUE) != I2C_SUCCESS) {
      //printf("I2CShared_ReadMultipleBytes: Error, could not configure I2C=%d to be a receiver\n", i2c);
      return FALSE;
    }

    fault_count = 0;
    while (!I2CReceivedDataIsAvailable(i2c)) {  // loop until data is ready to be read
      if (fault_count++ == TIMEOUT) {
        //printf("I2CShared_ReadMultipleBytes: Timeout waiting for I2CReceivedDataIsAvailable\n");
        return FALSE;
      }
    }

    // Read the byte from I2C
    temp = I2CGetByte(i2c);
    // Send ACK
    I2CAcknowledgeByte(i2c, TRUE);

    fault_count = 0;
    while(!I2CAcknowledgeHasCompleted(i2c)) {
      if (fault_count++ == TIMEOUT) {
        //printf("I2CShared_ReadMultipleBytes: Timeout waiting for I2CAcknowledgeHasCompleted\n");
        return FALSE;
      }
    }
    // place read data in buffer
    buffer[i] = temp;
    // END OF EACH READ
  }

  // Read last byte and send NACK
  if (I2CReceiverEnable(i2c, TRUE) != I2C_SUCCESS) {
    //printf("I2CShared_ReadMultipleBytes: Error, could not configure I2C=%d to be a receiver\n", i2c);
    return FALSE;
  }

  fault_count = 0;
  // Wait until data is ready to be read
  while (!I2CReceivedDataIsAvailable(i2c)) {
    if (fault_count++ == TIMEOUT) {
      //printf("I2CShared_ReadByte: Timeout waiting for I2CReceivedDataIsAvailable\n");
      return FALSE;
    }
  }

  // Get the data from the I2C
  temp = I2CGetByte(i2c);

  // Send NACK on last receive
  I2CAcknowledgeByte(i2c, FALSE);

  fault_count = 0;
  while(!I2CAcknowledgeHasCompleted(i2c)) {
    if (fault_count++ == TIMEOUT) {
      //printf("I2CShared_ReadByte: Timeout waiting for I2CAcknowledgeHasCompleted\n");
      return FALSE;
    }
  }

  // Place last data into buffer
  buffer[i] = temp;

  // Stop the transaction
  I2CShared_StopTransfer(i2c);
  return TRUE;
}
Exemplo n.º 9
0
/************************************************************************************************** 
  Function:
    BOOL I2CShared_ReadByte(const I2C_MODULE i2c, const UINT8 i2c_addr, const UINT8 i2c_register, UINT8 *const buffer)

  Author(s): 
    mkobit
  
  Summary: 
    Read a single byte from the (i2c) module from (i2c_register) and places it into (buffer)
  
  Description: 
    Waits until (i2c) bus is idle and then uses I2C protocol to start a transaction, write to the device and its register, restart with the read address
    and read the data. Sends a NACK at the end to stop the transmission and then stops the transaction
  
  Preconditions: 
    I2C module configured
  
  Parameters: 
    const I2C_MODULE i2c - I2C module to be used for this transaction
    const UINT8 i2c_write_addr - address of I2C device
    const UINT8 i2c_register - I2C register to read from
    UINT8 *const buffer - buffer to place read byte into
  
  Returns: 
    TRUE - read succesful
    FALSE - read unsuccessful
  
  Example: 
    <code>
    // example from accelerometer
    I2CShared_ReadByte(i2c, ACCEL_WRITE, ACCEL_READ, i2c_reg, buffer)
    </code>
  
  Conditions at Exit: 
    Buffer has byte of data in it read from (i2c)
    I2C bus idle
  
**************************************************************************************************/
BOOL I2CShared_ReadByte(const I2C_MODULE i2c, const UINT8 i2c_addr, const UINT8 i2c_register, UINT8 *const buffer) {
  int fault_count = 0;

  UINT8 i2c_write_addr = i2c_addr | I2C_WRITE;
  UINT8 i2c_read_addr = i2c_addr | I2C_READ;
  
  // START TRANSACTION
  if(!I2CShared_StartTransfer(i2c, FALSE)) {
    //printf("I2CShared_Read: Error, bus collision during transfer start to I2C=%d\n", i2c);
    return FALSE;
  }
    
  // SEND ADDRESS
  // Send write address for transaction, address is expected to already be formatted
  if (!I2CShared_TransmitOneByte(i2c, i2c_write_addr)) {
    //printf("I2CShared_Read: Error, could not send write address 0x%x to I2C=%d\n", (UINT8) i2c_write_addr, i2c);
    return FALSE;
  }
  
  // SEND INTERNAL REGISTER
  if (!I2CShared_TransmitOneByte(i2c, i2c_register)) {
    //printf("I2CShared_Read: Error, could not send i2c_register 0x%x to I2C=%d\n", (UINT8) i2c_register, i2c);
    return FALSE;
  }
  
  // SEND START AGAIN FOR READ
  // Send read address
  if(!I2CShared_StartTransfer(i2c, TRUE)) {
    //printf("I2CShared_Read: Error, bus collision during transfer start to I2C=%d\n", i2c);
    return FALSE;
  }

  if (!I2CShared_TransmitOneByte(i2c, i2c_read_addr)) {
    //printf("I2CShared_Read: Error, could not send i2c_address 0x%x to I2C=%d\n", i2c_read_addr, i2c);
    //I2CShared_StopTransfer(i2c);
    return FALSE;
  }
  
  // READ DATA BYTE
  // configure i2c module to receive
  if (I2CReceiverEnable(i2c, TRUE) != I2C_SUCCESS) {
    //printf("I2CShared_Read: Error, could not configure I2C=%d to be a receiver\n", i2c);
    return FALSE;
  }


  while (!I2CReceivedDataIsAvailable(i2c)) {// loop until data is ready to be read
    if (fault_count++ == TIMEOUT) {
      //printf("I2CShared_ReadByte: Timeout waiting for I2CReceivedDataIsAvailable\n");
      return FALSE;
    }
  }

  *buffer = I2CGetByte(i2c);
  I2CAcknowledgeByte(i2c, FALSE); // send nack on last byte
  fault_count = 0;
  while(!I2CAcknowledgeHasCompleted(i2c)) {
    if (fault_count++ == TIMEOUT) {
      //printf("I2CShared_ReadByte: Timeout waiting for I2CReceivedDataIsAvailable\n");
      return FALSE;
    }
  }
  I2CShared_StopTransfer(i2c);

  return TRUE;
}
Exemplo n.º 10
0
/**
 * Invokes an I2C acknowledge
 * 
 */
void I2C_Ack()
{
    I2CAcknowledgeByte(I2C1, TRUE);
    while (!I2CAcknowledgeHasCompleted(I2C1));
}
Exemplo n.º 11
0
/**
 * Invokes an I2C no acknowledge
 * 
 */
void I2C_Nack()
{
    I2CAcknowledgeByte(I2C1, FALSE);
    while (!I2CAcknowledgeHasCompleted(I2C1));
}
Exemplo n.º 12
0
Arquivo: I2C.c Projeto: sdajani/sdp
BOOL I2C_hasAcknowledged(I2C_MODULE I2C_ID) {
    I2CAcknowledgeHasCompleted(I2C_ID);
}
void* message_read(gestic_t *gestic, int *size)
{
    UINT8 RcvCount = 0;
    UINT8 writeIndex = 0;
    I2C_MODULE port = gestic->io.I2cPort;
    unsigned char *result = gestic->io.read_buffer;

    // Get TSLine
    if(gestic->io.device(GestICDev_TsLine_Get) == 1)
        result = NULL;

    if(result)
        gestic->io.device(GestICDev_TsLine_Assert);

    if(result && transfer_start(port, gestic->io.I2cSlaveAddr, 1))
        result = NULL;

    // Read the data from the desired address
    while(result) {
        UINT8 i2cbyte;

        /* Check for buffer overflow */
        if(I2CReceiverEnable(port, TRUE) == I2C_RECEIVE_OVERFLOW) {
            result = NULL;
            break;
        }

        while(!I2CReceivedDataIsAvailable(port));

        i2cbyte = I2CGetByte(gestic->io.I2cPort);
        result[writeIndex] = i2cbyte;
        writeIndex++;

        if(RcvCount == 0) {
            /* Set size if message fits into buffer */
            if(i2cbyte < sizeof(gestic->io.read_buffer))
                RcvCount = i2cbyte;
            else
                result = NULL;
        }

        /* In case of length == 0 or last byte (length == 1) send a NACK */
        if(RcvCount == 0 || RcvCount == 1)
            I2CAcknowledgeByte(port, FALSE);
        else
            I2CAcknowledgeByte(port, TRUE);

        while(!I2CAcknowledgeHasCompleted( gestic->io.I2cPort ));

        RcvCount--;
        if(RcvCount <= 0)
            break;
    }

    gestic->io.device(GestICDev_TsLine_Release);

    transfer_stop(gestic->io.I2cPort);
    
    if(result && size)
        *size = result[0];

    return result;
}