void TWI1_IrqHandler(void) {
	unsigned int status = TWI_GetStatus(TWI_STACK);

    if(((status & TWI_SR_SVACC) == TWI_SR_SVACC) && (eeprom.acquire == 0)) {
        TWI_DisableIt(TWI_STACK, TWI_IER_SVACC);
        TWI_EnableIt(TWI_STACK, TWI_IER_RXRDY |
                                TWI_IER_GACC |
                                TWI_IER_NACK |
                                TWI_IER_EOSACC |
                                TWI_IER_SCL_WS);
        eeprom.acquire++;
        eeprom.page = 0;
        eeprom.offset = 0;
    }

    if(((status & TWI_SR_SVACC) == TWI_SR_SVACC) &&
       ((status & TWI_SR_GACC) == 0) &&
       ((status & TWI_SR_RXRDY) == TWI_SR_RXRDY)) {

        if(eeprom.acquire == 1) {
            // Acquire LSB address
        	eeprom.page = (TWI_ReadByte(TWI_STACK) & 0xFF);
        	eeprom.acquire++;
        } else if(eeprom.acquire == 2) {
            // Acquire MSB address
        	eeprom.page |= (TWI_ReadByte(TWI_STACK) & 0xFF) << 8;
        	eeprom.acquire++;
        } else {
            // Read one byte of data from master to slave device
        	uint16_t addr = I2C_EEPROM_PAGE_SIZE*eeprom.page + eeprom.offset;
        	i2c_eeprom_slave_set_memory(addr, TWI_ReadByte(TWI_STACK) & 0xFF);
        	eeprom.offset++;
        }
    } else if(((status & TWI_SR_TXRDY) == TWI_SR_TXRDY) &&
              ((status & TWI_SR_TXCOMP) == TWI_SR_TXCOMP) &&
              ((status & TWI_SR_EOSACC) == TWI_SR_EOSACC)) {

        // End of transfer, end of slave access
    	eeprom.offset = 0;
    	eeprom.acquire = 0;
    	eeprom.page = 0;
        TWI_EnableIt(TWI_STACK, TWI_IER_SVACC);
        TWI_DisableIt(TWI_STACK, TWI_IER_RXRDY |
                                 TWI_IDR_GACC |
                                 TWI_IDR_NACK |
                                 TWI_IER_EOSACC |
                                 TWI_IER_SCL_WS);
    } else if(((status & TWI_SR_SVACC) == TWI_SR_SVACC) &&
              ((status & TWI_SR_GACC) == 0) &&
              (eeprom.acquire == 3) &&
              ((status & TWI_SR_SVREAD) == TWI_SR_SVREAD) &&
              ((status & TWI_SR_NACK) == 0)) {

        // Write one byte of data from slave to master device
    	uint16_t addr = I2C_EEPROM_PAGE_SIZE*eeprom.page + eeprom.offset;
        TWI_WriteByte(TWI_STACK, i2c_eeprom_slave_get_memory(addr));
        eeprom.offset++;
    }
}
Beispiel #2
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop) {
	if (quantity > BUFFER_LENGTH)
		quantity = BUFFER_LENGTH;

	// perform blocking read into buffer
	int readed = 0;
	TWI_StartRead(twi, address, iaddress, isize);
	do {
		// Stop condition must be set during the reception of last byte
		if (readed + 1 == quantity)
			TWI_SendSTOPCondition( twi);

		if (TWI_WaitByteReceived(twi, RECV_TIMEOUT))
			rxBuffer[readed++] = TWI_ReadByte(twi);
		else
			break;
	} while (readed < quantity);
	TWI_WaitTransferComplete(twi, RECV_TIMEOUT);

	// set rx buffer iterator vars
	rxBufferIndex = 0;
	rxBufferLength = readed;

	return readed;
}
Beispiel #3
0
//------------------------------------------------------------------------------
/// Interrupt handler for a TWI peripheral. Manages asynchronous transfer
/// occuring on the bus. This function MUST be called by the interrupt service
/// routine of the TWI peripheral if asynchronous read/write are needed.
/// \param pTwid  Pointer to a Twid instance.
//------------------------------------------------------------------------------
void TWID_Handler(Twid *pTwid)
{
    unsigned char status;
    AsyncTwi *pTransfer = (AsyncTwi *) pTwid->pTransfer;
    AT91S_TWI *pTwi = pTwid->pTwi;

    SANITY_CHECK(pTwid);

    // Retrieve interrupt status
    status = TWI_GetMaskedStatus(pTwi);

    // Byte received
    if (TWI_STATUS_RXRDY(status)) {

        pTransfer->pData[pTransfer->transferred] = TWI_ReadByte(pTwi);
        pTransfer->transferred++;

        // Transfer finished ?
        if (pTransfer->transferred == pTransfer->num) {

            TWI_DisableIt(pTwi, AT91C_TWI_RXRDY);
            TWI_EnableIt(pTwi, AT91C_TWI_TXCOMP);
        }
        // Last byte ?
        else if (pTransfer->transferred == (pTransfer->num - 1)) {

            TWI_Stop(pTwi);
        }
    }
    // Byte sent
    else if (TWI_STATUS_TXRDY(status)) {

        // Transfer finished ?
        if (pTransfer->transferred == pTransfer->num) {

            TWI_DisableIt(pTwi, AT91C_TWI_TXRDY);
            TWI_EnableIt(pTwi, AT91C_TWI_TXCOMP);
            TWI_SendSTOPCondition(pTwi);
        }
        // Bytes remaining
        else {

            TWI_WriteByte(pTwi, pTransfer->pData[pTransfer->transferred]);
            pTransfer->transferred++;
        }
    }
    // Transfer complete
    else if (TWI_STATUS_TXCOMP(status)) {

        TWI_DisableIt(pTwi, AT91C_TWI_TXCOMP);
        pTransfer->status = 0;
        if (pTransfer->callback) {
            
            pTransfer->callback((Async *) pTransfer);
        }
        pTwid->pTransfer = 0;
    }
}
Beispiel #4
0
/**
 * \brief Asynchronously reads data from a slave on the TWI bus. An optional
 * callback function is triggered when the transfer is complete.
 * \param pTwid  Pointer to a Twid instance.
 * \param address  TWI slave address.
 * \param iaddress  Optional slave internal address.
 * \param isize  Internal address size in bytes.
 * \param pData  Data buffer for storing received bytes.
 * \param num  Number of bytes to read.
 * \param pAsync  Asynchronous transfer descriptor.
 * \return 0 if the transfer has been started; otherwise returns a TWI error code.
 */
uint8_t TWID_Read(
    Twi *pTwi,
    uint8_t address,
    uint32_t iaddress,
    uint8_t isize,
    uint8_t *pData,
    uint32_t num)
{
    //Twi *pTwi;
    uint32_t timeout;

    assert( pTwi != NULL ) ;

    assert( (address & 0x80) == 0 ) ;
    assert( (iaddress & 0xFF000000) == 0 ) ;
    assert( isize < 4 ) ;

    /* Set STOP signal if only one byte is sent*/
    if (num == 1) {

        TWI_Stop(pTwi);
    }

    /* Synchronous transfer*/

    /* Start read*/
    TWI_StartRead(pTwi, address, iaddress, isize);

    /* Read all bytes, setting STOP before the last byte*/
    while (num > 0) {

        /* Last byte ?*/
        if (num == 1) {

            TWI_Stop(pTwi);
        }

        /* Wait for byte then read and store it*/
        timeout = 0;
        while( !TWI_ByteReceived(pTwi) && (++timeout<TWITIMEOUTMAX) );
        if (timeout == TWITIMEOUTMAX) {

            TRACE_ERROR("TWID Timeout BR\n\r");
        }
        *pData++ = TWI_ReadByte(pTwi);
        num--;
    }

    /* Wait for transfer to be complete */
    timeout = 0;
    while( !TWI_TransferComplete(pTwi) && (++timeout<TWITIMEOUTMAX) );
    if (timeout == TWITIMEOUTMAX) {
        TRACE_ERROR("TWID Timeout TC\n\r");
    }

    return 0;
}
// i2c_eeprom_master_read/write are based on twid.c from atmels at91lib and
// adapted for better handling when there is no eeprom present.
// This handling is needed for the bricklet initialization
bool i2c_eeprom_master_read(Twi *twi,
                            const uint16_t internal_address,
                            char *data,
                            const uint16_t length) {
	uint32_t timeout;

	mutex_take(mutex_twi_bricklet, MUTEX_BLOCKING);

	// Start read
	TWI_StartRead(twi,
	              bricklet_eeprom_address,
	              internal_address,
	              I2C_EEPROM_INTERNAL_ADDRESS_BYTES);

	for(uint16_t i = 0; i < length; i++) {
		// If last Byte -> send STOP
		if(i == length-1) {
			TWI_Stop(twi);
		}

		uint32_t timeout = 0;
		// Wait until byte is received, otherwise return false
		while(!TWI_ByteReceived(twi) && (++timeout < I2C_EEPROM_TIMEOUT));

		if(timeout == I2C_EEPROM_TIMEOUT) {
			logieew("read timeout (nothing received)\n\r");
			mutex_give(mutex_twi_bricklet);
			return false;
		}

		data[i] = TWI_ReadByte(twi);
	}

	timeout = 0;
	// Wait for transfer to be complete
	while(!TWI_TransferComplete(twi) && (++timeout < I2C_EEPROM_TIMEOUT));
	if (timeout == I2C_EEPROM_TIMEOUT) {
		logieew("read timeout (transfer incomplete)\n\r");
		mutex_give(mutex_twi_bricklet);
		return false;
	}

	mutex_give(mutex_twi_bricklet);
    return true;
}
Beispiel #6
0
unsigned char ReadAccelData(unsigned int iaddress, char *bytes, unsigned int num)
{
	unsigned int timeout;
	
	// wait for TWI bus to be ready
	while(!(TWI_TransferComplete(AT91C_BASE_TWI))) nop();
	
	// Start Reading
	TWI_StartRead(AT91C_BASE_TWI, ACCELADDR,iaddress,1);
	
	while (num > 0) {
		// Last byte
		if(num == 1) TWI_Stop(AT91C_BASE_TWI);
		
		// wait for byte then read and store it
		timeout = 0;
		while(!TWI_ByteReceived(AT91C_BASE_TWI) && (++timeout<TWITIMEOUTMAX)) nop();
		if(timeout == TWITIMEOUTMAX) return 2;
		*bytes++ = TWI_ReadByte(AT91C_BASE_TWI);
		num--;
	}
	
	return 0;
}
Beispiel #7
0
void TwoWire::onService(void) {
	// Retrieve interrupt status
	uint32_t sr = TWI_GetStatus(twi);

	if (status == SLAVE_IDLE && TWI_STATUS_SVACC(sr)) {
		TWI_DisableIt(twi, TWI_IDR_SVACC);
		TWI_EnableIt(twi, TWI_IER_RXRDY | TWI_IER_GACC | TWI_IER_NACK
				| TWI_IER_EOSACC | TWI_IER_SCL_WS | TWI_IER_TXCOMP);

		srvBufferLength = 0;
		srvBufferIndex = 0;

		// Detect if we should go into RECV or SEND status
		// SVREAD==1 means *master* reading -> SLAVE_SEND
		if (!TWI_STATUS_SVREAD(sr)) {
			status = SLAVE_RECV;
		} else {
			status = SLAVE_SEND;

			// Alert calling program to generate a response ASAP
			if (onRequestCallback)
				onRequestCallback();
			else
				// create a default 1-byte response
				write((uint8_t) 0);
		}
	}

	if (status != SLAVE_IDLE) {
		if (TWI_STATUS_TXCOMP(sr) && TWI_STATUS_EOSACC(sr)) {
			if (status == SLAVE_RECV && onReceiveCallback) {
				// Copy data into rxBuffer
				// (allows to receive another packet while the
				// user program reads actual data)
				for (uint8_t i = 0; i < srvBufferLength; ++i)
					rxBuffer[i] = srvBuffer[i];
				rxBufferIndex = 0;
				rxBufferLength = srvBufferLength;

				// Alert calling program
				onReceiveCallback( rxBufferLength);
			}

			// Transfer completed
			TWI_EnableIt(twi, TWI_SR_SVACC);
			TWI_DisableIt(twi, TWI_IDR_RXRDY | TWI_IDR_GACC | TWI_IDR_NACK
					| TWI_IDR_EOSACC | TWI_IDR_SCL_WS | TWI_IER_TXCOMP);
			status = SLAVE_IDLE;
		}
	}

	if (status == SLAVE_RECV) {
		if (TWI_STATUS_RXRDY(sr)) {
			if (srvBufferLength < BUFFER_LENGTH)
				srvBuffer[srvBufferLength++] = TWI_ReadByte(twi);
		}
	}

	if (status == SLAVE_SEND) {
		if (TWI_STATUS_TXRDY(sr) && !TWI_STATUS_NACK(sr)) {
			uint8_t c = 'x';
			if (srvBufferIndex < srvBufferLength)
				c = srvBuffer[srvBufferIndex++];
			TWI_WriteByte(twi, c);
		}
	}
}
Beispiel #8
0
/**
 * \brief Interrupt handler for a TWI peripheral. Manages asynchronous transfer
 * occuring on the bus. This function MUST be called by the interrupt service
 * routine of the TWI peripheral if asynchronous read/write are needed.
  * \param pTwid  Pointer to a Twid instance.
 */
void TWID_Handler( Twid *pTwid )
{
    uint8_t status;
    AsyncTwi *pTransfer ;
    Twi *pTwi ;

    assert( pTwid != NULL ) ;

    pTransfer = (AsyncTwi*)pTwid->pTransfer ;
    assert( pTransfer != NULL ) ;
    pTwi = pTwid->pTwi ;
    assert( pTwi != NULL ) ;

    /* Retrieve interrupt status */
    status = TWI_GetMaskedStatus(pTwi);

    /* Byte received */
    if (TWI_STATUS_RXRDY(status)) {

        pTransfer->pData[pTransfer->transferred] = TWI_ReadByte(pTwi);
        pTransfer->transferred++;

        /* check for transfer finish */
        if (pTransfer->transferred == pTransfer->num) {

            TWI_DisableIt(pTwi, TWI_IDR_RXRDY);
            TWI_EnableIt(pTwi, TWI_IER_TXCOMP);
        }
        /* Last byte? */
        else if (pTransfer->transferred == (pTransfer->num - 1)) {

            TWI_Stop(pTwi);
        }
    }
    /* Byte sent*/
    else if (TWI_STATUS_TXRDY(status)) {

        /* Transfer finished ? */
        if (pTransfer->transferred == pTransfer->num) {

            TWI_DisableIt(pTwi, TWI_IDR_TXRDY);
            TWI_EnableIt(pTwi, TWI_IER_TXCOMP);
            TWI_SendSTOPCondition(pTwi);
        }
        /* Bytes remaining */
        else {

            TWI_WriteByte(pTwi, pTransfer->pData[pTransfer->transferred]);
            pTransfer->transferred++;
        }
    }
    /* Transfer complete*/
    else if (TWI_STATUS_TXCOMP(status)) {

        TWI_DisableIt(pTwi, TWI_IDR_TXCOMP);
        pTransfer->status = 0;
        pTwid->pTransfer = 0;
        if (pTransfer->callback) {

            pTransfer->callback((Async *) pTransfer);
        }
    }
}
Beispiel #9
0
/**
 * \brief Asynchronously reads data from a slave on the TWI bus. An optional
 * callback function is triggered when the transfer is complete.
 * \param pTwid  Pointer to a Twid instance.
 * \param address  TWI slave address.
 * \param iaddress  Optional slave internal address.
 * \param isize  Internal address size in bytes.
 * \param pData  Data buffer for storing received bytes.
 * \param num  Number of bytes to read.
 * \param pAsync  Asynchronous transfer descriptor.
 * \return 0 if the transfer has been started; otherwise returns a TWI error code.
 */
uint8_t TWID_Read(
    Twid *pTwid,
    uint8_t address,
    uint32_t iaddress,
    uint8_t isize,
    uint8_t *pData,
    uint32_t num,
    Async *pAsync)
{
    Twi *pTwi;
    AsyncTwi *pTransfer;
    uint32_t timeout;

    assert( pTwid != NULL ) ;
    pTwi = pTwid->pTwi;
    pTransfer = (AsyncTwi *) pTwid->pTransfer;

    assert( (address & 0x80) == 0 ) ;
    assert( (iaddress & 0xFF000000) == 0 ) ;
    assert( isize < 4 ) ;

    /* Check that no transfer is already pending*/
    if (pTransfer) {
        TRACE_ERROR("TWID_Read: A transfer is already pending\n\r");
        return TWID_ERROR_BUSY;
    }

    /* Set STOP signal if only one byte is sent*/
    if (num == 1) {

        TWI_Stop(pTwi);
    }

    /* Asynchronous transfer*/
    if (pAsync) {
        /* Update the transfer descriptor */
        pTwid->pTransfer = pAsync;
        pTransfer = (AsyncTwi *) pAsync;
        pTransfer->status = ASYNC_STATUS_PENDING;
        pTransfer->pData = pData;
        pTransfer->num = num;
        pTransfer->transferred = 0;

        /* Enable read interrupt and start the transfer */
        TWI_EnableIt(pTwi, TWI_IER_RXRDY);
        TWI_StartRead(pTwi, address, iaddress, isize);
    }
    /* Synchronous transfer*/
    else {

        /* Start read*/
        TWI_StartRead(pTwi, address, iaddress, isize);

        /* Read all bytes, setting STOP before the last byte*/
        while (num > 0) {

            /* Last byte ?*/
            if (num == 1) {

                TWI_Stop(pTwi);
            }

            /* Wait for byte then read and store it*/
            timeout = 0;
            while( !TWI_ByteReceived(pTwi) && (++timeout<TWITIMEOUTMAX) ) {
            	if(pTwi->TWI_SR & TWI_SR_NACK) {
            		return 0;
            	}
            }
            if (timeout == TWITIMEOUTMAX) {
                TRACE_ERROR("TWID Timeout BR\n\r");
                *pData++ = 0;
            } else {
				*pData++ = TWI_ReadByte(pTwi);
            }
            num--;
        }

        /* Wait for transfer to be complete */
        timeout = 0;
        while( !TWI_TransferComplete(pTwi) && (++timeout<TWITIMEOUTMAX) );
        if (timeout == TWITIMEOUTMAX) {
            TRACE_ERROR("TWID Timeout TC\n\r");
        }
    }

    pTwi->TWI_SR;
    pTwi->TWI_RHR;

    return 0;
}
Beispiel #10
0
/**
 * \brief Asynchronously reads data from a slave on the TWI bus. An optional
 * callback function is triggered when the transfer is complete.
 * \param pTwid  Pointer to a Twid instance.
 * \param address  TWI slave address.
 * \param iaddress  Optional slave internal address.
 * \param isize  Internal address size in bytes.
 * \param pData  Data buffer for storing received bytes.
 * \param num  Number of bytes to read.
 * \param pAsync  Asynchronous transfer descriptor.
 * \return 0 if the transfer has been started; otherwise returns a TWI error code.
 */
uint8_t TWID_Read(
    Twid *pTwid,
    uint8_t address,
    uint32_t iaddress,
    uint8_t isize,
    uint8_t *pData,
    uint32_t num,
    Async *pAsync)
{
    Twi *pTwi;
    AsyncTwi *pTransfer;
    uint32_t timeout = 0;
    uint32_t i = 0;
    uint32_t status;

    assert( pTwid != NULL ) ;
    pTwi = pTwid->pTwi;
    pTransfer = (AsyncTwi *) pTwid->pTransfer;

    assert( (address & 0x80) == 0 ) ;
    assert( (iaddress & 0xFF000000) == 0 ) ;
    assert( isize < 4 ) ;

    /* Check that no transfer is already pending*/
    if (pTransfer) {

        TRACE_ERROR("TWID_Read: A transfer is already pending\n\r");
        return TWID_ERROR_BUSY;
    }

    /* Asynchronous transfer*/
    if (pAsync) {

        /* Update the transfer descriptor */
        pTwid->pTransfer = pAsync;
        pTransfer = (AsyncTwi *) pAsync;
        pTransfer->status = ASYNC_STATUS_PENDING;
        pTransfer->pData = pData;
        pTransfer->num = num;
        pTransfer->transferred = 0;

        /* Enable read interrupt and start the transfer */
        TWI_EnableIt(pTwi, TWI_IER_RXRDY);
        TWI_StartRead(pTwi, address, iaddress, isize);
    }
    /* Synchronous transfer*/
    else {

        /* Start read*/
        TWI_StartRead(pTwi, address, iaddress, isize);
        if (num != 1) 
        {
                status = TWI_GetStatus(pTwi);

                if(status & TWI_SR_NACK)
                    TRACE_ERROR("TWID NACK error\n\r");
                timeout = 0;
                while( ! (status & TWI_SR_RXRDY) && (++timeout<TWITIMEOUTMAX))
                {
                    status = TWI_GetStatus(pTwi);
                    //TRACE_ERROR("TWID status %x\n\r",TWI_GetStatus(pTwi));
                }

                pData[0] = TWI_ReadByte(pTwi);
                for( i = 1; i < num - 1; i++)
                {
                    status = TWI_GetStatus(pTwi);
                    if(status & TWI_SR_NACK)
                      TRACE_ERROR("TWID NACK error\n\r");
                    timeout = 0;
                    while( ! (status & TWI_SR_RXRDY) && (++timeout<TWITIMEOUTMAX))
                    {
                        status = TWI_GetStatus(pTwi);
                        //TRACE_ERROR("TWID status %x\n\r",TWI_GetStatus(pTwi));
                    }
                    pData[i] = TWI_ReadByte(pTwi);
                }
        }
        TWI_Stop(pTwi);
        status = TWI_GetStatus(pTwi);
        if(status & TWI_SR_NACK)
          TRACE_ERROR("TWID NACK error\n\r");
        timeout = 0;
        while( ! (status & TWI_SR_RXRDY)  && (++timeout<TWITIMEOUTMAX))
        {
            status = TWI_GetStatus(pTwi);
            //TRACE_ERROR("TWID status %x\n\r",TWI_GetStatus(pTwi));
        }

        pData[i] = TWI_ReadByte(pTwi);
        timeout = 0;
        status = TWI_GetStatus(pTwi);
        while( !(status & TWI_SR_TXCOMP) && (++timeout<TWITIMEOUTMAX))
        {
            status = TWI_GetStatus(pTwi);
            //TRACE_ERROR("TWID status %x\n\r",TWI_GetStatus(pTwi));
        }
#if 0
        /* Read all bytes, setting STOP before the last byte*/
        while (num > 0) {

            /* Last byte ?*/
            if (num == 1) {

                TWI_Stop(pTwi);
            }

            /* Wait for byte then read and store it*/
            timeout = 0;
            while( !TWI_ByteReceived(pTwi) && (++timeout<TWITIMEOUTMAX) );
            if (timeout == TWITIMEOUTMAX) {
                TRACE_ERROR("TWID Timeout BR\n\r");
            }
            *pData++ = TWI_ReadByte(pTwi);
            num--;
        }

        /* Wait for transfer to be complete */
        timeout = 0;
        while( !TWI_TransferComplete(pTwi) && (++timeout<TWITIMEOUTMAX) );
        if (timeout == TWITIMEOUTMAX) {
            TRACE_ERROR("TWID Timeout TC\n\r");
        }
#endif
    }

    return 0;
}
Beispiel #11
0
//-----------------------------------------------------------------------------
/// Asynchronously reads data from a slave on the TWI bus. An optional
/// callback function is triggered when the transfer is complete.
/// Returns 0 if the transfer has been started; otherwise returns a TWI error
/// code.
/// \param pTwid  Pointer to a Twid instance.
/// \param address  TWI slave address.
/// \param iaddress  Optional slave internal address.
/// \param isize  Internal address size in bytes.
/// \param pData  Data buffer for storing received bytes.
/// \param num  Number of bytes to read.
/// \param pAsync  Asynchronous transfer descriptor.
//-----------------------------------------------------------------------------
unsigned char TWID_Read(
    Twid *pTwid,
    unsigned char address,
    unsigned int iaddress,
    unsigned char isize,
    unsigned char *pData,
    unsigned int num,
    Async *pAsync)
{
    AT91S_TWI *pTwi = pTwid->pTwi;
    AsyncTwi *pTransfer = (AsyncTwi *) pTwid->pTransfer;
    unsigned int timeout;

    //TRACE_DEBUG("TWID_Read()\n\r");
    SANITY_CHECK(pTwid);
    SANITY_CHECK((address & 0x80) == 0);
    SANITY_CHECK((iaddress & 0xFF000000) == 0);
    SANITY_CHECK(isize < 4);

    // Check that no transfer is already pending
    if (pTransfer) {

        TRACE_ERROR("TWID_Read: A transfer is already pending\n\r");
        return TWID_ERROR_BUSY;
    }

    // Set STOP signal if only one byte is sent
    if (num == 1) {

        TWI_Stop(pTwi);
    }

    // Asynchronous transfer
    if (pAsync) {
    
        // Update the transfer descriptor
        pTwid->pTransfer = pAsync;
        pTransfer = (AsyncTwi *) pAsync;
        pTransfer->status = ASYNC_STATUS_PENDING;
        pTransfer->pData = pData;
        pTransfer->num = num;
        pTransfer->transferred = 0;
        
        // Enable read interrupt and start the transfer
        TWI_EnableIt(pTwi, AT91C_TWI_RXRDY);
        TWI_StartRead(pTwi, address, iaddress, isize);
    }
    // Synchronous transfer
    else {

        // Start read
        TWI_StartRead(pTwi, address, iaddress, isize);

        // Read all bytes, setting STOP before the last byte
        while (num > 0) {

            // Last byte
            if (num == 1) {

                TWI_Stop(pTwi);
            }

            // Wait for byte then read and store it
            timeout = 0;
            while( !TWI_ByteReceived(pTwi) && (++timeout<TWITIMEOUTMAX) );
            if (timeout == TWITIMEOUTMAX) {
                TRACE_ERROR("TWID Timeout BR\n\r");
                return TWID_ERROR_TIMEOUT;
            }
            *pData++ = TWI_ReadByte(pTwi);
            num--;
        }

        // Wait for transfer to be complete
        timeout = 0;
        while( !TWI_TransferComplete(pTwi) && (++timeout<TWITIMEOUTMAX) );
        if (timeout == TWITIMEOUTMAX) {
            TRACE_ERROR("TWID Timeout TC\n\r");
            return TWID_ERROR_TIMEOUT;
        }
    }

    return 0;
}