Exemplo n.º 1
0
Arquivo: I2C.c Projeto: sdajani/sdp
BOOL I2C_startTransfer(I2C_MODULE I2C_ID, BOOL restart){
    I2C_STATUS  status;

// Send the Start (or Restart) signal
    if(restart){
        if(I2CRepeatStart(I2C_ID) != I2C_SUCCESS){
            #ifdef DEBUG
            printf("Error: Bus collision during transfer Start at Read\n");
            #endif
            return FALSE;
        }
    }
    else{
    // Wait for the bus to be idle, then start the transfer
        while( !I2CBusIsIdle(I2C_ID) );
        if(I2CStart(I2C_ID) != I2C_SUCCESS){
            #ifdef DEBUG
            printf("Error: Bus collision during transfer Start at Write\n");
            #endif
            return FALSE;
        }
    }
    // Wait for the signal to complete
    do{
        status = I2CGetStatus(I2C_ID);
    }
    while (!(status & I2C_START) );

    return TRUE;
}
/****** I2C Driver implementation *******/
static bool StartTransfer(I2C_MODULE i2c_id, bool restart)
{
    I2C_STATUS status;

    // Send the Start (or Restart) signal
    if (restart)
    {
        I2CRepeatStart(i2c_id);
    }
    else
    {
        // Wait for the bus to be idle, then start the transfer
        while (!I2CBusIsIdle(i2c_id));

        if (I2CStart(i2c_id) != I2C_SUCCESS)
        {
            //DBPRINTF("Error: Bus collision during transfer Start\n");
            return FALSE;
        }
    }

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus(i2c_id);

    } while (!(status & I2C_START));

    return TRUE;
}
Exemplo n.º 3
0
/*
 Sends a start to begin i2c transaction (with no strings attached)
 */
void sendStart(I2C_MODULE i2c){
        while( ! I2CBusIsIdle(i2c));
        DelayMs(2);                     // timing for ADXL345
        I2CStart(i2c);
        while( ! (I2CGetStatus(i2c) & I2C_START) );
        I2CClearStatus(i2c, I2C_START);
}
Exemplo n.º 4
0
//==============================================================================
BOOL i2c_Start(UINT8 restart )
{
    I2C_STATUS  status;

    // Send the Start (or Restart) signal
    if(restart)
    {
        //I2CRepeatStart(EEPROM_I2C_BUS);
        I2C1CONbits.RSEN = 1;
    }
    else
    {
        // Wait for the bus to be idle, then start the transfer
        while( !I2CBusIsIdle(EEPROM_I2C_BUS) );

        if(I2CStart(EEPROM_I2C_BUS) != I2C_SUCCESS)
        {
            DBPRINTF("Error: Bus collision during transfer Start\n");
            return FALSE;
        }
    }

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus(EEPROM_I2C_BUS);
    } while ( !(status & I2C_START) );


    return TRUE;
}
int transfer_start(I2C_MODULE port, I2C_7_BIT_ADDRESS address, UINT8 rw)
{
    I2C_STATUS status;
    int result = 0;

    while(!I2CBusIsIdle(port));

    if(I2CStart(port) != I2C_SUCCESS)
        return -1;

    do {
        status = I2CGetStatus(port);
    } while(!(status & I2C_START));

    /* Send address */
    address.rw = rw;
    result = transmit_byte(port, address.byte);
    if(result < 0)
        return result;

    if(!I2CByteWasAcknowledged(port))
        return -1;
    
    return result;
}
Exemplo n.º 6
0
/**
 * Invokes an I2C stop condition
 * 
 */
void I2C_StopTransfer()
{
    // Send the Stop signal
    I2CStop(I2C1);

    // Wait for the signal to complete
    while (!(I2CGetStatus(I2C1) & I2C_STOP));
}
static int transfer_stop(I2C_MODULE port)
{
    I2C_STATUS status;

    I2CStop(port);

    do {
        status = I2CGetStatus(port);
    } while(!(status & I2C_STOP));
}
Exemplo n.º 8
0
/************************************************************************************************** 
  Function:
    void I2CShared_StopTransfer(const I2C_MODULE i2c)

  Author(s):
    mkobit

  Summary:
    Stops a transaction on the (i2c) module

  Description:
    Sends a stop signal and waits until the signal is complete
    Static function, used by internal library

  Preconditions:
    I2C module configured

  Parameters:
    const I2C_MODULE i2c - I2C module to be used for this transaction

  Returns:
    void

  Example:
    <code>
    I2CShared_StopTransfer(I2C1)
    </code>

  Conditions at Exit:
    I2C bus idle

**************************************************************************************************/
static void I2CShared_StopTransfer(const I2C_MODULE i2c) {
  I2C_STATUS status;

  // Send the Stop signal
  I2CStop(i2c);

  // Wait for the signal to complete
  do {
    status = I2CGetStatus(i2c);
  } while (!(status & I2C_STOP));
}
Exemplo n.º 9
0
Arquivo: I2C.c Projeto: sdajani/sdp
void I2C_stopTransfer(I2C_MODULE I2C_ID){
    I2C_STATUS  status;

    // Send the Stop signal
    I2CStop(I2C_ID);

    // Wait for the signal to complete
    do{
        status = I2CGetStatus(I2C_ID);
    }
    while (!(status & I2C_STOP));
}
Exemplo n.º 10
0
void StopTransfer( void )
{
    I2C_STATUS  status;

    // Send the Stop signal
    I2CStop(PIC24_I2C_BUS);

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus(PIC24_I2C_BUS);

    } while ( !(status & I2C_STOP) );
}
Exemplo n.º 11
0
//==============================================================================
void i2c_Stop( void )
{
    I2C_STATUS  status;

    // Send the Stop signal
    I2CStop(EEPROM_I2C_BUS);

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus(EEPROM_I2C_BUS);

    } while ( !(status & I2C_STOP) );
}
Exemplo n.º 12
0
/************************************************************************************************** 
  Function: 
    static void I2CShared_DebugStatus(const I2C_MODULE i2c)
  
  Author(s): 
    mkobit
  
  Summary: 
    Prints out statuses of the I2C module that is passed in
  
  Description: 
    Useful for debugging when errors occur to see what the current state of the I2C module is
    Static function, used by internal library
  
  Preconditions: 
    I2C module 
    UART2 preconfigured as debugging output
  
  Parameters: 
    const I2C_MODULE i2c - I2C module to print out status messages
  
  Returns: 
    void
  
  Example: 
    <code>
    I2CShared_DebugStatus(I2C1)
    </code>
  
  Conditions at Exit: 
    None
  
**************************************************************************************************/
static void I2CShared_DebugStatus(const I2C_MODULE i2c) {
    I2C_STATUS status;
    status = I2CGetStatus(i2c);
    
    if (I2C_TRANSMITTER_FULL & status) {
      //printf("I2CShared_DebugStatus: I2C_TRANSMITTER_FULL, 0x%x\n", I2C_TRANSMITTER_FULL & status);
    }
    if (I2C_DATA_AVAILABLE & status) {
      //printf("I2CShared_DebugStatus:) I2C_DATA_AVAILABLE, 0x%x\n", I2C_DATA_AVAILABLE & status);
    }
    if (I2C_SLAVE_READ & status) {
      //printf("I2CShared_DebugStatus:) I2C_SLAVE_READ, 0x%x\n", I2C_SLAVE_READ & status);
    }
    if (I2C_START & status) {
      //printf("I2CShared_DebugStatus: I2C_START, 0x%x\n", I2C_START & status);
    }
    if (I2C_STOP & status) {
      //printf("I2CShared_DebugStatus: I2C_STOP, 0x%x\n", I2C_STOP & status);
    }
    if (I2C_SLAVE_DATA & status) {
      //printf("I2CShared_DebugStatus: I2C_SLAVE_DATA, 0x%x\n", I2C_SLAVE_DATA & status);
    }
    if (I2C_RECEIVER_OVERFLOW & status) {
      //printf("I2CShared_DebugStatus: I2C_RECEIVER_OVERFLOW, 0x%x\n", I2C_RECEIVER_OVERFLOW & status);
    }
    if (I2C_TRANSMITTER_OVERFLOW & status) {
      //printf("I2CShared_DebugStatus: I2C_TRANSMITTER_OVERFLOW, 0x%x\n", I2C_TRANSMITTER_FULL & status);
    }
    if (I2C_10BIT_ADDRESS & status) {
      //printf("I2CShared_DebugStatus: I2C_10BIT_ADDRESS, 0x%x\n", I2C_10BIT_ADDRESS & status);
    }
    if (I2C_GENERAL_CALL & status) {
      //printf("I2CShared_DebugStatus: I2C_GENERAL_CALL, 0x%x\n", I2C_GENERAL_CALL & status);
    }
    if (I2C_ARBITRATION_LOSS & status) {
      //printf("I2CShared_DebugStatus: I2C_ARBITRATION_LOSS, 0x%x\n", I2C_ARBITRATION_LOSS & status);
    }
    if (I2C_TRANSMITTER_BUSY & status) {
      //printf("I2CShared_DebugStatus: I2C_TRANSMITTER_BUSY, 0x%x\n", I2C_TRANSMITTER_BUSY & status);
    }
    if (I2C_BYTE_ACKNOWLEDGED & status) {
      //printf("I2CShared_DebugStatus: I2C_BYTE_ACKNOWLEDGE, 0x%x\n", I2C_BYTE_ACKNOWLEDGED & status);
    }
    if (status == 0) {
      //printf("I2CShared_DebugStatus: no status\n");
    }
}
Exemplo n.º 13
0
/**
 * Invokes an I2C start condition
 * 
 * @param restart invoke a restart condition if true
 */
void I2C_StartTransfer(bool restart)
{
    // Send the Start (or Restart) signal
    if (restart) {
        I2CRepeatStart(I2C1);
    } else {
        // Wait for the bus to be idle, then start the transfer
        while (!I2CBusIsIdle(I2C1));

        if (I2CStart(I2C1) != I2C_SUCCESS) {
            //serialPrint("Error: Bus collision during transfer Start\r\n");
            while (1);
        }
    }
    // Wait for the signal to complete
    while (!(I2CGetStatus(I2C1) & I2C_START));
}
Exemplo n.º 14
0
/************************************************************************************************** 
  Function: 
    static BOOL I2CShared_StartTransfer(const I2C_MODULE i2c, const BOOL restart)
  
  Author(s):
    mkobit
  
  Summary: 
    Starts or restarts a transaction
  
  Description: 
    Blocks until transaction has been started, restarts the transaction if (restart) is TRUE
    Static function, used by internal library
  
  Preconditions: 
    I2C module configured
    *Transaction started  - *only if (restart) is TRUE
  
  Parameters: 
    const I2C_MODULE i2c - I2C module to be used for this transaction
    const BOOL restart - restart transaction when TRUE, just start when FALSE
  
  Returns: 
    TRUE - If successful
    FALSE - If unsuccessful
  
  Example: 
    <code>
    I2CShared_StartTransfer(i2c, FALSE)
    </code>
  
  Conditions at Exit: 
    Bus transaction started
    I2C waiting for next action
  
**************************************************************************************************/
static BOOL I2CShared_StartTransfer(const I2C_MODULE i2c, const BOOL restart) {
  I2C_STATUS status;
  int fault_count = 0;

  // Send the start/restart signal
  if(restart) {
    I2CRepeatStart(i2c);
  }
  else {
    // Wait for the bus to be idle, then start the transfer
    while(!I2CBusIsIdle(i2c)){
      if (fault_count++ == TIMEOUT) {
        //printf("I2CShared_StartTransfer: Timeout waiting for bus to be idle\n");
        return FALSE;
      }
    }

    if(I2CStart(i2c) == I2C_ARBITRATION_LOSS)
    {
      //printf("I2CShared_StartTransfer: I2CStart experienced I2C_ARBITRATION_LOSS\n");
      //I2CShared_DebugStatus(i2c);
      return FALSE;
    }
  }

  fault_count = 0;
  // Wait for the signal to complete
  do {
    status = I2CGetStatus(i2c);
    if (status & I2C_ARBITRATION_LOSS) {
      //printf("I2CShared_StartTransfer: lost arbitration on i2c bus\n");
      return FALSE;
    }
    if (fault_count++ == TIMEOUT) {
      //printf("I2CShared_StartTransfer: Timeout waiting for bus to start\n");
      return FALSE;
    }
  } while (!(status & I2C_START));

  return TRUE;
}
Exemplo n.º 15
0
void MPU6050::StopTransfer( void )
{
    I2C_STATUS  status;
    UINT8 count = 0;

    // Send the Stop signal
    I2CStop( this->i2cBusId );

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus( this->i2cBusId );
        count++;

    } while ( !(status & I2C_STOP) && count < 200);

    if ( count >= 200 )
    {
        sprintf( filename, "Error: StopTransfer(). Loop timeout for I2CGetStatus().\n" );
        putsUART1( filename );
    }
}
Exemplo n.º 16
0
void i2c1_interrupt_handler(void)
{
	unsigned long woken = FALSE;
	int status = I2CGetStatus(I2C1);

	if (state == I2C_STATE_START && !(status & I2C_TRANSMITTER_BUSY)) {

	} else if (state == I2C_STATE_TRANSMIT && !(status & I2C_TRANSMITTER_FULL)) {
		
	} else if (state == I2C_STATE_REPEATED_START && !(status & I2C_TRANSMITTER_BUSY)) {
		
	} else if (state == I2C_STATE_STOP && !(status & I2C_STOP)) {
		
	} else if (state == I2C_STATE_RECEIVE && !(status & I2C_DATA_AVAILABLE)) {
		
	} else if (state == I2C_STATE_ACKNOWLEDGE && !(status & I2C_TRANSMITTER_BUSY)) {
		
	} else {
		return;
	}

	semaphore_give_from_isr(bus_lock, &woken);
}
Exemplo n.º 17
0
BOOL StartTransfer( BOOL restart )
{
    I2C_STATUS  status;

    // Send the Start (or Restart) signal
    if(restart)
    {
        I2CRepeatStart(PIC24_I2C_BUS);
    }
    else
    {
        // Wait for the bus to be idle, then start the transfer
        while( !I2CBusIsIdle(PIC24_I2C_BUS) );

        if(I2CStart(PIC24_I2C_BUS) != I2C_SUCCESS)
        {
            DBPRINTF("Error: Bus collision during transfer Start\n");
            printf("bus coll\n");
            //Succ = FALSE;
            //goto RECOVER;
        }
    }

    // Wait for the signal to complete
    do
    {
        status = I2CGetStatus(PIC24_I2C_BUS);
        //printf("waiting for signal\n");

    } while ( !(status & I2C_START) );

RECOVER:
    //if(!Succ) return FALSE;
    //else return TRUE;
return TRUE;
}
Exemplo n.º 18
0
BOOL MPU6050::StartTransfer( BOOL restart )
{
    I2C_STATUS status = I2C_START;
    //UINT16 count = 0;

    //sprintf(filename, "Starting StartTransfer(), status = %d.\n", status);
    //putsUART1( filename );

    // Send the Start (or Restart) signal
    if(restart)
    {
        I2C_RESULT res = I2C_SUCCESS;
        if((res = I2CRepeatStart( this->i2cBusId )) != I2C_SUCCESS)
        {
            sprintf(filename, "Repeat start, status = %d.\n",res);
            putsUART1( filename );
            // Do not return, try to connect anyway and fail
        }
    }
    else
    {
        // Wait for the bus to be idle, then start the transfer
        //while( !I2CBusIsIdle(MPU6050_I2C_BUS) );

        // Checks if the bus is idle, and starts the transfer if so
        if( I2CBusIsIdle( this->i2cBusId ) )
        {
            if(I2CStart( this->i2cBusId ) != I2C_SUCCESS)
            {
                //DBPRINTF("Error: Bus collision during transfer Start\n");

                sprintf( filename, "Error in I2CStart(). Bus collision on bus %u during transfer Start.\n", (unsigned)this->i2cBusId );
                putsUART1( filename );

                return FALSE;
            }
        }
        else
        {
            sprintf( filename, "Error in I2CBusIsIdle(). Bus %u is not idle.\n", (unsigned)this->i2cBusId );
            putsUART1( filename );

            return FALSE;
        }
    }

    //sprintf( filename, "StartTransfer(). Checking for Start response...\n" );
    //putsUART1( filename );
    UINT16 max_tries = 64000, count = 0;
    // Wait for the signal to complete or until tries are out
    do
    {
        status = I2CGetStatus( this->i2cBusId );
        //sprintf( filename, "StartTransfer(). Status is %u \n", status & I2C_START );
        //putsUART1( filename );
    } while (!(status & I2C_START) && ++count < max_tries);
    
    if( count >= max_tries )
    {
        sprintf( filename, "Error in StartTransfer(). Timeout!\n" );
        putsUART1( filename );  

        return FALSE;
    }

    //sprintf( filename, "StartTransfer(). Function successfully completed!\n" );
    //putsUART1( filename );

    return TRUE;
}
Exemplo n.º 19
0
void i2cStart(I2C_MODULE id){
    while( !I2CBusIsIdle(id) );
    I2CStart(id);
    while ( !(I2CGetStatus(id) & I2C_START) );
}void i2cSendByte(I2C_MODULE id, BYTE data){
Exemplo n.º 20
0
void i2cRepeatedStart(I2C_MODULE id){
    I2CRepeatStart(id);
    while ( !(I2CGetStatus(id) & I2C_START) );
}
Exemplo n.º 21
0
void i2cStop(I2C_MODULE id){
    I2CStop(id);
    while ( !(I2CGetStatus(id) & I2C_STOP) );
}
Exemplo n.º 22
0
/*
 Sends a start inside i2c transaction (with no strings attached)
 */
void repeatStart(I2C_MODULE I2C){
    I2CRepeatStart(I2C);
    while( ! (I2CGetStatus(I2C) & I2C_START) );
    I2CClearStatus(I2C, I2C_START);
}
Exemplo n.º 23
0
//************************************************************
// Synchronous READ - I2C API (visible externally) component
//************************************************************
uint	I2CSyncRead(	uint	I2Cx,
						byte 	DevAddr, 
						byte	Register,
						byte* 	Buffer,
						uint  	BufLen )
	{
	if (!_I2C_Init)		return I2CRC_NRDY;
	//=========================================================
	// Obtain references to proper Control Blocks and registers
	//---------------------------------------------------------
	_I2C_CB*		pCB;
	if ( NULL == (pCB = I2CpCB(I2Cx)) )		return I2CRC_NOTA;
	//---------------------------------------------------------
	I2C_CONBITS*	pCON	= I2CpCON(pCB);
	I2C_STATBITS*	pSTAT	= I2CpSTAT(pCB);
	//=========================================================
	// Validate run-time conditions
	//---------------------------------------------------------
	uint	RC;
	BYTE	CPU_IPL;
	uint	RetryCount	= 0;

	//---------------------------------------------------------
	// Enter I2C (and related modules) CRITICAL SECTION
	//---------------------------------------------------------
Retry:
	RC	= I2CRC_OK;
	//------------------------------------
	SET_AND_SAVE_CPU_IPL(CPU_IPL, _I2C_IL);
		{
		if (I2CRC_OK == (RC=I2CGetStatus(pCB, pCON, pSTAT)))
			//---------------------------------------------------------
			// Set Flag indicating Synchronous operation is in progress
			//---------------------------------------------------------
			pCB->_I2C_SBSY	= 1;	// Should be cleared at exit
		}  
	//---------------------------------------------------------
	// Leave I2C CRITICAL SECTION
	//---------------------------------------------------------
  	RESTORE_CPU_IPL(CPU_IPL);
	//=========================================================
	switch (RC)	
		{
		case	I2CRC_OK:
			break;		// Run-time conditions are OK
		//-------------------------------------------
		case	I2CRC_ABSY:
		case	I2CRC_SBSY:
		case	I2CRC_BUSY:
			// Situation could be helped by delay/retry
			if (RetryCount < I2C_BUSYRetry)
				{
				TMRDelayTicks(1);	// Small delay ~125 usec
				RetryCount++;
				goto Retry;					// Attempt retry
				}
			else;		// Fall through to "default"
		//-------------------------------------------
		default:
			return RC;	// Run-time conditions are not met
		}
	//=========================================================

	//---------------------------------------------------------
	// Event sequence to accomplish simple READ from the target
	// slave device in the MASTER mode:
	//	1. Assert a Start condition on SDAx and SCLx.
	//	2. Send the I2C device address byte to the slave with a
	//	   read indication ("1" in the LSB of address)
	//	3. Wait for and verify an Acknowledge from the slave.
	//	4. Assert receiving state (RCEN = 1).
	//	5. Receive byte and send ACK to the slave.
	//	6. Repeat steps 4 and 5 for every byte in a message, 
	//	   except the last - after receiving last byte send NACK.
	//	7. Generate a Stop condition on SDAx and SCLx.
	//---------------------------------------------------------
	// Prepare READ and WRITE forms of device address
	byte	i2cAddrW	= DevAddr & 0xFE;	// Set WRITE cond.
	byte	i2cAddrR	= DevAddr | 0x01;	// Set READ cond.
	//---------------------------------------------------------
	RC			= I2CRC_OK;
	//---------------------------------------------------------
	I2CStart(pCB);		// Signal START on SDA and SCL pins
	//---------------------------------------------------------
	// Send Device WRITE address
	//---------------------------------------------------------
	RC = I2CMasterWriteByte(pCB, i2cAddrW);
	if (RC != I2CRC_OK)	goto Finally;
	//---------------------------------------------------------
	// Send Register address (as data))
	//---------------------------------------------------------
	RC = I2CMasterWriteByte(pCB, Register);
	if (RC != I2CRC_OK)	goto Finally;
	//---------------------------------------------------------
	I2CReStart(pCB);	// Signal Repeated-START on SDA and SCL pins
	//---------------------------------------------------------
	// Send Device READ address
	//---------------------------------------------------------
	RC = I2CMasterWriteByte(pCB, i2cAddrR);
	if (RC != I2CRC_OK)	goto Finally;
	//---------------------------------------------------------
	// Receive data
	//---------------------------------------------------------
	pSTAT->I2COV = 0;	// Clear receive OVERFLOW bit, if any
	//---------------------------------------------------------
	uint	BufPos;
	uint	BufCnt	= BufLen;
	//---------------------------------------------------------
	for (BufPos = 0; BufPos < BufLen; BufPos++)
		{
		BufCnt--;	// Used as a FLAG (BufCnt=0) to indicate last byte
		//---------------------------------------------------------
		RC = I2CMasterReadByte(pCB, Buffer, BufCnt);
		if (RC != I2CRC_OK)
			break;			// Error...
		Buffer++;
		}
	//---------------------------------------------------------
Finally:
	I2CStop(pCB);			// Signal STOP on SDA and SCL pins
	//---------------------------------------------------------
	pCB->_I2C_SBSY	= 0;	// Clear SYNC flag
	return RC;				// Return last Error Code...
	}