Microchip® Advanced Software Framework

Quickstart guide for SAM TWIS driver

This is the quickstart guide for the SAM TWIS driver, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

The basic use case demonstrate how to initialize the TWIS module and configure it.

Prerequisites

  1. System Clock Management (Sysclock)

Setup steps

Example code

Enable the following macro in the conf_clock.h:

#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_DFLL
#define CONFIG_DFLL0_SOURCE GENCLK_SRC_OSC0

Add the following code in the application C-file:

Workflow

  1. Set system clock source as DFLL:
    • #define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_DFLL
  2. Set DFLL source as OSC0:
  3. Initialize the system clock.

Usage steps

Example code

Add to, e.g., main loop in application C-file:

struct twis_config config;
config.chip = SLAVE_ADDRESS;
twis_callback_t slave_callbacks;
slave_callbacks.rx = TWIS_RX_HANDLER_POINTER;
slave_callbacks.tx = TWIS_TX_HANDLER_POINTER;
slave_callbacks.stop = TWIS_STOP_HANDLER_POINTER;
slave_callbacks.error = TWIS_ERROR_HANDLER_POINTER;

Workflow

  1. Initialize the TWIS module with the given self slave address SLAVE_ADDRESS:
  2. Set up the callback functions to handle reception, transmission, STOP condition and errors. In the end, enable the slave address match interrupt:
    twis_callback_t slave_callbacks;
    slave_callbacks.rx = TWIS_RX_HANDLER_POINTER;
    slave_callbacks.tx = TWIS_TX_HANDLER_POINTER;
    slave_callbacks.stop = TWIS_STOP_HANDLER_POINTER;
    slave_callbacks.error = TWIS_ERROR_HANDLER_POINTER;
  3. Enable TWIS module: