Difference between revisions of "Example i2c test"

From wiki.emacinc.com
Jump to: navigation, search
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{todo|Review (11.14.13-13:25->JG+);(11.14.13-15:45->MD-);(01.30.14-16:20->BS+)|Brian Serrano|project=oe 4,oe 5,md,Review,bs}}
+
{{todo|Buggy (11.14.13-13:25->JG+);(11.14.13-15:45->MD-);(01.30.14-16:20->BS+);(01.30.14-18:30->MD-);(11.10.2015-12:50->MD+)|Brian Serrano|project=oe 4,oe 5,md,Buggy,bs}}
 
 
  
 
This is a guide to the <code>i2c_test</code> example project included in the EMAC OE SDK.
 
This is a guide to the <code>i2c_test</code> example project included in the EMAC OE SDK.
Line 8: Line 7:
 
This procedure provides an overview of how to compile and run the <code>i2c_test</code> C example project. This is an example test interface for reading/writing to the Linux <code>I²C</code> device interface. It is only relevant if the <code>I²C</code> device interface is enabled and an external <code>I²C</code> device is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.
 
This procedure provides an overview of how to compile and run the <code>i2c_test</code> C example project. This is an example test interface for reading/writing to the Linux <code>I²C</code> device interface. It is only relevant if the <code>I²C</code> device interface is enabled and an external <code>I²C</code> device is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.
  
For more information about the protocol see the following page:http://en.wikipedia.org/wiki/I²C
+
For more information about the protocol see the following page: http://www.nxp.com/documents/user_manual/UM10204.pdf
 
 
The <code>i2c_test</code> project build one executable: <code>i2c_test</code>.
 
  
 +
The <code>i2c_test</code> project builds one executable: <code>i2c_test</code>.
 
== Opening, Building and Uploading the Project Files ==
 
== Opening, Building and Uploading the Project Files ==
  
 
For information on opening the project from within Eclipse, please see [[Importing the EMAC OE SDK Projects with Eclipse]].  Then, follow [[Using the EMAC OE SDK Projects with Eclipse]] for information on how to build, upload and execute the example.
 
For information on opening the project from within Eclipse, please see [[Importing the EMAC OE SDK Projects with Eclipse]].  Then, follow [[Using the EMAC OE SDK Projects with Eclipse]] for information on how to build, upload and execute the example.
  
Alternatively, the <code>Makefile</code> can be used with the <code>make</code> command from the commandline to build and upload the example.  For more information on this method, please see [[Using EMAC OE SDK Example Projects]].
+
Alternatively, the <code>Makefile</code> can be used with the <code>make</code> command from the commandline to build and upload the example.  For information on this method, please see [[Using EMAC OE SDK Example Projects]].
 +
 
 +
====EMAC SDK 5.X====
 +
 
 +
For information on opening the project from within QtCreator, please see [[Getting_Started_With_Qt_Creator#Adding_Source_Files | QtCreator: Adding Source Files]].  Then, follow [[Getting Started With Qt Creator]] for information on how to build, upload and execute the example.
 +
 
 +
Alternatively, the <code>CMakefile.txt</code> can be used with the <code>cmake</code> command from the commandline to build and upload the example.  For information on this method, please see [[Getting_Started_with_the_EMAC_OE_SDK#Target_Machine_Compiling | Getting Started with the EMAC OE SDK]].
 +
 
 +
Code can be found at http://git.emacinc.com/OE/example-projects
 
== Usage and Behavior ==
 
== Usage and Behavior ==
===Hardware Requirements===
+
=== Hardware Requirements ===
  
The <code>i2c_test</code> C example project will run on any EMAC carrier board which has an <code>I²C</code> interface (see also the [http://wikidev.emacinc.com/wiki/EMAC_I%C2%B2C_Programming EMAC I²C Programming]).
+
The <code>i2c_test</code> C example project will run on any EMAC carrier board which has an <code>I²C</code> interface (see also the [[EMAC_I²C_Programming | EMAC I²C Programming]] page).
  
 
===Using i2c_test===
 
===Using i2c_test===
  
The <code>i2c_test</code> program is executed from the console. It takes no parameters.
+
The <code>i2c_test</code> program is executed from the console. If using the iPac9x25 it takes an optional argument which is the string representing the device path. By default, "/dev/i2c-1" is set, but if another device path needs to be used, append that to the command.  
  
root@emac-oe~:$ ./i2c_test
+
{{cli |./i2c_test <optional device_path>}}
  
 
A menu will appear in the terminal:
 
A menu will appear in the terminal:
<syntaxhighlight lang=console>
+
 
 +
{{clop}}<nowiki>
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address
 
     a: Set slave address
Line 37: Line 44:
 
     e: Read 2 bytes from register
 
     e: Read 2 bytes from register
 
     q: Quit
 
     q: Quit
</syntaxhighlight>
+
</nowiki>
 +
{{closp}}
  
 
Select a function (a,b,c,d,e) to test or q to quit. You will be prompted for read, write and register values as appropriate. Test results will be displayed in the terminal.
 
Select a function (a,b,c,d,e) to test or q to quit. You will be prompted for read, write and register values as appropriate. Test results will be displayed in the terminal.
  
 
==== Test Options ====
 
==== Test Options ====
<cl>
+
<!--<cl>-->
* '''Set Slave Address'''<br />
+
* '''Set Slave Address'''<br />Press the ''A'' key to select the Set slave address function, then press ''Enter''.
Press the ''A'' key to select the Set slave address function, then press ''Enter''.
+
{{clop | indent=1 }}<nowiki>
<syntaxhighlight lang=console>
 
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address
 
     a: Set slave address
Line 60: Line 67:
 
Calling i2c_set_slave(3, 0x48)
 
Calling i2c_set_slave(3, 0x48)
 
i2c_set_slave reports success
 
i2c_set_slave reports success
</syntaxhighlight>
+
</nowiki>
The slave address receives the clock and responds when addressed by the master.
+
{{closp}}
 +
<p style="margin-left:2%;">
 +
The slave address receives the clock and responds when addressed by the master.</p>
  
This function is to set the slave address of the <code>I²C</code> device. This only need to be done once to initialize the address and then each time that a new device is addressed.
+
<p style="margin-left:2%;">This function is to set the slave address of the <code>I²C</code> device. This only need to be done once to initialize the address and then each time that a new device is addressed.</p>
  
* '''Write byte to register'''<br />
+
* '''Write byte to register'''<br />Press the ''B'' key to select the Write byte to register function, then press ''Enter''
Press the ''B'' key to select the Write byte to register function, then press ''Enter''
+
{{clop | indent=1 }}<nowiki>
<syntaxhighlight lang=console>
 
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address
 
     a: Set slave address
Line 82: Line 90:
 
Calling i2c_write_cmd(3, 0x1, 0xAA)
 
Calling i2c_write_cmd(3, 0x1, 0xAA)
 
i2c_write_cmd reports success
 
i2c_write_cmd reports success
</syntaxhighlight>
+
</nowiki>
This function uses the Linux <code>write</code> system call to write one byte to the selected register on the <code>I²C</code> device.
+
{{closp}}
  
* '''Write 2 bytes to register'''<br />
+
<p style="margin-left:2%;">This function uses the Linux <code>write</code> system call to write one byte to the selected register on the <code>I²C</code> device. </p>
Press the ''C'' key to select the Write 2 bytes to register function, then press ''Enter''.
+
 
<syntaxhighlight lang=console>
+
* '''Write 2 bytes to register'''<br />Press the ''C'' key to select the Write 2 bytes to register function, then press ''Enter''.
 +
{{clop | indent=1 }}<nowiki>
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address
 
     a: Set slave address
Line 102: Line 111:
 
Calling i2c_write_cmd2(3, 0x1, 0xAABB)
 
Calling i2c_write_cmd2(3, 0x1, 0xAABB)
 
i2c_write_cmd2 reports success
 
i2c_write_cmd2 reports success
</syntaxhighlight>
+
</nowiki>
This function uses the same system call as the previous option, but writes 2 bytes to a register instead of just one.
+
{{closp}}
 +
 
 +
<p style="margin-left:2%;">This function uses the same system call as the previous option, but writes 2 bytes to a register instead of just one.</p>
  
* ''' Read byte from register '''<br />
+
* ''' Read byte from register '''<br />Press the ''D'' key to select the Read byte from register function, then press ''Enter''.
Press the ''D'' key to select the Read byte from register function, then press ''Enter''.
+
{{clop | indent=1 }}<nowiki>
<syntaxhighlight lang=console>
 
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address     
 
     a: Set slave address     
Line 122: Line 132:
 
i2c_read_reg reports success
 
i2c_read_reg reports success
 
Read value 0xB
 
Read value 0xB
</syntaxhighlight>
+
</nowiki>
This function uses the Linux <code>read</code> system call to read a byte to the selected register on the <code>I²C</code> device.
+
{{closp}}
 +
 
 +
<p style="margin-left:2%;">This function uses the Linux <code>read</code> system call to read a byte to the selected register on the <code>I²C</code> device.</p>
  
* '''Read 2 bytes from register'''<br />
+
* '''Read 2 bytes from register'''<br />Press the ''E'' key to select the Read 2 bytes from register function, then press ''Enter''.
Press the ''E'' key to select the Read 2 bytes from register function, then press ''Enter''.
+
{{clop | indent=1 }}<nowiki>
<syntaxhighlight lang=console>
 
 
Select a function to test:
 
Select a function to test:
 
     a: Set slave address         
 
     a: Set slave address         
Line 142: Line 153:
 
i2c_read_reg2 reports success
 
i2c_read_reg2 reports success
 
Read value 0xB0B
 
Read value 0xB0B
</syntaxhighlight>
+
</nowiki>
This function uses the same system call as the previous option, but shifts to the right.
+
{{closp}}
  
The <code>i2c_test</code> example writes a byte to a selected register. The register then reads the byte, shifts the byte 8 bits to the right and then reads the address to the selected register on the <code>I²C</code> device.
+
<p style="margin-left:2%;">This function uses the same system call as the previous option, but shifts to the right.</p>
  
*'''Quit'''<br />
+
<p style="margin-left:2%;">The <code>i2c_test</code> example writes a byte to a selected register. The register then reads the byte, shifts the byte 8 bits to the right and then reads the address to the selected register on the <code>I²C</code> device. </p>
Press the ''Q'' key to quit of the <code>i2c_test</code> example, then press ''Enter''.
+
 
<syntaxhighlight lang=console>
+
*'''Quit'''<br />Press the ''Q'' key to quit out of the <code>i2c_test</code> example, then press ''Enter''.
 +
{{clop | indent=1 }}<nowiki>
 
Select a function to test:     
 
Select a function to test:     
 
     a: Set slave address             
 
     a: Set slave address             
Line 159: Line 171:
 
>> q
 
>> q
 
Quiting...
 
Quiting...
</syntaxhighlight>
+
</nowiki>
This function quits you out of the <code>i2c_test</code> example on the <code>I²C</code> device.
+
{{closp}}
</cl>
+
 
 +
<p style="margin-left:2%;">This function exits you out of the <code>i2c_test</code> example on the <code>I²C</code> device.</p>
 +
<!--</cl>-->
 +
 
 +
== Real-World Example ==
 +
=== Hardware Requirements ===
 +
The <code>i2c_test</code> C Real-World example project will be running on an EMAC SOM-210ES carrier board. This example will be communicating with the RTC clock on the SOM-3517M.
 +
 
 +
For more information about the RTC Clock see the following page for the data sheet: http://www.nxp.com/documents/data_sheet/PCA8565.pdf
 +
 
 +
=== Using Real-World Example i2c_test ===
 +
 
 +
Just like before, the <code>i2c_test</code> program is executed from the console. It takes no parameters.
 +
 
 +
{{cli |./i2c_test}}
 +
 
 +
A menu will appear in the terminal:
 +
{{clop}}<nowiki>
 +
Select a function to test:
 +
    a: Set slave address
 +
    b: Write byte to register
 +
    c: Write 2 bytes to register
 +
    d: Read byte from register
 +
    e: Read 2 bytes from register
 +
    q: Quit
 +
</nowiki>
 +
{{closp}}
 +
 
 +
==== Test Options ====
 +
 
 +
* '''Set Slave Address'''<br />Press the ''A'' key to select the Set slave address function, then press ''Enter''.
 +
 
 +
The RTC Clock slave address in binary is 1010001 (58 in Hexadecimal).
 +
 
 +
{{note | The slave address must be shifted to the right one bit. }}
 +
 
 +
{{clop}}<nowiki>
 +
Select a function to test:
 +
    a: Set slave address
 +
    b: Write byte to register
 +
    c: Write 2 bytes to register
 +
    d: Read byte from register
 +
    e: Read 2 bytes from register
 +
    q: Quit
 +
>> a
 +
 
 +
Enter the slave address (in hex): 28
 +
 
 +
Calling i2c_set_slave(3, 0x48)
 +
i2c_set_slave reports success
 +
</nowiki>
 +
{{closp}}
  
 
== Summary ==
 
== Summary ==
 +
 
The <code>i2c_test</code> C example project writes and reads bytes to a selected register on the <code>I²C</code> device.
 
The <code>i2c_test</code> C example project writes and reads bytes to a selected register on the <code>I²C</code> device.

Latest revision as of 14:21, 30 November 2022

TODO: {{#todo:Buggy (11.14.13-13:25->JG+);(11.14.13-15:45->MD-);(01.30.14-16:20->BS+);(01.30.14-18:30->MD-);(11.10.2015-12:50->MD+)|Brian Serrano|oe 4,oe 5,md,Buggy,bs}}

This is a guide to the i2c_test example project included in the EMAC OE SDK.

I²C is a two wire serial interface used to connect to a variety of sensor and I/O devices. The interface uses only two bi-directional open-drain I/Os, Serial Data Line (SDA) and Serial Clock (SCL), to communicate to devices based on an address encoded within the data transmission. SDA is a data signal which sends and receives serially transmitted data. SCL is a clock signal which is used to determine when to latch data from the SDA line.

This procedure provides an overview of how to compile and run the i2c_test C example project. This is an example test interface for reading/writing to the Linux I²C device interface. It is only relevant if the I²C device interface is enabled and an external I²C device is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.

For more information about the protocol see the following page: http://www.nxp.com/documents/user_manual/UM10204.pdf

The i2c_test project builds one executable: i2c_test.

Opening, Building and Uploading the Project Files

For information on opening the project from within Eclipse, please see Importing the EMAC OE SDK Projects with Eclipse. Then, follow Using the EMAC OE SDK Projects with Eclipse for information on how to build, upload and execute the example.

Alternatively, the Makefile can be used with the make command from the commandline to build and upload the example. For information on this method, please see Using EMAC OE SDK Example Projects.

EMAC SDK 5.X

For information on opening the project from within QtCreator, please see QtCreator: Adding Source Files. Then, follow Getting Started With Qt Creator for information on how to build, upload and execute the example.

Alternatively, the CMakefile.txt can be used with the cmake command from the commandline to build and upload the example. For information on this method, please see Getting Started with the EMAC OE SDK.

Code can be found at http://git.emacinc.com/OE/example-projects

Usage and Behavior

Hardware Requirements

The i2c_test C example project will run on any EMAC carrier board which has an I²C interface (see also the EMAC I²C Programming page).

Using i2c_test

The i2c_test program is executed from the console. If using the iPac9x25 it takes an optional argument which is the string representing the device path. By default, "/dev/i2c-1" is set, but if another device path needs to be used, append that to the command.

root@som9x25:~# ./i2c_test <optional device_path>

A menu will appear in the terminal:

Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit

Select a function (a,b,c,d,e) to test or q to quit. You will be prompted for read, write and register values as appropriate. Test results will be displayed in the terminal.

Test Options

  • Set Slave Address
    Press the A key to select the Set slave address function, then press Enter.
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> a Enter the slave address (in hex): 48 Calling i2c_set_slave(3, 0x48) i2c_set_slave reports success

The slave address receives the clock and responds when addressed by the master.

This function is to set the slave address of the I²C device. This only need to be done once to initialize the address and then each time that a new device is addressed.

  • Write byte to register
    Press the B key to select the Write byte to register function, then press Enter
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> b Enter a byte to write (in hex): aa Enter a register value to write (in hex): 01 Calling i2c_write_cmd(3, 0x1, 0xAA) i2c_write_cmd reports success

This function uses the Linux write system call to write one byte to the selected register on the I²C device.

  • Write 2 bytes to register
    Press the C key to select the Write 2 bytes to register function, then press Enter.
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> c Enter a 2 byte number to write (in hex): aabb Enter a register value to write (in hex): 01 Calling i2c_write_cmd2(3, 0x1, 0xAABB) i2c_write_cmd2 reports success

This function uses the same system call as the previous option, but writes 2 bytes to a register instead of just one.

  • Read byte from register
    Press the D key to select the Read byte from register function, then press Enter.
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> d Enter a register value to read from (in hex): bb Calling i2c_read_reg(3, (__u8 *)&val, 0xBB) i2c_read_reg reports success Read value 0xB

This function uses the Linux read system call to read a byte to the selected register on the I²C device.

  • Read 2 bytes from register
    Press the E key to select the Read 2 bytes from register function, then press Enter.
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> e Enter a register value to read from (in hex): bbcc Calling i2c_read_reg2(3, &val, 0xBBCC) i2c_read_reg2 reports success Read value 0xB0B

This function uses the same system call as the previous option, but shifts to the right.

The i2c_test example writes a byte to a selected register. The register then reads the byte, shifts the byte 8 bits to the right and then reads the address to the selected register on the I²C device.

  • Quit
    Press the Q key to quit out of the i2c_test example, then press Enter.
Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> q Quiting...

This function exits you out of the i2c_test example on the I²C device.

Real-World Example

Hardware Requirements

The i2c_test C Real-World example project will be running on an EMAC SOM-210ES carrier board. This example will be communicating with the RTC clock on the SOM-3517M.

For more information about the RTC Clock see the following page for the data sheet: http://www.nxp.com/documents/data_sheet/PCA8565.pdf

Using Real-World Example i2c_test

Just like before, the i2c_test program is executed from the console. It takes no parameters.

root@som9x25:~# ./i2c_test

A menu will appear in the terminal:

Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit

Test Options

  • Set Slave Address
    Press the A key to select the Set slave address function, then press Enter.

The RTC Clock slave address in binary is 1010001 (58 in Hexadecimal).



NOTE
The slave address must be shifted to the right one bit.


Select a function to test: a: Set slave address b: Write byte to register c: Write 2 bytes to register d: Read byte from register e: Read 2 bytes from register q: Quit >> a Enter the slave address (in hex): 28 Calling i2c_set_slave(3, 0x48) i2c_set_slave reports success

Summary

The i2c_test C example project writes and reads bytes to a selected register on the I²C device.