Difference between revisions of "Component: I2C Master (Comms: Interface)"

From Flowcode Help
Jump to navigationJump to search
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
+
{| style="width:50%"
 
 
{| width="50%"
 
 
|-
 
|-
| width="20%" style="color: gray;" | Author
+
| width="20%" style="color:gray;" | Author
 
| Matrix Ltd.
 
| Matrix Ltd.
 
|-
 
|-
| width="20%" style="color: gray;" | Version
+
| width="20%" style="color:gray;" | Version
| 1.4 (Release)
+
| 1.4
 
|-
 
|-
| width="20%" style="color: gray;" | Category
+
| width="20%" style="color:gray;" | Category
 
| Comms: Interface
 
| Comms: Interface
 
|}
 
|}
  
  
 +
==I2C Master component==
 +
Generic Two Wire I2C Communications Interface
 +
 +
==Component Pack==
  
==[[File:Component Icon 24219805_113d_4b12_b6bd_e1b0b0c33981.png|Image]] I2C Master component==
+
COMMSA
Generic Two Wire I2C Communications Interface
 
  
 
==Detailed description==
 
==Detailed description==
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
 
===Overview===
 
===Overview===
Line 74: Line 95:
 
Stop
 
Stop
  
==Examples==
 
More information on I2C can be found here,
 
  
[http://www.matrixtsl.com/blog/simplified-communications-i%c2%b2c-and-spi/ Matrix Flowcode Blog: Simplified communications I2C and SPI]
 
  
  
===Generic I2C EEPROM===
 
  
Example file demonstrating how to read and write bytes from a generic I2C EEPROM device.
 
{{Fcfile|I2CEEPROM.fcfx|I2CEEPROM}}
 
===Simulated I2C devices===
 
  
This example uses the I2C master component combined with the DS1307 injector to simulate an I2C communications bus between the target Microcontroller and the virtual DS1307 device.
 
{{Fcfile|DS1307InjectorTest.fcfx|I2C DS1307 Example}}
 
The panel displays the current time from the DS1307 RAM which is populated to match the system time.
 
  
[[File:DS1307Panel.jpg]]
 
  
  
The I2C console shows the communications between the target microcontroller and simulated I2C device.
+
==Examples==
  
[[File:DS1307Data.jpg]]
 
  
  
The DS1307 console shows the contents of RAM memory on the simulated I2C device.
 
  
[[File:DS1307Ram.jpg]]
 
  
==Downloadable macro reference==
 
  
===<span style="font-weight: normal;"><u><tt>ReceiveByte</tt></u></span>===
 
Receives a byte from the I²C bus.
 
  
Set Last=0 if there is following more Master_RxByte macros.
 
  
Set Last=1 if this is the last Master_RxByte and it is followed with Master_Stop.
 
  
If Last = 1 the I2C bus will be presented with a 'Nack' condition.
 
  
  
'''Parameters'''
 
  
:[[Variable Types|BYTE]] ''Last''
 
::Used to signify the last byte when streaming incoming data. 0=Not last byte, 1=Last Byte
 
  
  
'''Return value'''
 
  
:[[Variable Types|BYTE]]
 
  
===<span style="font-weight: normal;"><u><tt>Restart</tt></u></span>===
 
Outputs a restart condition onto the I²C bus.
 
  
'''Parameters'''
 
  
:''This macro has no parameters''
 
  
  
'''Return value'''
+
More information on I2C can be found here,
  
:''This call does not return a value''
+
[http://www.matrixtsl.com/blog/simplified-communications-i%c2%b2c-and-spi/ Matrix Flowcode Blog: Simplified communications I2C and SPI]
  
  
===<span style="font-weight: normal;"><u><tt>Stop</tt></u></span>===
+
===Generic I2C EEPROM===
Outputs a stop condition onto the I²C bus.
 
  
'''Parameters'''
+
Example file demonstrating how to read and write bytes from a generic I2C EEPROM device.
 +
{{Fcfile|I2CEEPROM.fcfx|I2CEEPROM}}
 +
===Simulated I2C devices===
  
:''This macro has no parameters''
+
This example uses the I2C master component combined with the DS1307 injector to simulate an I2C communications bus between the target Microcontroller and the virtual DS1307 device.
 +
{{Fcfile|DS1307InjectorTest.fcfx|I2C DS1307 Example}}
 +
The panel displays the current time from the DS1307 RAM which is populated to match the system time.
  
 +
[[File:DS1307Panel.jpg]]
  
'''Return value'''
 
  
:''This call does not return a value''
+
The I2C console shows the communications between the target microcontroller and simulated I2C device.
  
 +
[[File:DS1307Data.jpg]]
  
===<span style="font-weight: normal;"><u><tt>ReceiveByteTransaction</tt></u></span>===
 
Function to perform a generic I2C read transaction. The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit. Returns the data from the location specified.
 
  
'''Parameters'''
+
The DS1307 console shows the contents of RAM memory on the simulated I2C device.
  
:[[Variable Types|BYTE]] ''Device_ID''
+
[[File:DS1307Ram.jpg]]
::7-bit Device Address ID
 
  
:[[Variable Types|BYTE]] ''AddrH''
 
::Internal Address High Byte
 
  
:[[Variable Types|BYTE]] ''AddrL''
+
===I2C Transaction Mode===
::Internal Address Low Byte
 
  
 +
Transction mode can be used on all target devices.
  
'''Return value'''
+
If not using transaction mode then your code won't work on ARM or ESP.
  
:[[Variable Types|BYTE]]
+
With transaction mode there are fewer component macros required than Non-transaction read/write mode.
  
 +
Example file demonstrating how I2C transaction component macros can be used to write and read to the minutes register of DS1307/DS3231 RTC.
  
===<span style="font-weight: normal;"><u><tt>TransmitByte</tt></u></span>===
+
If simulation Mode within properties is set to Yes, then the minutes are read from your PC time via a dll.
Sends a byte on the I²C bus. Returns the acknowledge if any.  
 
  
0 represents that data was acknowledged and 1 represents no acknowledge was detected.
+
You can expand using the dll to retrieve the whole date and time.
  
'''Parameters'''
+
{{Fcfile|I2C Transaction Example.fcfx|I2C Transaction Example}}
  
:[[Variable Types|BYTE]] ''Data''
+
==Downloadable macro reference==
::Data byte to send on the I²C bus.
 
  
 +
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReceiveByte'''
 +
|-
 +
| colspan="2" | Receives a byte from the I²C bus. &nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Last
 +
|-
 +
| colspan="2" | Used to signify the last byte when streaming incoming data. 0=Not last byte, 1=Last Byte&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
'''Return value'''
 
  
:[[Variable Types|BYTE]]
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Restart'''
 +
|-
 +
| colspan="2" | Outputs a restart condition onto the I²C bus.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
  
===<span style="font-weight: normal;"><u><tt>SendByteTransaction</tt></u></span>===
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
Function to perform a generic I2C Write transaction. The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit.
+
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Stop'''
 +
|-
 +
| colspan="2" | Outputs a stop condition onto the I²C bus.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
'''Parameters'''
 
  
:[[Variable Types|BYTE]] ''Device_ID''
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
::7-bit Device Address ID
+
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Transaction_Write'''
 +
|-
 +
| colspan="2" | Attempt to write Length number of bytes to the I2C slave from the given Buffer. Ensure that the Buffer is large enough.  Returns the number of bytes written, the caller should check that this matches requested Length&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Buffer
 +
|-
 +
| colspan="2" | Buffer of bytes to write to the I2C&nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" | Length
 +
|-
 +
| colspan="2" | Number of bytes to send out, MS bit 0x8000 signifies no Stop if set&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
:[[Variable Types|BYTE]] ''AddrH''
 
::Internal Address High Byte
 
  
:[[Variable Types|BYTE]] ''AddrL''
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
::Internal Address Low Byte
+
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''TransmitByte'''
 +
|-
 +
| colspan="2" | Sends a byte on the I²C bus. Returns the acknowledge if any.  0 represents that data was acknowledged and 1 represents no acknowledge was detected.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Data
 +
|-
 +
| colspan="2" | Data byte to send on the I²C bus.&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
:[[Variable Types|BYTE]] ''Data''
 
::Data Byte
 
  
 +
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GenericWriteRegister'''
 +
|-
 +
| colspan="2" | Function to perform a generic I2C Write transaction. The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Device_ID
 +
|-
 +
| colspan="2" | 7-bit Device Address ID&nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" | Address
 +
|-
 +
| colspan="2" | &nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Data
 +
|-
 +
| colspan="2" | Data Byte&nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | AddressByteCount
 +
|-
 +
| colspan="2" | Specifies the number of address bytes, Range: 1 or 2&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
'''Return value'''
 
  
:''This call does not return a value''
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Transaction_Uninit'''
 +
|-
 +
| colspan="2" | Uninitialise the I2C interface&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
  
===<span style="font-weight: normal;"><u><tt>Start</tt></u></span>===
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
Outputs a start condition onto the I²C bus.
+
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GenericReadRegister'''
 +
|-
 +
| colspan="2" | Function to perform a generic I2C read.  The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit. Returns the data from the location specified.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Device_ID
 +
|-
 +
| colspan="2" | 7-bit Device Address ID&nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" | Address
 +
|-
 +
| colspan="2" | &nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | AddressByteCount
 +
|-
 +
| colspan="2" | Specifies the number of address bytes, Range: 1 or 2&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
'''Parameters'''
 
  
:''This macro has no parameters''
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Transaction_Read'''
 +
|-
 +
| colspan="2" | Attempt to read Length number of bytes from the I2C slave to the given Buffer. Ensure that the Buffer is large enough.  Returns number of bytes read, caller should check that this matches requested Length&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Buffer
 +
|-
 +
| colspan="2" | Buffer to store the incoming byte data&nbsp;
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" | Length
 +
|-
 +
| colspan="2" | Number of bytes to read&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u16-icon.png]] - UINT
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
  
'''Return value'''
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Start'''
 +
|-
 +
| colspan="2" | Outputs a start condition onto the I²C bus.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
:''This call does not return a value''
 
  
 +
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Transaction_Initialise'''
 +
|-
 +
| colspan="2" | Initialise the I2C interface to communicate with a Slave device at Address Returns 0 on fail, 1 on success&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" | Address
 +
|-
 +
| colspan="2" | 7-bit I2C Address without the shift for the R/W bit&nbsp;
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
===<span style="font-weight: normal;"><u><tt>Initialise</tt></u></span>===
 
Enables the I²C hardware and performs some initialization.
 
 
Should be called at the start of the program or at least before any of the other I²C functions are called.
 
 
'''Parameters'''
 
 
:''This macro has no parameters''
 
 
 
'''Return value'''
 
 
:''This call does not return a value''
 
  
 +
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 +
|-
 +
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
 +
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Initialise'''
 +
|-
 +
| colspan="2" | Enables the I²C hardware and performs some initialization. Should be called at the start of the program or at least before any of the other I²C functions are called.&nbsp;
 +
|-
 +
|-
 +
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
 +
| width="90%" style="border-top: 2px solid #000;" | ''Return''
 +
|}
  
  
==Simulation macro reference==
 
 
''This component does not contain any simulation macros''
 
  
  
 
==Property reference==
 
==Property reference==
<span style="font-weight: normal;"><u>Channel</u></span>
 
  
This property is of type ''Fixed list of ints'' and can be referenced with the variable name ''CHANNEL''.
+
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 
+
|-
Specifies the I2C peripheral used by the component.
+
| width="10%" align="center" style="background-color:#D8C9D8;" | [[File:Fc9-prop-icon.png]]
 
+
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Properties'''  
Hardware channels are defined by the number of I2C peripherals available onboard your target microcontroller.
+
|-
 
+
|-
Software channels use standard I/O pins allowing you to connect the I2C bus to pretty much any pin.
+
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
 
+
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Bus Settings
<span style="font-weight: normal;"><u>Baud Select</u></span>
+
|-
 
+
|-
This property is of type ''Fixed list of ints'' and can be referenced with the variable name ''BAUD_LIST''.
+
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
 
+
| width="90%" | Channel
Sets the rate of the I2C clock which sets the maximum number of bits per second that can be sent or received.
+
|-
 
+
| colspan="2" | Channel selection&nbsp;
<span style="font-weight: normal;"><u>Slew Rate Control</u></span>
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
This property is of type ''Fixed list of ints'' and can be referenced with the variable name ''SRC''.
+
| width="90%" | Baud Select
 
+
|-
The slew rate control only works with hardware I2C channels but acts to modify the overall baud to ensure
+
| colspan="2" | Baud rate option selector&nbsp;
 
+
|-
that signals are being transferred correctly based on how fast the control signals are able to rise and fall.
+
| width="10%" align="center" | [[File:Fc9-type-14-icon.png]]
 
+
| width="90%" | Baud Rate
<span style="font-weight: normal;"><u>SMBus Inputs</u></span>
+
|-
 
+
| colspan="2" | Baud rate to be used&nbsp;
This property is of type ''Fixed list of ints'' and can be referenced with the variable name ''SMB''.
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-7-icon.png]]
The SMBus Inputs only works with hardware I2C channels but acts to modify the I2C driver to be compatible with SM Bus.
+
| width="90%" | Stop Delay
 
+
|-
<span style="font-weight: normal;"><u>Stop Delay</u></span>
+
| colspan="2" | On older microcontroller devices there is a potential for the I2C hardware channel to lock up if there is not a 10ms delay between an I2C stop event and the next I2C start event. Most modern microcontrollers will not have a problem so this property can be disabled to speed up the I2C communications. &nbsp;
 
+
|-
This property is of type ''True or false'' and can be referenced with the variable name ''StopDelay''.
+
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
 
+
| width="90%" | Slew Rate Control
On older microcontroller devices there is a potential for the I2C hardware channel to lock up if there is not  
+
|-
 
+
| colspan="2" | Slew Rate Control Enabled or Disabled&nbsp;
a 10ms delay between an I2C stop event and the next I2C start event.
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
 
+
| width="90%" | SMBus Inputs
 
+
|-
Most modern microcontrollers will not have a problem so this property can be disabled to speed up the  
+
| colspan="2" |  When Enabled input logic thresholds are compliant with SMBus specification&nbsp;
 
+
|-
I2C communications.
+
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
 
+
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Connections
<span style="font-weight: normal;"><u>Data (SDA)</u></span>
+
|-
 
+
|-
This property is of type ''Single digital pin'' and can be referenced with the variable name ''SDA''.
+
| width="10%" align="center" | [[File:Fc9-type-5-icon.png]]
 
+
| width="90%" | SDA
I2C data pin commonly referred to as SDA which represents serial data
+
|-
 
+
| colspan="2" | Pin used for SDA (data signal)&nbsp;
<span style="font-weight: normal;"><u>Clock (SCL)</u></span>
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-5-icon.png]]
This property is of type ''Single digital pin'' and can be referenced with the variable name ''SCL''.
+
| width="90%" | SCL
 
+
|-
I2C clock pin commonly referred to as SCL which represents serial clock
+
| colspan="2" | Pin used for SCL (clock signal)&nbsp;
 
+
|-
<span style="font-weight: normal;"><u>Label</u></span>
+
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
 
+
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Simulation
This property is of type ''Line of text'' and can be referenced with the variable name ''label''.
+
|-
 
+
|-
Label shown on the comms flasher on the simulation panel.
+
| width="10%" align="center" | [[File:Fc9-type-10-icon.png]]
 
+
| width="90%" | Label
<span style="font-weight: normal;"><u>Scope Traces</u></span>
+
|-
 
+
| colspan="2" | Label shown on the comms flasher on the simulation panel.&nbsp;
This property is of type ''True or false'' and can be referenced with the variable name ''ScopeTraces''.
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-7-icon.png]]
Selects if the scope traces are automatically generated or not
+
| width="90%" | Scope Traces
 
+
|-
<span style="font-weight: normal;"><u>Console Data</u></span>
+
| colspan="2" | Selects if the scope traces are automatically generated or not&nbsp;
 
+
|-
This property is of type ''True or false'' and can be referenced with the variable name ''ConsoleData''.
+
| width="10%" align="center" | [[File:Fc9-type-7-icon.png]]
 
+
| width="90%" | Console Data
Selects if the console data is automatically generated or not
+
|-
 
+
| colspan="2" | Selects if the console data is automatically generated or not&nbsp;
<span style="font-weight: normal;"><u>Injector</u></span>
+
|-
 
+
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
This property is of type ''Fixed list of ints'' and can be referenced with the variable name ''Injector''.
+
| width="90%" | API
 
+
|-
Lists all the available injector components on the panel to allow the I2C data to have meaning.
+
| colspan="2" | &nbsp;
 
+
|}
Injectors are available to let you directly type in values, simulate devices and communicate between multiple instances of Flowcode.
 

Latest revision as of 17:20, 9 November 2022

Author Matrix Ltd.
Version 1.4
Category Comms: Interface


I2C Master component

Generic Two Wire I2C Communications Interface

Component Pack

COMMSA

Detailed description

Overview

The I2C bus is a medium speed communications bus which is usually best suited for talking between devices situated on the same circuit board. Due to the high frequency digital nature of the bus care should be taken to keep tracks as short as possible and as far away as possible from other sources of noise. A typical I2C bus consists of two signals, data and clock. The I2C bus usually consists of a single master device and then one or more slave devices. The master device initiates all the communications and can only communicate with a single device on the bus at a time by sending a unique device address as the first byte.

Each I2C transaction consists of a start and a stop as well as one or more data bytes made up of 8 clock cycles allowing the 8-bits of each byte to be transferred. Each byte send is followed by an Ack (acknowledged) or a Nak (not acknowledged) from the receiving device.

I2C Bytes.jpg


Pull up resistors

The I2C bus usually requires pull up resistors in the range of 4.7K to 10K between the two I2C signals and VCC. Some I2C devices have the pull up resistors built in so as to avoid external components.

The pull up resistors can be useful when interfacing a 5V microcontroller to a 3V3 sensor as the pull up resistor can be connected to 3V3 to eliminate the need for voltage level shifting.


Start / Restart / Stop

The Start, Restart and Stop operations are each states which the bus can be put into using the I2C specification.


Generic Write Transaction

A generic write transaction to a memory device might look something like this:

Start

Send External Device Address Byte (Write mode)

Send Internal Address Byte

Send Data Byte

Stop


Generic Read Transaction

A generic read transaction to a memory device might look something like this:

Start

Send External Device Address Byte (Write mode)

Send Internal Address Byte

Restart

Send External Device Address Byte (Read mode)

Read Data Byte

Stop





Examples

More information on I2C can be found here,

Matrix Flowcode Blog: Simplified communications I2C and SPI


Generic I2C EEPROM

Example file demonstrating how to read and write bytes from a generic I2C EEPROM device. FC6 Icon.png I2CEEPROM

Simulated I2C devices

This example uses the I2C master component combined with the DS1307 injector to simulate an I2C communications bus between the target Microcontroller and the virtual DS1307 device. FC6 Icon.png I2C DS1307 Example The panel displays the current time from the DS1307 RAM which is populated to match the system time.

DS1307Panel.jpg


The I2C console shows the communications between the target microcontroller and simulated I2C device.

DS1307Data.jpg


The DS1307 console shows the contents of RAM memory on the simulated I2C device.

DS1307Ram.jpg


I2C Transaction Mode

Transction mode can be used on all target devices.

If not using transaction mode then your code won't work on ARM or ESP.

With transaction mode there are fewer component macros required than Non-transaction read/write mode.

Example file demonstrating how I2C transaction component macros can be used to write and read to the minutes register of DS1307/DS3231 RTC.

If simulation Mode within properties is set to Yes, then the minutes are read from your PC time via a dll.

You can expand using the dll to retrieve the whole date and time.

FC6 Icon.png I2C Transaction Example

Downloadable macro reference

Fc9-comp-macro.png ReceiveByte
Receives a byte from the I²C bus.  
Fc9-u8-icon.png - BYTE Last
Used to signify the last byte when streaming incoming data. 0=Not last byte, 1=Last Byte 
Fc9-u8-icon.png - BYTE Return


Fc9-comp-macro.png Restart
Outputs a restart condition onto the I²C bus. 
Fc9-void-icon.png - VOID Return


Fc9-comp-macro.png Stop
Outputs a stop condition onto the I²C bus. 
Fc9-void-icon.png - VOID Return


Fc9-comp-macro.png Transaction_Write
Attempt to write Length number of bytes to the I2C slave from the given Buffer. Ensure that the Buffer is large enough. Returns the number of bytes written, the caller should check that this matches requested Length 
Fc9-u8-icon.png - BYTE Buffer
Buffer of bytes to write to the I2C 
Fc9-u16-icon.png - UINT Length
Number of bytes to send out, MS bit 0x8000 signifies no Stop if set 
Fc9-u16-icon.png - UINT Return


Fc9-comp-macro.png TransmitByte
Sends a byte on the I²C bus. Returns the acknowledge if any. 0 represents that data was acknowledged and 1 represents no acknowledge was detected. 
Fc9-u8-icon.png - BYTE Data
Data byte to send on the I²C bus. 
Fc9-u8-icon.png - BYTE Return


Fc9-comp-macro.png GenericWriteRegister
Function to perform a generic I2C Write transaction. The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit. 
Fc9-u8-icon.png - BYTE Device_ID
7-bit Device Address ID 
Fc9-u16-icon.png - UINT Address
 
Fc9-u8-icon.png - BYTE Data
Data Byte 
Fc9-u8-icon.png - BYTE AddressByteCount
Specifies the number of address bytes, Range: 1 or 2 
Fc9-void-icon.png - VOID Return


Fc9-comp-macro.png Transaction_Uninit
Uninitialise the I2C interface 
Fc9-void-icon.png - VOID Return


Fc9-comp-macro.png GenericReadRegister
Function to perform a generic I2C read. The 7-bit device ID is automatically shifted up by one bit to make room for the read/write bit. Returns the data from the location specified. 
Fc9-u8-icon.png - BYTE Device_ID
7-bit Device Address ID 
Fc9-u16-icon.png - UINT Address
 
Fc9-u8-icon.png - BYTE AddressByteCount
Specifies the number of address bytes, Range: 1 or 2 
Fc9-u8-icon.png - BYTE Return


Fc9-comp-macro.png Transaction_Read
Attempt to read Length number of bytes from the I2C slave to the given Buffer. Ensure that the Buffer is large enough. Returns number of bytes read, caller should check that this matches requested Length 
Fc9-u8-icon.png - BYTE Buffer
Buffer to store the incoming byte data 
Fc9-u16-icon.png - UINT Length
Number of bytes to read 
Fc9-u16-icon.png - UINT Return


Fc9-comp-macro.png Start
Outputs a start condition onto the I²C bus. 
Fc9-void-icon.png - VOID Return


Fc9-comp-macro.png Transaction_Initialise
Initialise the I2C interface to communicate with a Slave device at Address Returns 0 on fail, 1 on success 
Fc9-u8-icon.png - BYTE Address
7-bit I2C Address without the shift for the R/W bit 
Fc9-u8-icon.png - BYTE Return


Fc9-comp-macro.png Initialise
Enables the I²C hardware and performs some initialization. Should be called at the start of the program or at least before any of the other I²C functions are called. 
Fc9-void-icon.png - VOID Return



Property reference

Fc9-prop-icon.png Properties
Fc9-conn-icon.png Bus Settings
Fc9-type-16-icon.png Channel
Channel selection 
Fc9-type-16-icon.png Baud Select
Baud rate option selector 
Fc9-type-14-icon.png Baud Rate
Baud rate to be used 
Fc9-type-7-icon.png Stop Delay
On older microcontroller devices there is a potential for the I2C hardware channel to lock up if there is not a 10ms delay between an I2C stop event and the next I2C start event. Most modern microcontrollers will not have a problem so this property can be disabled to speed up the I2C communications.  
Fc9-type-16-icon.png Slew Rate Control
Slew Rate Control Enabled or Disabled 
Fc9-type-16-icon.png SMBus Inputs
When Enabled input logic thresholds are compliant with SMBus specification 
Fc9-conn-icon.png Connections
Fc9-type-5-icon.png SDA
Pin used for SDA (data signal) 
Fc9-type-5-icon.png SCL
Pin used for SCL (clock signal) 
Fc9-conn-icon.png Simulation
Fc9-type-10-icon.png Label
Label shown on the comms flasher on the simulation panel. 
Fc9-type-7-icon.png Scope Traces
Selects if the scope traces are automatically generated or not 
Fc9-type-7-icon.png Console Data
Selects if the console data is automatically generated or not 
Fc9-type-16-icon.png API