Atmel Software Framework

twim.c File Reference

XMEGA TWI master source file.

Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.

#include "twim.h"

Functions

status_code_t twi_master_init (TWI_t *twi, const twi_options_t *opt)
 Initialize the twi master module. More...
 
status_code_t twi_master_transfer (TWI_t *twi, const twi_package_t *package, bool read)
 Perform a TWI master write or read transfer. More...
 
static status_code_t twim_acquire (bool no_wait)
 Get exclusive access to global TWI resources. More...
 
static bool twim_idle (const TWI_t *twi)
 Test for an idle bus state. More...
 
static void twim_interrupt_handler (void)
 TWI Master Interrupt Vectors. More...
 
static void twim_read_handler (void)
 TWI master read interrupt handler. More...
 
static status_code_t twim_release (void)
 Release exclusive access to global TWI resources. More...
 
static void twim_write_handler (void)
 TWI master write interrupt handler. More...
 

Variables

struct {
   int   addr_count
 
   TWI_t *   bus
 
   unsigned int   data_count
 
   bool   locked
 
   twi_package_t *   pkg
 
   bool   read
 
   volatile status_code_t   status
 
transfer
 

static status_code_t twim_acquire ( bool  no_wait)
inlinestatic

Get exclusive access to global TWI resources.

Wait to acquire bus hardware interface and ISR variables.

Parameters
no_waitSet true to return instead of doing busy-wait (spin-lock).
Returns
STATUS_OK if the bus is acquired, else ERR_BUSY.

References cpu_irq_restore(), cpu_irq_save(), ERR_BUSY, OPERATION_IN_PROGRESS, STATUS_OK, and transfer.

Referenced by twi_master_transfer().

static bool twim_idle ( const TWI_t *  twi)
inlinestatic

Test for an idle bus state.

Software can determine the TWI master bus state (unknown, idle, owner, or busy) by reading the bus master status register:

    TWI_MASTER_BUSSTATE_UNKNOWN_gc Bus state is unknown.
    TWI_MASTER_BUSSTATE_IDLE_gc    Bus state is idle.
    TWI_MASTER_BUSSTATE_OWNER_gc   Bus state is owned by the master.
    TWI_MASTER_BUSSTATE_BUSY_gc    Bus state is busy.
Parameters
twiBase address of the TWI (i.e. &TWI_t).
Return values
trueThe bus is currently idle.
falseThe bus is currently busy.

Referenced by twim_release().

static void twim_interrupt_handler ( void  )
static

TWI Master Interrupt Vectors.

Common TWI master interrupt service routine.

The TWI master interrupt request entry points are conditionally compiled for the TWI interfaces supported by the XMEGA MCU variant. All of these entry points call a common service function, twim_interrupt_handler(), to handle bus events. This handler uses the bus interface and message parameters specified in the global transfer structure.

Check current status and calls the appropriate handler.

References ERR_BUSY, ERR_IO_ERROR, ERR_PROTOCOL, transfer, twim_read_handler(), and twim_write_handler().

static void twim_read_handler ( void  )
inlinestatic

TWI master read interrupt handler.

This is the master read interrupt handler that takes care of reading bytes from the TWI slave.

References twi_package_t::buffer, bus, data, ERR_NO_MEMORY, twi_package_t::length, pkg, STATUS_OK, and transfer.

Referenced by twim_interrupt_handler().

static status_code_t twim_release ( void  )
inlinestatic

Release exclusive access to global TWI resources.

Release bus hardware interface and ISR variables previously locked by a call to twim_acquire(). This function will busy-wait for pending driver operations to complete.

Returns
status_code_t
  • STATUS_OK if the transfer completes
  • ERR_BUSY to indicate an unavailable bus
  • ERR_IO_ERROR to indicate a bus transaction error
  • ERR_NO_MEMORY to indicate buffer errors
  • ERR_PROTOCOL to indicate an unexpected bus state

References barrier, OPERATION_IN_PROGRESS, status, transfer, and twim_idle().

Referenced by twi_master_transfer().

static void twim_write_handler ( void  )
inlinestatic

TWI master write interrupt handler.

Handles TWI transactions (master write) and responses to (N)ACK.

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, bus, data, twi_package_t::length, pkg, STATUS_OK, and transfer.

Referenced by twim_interrupt_handler().

int addr_count
TWI_t* bus
unsigned int data_count

Referenced by main().

bool locked
bool read

Referenced by sensor_read(), and twi_master_transfer().

volatile status_code_t status

Referenced by ak8975_ioctl(), appUartSendAck(), at25dfx_read_status(), at45dbx_mem_check(), at45dbx_wait_ready(), bma020_init(), bma150_event(), bma150_init(), bma180_init(), bma220_event(), bma220_init(), bma222_event(), bma222_init(), bma250_event(), bma250_init(), bmp085_init(), bmp180_init(), bmp_ioctl(), dma_interrupt(), dump_trx_register_values(), hmc5883l_init(), hmc5883l_ioctl(), hmc5883l_selftest(), imu3000_default_init(), imu3000_event(), imu3000_init(), itg3200_default_init(), itg3200_event(), itg3200_init(), kxtf9_event(), kxtf9_init(), mac_gen_mcps_data_conf(), mac_gen_mlme_gts_conf(), mac_process_tal_tx_status(), mac_reset(), mem_read_capacity(), mem_test_unit_ready(), memory_2_ram(), memory_2_usb(), mlme_get(), mlme_reset_request(), mlme_set(), mlme_set_request(), mxt_init(), mxt_init_device(), mxt_probe_device(), mxt_process_messages(), mxt_read_info_block(), mxt_read_touch_event(), nwkTxConfirm(), pal_timer_get_id(), pal_timer_start(), pal_timer_stop(), parse_mpdu(), peer_search_initiator_tx_done_cb(), per_mode_receptor_tx_done_cb(), PHY_TaskHandler(), ram_2_memory(), run_buffer_access_test(), run_byte_access_test(), run_memory_check_test(), run_tal_pib_set_test(), run_tal_reset_test(), run_tfa_pib_set_test(), send_reset_conf(), sensor_device_id(), sensor_get_sample_interval(), sensor_get_threshold(), sensor_irq_connect(), set_paramter_on_recptor_node(), set_tal_pib_internal(), sfh5712_init(), sfh5712_ioctl(), sfh7770_event(), sfh7770_init(), sfh7770_ioctl(), sfh7770_set_current(), sfh7770_set_rate(), sio2host_tx(), sio2ncp_tx(), stream_mem_to_mem(), tal_tx_frame_done_cb(), twi_master_transfer(), twim_release(), tx_done_handling(), udi_msc_process_trans(), udi_msc_trans_ack(), usb_2_memory(), usr_cont_pulse_tx_confirm(), usr_cont_wave_tx_confirm(), usr_ed_scan_start_confirm(), usr_get_current_config_confirm(), usr_identify_board_confirm(), usr_identify_peer_node_confirm(), usr_mcps_data_conf(), usr_mlme_gts_conf(), usr_mlme_reset_conf(), usr_mlme_scan_conf(), usr_mlme_set_conf(), usr_peer_disconnect_confirm(), usr_per_test_end_indication(), usr_per_test_start_confirm(), usr_perf_get_confirm(), usr_perf_set_confirm(), usr_perf_start_confirm(), usr_pkt_stream_confirm(), usr_range_test_start_confirm(), usr_range_test_stop_confirm(), usr_register_dump_confirm(), usr_register_read_confirm(), usr_register_write_confirm(), usr_rx_on_confirm(), usr_sensor_data_get_confirm(), usr_set_default_config_confirm(), and wait_for_dma_transfer().