Description
Source
Call Graph
Start Line: 173
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");
}
*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;
}