AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE]
42274A-MCU-05/2014
22
8. Quick Start Guide
This is the quick start guide for the SAM4 TWI Master Mode Driver, with step-by-step instructions on how to
configure and use the driver for a specific use case.The code examples can be copied into the main application
loop or any other function that will need to control the TWIM module.
8.1 Use Cases
● TWIM Basic Usage
8.2 TWIM Basic Usage
This use case will demonstrate how to initialize the TWIM module.
8.3 Setup Steps
8.3.1 Prerequisites
This module requires the following service register
● clk_group
8.3.2 Basic Setup Workflow
Setup TWIM options, including TWIM clock frequency, the desired TWI bus speed, the target chip slave address
(optional) and being in SMBus mode or not. See twim_config for more information.
struct twim_config twim_conf;
Get the system clock frequency, in Hertz.
twim_conf.twim_clk = sysclk_get_cpu_hz();
Select the desired TWI speed. These are defined by a number of macros e.g. TWI_STD_MODE_SPEED,
TWI_FAST_MODE_SPEED for the complete list see the Macro definitions section.
twim_conf.speed = DESIRED_TWI_BUS_SPEED;
twim_conf.smbus = false; // Not SMBus mode
Set other settings to defaults.
twim_conf.hsmode_speed = 0;
twim_conf.data_setup_cycles = 0;
twim_conf.hsmode_data_setup_cycles = 0;
Now make the configuration active:
twim_set_config(TWIM0, &twim_conf);
Note
The TWIM driver supports I
2
C standard speed, fast speed, fast speed plus, and high speed.
The default callback function must be set before calling read/write functions.
twim_set_callback(TWIM0, 0, twim_default_callback, 1);