EasyManuals Logo

Atmel SAM4 Series User Manual

Atmel SAM4 Series
26 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #23 background imageLoading...
Page #23 background image
AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE]
42274A-MCU-05/2014
23
Note
The read/write functions will enable and disable the corresponding interrupt sources.
8.4 Usage Steps
8.4.1 twim_basic_usage_code
We can send data to the target slave device. Firstly, the data package should be prepared. In one data package,
several items should be set; the target slave address, the internal address (if needed), the length of the internal
address (if needed), the data buffer to be written, and the length of the data buffer.
twi_package_t packet_tx;
packet_tx.chip = TARGET_SLAVE_ADDRESS; // The address of the TWI Slave device.
packet_tx.addr[0] = (INTERNAL_ADDRESS >> 16) & 0xFF;
packet_tx.addr[1] = (INTERNAL_ADDRESS >> 8) & 0xFF;
packet_tx.addr_length = INTERNAL_ADDRESS_LENGTH;
packet_tx.buffer = (void *) data_buf_tx;
packet_tx.length = DATA_BUF_TX_LENGTH;
Note
The TWIM driver supports 1-3 bytes of internal address.
After the data package is ready, we can call twi_master_write() to send the package to the slave address. The
callback set before will be handled in ISR.
twi_master_write(TWIM1, &packet_tx);
Note
If the function returns STATUS_OK, the package has been sent to the target slave device
successfully. Otherwise, the transmission fails.
We can receive data from the target slave device. Firstly, the data package should be prepared. In one data
package, several items should be set; the target slave address, the internal address (if needed), the length of the
internal address (if needed), the data buffer used to store received data and the length of the data to be received.
twi_package_t packet_rx;
packet_rx.chip = TARGET_SLAVE_ADDRESS;
packet_rx.addr[0] = (INTERNAL_ADDRESS >> 16) & 0xFF;
packet_rx.addr[1] = (INTERNAL_ADDRESS >> 8) & 0xFF;
packet_rx.addr_length = INTERNAL_ADDRESS_LENGTH;
packet_rx.buffer = (void *) data_buf_rx;
packet_rx.length = DATA_BUF_RX_LENGTH;
Note
The TWIM driver supports 1-3 bytes of internal address.
Once the data package is ready, we can call twi_master_read() to receive a data package, in responde, from the
slave device. The callback set before will be handled in ISR.
twi_master_read(TWIM1, &packet_rx);
Note
If the function returns STATUS_OK, the package has been received from the target slave device and
the data has been stored in the data buffer successfully. Otherwise, the transmission failed.

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Atmel SAM4 Series and is the answer not in the manual?

Atmel SAM4 Series Specifications

General IconGeneral
BrandAtmel
ModelSAM4 Series
CategoryControl Unit
LanguageEnglish