Microchip® Advanced Software Framework

twim.c File Reference

TWIM driver for SAM.

This file defines a useful set of functions for the TWIM on SAM4L devices.

Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.

#include "twim.h"
#include "sleepmgr.h"
#include "sysclk.h"

Macros

#define NCMDR_FREE_WAIT   2000
 
#define TWIM_MAX_NBYTES_PER_XFER   (TWIM_CMDR_NBYTES_Msk >> TWIM_CMDR_NBYTES_Pos)
 

Functions

static uint32_t find_twim_channel_num (Twim *twim)
 Get TWIM channel number. More...
 
status_code_t twi_master_read (Twim *twim, struct twim_package *package)
 Read multiple bytes from a TWI compatible slave device. More...
 
status_code_t twi_master_write (Twim *twim, struct twim_package *package)
 Write multiple bytes to a TWI compatible slave device. More...
 
void twim_clear_status (Twim *twim, uint32_t clear_status)
 Clear the current status of the TWIM. More...
 
void twim_default_callback (Twim *twim)
 TWIM default callback function. More...
 
void twim_disable_interrupt (Twim *twim, uint32_t interrupt_source)
 Disable the TWIM interrupts and clear their status. More...
 
void twim_enable_interrupt (Twim *twim, uint32_t interrupt_source)
 Enable the TWIM interrupts. More...
 
uint32_t twim_get_interrupt_mask (Twim *twim)
 Get the TWIM interrupt mask. More...
 
uint32_t twim_get_status (Twim *twim)
 Information about the current status of the TWIM. More...
 
void twim_pdca_transfer_prepare (Twim *twim, twi_package_t *package, bool read)
 Set TWIM for PDCA transfer. More...
 
status_code_t twim_probe (Twim *twim, uint32_t chip_addr)
 Test if a chip answers for a given TWI address. More...
 
void twim_set_callback (Twim *twim, uint32_t interrupt_source, twim_callback_t callback, uint8_t irq_level)
 Set callback for TWIM. More...
 
status_code_t twim_set_config (Twim *twim, struct twim_config *config)
 Initialize the TWIM module. More...
 
status_code_t twim_set_hsmode_speed (Twim *twim, uint32_t speed, uint32_t clk, uint8_t cycles)
 Set the TWI bus speed in conjunction with the clock frequency in high speed mode. More...
 
status_code_t twim_set_speed (Twim *twim, uint32_t speed, uint32_t clk, uint8_t cycles)
 Set the TWI bus speed in conjunction with the clock frequency. More...
 

Variables

static volatile uint8_t * p_twim_next_rx_data [NUM_TWIM_CH] = {NULL}
 
static const volatile uint8_t * p_twim_next_tx_data [NUM_TWIM_CH] = {NULL}
 
static volatile uint8_t * p_twim_rx_data [NUM_TWIM_CH] = {NULL}
 
static const volatile uint8_t * p_twim_tx_data [NUM_TWIM_CH] = {NULL}
 
static volatile
twim_transfer_status_t 
transfer_status [NUM_TWIM_CH]
 
twim_callback_t twim_callback_pointer [NUM_TWIM_CH]
 TWIM callback function pointer array. More...
 
static volatile uint32_t twim_it_mask [NUM_TWIM_CH]
 
static volatile bool twim_next_cmd_xfer_valid [NUM_TWIM_CH] = {false}
 
static volatile uint32_t twim_next_rx_nb_bytes [NUM_TWIM_CH] = {0}
 
static volatile uint32_t twim_next_tx_nb_bytes [NUM_TWIM_CH] = {0}
 
static volatile uint32_t twim_rx_nb_bytes [NUM_TWIM_CH] = {0}
 
static volatile uint32_t twim_tx_nb_bytes [NUM_TWIM_CH] = {0}
 

#define NCMDR_FREE_WAIT   2000

Referenced by twim_default_callback().

#define TWIM_MAX_NBYTES_PER_XFER   (TWIM_CMDR_NBYTES_Msk >> TWIM_CMDR_NBYTES_Pos)

Max value of NBYTES per transfer by hardware

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

static uint32_t find_twim_channel_num ( Twim *  twim)
static

Get TWIM channel number.

Parameters
twimBase address of the TWIM
Returns
TWIM channel number

Referenced by twi_master_read(), twi_master_write(), twim_default_callback(), and twim_set_callback().

volatile uint8_t* p_twim_next_rx_data[NUM_TWIM_CH] = {NULL}
static

Pointer to the application TWI receive buffer for next transfer.

Referenced by twi_master_read(), and twim_default_callback().

const volatile uint8_t* p_twim_next_tx_data[NUM_TWIM_CH] = {NULL}
static

Pointer to the application TWI transmit buffer for next transfer.

Referenced by twi_master_write(), and twim_default_callback().

volatile uint8_t* p_twim_rx_data[NUM_TWIM_CH] = {NULL}
static

Pointer to the application TWI receive buffer.

Referenced by twi_master_read(), and twim_default_callback().

const volatile uint8_t* p_twim_tx_data[NUM_TWIM_CH] = {NULL}
static

Pointer to the application TWI transmit buffer.

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

volatile twim_transfer_status_t transfer_status[NUM_TWIM_CH]
static

Status of the bus transfer

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

twim_callback_t twim_callback_pointer[NUM_TWIM_CH]

TWIM callback function pointer array.

Referenced by twim_set_callback().

volatile uint32_t twim_it_mask[NUM_TWIM_CH]
static
volatile bool twim_next_cmd_xfer_valid[NUM_TWIM_CH] = {false}
static

If next transfer command valid

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

volatile uint32_t twim_next_rx_nb_bytes[NUM_TWIM_CH] = {0}
static

Remaining number of bytes to receive for next transfer.

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

volatile uint32_t twim_next_tx_nb_bytes[NUM_TWIM_CH] = {0}
static

Remaining number of bytes to transmit for next transfer.

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().

volatile uint32_t twim_rx_nb_bytes[NUM_TWIM_CH] = {0}
static

Remaining number of bytes to receive per command.

Referenced by twi_master_read(), and twim_default_callback().

volatile uint32_t twim_tx_nb_bytes[NUM_TWIM_CH] = {0}
static

Remaining number of bytes to transmit per command.

Referenced by twi_master_read(), twi_master_write(), and twim_default_callback().