<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.emacinc.com/index.php?action=history&amp;feed=atom&amp;title=Libgpiod_IO_Example</id>
	<title>Libgpiod IO Example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.emacinc.com/index.php?action=history&amp;feed=atom&amp;title=Libgpiod_IO_Example"/>
	<link rel="alternate" type="text/html" href="https://wiki.emacinc.com/index.php?title=Libgpiod_IO_Example&amp;action=history"/>
	<updated>2026-04-24T06:01:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.6</generator>
	<entry>
		<id>https://wiki.emacinc.com/index.php?title=Libgpiod_IO_Example&amp;diff=14123&amp;oldid=prev</id>
		<title>Btisler: Creation of page with new name from io_demo_update_2019, minor changes and a bugfix</title>
		<link rel="alternate" type="text/html" href="https://wiki.emacinc.com/index.php?title=Libgpiod_IO_Example&amp;diff=14123&amp;oldid=prev"/>
		<updated>2020-09-08T21:02:05Z</updated>

		<summary type="html">&lt;p&gt;Creation of page with new name from io_demo_update_2019, minor changes and a bugfix&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{todo|SEOKWREV (01.03.14-16:47-&amp;gt;JG+);(01.03.14-17:35-&amp;gt;MD+);(04.07.14-09:45-&amp;gt;BS+);(04.09.14-16:15-&amp;gt;BS+)|Jgreene|project=oe 4,oe 5,jg,md,SEOKWREV,ky,bs}}&lt;br /&gt;
&lt;br /&gt;
{{#seo:&lt;br /&gt;
|title=io demo&lt;br /&gt;
|titlemode=append&lt;br /&gt;
|keywords=ADC example,Ring Counter,Binary Counter&lt;br /&gt;
|description=This is a guide to the &amp;lt;code&amp;gt;io_demo&amp;lt;/code&amp;gt; C example project included in the EMAC OE SDK.&lt;br /&gt;
}}&lt;br /&gt;
This demo covers basic GPIO usage for the IPAC single board computer, and 150 and 200 series carrier boards. This demo first cover GPIO manipulation through the Linux terminal and then demonstrates gpio usage in a C program using an io example program.&lt;br /&gt;
&lt;br /&gt;
GPIO control is accomplished through use of the libgpiod driver, as a result only the libgpiod library will be covered in this tutorial. For manipulation of non-GPIO peripherals use sysfs.&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
===Hardware Requirements===&lt;br /&gt;
To use the '''io demo''' program you will need the following target hardware.&lt;br /&gt;
&lt;br /&gt;
* An EMAC 150 series carrier board with a compatible SoM &amp;lt;br /&amp;gt;&lt;br /&gt;
'''OR''' &amp;lt;br /&amp;gt;&lt;br /&gt;
* An EMAC 200 series carrier board with a compatible SoM &amp;lt;br /&amp;gt;&lt;br /&gt;
'''OR'''&lt;br /&gt;
* An IPAC single board computer, Kernel v4.4 and up&amp;lt;br /&amp;gt;&lt;br /&gt;
'''AND'''&lt;br /&gt;
* additional components to establish to serial and Ethernet connections for your target hardware &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Software Requirments===&lt;br /&gt;
&lt;br /&gt;
These tools should already be installed if you installed the EMAC SDK using the automatic installer.&lt;br /&gt;
* [[Getting_Started_with_the_EMAC_OE_SDK | EMAC OE 5.X SDK]]&lt;br /&gt;
* EMAC Qt Creator&lt;br /&gt;
* GNU &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt;&lt;br /&gt;
* CMake&lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
If you haven't already, please review the EMAC_OE_SDK [[Getting_Started_with_the_EMAC_OE_SDK| &amp;quot;getting started&amp;quot; page]]. EMAC_OE_SDK will need to be installed and serial and network connections will need to be established between the developer machine and the target device.See the [[Serial Connections]] or [[Network Connections]] page for more information on how to connect to the target EMAC product using a serial port or Ethernet connection.&lt;br /&gt;
&lt;br /&gt;
== GPIO manipulation  through Linux Terminal == &lt;br /&gt;
GPIOs can be manipulated directly through the target devices serial terminal. The Libgpiod libary is used for this purpose. Linux terminal commands are defined in the libgpiod github readme (https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree) and are listed here for convenience:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;There are currently six command-line tools available: &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''gpiodetect''' - list all gpiochips present on the system, their names, labels and number of GPIO lines&lt;br /&gt;
&lt;br /&gt;
'''gpioinfo'''   - list all lines of specified gpiochips, their names, consumers, direction, active state and additional flags&lt;br /&gt;
&lt;br /&gt;
'''gpioget'''    - read values of specified GPIO lines&lt;br /&gt;
&lt;br /&gt;
'''gpioset'''    - set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal&lt;br /&gt;
&lt;br /&gt;
'''gpiofind'''   - find the gpiochip name and line offset given the line name&lt;br /&gt;
&lt;br /&gt;
'''gpiomon'''    - wait for events on GPIO lines, specify which events to watch,how many events to process before exiting or if the events should be reported to the console&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In libgpiod ports and pins correspond to gpiochips and lines respectively. GPIO chip numbers are assigned by the kernel, and are hardware depended. &lt;br /&gt;
== GPIO manipulation in C ==&lt;br /&gt;
A more common manipulation of GPIOs is through a C program. The following example will demonstrated GPIO manipulation using the Libgpiod libary.&lt;br /&gt;
&lt;br /&gt;
'''Program Setup''' &amp;lt;br \&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''On the target device'':&lt;br /&gt;
&amp;lt;cl&amp;gt;&lt;br /&gt;
1. Login, the default username and password is give below: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
username: root&lt;br /&gt;
password: emac_inc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Change the file to read write by entering '''oemntrw''' into the device terminal. &lt;br /&gt;
{{cli | oemntrw | hostname=somimx6ul}}&lt;br /&gt;
&lt;br /&gt;
{{note|This will only change the root filesystem to read-write for the current boot.  When the system is rebooted, the root filesystem will once again be mounted read-only.}}&lt;br /&gt;
* Obtain the device IP-address by entering '''ifconfig''' into the terminal. This is needed for device configuration in Qt Creator below.&amp;lt;br \&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=console highlight=3&amp;gt;&lt;br /&gt;
root@emac-oe:~# ifconfig&lt;br /&gt;
eth0      Link encap:Ethernet  HWaddr 00:50:C2:E6:60:F4&lt;br /&gt;
          inet addr:10.0.2.100  Bcast:10.0.255.255  Mask:255.255.0.0&lt;br /&gt;
          inet6 addr: fe80::5054:ff:feb1:f83e/64 Scope:Link&lt;br /&gt;
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1&lt;br /&gt;
          RX packets:1825 errors:0 dropped:0 overruns:0 frame:0&lt;br /&gt;
          TX packets:553 errors:0 dropped:0 overruns:0 carrier:0&lt;br /&gt;
          collisions:0 txqueuelen:1000&lt;br /&gt;
          RX bytes:139124 (135.8 KiB)  TX bytes:50603 (49.4 KiB)&lt;br /&gt;
          Interrupt:25 Base address:0xc000&lt;br /&gt;
&lt;br /&gt;
lo        Link encap:Local Loopback&lt;br /&gt;
          inet addr:127.0.0.1  Mask:255.0.0.0&lt;br /&gt;
          inet6 addr: ::1/128 Scope:Host&lt;br /&gt;
          UP LOOPBACK RUNNING  MTU:16436  Metric:1&lt;br /&gt;
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0&lt;br /&gt;
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0&lt;br /&gt;
          collisions:0 txqueuelen:0&lt;br /&gt;
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/cl&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
'''On development machine''': &amp;lt;br /&amp;gt;&lt;br /&gt;
1. Open Qt Creator          &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''''Option 1:'''''&lt;br /&gt;
&lt;br /&gt;
2. Create a qmake project&lt;br /&gt;
*select '''+ New Project''' on the 'Welcome' window, the New Project window will appear.&lt;br /&gt;
*Under Projects select '''Non-Qt Project''', Under the middle window select '''Plain C Application''', Click '''Choose...''', The Location window will appear.&lt;br /&gt;
*Name your project '''LibGPIOd_example''', and choose a Create-in location, Click '''Next &amp;gt;''' to advance to the Build System window.&lt;br /&gt;
*On the Build System Window choose the '''qmake''' option, Click '''Next &amp;gt;''' to advance to the Kits window.&lt;br /&gt;
*On the Kits window check only '''ARM Qt5''', '''Click Next &amp;gt;'''  to advance to the Summary window&lt;br /&gt;
*The options on the Summary window will default to '''&amp;lt;None&amp;gt;'', Click '''Finish'''. Your Project will now be generated.&lt;br /&gt;
3. Modify the qmake the build file ('''project_name.pro''').&lt;br /&gt;
*Copy and paste the following into your .pro file.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
TEMPLATE = app&lt;br /&gt;
CONFIG += console&lt;br /&gt;
CONFIG -= app_bundle&lt;br /&gt;
CONFIG -= qt&lt;br /&gt;
LIBS +=  -lgpiod&lt;br /&gt;
SOURCES += main.c \&lt;br /&gt;
    EMAC_Libgpiod.c&lt;br /&gt;
target.path=/home/developer&lt;br /&gt;
INSTALLS += target&lt;br /&gt;
HEADERS += \&lt;br /&gt;
    EMAC_Libgpiod.h&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
4. Add the EMAC_Libgpiod.h file&lt;br /&gt;
*With you project selected, click '''File-&amp;gt;New File or Project''', the New File or Project window will appear.&lt;br /&gt;
*Under Files and Classes select General, and in the middle column select '''Empty file''', Click '''Choose...''', The Location window will appear&lt;br /&gt;
*Name the file '''EMAC_Libgpiod.h''' and choose your project location as the path, Click '''Next&amp;gt;''' to advance to the Summary window&lt;br /&gt;
*Click '''Finish''', the file will now open.&lt;br /&gt;
*Copy and paste the provided .h file, [http://git.emacinc.com/OE/qt-creator-example-projects/raw/master/libgpiod_io_example/EMAC_Libgpiod.h EMAC_Libgpiod.h] into &lt;br /&gt;
*Save the file, by click '''File-&amp;gt;Save All''', the file will now be added to your project under '''Libgpiod_example-&amp;gt;Headers'''&lt;br /&gt;
5. Add the EMAC_Libgpiod.c file&lt;br /&gt;
*repeat same steps as for the .h file, but replace the file name enter '''EMAC_Libgpiod.c''' and Copy and paste the provided [http://git.emacinc.com/OE/qt-creator-example-projects/raw/master/libgpiod_io_example/EMAC_Libgpiod.c EMAC_Libgpiod.c file]&lt;br /&gt;
*select '''File-&amp;gt;Save All'''&lt;br /&gt;
&lt;br /&gt;
'''Option 2:'''&lt;br /&gt;
&lt;br /&gt;
2. Create a cmake project&lt;br /&gt;
*select '''+ New Project''' on the 'Welcome' window, the New Project window will appear.&lt;br /&gt;
*Under Projects select '''EMAC Project''', Under the middle window select '''EMAC C Project (CMake Build)''', Click '''Choose...''', The Location window will appear.&lt;br /&gt;
*Name your project '''LibGPIOd_example''', and choose a Create-in location, Click '''Next &amp;gt;''' to advance to the Summary window.&lt;br /&gt;
*The options on the Summary window will default to '''&amp;lt;None&amp;gt;'', Click '''Finish'''. Your Project will now be generated.&lt;br /&gt;
*Click on the '''Projects''' tab, select only the '''Arm''' option and click '''Configure Project'''. A CMakeLists.txt file will now be generated under the project.&lt;br /&gt;
*In the CMakeList.txt file add, to the bottom of the file, line: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;target_link_libraries(${PROJECT_NAME} gpiod)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
3. Add the EMAC_Libgpiod.h file&lt;br /&gt;
*Under the project folder right click on '''CMakeLists.txt''' and select '''Show Containing Folder''' to open the project directory.&lt;br /&gt;
*Save this [http://git.emacinc.com/OE/qt-creator-example-projects/blob/master/libgpiod_io_example/EMAC_Libgpiod.h EMAC_Libgpiod.h file] to the project directory. Do this in git by right clicking on '''Raw''', selecting '''Save link as...''' and choosing the project directory.&lt;br /&gt;
4. Add the [http://git.emacinc.com/OE/qt-creator-example-projects/blob/master/libgpiod_io_example/EMAC_Libgpiod.c EMAC_Libgpiod.c file] in the same way. &amp;lt;/br&amp;gt;  &lt;br /&gt;
5. Right click on the project folder and select '''Run CMake''' to add the files to the project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''End of Options, Continuing:'''&amp;lt;br /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
6. Open main.c under '''LibGPIOd_example-&amp;gt;Sources''' and copy and paste the provided [http://git.emacinc.com/OE/qt-creator-example-projects/raw/master/libgpiod_io_example/main.c main.c].&lt;br /&gt;
*Note, the program must be configured to run for your target device. In the main.c file select your target device by un-commenting the #define preprocessor directive corresponding to the device '''you're using'''. All other devices should be commented out. For this example we're using a 150ES carrier board so we'll select the '''CARRIER_BOARD_150'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
//In main.c&lt;br /&gt;
&lt;br /&gt;
#define CARRIER_BOARD_150 &lt;br /&gt;
//#define CARRIER_BOARD_200&lt;br /&gt;
//#define IPAC                &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, 1 of 4 example programs can be selected to run, example 4 is selected by default, we'll continue with this option.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
//In main.c&lt;br /&gt;
&lt;br /&gt;
//#define EXAMPLE_1&lt;br /&gt;
//#define EXAMPLE_2&lt;br /&gt;
//#define EXAMPLE_3&lt;br /&gt;
#define EXAMPLE_4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*select '''File-&amp;gt;Save All''', All necessary files have now been added to the project&lt;br /&gt;
&amp;lt;li&amp;gt;Configure build options&amp;lt;/li&amp;gt;&lt;br /&gt;
*Go to '''Tools-&amp;gt;Options...''' to open the options window&lt;br /&gt;
*Select Devices in the left hand column to open the Devices tab&lt;br /&gt;
*From the '''Devices''' tab select '''Devices''' sub-tab and Click '''Add...''' in the right hand column&lt;br /&gt;
*Select Generic Linux Device, then click '''Start Wizard'''.&lt;br /&gt;
&lt;br /&gt;
[[File:Generic_linux.png|600px|left|thumb|Figure 6: Qt Creator Wizard Selection]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Type in a name, the IP address, the user name, and the password for the device. Use the credentials below to sign in.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;2&amp;quot;|Table 4: Default Login Credentials &lt;br /&gt;
|-&lt;br /&gt;
! Username !! Password&lt;br /&gt;
|-&lt;br /&gt;
| root || emac_inc&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[File:Libgpiod_device_config_12.PNG]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Click '''Next'''.&lt;br /&gt;
&lt;br /&gt;
*Click '''Finish'''.&lt;br /&gt;
&lt;br /&gt;
*A connection will be established with the target device.&lt;br /&gt;
&lt;br /&gt;
[[File:Device_test.png|600px|left|thumb|Figure 8: Qt Creator Device Test]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Click '''Close'''.&lt;br /&gt;
&lt;br /&gt;
*Click '''Apply'''.&lt;br /&gt;
&lt;br /&gt;
*Click '''OK'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Now, on the left side of Qt Creator, you will see the project icon, which looks like a monitor. Click this and select '''Release''' or '''Debug''' under build.&amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{{note|If multiple build options were selected three sub-menus will appear ('''Kit, Build''' and '''Run'''). Click this and make sure '''Arm Qt5''' is selected under '''Kit''', '''Release''' is selected under '''Build''', and the project is set to be run on the remote device under '''Run'''.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Libgpiod_14.PNG|600px|left|thumb]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Now, simply press the green play button. The program should run on the target device and yield an output.&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
As previously stated the main.c program must be configured to run for your target device. In the main.c file select your target device by un-commenting the '''#define''' preprocessor directive corresponding to the device you're using. All other devices should be commented out. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
//In main.c&lt;br /&gt;
&lt;br /&gt;
#define CARRIER_BOARD_150 &lt;br /&gt;
//#define CARRIER_BOARD_200&lt;br /&gt;
//#define IPAC                &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, 1 of 4 example programs can be selected to run, un-comment 1 of the &amp;quot;example&amp;quot; '''#define''' preprocessor directives and leave the rest commented out. Changes these options will require rebuilding of the program (Simply press the green play button again, this should automatically rebuild and launch the project on the target device.)&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
//In main.c&lt;br /&gt;
&lt;br /&gt;
//#define EXAMPLE_1&lt;br /&gt;
//#define EXAMPLE_2&lt;br /&gt;
//#define EXAMPLE_3&lt;br /&gt;
#define EXAMPLE_4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All examples use the Libgpiod libary ('''#include &amp;lt;gpiod.h&amp;gt;'''), with example 4 using custom built functions provided in '''EMAC_Libgpiod.h'''. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''EXAMPLE_1''' demonstrates basic IO configuration using Libgpiod's contextless functions &amp;lt;br /&amp;gt;&lt;br /&gt;
'''EXAMPLE_2''' demonstrates basic IO configuration without contextless functions &amp;lt;br /&amp;gt;&lt;br /&gt;
'''EXAMPLE_3''' LED demo using only Libgpiod&amp;lt;br /&amp;gt;&lt;br /&gt;
'''EXAMPLE_4''' LED demo using custom built Libgpiod-based helper functions &amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Btisler</name></author>
		
	</entry>
</feed>