Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Asfdoc_sam0_sercom_i2c_group

Data Structures

struct  i2c_master_config
 Configuration structure for the I2C Master device. More...
 
struct  i2c_master_module
 SERCOM I2C Master driver software device instance structure. More...
 
struct  i2c_master_packet
 I2C master packet for read/write. More...
 

Enumerations

enum  i2c_master_baud_rate {
  I2C_MASTER_BAUD_RATE_100KHZ = 100,
  I2C_MASTER_BAUD_RATE_400KHZ = 400
}
 I2C frequencies. More...
 
enum  i2c_master_inactive_timeout {
  I2C_MASTER_INACTIVE_TIMEOUT_DISABLED = SERCOM_I2CM_CTRLA_INACTOUT(0),
  I2C_MASTER_INACTIVE_TIMEOUT_55US = SERCOM_I2CM_CTRLA_INACTOUT(1),
  I2C_MASTER_INACTIVE_TIMEOUT_105US = SERCOM_I2CM_CTRLA_INACTOUT(2),
  I2C_MASTER_INACTIVE_TIMEOUT_205US = SERCOM_I2CM_CTRLA_INACTOUT(3)
}
 Values for inactive bus time-out. More...
 
enum  i2c_master_interrupt_flag {
  I2C_MASTER_INTERRUPT_WRITE = 0,
  I2C_MASTER_INTERRUPT_READ = 1
}
 Interrupt flags. More...
 
enum  i2c_master_start_hold_time {
  I2C_MASTER_START_HOLD_TIME_DISABLED = SERCOM_I2CM_CTRLA_SDAHOLD(0),
  I2C_MASTER_START_HOLD_TIME_50NS_100NS = SERCOM_I2CM_CTRLA_SDAHOLD(1),
  I2C_MASTER_START_HOLD_TIME_300NS_600NS = SERCOM_I2CM_CTRLA_SDAHOLD(2),
  I2C_MASTER_START_HOLD_TIME_400NS_800NS = SERCOM_I2CM_CTRLA_SDAHOLD(3)
}
 Values for hold time after start bit. More...
 

Lock/Unlock

static enum status_code i2c_master_lock (struct i2c_master_module *const module)
 Attempt to get lock on driver instance. More...
 
static void i2c_master_unlock (struct i2c_master_module *const module)
 Unlock driver instance. More...
 

Configuration and Initialization

static bool i2c_master_is_syncing (const struct i2c_master_module *const module)
 Returns the synchronization status of the module. More...
 
static void i2c_master_get_config_defaults (struct i2c_master_config *const config)
 Gets the I2C master default configurations. More...
 
enum status_code i2c_master_init (struct i2c_master_module *const module, Sercom *const hw, const struct i2c_master_config *const config)
 Initializes the requested I2C hardware module. More...
 
static void i2c_master_enable (const struct i2c_master_module *const module)
 Enables the I2C module. More...
 
static void i2c_master_disable (const struct i2c_master_module *const module)
 Disable the I2C module. More...
 
void i2c_master_reset (struct i2c_master_module *const module)
 Resets the hardware module. More...
 

Read and Write

enum status_code i2c_master_read_packet_wait (struct i2c_master_module *const module, struct i2c_master_packet *const packet)
 Reads data packet from slave. More...
 
enum status_code i2c_master_read_packet_wait_no_stop (struct i2c_master_module *const module, struct i2c_master_packet *const packet)
 Reads data packet from slave without sending a stop condition when done. More...
 
enum status_code i2c_master_write_packet_wait (struct i2c_master_module *const module, struct i2c_master_packet *const packet)
 Writes data packet to slave. More...
 
enum status_code i2c_master_write_packet_wait_no_stop (struct i2c_master_module *const module, struct i2c_master_packet *const packet)
 Writes data packet to slave without sending a stop condition when done. More...
 
void i2c_master_send_stop (struct i2c_master_module *const module)
 Sends stop condition on bus. More...
 
void i2c_master_send_nack (struct i2c_master_module *const module)
 Sends nack signal on bus. More...
 
enum status_code i2c_master_read_byte (struct i2c_master_module *const module, uint8_t *byte)
 Reads one byte data from slave. More...
 
enum status_code i2c_master_write_byte (struct i2c_master_module *const module, uint8_t byte)
 Write one byte data to slave. More...
 
enum status_code i2c_master_read_packet_wait_no_nack (struct i2c_master_module *const module, struct i2c_master_packet *const packet)
 Reads data packet from slave without sending a nack signal and a stop condition when done. More...
 

I2C frequencies.

Values for I2C speeds supported by the module. The driver will also support setting any other value, in which case set the value in the i2c_master_config at desired value divided by 1000.

Example: If 10KHz operation is required, give baud_rate in the configuration structure the value 10.

Enumerator
I2C_MASTER_BAUD_RATE_100KHZ 

Baud rate at 100KHz (Standard-mode)

I2C_MASTER_BAUD_RATE_400KHZ 

Baud rate at 400KHz (Fast-mode)

Values for inactive bus time-out.

If the inactive bus time-out is enabled and the bus is inactive for longer than the time-out setting, the bus state logic will be set to idle.

Enumerator
I2C_MASTER_INACTIVE_TIMEOUT_DISABLED 

Inactive bus time-out disabled.

I2C_MASTER_INACTIVE_TIMEOUT_55US 

Inactive bus time-out 5-6 SCL cycle time-out.

I2C_MASTER_INACTIVE_TIMEOUT_105US 

Inactive bus time-out 10-11 SCL cycle time-out.

I2C_MASTER_INACTIVE_TIMEOUT_205US 

Inactive bus time-out 20-21 SCL cycle time-out.

Interrupt flags.

Flags used when reading or setting interrupt flags.

Enumerator
I2C_MASTER_INTERRUPT_WRITE 

Interrupt flag used for write.

I2C_MASTER_INTERRUPT_READ 

Interrupt flag used for read.

Values for hold time after start bit.

Values for the possible I2C master mode SDA internal hold times after start bit has been sent.

Enumerator
I2C_MASTER_START_HOLD_TIME_DISABLED 

Internal SDA hold time disabled.

I2C_MASTER_START_HOLD_TIME_50NS_100NS 

Internal SDA hold time 50ns - 100ns.

I2C_MASTER_START_HOLD_TIME_300NS_600NS 

Internal SDA hold time 300ns - 600ns.

I2C_MASTER_START_HOLD_TIME_400NS_800NS 

Internal SDA hold time 400ns - 800ns.

static void i2c_master_disable ( const struct i2c_master_module *const  module)
inlinestatic

Disable the I2C module.

Disables the requested I2C module.

Parameters
[in]modulePointer to the software module struct

References _sercom_get_interrupt_vector(), Assert, and system_interrupt_disable().

Referenced by i2c_master_reset().

static void i2c_master_enable ( const struct i2c_master_module *const  module)
inlinestatic

Enables the I2C module.

Enables the requested I2C module and set the bus state to IDLE after the specified timeout period if no stop bit is detected.

Parameters
[in]modulePointer to the software module struct

References _sercom_get_interrupt_vector(), Assert, and system_interrupt_enable().

Referenced by edbg_eui_read_eui64().

static void i2c_master_get_config_defaults ( struct i2c_master_config *const  config)
inlinestatic

Gets the I2C master default configurations.

Use to initialize the configuration structure to known default values.

The default configuration is as follows:

  • Baudrate 100KHz
  • GCLK generator 0
  • Do not run in standby
  • Start bit hold time 300ns - 600ns
  • Buffer timeout = 65535
  • Unknown bus status timeout = 65535
  • Do not run in standby
  • PINMUX_DEFAULT for SERCOM pads

Those default configuration only available if the device supports it:

  • High speed baudrate 3.4MHz
  • Standard-mode and Fast-mode transfer speed
  • SCL stretch disabled
  • Slave SCL low extend time-out disabled
  • Master SCL low extend time-out disabled
Parameters
[out]configPointer to configuration structure to be initiated

References Assert, i2c_master_config::baud_rate, i2c_master_config::buffer_timeout, GCLK_GENERATOR_0, i2c_master_config::generator_source, I2C_MASTER_BAUD_RATE_100KHZ, I2C_MASTER_INACTIVE_TIMEOUT_DISABLED, I2C_MASTER_START_HOLD_TIME_300NS_600NS, i2c_master_config::inactive_timeout, PINMUX_DEFAULT, i2c_master_config::pinmux_pad0, i2c_master_config::pinmux_pad1, i2c_master_config::run_in_standby, i2c_master_config::scl_low_timeout, i2c_master_config::sda_scl_rise_time_ns, i2c_master_config::start_hold_time, and i2c_master_config::unknown_bus_state_timeout.

Referenced by edbg_eui_read_eui64().

enum status_code i2c_master_init ( struct i2c_master_module *const  module,
Sercom *const  hw,
const struct i2c_master_config *const  config 
)

Initializes the requested I2C hardware module.

Initializes the SERCOM I2C master device requested and sets the provided software module struct. Run this function before any further use of the driver.

Parameters
[out]modulePointer to software module struct
[in]hwPointer to the hardware instance
[in]configPointer to the configuration struct
Returns
Status of initialization.
Return values
STATUS_OKModule initiated correctly
STATUS_ERR_DENIEDIf module is enabled
STATUS_BUSYIf module is busy resetting
STATUS_ERR_ALREADY_INITIALIZEDIf setting other GCLK generator than previously set
STATUS_ERR_BAUDRATE_UNAVAILABLEIf given baudrate is not compatible with set GCLK frequency

References _sercom_get_sercom_inst_index(), _sercom_instances, _sercom_set_handler(), Assert, i2c_master_config::generator_source, NULL, sercom_set_gclk_generator(), system_gclk_chan_config::source_generator, STATUS_BUSY, STATUS_ERR_DENIED, STATUS_OK, system_apb_clock_set_mask(), SYSTEM_CLOCK_APB_APBC, system_gclk_chan_enable(), system_gclk_chan_get_config_defaults(), and system_gclk_chan_set_config().

Referenced by edbg_eui_read_eui64().

static bool i2c_master_is_syncing ( const struct i2c_master_module *const  module)
inlinestatic

Returns the synchronization status of the module.

Returns the synchronization status of the module.

Parameters
[in]modulePointer to software module structure
Returns
Status of the synchronization.
Return values
trueModule is busy synchronizing
falseModule is not synchronizing

References Assert.

static enum status_code i2c_master_lock ( struct i2c_master_module *const  module)
inlinestatic

Attempt to get lock on driver instance.

This function checks the instance's lock, which indicates whether or not it is currently in use, and sets the lock if it was not already set.

The purpose of this is to enable exclusive access to driver instances, so that, e.g., transactions by different services will not interfere with each other.

Parameters
[in,out]modulePointer to the driver instance to lock
Return values
STATUS_OKIf the module was locked
STATUS_BUSYIf the module was already locked

References status, STATUS_BUSY, STATUS_OK, system_interrupt_enter_critical_section(), and system_interrupt_leave_critical_section().

enum status_code i2c_master_read_byte ( struct i2c_master_module *const  module,
uint8_t *  byte 
)

Reads one byte data from slave.

Parameters
[in,out]modulePointer to software module struct
[out]byteRead one byte data to slave
Returns
Status of reading byte.
Return values
STATUS_OKOne byte was read successfully
STATUS_ERR_TIMEOUTIf no response was given within specified timeout period
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address

References _i2c_master_wait_for_bus().

enum status_code i2c_master_read_packet_wait ( struct i2c_master_module *const  module,
struct i2c_master_packet *const  packet 
)

Reads data packet from slave.

Reads a data packet from the specified slave address on the I2C bus and sends a stop condition when finished.

Note
This will stall the device from any other operation. For interrupt-driven operation, see i2c_master_read_packet_job.
Parameters
[in,out]modulePointer to software module struct
[in,out]packetPointer to I2C packet to transfer
Returns
Status of reading packet.
Return values
STATUS_OKThe packet was read successfully
STATUS_ERR_TIMEOUTIf no response was given within specified timeout period
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address

References _i2c_master_read_packet(), Assert, and STATUS_BUSY.

Referenced by edbg_eui_read_eui64().

enum status_code i2c_master_read_packet_wait_no_nack ( struct i2c_master_module *const  module,
struct i2c_master_packet *const  packet 
)

Reads data packet from slave without sending a nack signal and a stop condition when done.

Starts blocking read operation. Reads a data packet from the specified slave address on the I2C bus without sending a nack signal and a stop condition when done, thus retaining ownership of the bus when done. To end the transaction, a read or write with stop condition must be performed.

Note
This will stall the device from any other operation. For interrupt-driven operation, see i2c_master_read_packet_job.
Parameters
[in,out]modulePointer to software module struct
[in,out]packetPointer to I2C packet to transfer
Returns
Status of reading packet.
Return values
STATUS_OKThe packet was read successfully
STATUS_ERR_TIMEOUTIf no response was given within specified timeout period
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address

References _i2c_master_read_packet(), Assert, and STATUS_BUSY.

enum status_code i2c_master_read_packet_wait_no_stop ( struct i2c_master_module *const  module,
struct i2c_master_packet *const  packet 
)

Reads data packet from slave without sending a stop condition when done.

Reads a data packet from the specified slave address on the I2C bus without sending a stop condition when done, thus retaining ownership of the bus when done. To end the transaction, a read or write with stop condition must be performed.

Note
This will stall the device from any other operation. For interrupt-driven operation, see i2c_master_read_packet_job.
Parameters
[in,out]modulePointer to software module struct
[in,out]packetPointer to I2C packet to transfer
Returns
Status of reading packet.
Return values
STATUS_OKThe packet was read successfully
STATUS_ERR_TIMEOUTIf no response was given within specified timeout period
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address

References _i2c_master_read_packet(), Assert, and STATUS_BUSY.

void i2c_master_reset ( struct i2c_master_module *const  module)

Resets the hardware module.

Reset the module to hardware defaults.

Parameters
[in,out]modulePointer to software module structure

References _sercom_get_interrupt_vector(), Assert, i2c_master_disable(), system_interrupt_clear_pending(), system_interrupt_enter_critical_section(), and system_interrupt_leave_critical_section().

Referenced by edbg_eui_read_eui64().

void i2c_master_send_nack ( struct i2c_master_module *const  module)

Sends nack signal on bus.

Sends a nack signal on bus.

Note
This function can only be used after the i2c_master_write_packet_wait_no_nack function, or i2c_master_read_byte function.
Parameters
[in,out]modulePointer to the software instance struct

References Assert.

void i2c_master_send_stop ( struct i2c_master_module *const  module)

Sends stop condition on bus.

Sends a stop condition on bus.

Note
This function can only be used after the i2c_master_write_packet_wait_no_stop function. If a stop condition is to be sent after a read, the i2c_master_read_packet_wait function must be used.
Parameters
[in,out]modulePointer to the software instance struct

References Assert.

static void i2c_master_unlock ( struct i2c_master_module *const  module)
inlinestatic

Unlock driver instance.

This function clears the instance lock, indicating that it is available for use.

Parameters
[in,out]modulePointer to the driver instance to lock
Return values
STATUS_OKIf the module was locked
STATUS_BUSYIf the module was already locked
enum status_code i2c_master_write_byte ( struct i2c_master_module *const  module,
uint8_t  byte 
)

Write one byte data to slave.

Parameters
[in,out]modulePointer to software module struct
[in]byteSend one byte data to slave
Returns
Status of writing byte.
Return values
STATUS_OKOne byte was write successfully
STATUS_ERR_TIMEOUTIf no response was given within specified timeout period
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address

References _i2c_master_wait_for_bus().

enum status_code i2c_master_write_packet_wait ( struct i2c_master_module *const  module,
struct i2c_master_packet *const  packet 
)

Writes data packet to slave.

Writes a data packet to the specified slave address on the I2C bus and sends a stop condition when finished.

Note
This will stall the device from any other operation. For interrupt-driven operation, see i2c_master_read_packet_job.
Parameters
[in,out]modulePointer to software module struct
[in,out]packetPointer to I2C packet to transfer
Returns
Status of write packet.
Return values
STATUS_OKIf packet was write successfully
STATUS_BUSYIf master module is busy with a job
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address
STATUS_ERR_TIMEOUTIf timeout occurred
STATUS_ERR_OVERFLOWIf slave did not acknowledge last sent data, indicating that slave does not want more data and was not able to read last data sent

References _i2c_master_write_packet(), Assert, and STATUS_BUSY.

enum status_code i2c_master_write_packet_wait_no_stop ( struct i2c_master_module *const  module,
struct i2c_master_packet *const  packet 
)

Writes data packet to slave without sending a stop condition when done.

Writes a data packet to the specified slave address on the I2C bus without sending a stop condition, thus retaining ownership of the bus when done. To end the transaction, a read or write with stop condition or sending a stop with the i2c_master_send_stop function must be performed.

Note
This will stall the device from any other operation. For interrupt-driven operation, see i2c_master_read_packet_job.
Parameters
[in,out]modulePointer to software module struct
[in,out]packetPointer to I2C packet to transfer
Returns
Status of write packet.
Return values
STATUS_OKIf packet was write successfully
STATUS_BUSYIf master module is busy
STATUS_ERR_DENIEDIf error on bus
STATUS_ERR_PACKET_COLLISIONIf arbitration is lost
STATUS_ERR_BAD_ADDRESSIf slave is busy, or no slave acknowledged the address
STATUS_ERR_TIMEOUTIf timeout occurred
STATUS_ERR_OVERFLOWIf slave did not acknowledge last sent data, indicating that slave do not want more data

References _i2c_master_write_packet(), Assert, and STATUS_BUSY.

Referenced by edbg_eui_read_eui64().