Skip to content

I2C Master Lib API

Chip Perason edited this page Nov 29, 2014 · 2 revisions

The I2C_Master_lib is an interrupt driven I2C Master. The state machine like code is designed to automatically send messages from its i2c_buffer_out and receive message data into its i2c_buffer_in and record the number of byte received.

Variables  
i2c_slave_adrs			Current Slave being addressed.  

i2c_error				Error flag bit.  
	0x80				(On Read) Overflow.  
	0x40				(On Read) Arbitration lost.  
	0x04				(on Write) Arbitration lost.  

i2c_comm_status  		Current comm Read status.
	0					Empty input buffer.
	n					Number of bytes read in.

i2c_comm_state
	I2C_COMM_IDLE		No messages being processed.  
	I2C_COMM_BUSY		Sending or receiving a message.  
	I2C_COMM_ERROR		Last message sent or received had error(s).  

i2c_buff_in_max			Maximum message length.  
i2c_buff_in_cnt			Number of byte read in.
i2c_buffer_in			Receiving buffer.  

i2c_buff_out_cnt		Decrements to 0 while sending message.  
i2c_buff_out			Sending buffer.

i2c_XH:XL				Buffer address being used.


Functions  
i2c_init_master  
	Initialize the TWI hardware to support I2C Master interface.  
	This needs to be called once.  
	input reg:	none  
	output reg:	none  

i2c_getState  
	Returns the i2c_comm_state.  
	input reg:	none  
	output reg:	i2c_comm_state  

i2c_getErrorFlags  
	Returns the i2c_error flags.  
	input reg:	none  
	output reg:	i2c_error  

i2c_getReadStatus  
	Returns the i2c_comm_status.  
	input reg:	none  
	output reg:	i2c_comm_status  

i2c_write  
	Sets up to automatically send a message to the targeted Slave.  
	The output buffer is set by the X register pair.  
	input reg:	R17 - Slave address  
				R18 - number of bytes to be sent  
				X - buffer address in SRAM  
	output reg:	none  

i2c_read  
	Sets up to automatically read in a message from the targeted Slave.  
	The input buffer is set by the X register pair.  
	input reg:	R17 - Slave address  
				R18 - maximum number of bytes to be received  
				X - buffer address in SRAM  
	output reg:	none  

i2c_iter  
	This is the I2C Master interrupt service routine. It is enabled by an  
    i2c_read() call or an i2c_write() call.  
Clone this wiki locally