This is the quick start guide for the TWI Driver, with step-by-step instructions on how to configure and use the driver for specific use cases.
The section described below can be compiled into e.g. the main application loop or any other function that might use the TWI functionality.
In our basic use case, the TWI driver is used to set up internal communication between two TWI modules on the XMEGA A1 Xplained board, since this is the most simple way to show functionality without external dependencies. TWIC is set up in master mode, and TWIF is set up in slave mode, and these are connected together on the board by placing a connection between SDA/SCL on J1 to SDA/SCL on J4.
The System Clock Management module is required to enable the clock to the TWI modules. The TWI Master driver and TWI Slave driver must also be included.
When the System Clock Management module has been included, it must be initialized:
We first create some definitions. TWI master and slave, speed, and addresses:
We create a handle to contain information about the slave module:
We create two variables, one which contains data that will be transmitted, and one which will contain the received data:
Options for the TWI module initialization procedure are given below:
The TWI slave will fire an interrupt when it has received data, and the function below will be called, which will copy the data from the driver to our recv_data buffer:
Set up the interrupt handler:
We create a packet for the data that we will send to the slave TWI:
We need to set SDA/SCL pins for the master TWI to be wired and enable pull-up:
We enable all interrupt levels:
We enable the clock to the master module, and initialize it with the options we described before:
We do the same for the slave, using the slave portion of the driver, passing through the slave_process function, its address, and set medium interrupt level:
We zero out the receive buffer in the slave handle:
And enable interrupts:
Finally, we write our packet through the master TWI module:
We wait for the slave to finish receiving: