I2C - Digi International

Download Report

Transcript I2C - Digi International

NET+OS 6.1 Training
7/17/2015
NetSilicon & Digi Confidential
I2C
7/17/2015
NetSilicon & Digi Confidential
I2C device topics
• Introduction
• I2C APIs
• I2C examples
7/17/2015
NetSilicon & Digi Confidential
I2C device basic
• Simple 4-wire device. Multiple I2C devices
can be connected to the same I2C bus.
• We support two types (all) I2C devices – I2C
Master and I2C Slave.
7/17/2015
NetSilicon & Digi Confidential
Performance
• We support 100Kbits/sec or 400Kbits/sec.
• Support 7 & 10 bit addressing
• Dedicated I/O. No change to the gpio.h
7/17/2015
NetSilicon & Digi Confidential
IC2 Master Interface
I2C Master Interrupt state diagram
7/17/2015
NetSilicon & Digi Confidential
I2C Slave
7/17/2015
NetSilicon & Digi Confidential
7bit addressing format
1. Received of slave side
S
Slave Address
RW
A
Data(8Bit)
A
0
Slave Address
RW
A
P
From master to slave
From slave to master
2. Transmitted of slave side
S
Data(8Bit)
A
Data(8Bit)
A
Data(8Bit)
A
1
S = Start
A = ACK
P = STOP / Repeat Start
7/17/2015
NetSilicon & Digi Confidential
P
10bit addressing format
1. Received of slave side
S
Address Upper
RW
A
Address Lower
A
Data(8Bit)
A
From master to slave
0
Data(8Bit)
A
P
From slave to master
2. Transmitted of slave side
S
Address Upper
RW
A
Address Lower
A
P
Address Upper
RW
0
S = Start
A = ACK
P = STOP / Repeat Start
7/17/2015
1
A
NetSilicon & Digi Confidential
Data(8Bit)
A
P
Device Open()
• Calls i2c_dev_open()
• Example
i2cPort = open("/i2c/0",I2C_SLAVE); or
i2cPort = open("/i2c/0",I2C_MASTER);
• I2C device channel: /i2c/0
• I2C device mode: I2C_MASTER, or
I2C_SLAVE.
7/17/2015
NetSilicon & Digi Confidential
Device close()
• Calls i2c_dev_close()
• Closes the I2C channel.
• Example:
ret = close(fd);
7/17/2015
NetSilicon & Digi Confidential
Device Read()
• Calls i2c_dev_read().
• int i2c_dev_read(int channel, void * i2cMsg,
int notUsed, int * notUsed1);
Note: i2cMsg is pointer to the i2cMsg type.
• The i2cMeg will be returned by a read
callback function in an ISR. DO NOT access
it before it is returned.
7/17/2015
NetSilicon & Digi Confidential
Device Write()
• Calls i2c_dev_write().
• int i2c_dev_write(int channel, void *
i2cMsg, int notUsed, int * notUsed1)
• The i2cMsg will be returned by a write
callback function in an ISR context.
• DO NOT access the message before it is
returned.
7/17/2015
NetSilicon & Digi Confidential
Device Ioctl()
•
•
•
•
•
•
•
Calls i2c_dev_ioctl()
Important flags:
I2C_SET_READ_CALLBACK_FUNC
I2C_SET_WRITE_CALLBACK_FUNC
I2C_SET_CLOCK_SPEED
I2C_SET_GCA_IRQ_ENABLE
I2C_FLUSH_BUFFER
7/17/2015
NetSilicon & Digi Confidential
Other APIs
• void
MCI2cBuildMsg(MC_I2C_MESSAGE_TYP
E* i2cMsg, char* buf, unsigned long
bufLength, unsigned char addr);
7/17/2015
NetSilicon & Digi Confidential
I2C Specific….
• I2C driver is interrupt driven and the I2C interrupt is part of the BBUS
interrupt. After we install the I2C interrupt function, on receiving
every data byte and I2C commannd, we will receive an interrupt
generated by the I2C IP. The MCI2cInterrupt() function will then be
called by the ISR handler. Based on the state of the current I2C bus
and whether the Mercury is acting as an I2C Master or Slave, the
appropriate function will be called. Please refer to the I2C Master or
Slave State machine for ISR handling details.
• A semaphore is used to keep multiple I2C port from opening at the
same time.
• A Event flag is created in the application example; but it is up to the
application to decide whether to use it or not. The same applies to the
Event flag in the nausbdevapp example as well.
7/17/2015
NetSilicon & Digi Confidential
Example
• Typical User application
A typical open function call is
fd = open(“i2c/0”, I2C_MASTER);
Or
fd = open(“i2c/0”, I2C_SLAVE);
A typical close function call is
Ret = close(fd);
7/17/2015
NetSilicon & Digi Confidential
Read() Example
• Typical usage: The third parameter is
ignored.
• Ret=Read(fd, i2cMsg, 0x0);
• If (ret == -1) printf(“ read failed.\n”);
7/17/2015
NetSilicon & Digi Confidential
Write() example
• Typical usage: The third parameter is
ignored.
• Ret=write(fd, i2cMsg, 0x0);
• If (ret == -1) printf(“ write failed.\n”);
7/17/2015
NetSilicon & Digi Confidential
I2C message defination
• typedef struct _MESSAGE_TYPE {
char * buffer;
unsigned short address;
unsigned long bufLength; /* size of the
buffer */
unsigned long availableDataLen;
MC_I2C_BUFFER_STATE state;
} MC_I2C_MESSAGE_TYPE;
7/17/2015
NetSilicon & Digi Confidential
Typical write Callback function
• void writeCallBackFunc(MC_I2C_MESSAGE_TYPE * ptrData)
{
if (ptrData->state != I2C_INIT)
{
tx_event_flags_set(&i2cEvent,I2C_WRITE_EVENT_FLAG, TX_OR);
/* free(ptrData); */
}
return;
}
7/17/2015
NetSilicon & Digi Confidential
Typical read callback function
• void readCallBackFunc(MC_I2C_MESSAGE_TYPE * ptrData)
{
if (ptrData->state != I2C_INIT)
{
tx_event_flags_set(&i2cEvent,I2C_READ_EVENT_FLAG, TX_OR);
/* free(ptrData); */
}
return;
}
7/17/2015
NetSilicon & Digi Confidential
Example application
MCI2cBuildMsg(i2cMsg, ptrTemp, 26, 0x60);
ret = write(i2cPort, i2cMsg, 0);
MCInstallIsr (I2C_INTERRUPT, (MC_ISR_HANDLER)
MCI2cInterrupt, (void *) 0, 0);
while( 1 /* mySleepCount < 6 */)
{
ret = tx_event_flags_get(&i2cEvent,
I2C_WRITE_EVENT_FLAG|I2C_READ_EVENT_FLAG,
TX_OR_CLEAR, &actualEvent, TX_WAIT_FOREVER);
……
7/17/2015
NetSilicon & Digi Confidential
Important files
• Driver: Src/bsp/devices/i2c/*
• Example:
Src/examples/nai2capp/itc_test.c
7/17/2015
NetSilicon & Digi Confidential