Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TWIS - Two-Wire Slave Interface

See Quickstart guide for SAM TWIS driver.

Driver for the TWIS (Two-Wire Slave Interface). This driver provides access to the main features of the TWIS controller. The TWIS interconnects components on a unique two-wire bus. The TWIS is programmable as a slave with sequential or single-byte access. High speed mode capability is supported.

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the TWIS - Two-Wire Slave Interface.
 

Data Structures

struct  twis_callback
 
struct  twis_config
 Input parameters when initializing the TWIS module mode. More...
 
struct  twis_dev_inst
 TWI slave driver software instance structure. More...
 

Macros

#define TWIS_WAIT_TIMEOUT   1000
 

Typedefs

typedef struct twis_callback twis_callback_t
 
typedef enum twis_interrupt_source twis_interrupt_source_t
 TWIS interrupt source type. More...
 

Enumerations

enum  twis_interrupt_source {
  TWIS_INTERRUPT_RX_BUFFER_READY = TWIS_IER_RXRDY,
  TWIS_INTERRUPT_TX_BUFFER_READY = TWIS_IER_TXRDY,
  TWIS_INTERRUPT_TRANS_COMP = TWIS_IER_TCOMP,
  TWIS_INTERRUPT_UNDER_RUN = TWIS_IER_URUN,
  TWIS_INTERRUPT_OVER_RUN = TWIS_IER_ORUN,
  TWIS_INTERRUPT_NAK_RECEIVED = TWIS_IER_NAK,
  TWIS_INTERRUPT_SMBUS_TIMEOUT = TWIS_IER_SMBTOUT,
  TWIS_INTERRUPT_SMBUS_PEC_ERROR = TWIS_IER_SMBPECERR,
  TWIS_INTERRUPT_BUS_ERROR = TWIS_IER_BUSERR,
  TWIS_INTERRUPT_SLAVEADR_MATCH = TWIS_IER_SAM,
  TWIS_INTERRUPT_GENCALL_MATCH = TWIS_IER_GCM,
  TWIS_INTERRUPT_SMBUS_HHADR_MATCH = TWIS_IER_SMBHHM,
  TWIS_INTERRUPT_SMBUS_DEFADR_MATCH = TWIS_IER_SMBDAM,
  TWIS_INTERRUPT_STOP_RECEIVED = TWIS_IER_STO,
  TWIS_INTERRUPT_RESTART_RECEIVED = TWIS_IER_REP,
  TWIS_INTERRUPT_BYTE_TRANS_FINISHED = TWIS_IER_BTF,
  TWIS_INTERRUPT_ERRORS,
  TWIS_INTERRUPT_ALL = ~0UL
}
 TWIS interrupt source type. More...
 

Functions

static void twis_clear_status (struct twis_dev_inst *const dev_inst, uint32_t clear_status)
 Clear the current status of the TWIS. More...
 
void twis_disable (struct twis_dev_inst *const dev_inst)
 Disable TWIS Module. More...
 
static void twis_disable_interrupt (struct twis_dev_inst *const dev_inst, twis_interrupt_source_t interrupt_source)
 Disable the TWIS interrupts. More...
 
void twis_enable (struct twis_dev_inst *const dev_inst)
 Enable TWIS Module. More...
 
static void twis_enable_interrupt (struct twis_dev_inst *const dev_inst, twis_interrupt_source_t interrupt_source)
 Enable the TWIS interrupts. More...
 
void twis_get_config_defaults (struct twis_config *const cfg)
 Get the TWIS master default configurations. More...
 
static uint32_t twis_get_interrupt_mask (struct twis_dev_inst *const dev_inst)
 Get the TWIS interrupt mask. More...
 
static uint8_t twis_get_smbus_pec (struct twis_dev_inst *const dev_inst)
 Get the calculated PEC value. More...
 
static uint8_t twis_get_smbus_transfer_nb (struct twis_dev_inst *const dev_inst)
 Get the progress of the transfer in SMBus mode. More...
 
static uint32_t twis_get_status (struct twis_dev_inst *const dev_inst)
 Information about the current status of the TWIS. More...
 
enum status_code twis_init (struct twis_dev_inst *const dev_inst, Twis *const twis, struct twis_config *config)
 Initialize the TWI Slave Module. More...
 
static enum status_code twis_read (struct twis_dev_inst *const dev_inst, uint8_t *data)
 Get the last byte data received from TWI bus. More...
 
static void twis_send_data_ack (struct twis_dev_inst *const dev_inst)
 Enable ACK transfer in Slave Receiver Mode. More...
 
static void twis_send_data_nack (struct twis_dev_inst *const dev_inst)
 Enable NACK transfer in Slave Receiver Mode. More...
 
void twis_set_callback (struct twis_dev_inst *const dev_inst, twis_interrupt_source_t source, twis_callback_t callback, uint8_t irq_level)
 Set callback for TWIS. More...
 
static void twis_set_smbus_transfer_nb (struct twis_dev_inst *const dev_inst, uint8_t nb, bool increment)
 Set the total number of data bytes in the transmission. More...
 
static enum status_code twis_write (struct twis_dev_inst *const dev_inst, uint8_t byte)
 Write one byte data to TWI bus. More...
 

#define TWIS_WAIT_TIMEOUT   1000

Referenced by twis_read(), and twis_write().

TWIS interrupt source type.

TWIS interrupt source type.

Enumerator
TWIS_INTERRUPT_RX_BUFFER_READY 
TWIS_INTERRUPT_TX_BUFFER_READY 
TWIS_INTERRUPT_TRANS_COMP 
TWIS_INTERRUPT_UNDER_RUN 
TWIS_INTERRUPT_OVER_RUN 
TWIS_INTERRUPT_NAK_RECEIVED 
TWIS_INTERRUPT_SMBUS_TIMEOUT 
TWIS_INTERRUPT_SMBUS_PEC_ERROR 
TWIS_INTERRUPT_BUS_ERROR 
TWIS_INTERRUPT_SLAVEADR_MATCH 
TWIS_INTERRUPT_GENCALL_MATCH 
TWIS_INTERRUPT_SMBUS_HHADR_MATCH 
TWIS_INTERRUPT_SMBUS_DEFADR_MATCH 
TWIS_INTERRUPT_STOP_RECEIVED 
TWIS_INTERRUPT_RESTART_RECEIVED 
TWIS_INTERRUPT_BYTE_TRANS_FINISHED 
TWIS_INTERRUPT_ERRORS 
TWIS_INTERRUPT_ALL 

static void twis_clear_status ( struct twis_dev_inst *const  dev_inst,
uint32_t  clear_status 
)
inlinestatic

Clear the current status of the TWIS.

Parameters
dev_instDevice structure pointer
clear_statusThe TWIS status to be clear

References Assert, and twis_dev_inst::hw_dev.

Referenced by twis_interrupt_handler().

void twis_disable ( struct twis_dev_inst *const  dev_inst)

Disable TWIS Module.

Parameters
dev_instDevice structure pointer

References Assert, twis_dev_inst::hw_dev, SLEEPMGR_SLEEP_1, and sleepmgr_unlock_mode().

static void twis_disable_interrupt ( struct twis_dev_inst *const  dev_inst,
twis_interrupt_source_t  interrupt_source 
)
inlinestatic

Disable the TWIS interrupts.

Parameters
dev_instDevice structure pointer.
interrupt_sourceThe TWIS interrupt to be disabled

References Assert, and twis_dev_inst::hw_dev.

Referenced by twis_interrupt_handler().

void twis_enable ( struct twis_dev_inst *const  dev_inst)

Enable TWIS Module.

Parameters
dev_instDevice structure pointer

References Assert, twis_dev_inst::hw_dev, sleepmgr_lock_mode(), and SLEEPMGR_SLEEP_1.

Referenced by main().

static void twis_enable_interrupt ( struct twis_dev_inst *const  dev_inst,
twis_interrupt_source_t  interrupt_source 
)
inlinestatic

Enable the TWIS interrupts.

Parameters
dev_instDevice structure pointer
interrupt_sourceThe TWIS interrupt source to be enabled

References Assert, and twis_dev_inst::hw_dev.

Referenced by twis_interrupt_handler(), and twis_set_callback().

void twis_get_config_defaults ( struct twis_config *const  cfg)

Get the TWIS master default configurations.

Use to initialize the configuration structure to known default values. This function should be called at the start of any TWIS initiation.

The default configuration is as follows:

  • 7-bit addressing
  • Self address is 0x50.
  • Normal mode
  • Do not stretch clock on data byte reception
  • Do not stretch clock on address match
  • Stretch clock if RHR is full or THR is empty
  • Do not acknowledge the general call address
  • Acknowledge the specified slave address
  • Disable packet error checking
  • Do not acknowledge the SMBus host header
  • Do not acknowledge the SMBus default address
  • 0 data setup cycles in F/S mode and high speed mode
  • Zero-initialization for slew rate setting in F/S mode and high speed mode
  • Clock Prescaler is 0
  • 0 SMBus TIMEOUT cycle
  • 0 SMBus Low:Sext cycle
Parameters
cfgPointer to configuration structure to be initiated.

References twis_config::ack_general_call, twis_config::ack_slave_addr, twis_config::ack_smbus_default_addr, twis_config::ack_smbus_host_header, Assert, twis_config::chip, twis_config::enable_pec, twis_config::exp, twis_config::fs_dadrivel, twis_config::fs_daslew, twis_config::fs_filter, twis_config::hddat, twis_config::hs_dadrivel, twis_config::hs_daslew, twis_config::hs_filter, twis_config::smbus, twis_config::stretch_clk_addr, twis_config::stretch_clk_data, twis_config::stretch_clk_hr, twis_config::sudat, twis_config::ten_bit, twis_config::tlows, and twis_config::ttouts.

Referenced by main().

static uint32_t twis_get_interrupt_mask ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Get the TWIS interrupt mask.

Parameters
dev_instDevice structure pointer.
Return values
TWISinterrupt mask

References Assert, and twis_dev_inst::hw_dev.

Referenced by twis_interrupt_handler().

static uint8_t twis_get_smbus_pec ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Get the calculated PEC value.

Only for SMBus mode.

Parameters
dev_instDevice structure pointer.
Return values
CalculatedPEC value

References Assert, and twis_dev_inst::hw_dev.

static uint8_t twis_get_smbus_transfer_nb ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Get the progress of the transfer in SMBus mode.

Parameters
dev_instDevice structure pointer.
Return values
Theleft number of data bytes in the transmission.

References Assert, and twis_dev_inst::hw_dev.

static uint32_t twis_get_status ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Information about the current status of the TWIS.

Parameters
dev_instDevice structure pointer.

References Assert, and twis_dev_inst::hw_dev.

Referenced by twis_interrupt_handler().

enum status_code twis_init ( struct twis_dev_inst *const  dev_inst,
Twis *const  twis,
struct twis_config config 
)

Initialize the TWI Slave Module.

Parameters
dev_instDevice structure pointer
twisBase address of the TWIS
configConfiguration for the TWIS
Returns
Status of module initialization.
Return values
STATUS_OKThe data is written correctly.
STATUS_ERR_DENIEDInitialization failed due to the module was enable before.
STATUS_ERR_BUSYInitialization failed due to the module is busy with transfer.

References Assert, dev_inst, twis_dev_inst::hw_dev, STATUS_ERR_BUSY, STATUS_ERR_DENIED, STATUS_OK, sysclk_enable_peripheral_clock(), twis_find_ch_num(), and twis_set_config().

Referenced by main().

static enum status_code twis_read ( struct twis_dev_inst *const  dev_inst,
uint8_t *  data 
)
inlinestatic

Get the last byte data received from TWI bus.

Parameters
dev_instDevice structure pointer.
*dataThe read data.
Returns
Status of write operation.
Return values
STATUS_OKThe data is read correctly.
STATUS_ERR_TIMEOUTThe read operation aborted due to timeout.

References Assert, twis_dev_inst::hw_dev, STATUS_ERR_TIMEOUT, STATUS_OK, and TWIS_WAIT_TIMEOUT.

static void twis_send_data_ack ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Enable ACK transfer in Slave Receiver Mode.

Parameters
dev_instDevice structure pointer.

References Assert, and twis_dev_inst::hw_dev.

static void twis_send_data_nack ( struct twis_dev_inst *const  dev_inst)
inlinestatic

Enable NACK transfer in Slave Receiver Mode.

Parameters
dev_instDevice structure pointer.

References Assert, and twis_dev_inst::hw_dev.

void twis_set_callback ( struct twis_dev_inst *const  dev_inst,
twis_interrupt_source_t  source,
twis_callback_t  callback,
uint8_t  irq_level 
)

Set callback for TWIS.

Note
Slave address match interrupt is enabled in default so that TWIS ISR can work appropriately.
Parameters
dev_instDevice structure pointer
sourceInterrupt source
callbackCallback function pointer
irq_levelInterrupt level

References Assert, callback, twis_dev_inst::hw_dev, irq_register_handler, twis_enable_interrupt(), twis_find_ch_num(), and TWIS_INTERRUPT_SLAVEADR_MATCH.

Referenced by main().

static void twis_set_smbus_transfer_nb ( struct twis_dev_inst *const  dev_inst,
uint8_t  nb,
bool  increment 
)
inlinestatic

Set the total number of data bytes in the transmission.

Only for SMBus mode.

Parameters
dev_instDevice structure pointer
nbTotal number of data bytes in the transmission
incrementCount up per byte transferred if true, otherwise count down

References Assert, and twis_dev_inst::hw_dev.

static enum status_code twis_write ( struct twis_dev_inst *const  dev_inst,
uint8_t  byte 
)
inlinestatic

Write one byte data to TWI bus.

Parameters
dev_instDevice structure pointer.
byteThe byte data to write.
Returns
Status of write operation.
Return values
STATUS_OKThe data is written correctly.
STATUS_ERR_TIMEOUTThe write operation aborted due to timeout.

References Assert, twis_dev_inst::hw_dev, STATUS_ERR_TIMEOUT, STATUS_OK, and TWIS_WAIT_TIMEOUT.