87 #define F_CPU 32000000 90 #include <util/delay.h> 106 twi->
interface->MASTER.CTRLA = intLevel | TWI_MASTER_RIEN_bm | TWI_MASTER_WIEN_bm | TWI_MASTER_ENABLE_bm;
107 twi->
interface->MASTER.BAUD = baudRateRegisterSetting;
108 twi->
interface->MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
125 TWI_MASTER_BUSSTATE_t twi_status;
126 twi_status = (TWI_MASTER_BUSSTATE_t) (twi->
interface->MASTER.STATUS & TWI_MASTER_BUSSTATE_gm);
209 if ( bytesToRead > TWIM_READ_BUFFER_SIZE )
218 twi->
status = TWIM_STATUS_BUSY;
219 twi->
result = TWIM_RESULT_UNKNOWN;
224 for ( uint8_t bufferIndex = 0; bufferIndex < bytesToWrite; bufferIndex++ )
226 twi->
writeData[bufferIndex] = writeData[bufferIndex];
239 uint8_t writeAddress = twi->
address & ~0x01;
240 twi->
interface->MASTER.ADDR = writeAddress;
248 uint8_t readAddress = twi->
address | 0x01;
249 twi->
interface->MASTER.ADDR = readAddress;
270 uint8_t currentStatus = twi->
interface->MASTER.STATUS;
273 if ( (currentStatus & TWI_MASTER_ARBLOST_bm) || (currentStatus & TWI_MASTER_BUSERR_bm) )
280 else if ( currentStatus & TWI_MASTER_WIF_bm )
286 else if ( currentStatus & TWI_MASTER_RIF_bm )
294 printf (
"\n\rUnexpected State: %x \n\r", currentStatus );
308 uint8_t currentStatus = twi->
interface->MASTER.STATUS;
311 if ( currentStatus & TWI_MASTER_BUSERR_bm )
313 twi->
result = TWIM_RESULT_BUS_ERROR;
318 twi->
result = TWIM_RESULT_ARBITRATION_LOST;
322 twi->
interface->MASTER.STATUS = currentStatus | TWI_MASTER_ARBLOST_bm;
341 if ( twi->
interface->MASTER.STATUS & TWI_MASTER_RXACK_bm )
343 twi->
interface->MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
344 twi->
result = TWIM_RESULT_NACK_RECEIVED;
361 uint8_t readAddress = twi->
address | 0x01;
362 twi->
interface->MASTER.ADDR = readAddress;
368 twi->
interface->MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
383 if ( twi->
bytesRead < TWIM_READ_BUFFER_SIZE )
385 uint8_t data = twi->
interface->MASTER.DATA;
393 twi->
interface->MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
403 twi->
interface->MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
409 twi->
interface->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
register8_t writeData[TWIM_WRITE_BUFFER_SIZE]
void TWI_MasterInit(TWI_Master_t *twi, TWI_t *module, TWI_MASTER_INTLVL_t intLevel, uint8_t baudRateRegisterSetting)
Initialise the TWI module.
register8_t readData[TWIM_READ_BUFFER_SIZE]
TWI_MASTER_BUSSTATE_t TWI_MasterState(TWI_Master_t *twi)
Returns the TWI bus state.
void TWI_MasterWriteHandler(TWI_Master_t *twi)
TWI master write interrupt handler.
void TWI_MasterArbitrationLostBusErrorHandler(TWI_Master_t *twi)
TWI master arbitration lost and bus error interrupt handler.
XMEGA TWI master driver header file.
bool TWI_MasterWriteRead(TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite, uint8_t bytesToRead)
TWI write and/or read transaction.
void TWI_MasterReadHandler(TWI_Master_t *twi)
TWI master read interrupt handler.
bool TWI_MasterReady(TWI_Master_t *twi)
Returns true if transaction is ready.
void TWI_MasterInterruptHandler(TWI_Master_t *twi)
Common TWI master interrupt service routine.
bool TWI_MasterWrite(TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite)
TWI write transaction.
void TWI_MasterTransactionFinished(TWI_Master_t *twi, uint8_t result)
TWI transaction finished handler.
TWI master driver struct.
bool TWI_MasterRead(TWI_Master_t *twi, uint8_t address, uint8_t bytesToRead)
TWI read transaction.
#define TWIM_WRITE_BUFFER_SIZE
#define TWIM_STATUS_READY