Description
Source
Call Graph
Start Line: 267
unsigned char TWID_Write(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_Write()\n\r");
//TRACE_DEBUG("0x%X\n\r", pData[0]);
SANITY_CHECK(pTwi);
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("TWI_Write: 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 = 1;
// Enable write interrupt and start the transfer
TWI_StartWrite(pTwi, address, iaddress, isize, *pData);
TWI_EnableIt(pTwi, AT91C_TWI_TXRDY);
}
// Synchronous transfer
else {
// Start write
TWI_StartWrite(pTwi, address, iaddress, isize, *pData++);
num--;
// Send all bytes
while (num > 0) {
// Wait before sending the next byte
timeout = 0;
while( !TWI_ByteSent(pTwi) && (++timeout<TWITIMEOUTMAX) );
if (timeout == TWITIMEOUTMAX) {
TRACE_ERROR("TWID Timeout BS\n\r");
}
TWI_WriteByte(pTwi, *pData++);
num--;
}
// Wait for actual end of transfer
timeout = 0;
#ifdef TWI_V3XX
// Send a STOP condition
TWI_SendSTOPCondition(pTwi);
#endif
while( !TWI_TransferComplete(pTwi) && (++timeout<TWITIMEOUTMAX) );
if (timeout == TWITIMEOUTMAX) {
TRACE_ERROR("TWID Timeout TC2\n\r");
}
}
return 0;
}