<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><description>An exercise to build a C++ library for the GPIO pins. The programmer can decide what underlying mechanism is used to talk to the pins. In this initial release, one mechanism is supported: the sysfs interface.
typedef dgpio::pin&amp;lt;dgpio::sysfsdev...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Fri, 05 Jan 2024 12:14:54 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;This&amp;nbsp;follow-up post has 3 ways to talk to the GPIOs. You select the method of choice by plugging in the desired driver:&amp;nbsp;&amp;nbsp;[mention:719db58238574e7d93b41c6932327480:f7d226abd59f475c9d224a79e3f0ec07]&amp;nbsp;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Thu, 04 Jan 2024 23:50:39 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>shabaz</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;This is great! It&amp;#39;s a disappointment that this sort of thing is so overlooked for C/C++ by SBC manufacturers. It would speed up adoption of SBCs if there was a known easy way to work with the GPIO such as this.&lt;/p&gt;
&lt;p&gt;I was thinking the other day, there should be an RFC or software-world equivalent! on the topic of improving GPIO.&lt;/p&gt;
&lt;p&gt;One idea (still only partially baked) was what if there was a config file with every program, such as this:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:9aa899cb-11b8-43c3-a544-30d178b75390:type=json&amp;text=%7B%0A%20%20%20%20%22INTRO%22%3A%20%22Blinky%20GPIO%20setup%20rev%201%22%2C%0A%20%20%20%20%22LED%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22CIRCUIT_DESC%22%3A%20%22Connect%20a%20220%20ohm%20resistor%20and%20LED%20to%20this%20pin%20and%20GND%22%2C%0A%20%20%20%20%20%20%20%20%22BCM_PIN%22%3A%2012%2C%0A%20%20%20%20%20%20%20%20%22DIR%22%3A%20%22OUTPUT%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22BUTTON1%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22CIRCUIT_DESC%22%3A%20%22Connect%20a%20push-button%20between%20this%20pin%20and%20GND%22%2C%0A%20%20%20%20%20%20%20%20%22BCM_PIN%22%3A%2013%2C%0A%20%20%20%20%20%20%20%20%22DIR%22%3A%20%22INPUT%22%2C%0A%20%20%20%20%20%20%20%20%22PULLUP%22%3A%201%0A%20%20%20%20%7D%0A%7D%0A]&lt;/p&gt;
&lt;p&gt;The idea being that the names &amp;quot;LED&amp;quot; and &amp;quot;BUTTON1&amp;quot; would somehow become part of the definitions seen by the compiler at compile-time, but the GPIO library would read the file during execution to see the pin numbering. That way, users could build code (e.g. blinky code) and then later edit the config file to suit how they wire things up, without recompiling, they would just rerun the code to re-read the file. It would be slower, but barely, since C/C++ is speedy anyway.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The &amp;quot;DIR&amp;quot; and &amp;quot;PULLUP&amp;quot; would not be edited by the user, but was just an idea to speed up configuration of pins by the software developer if they didn&amp;#39;t need to do it in the code.&amp;nbsp;Things could break easily if the config file was wrong, but then things break if source code is wrong too.&lt;/p&gt;
&lt;p&gt;Also, since a lot of code comes with badly drawn Fritzing, it may be easier to simply see how to wire pins by reading the CIRCUIT_DESC lines in the config file.&lt;/p&gt;
&lt;p&gt;For sure&amp;nbsp;it&amp;#39;s unnecessary since people still get by without it, this is just thinking out loud, flaws and all.&lt;/p&gt;
&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Thu, 04 Jan 2024 19:50:36 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I have this working with &amp;quot;gpio via registers&amp;quot; too.&lt;/p&gt;
&lt;p&gt;You can choose between sysfs gpio (/sys/class/gpio) or memmapped register gpio by changing an include and a typedef in your code.&lt;/p&gt;
&lt;p&gt;For talking to GPIO via sysfs :&lt;/p&gt;
&lt;pre&gt;#include &amp;quot;sysfsdevice.h&amp;quot;&lt;br /&gt;typedef dgpio::pin&amp;lt;dgpio::sysfsdevice&amp;gt; pin;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;... and via memmapped register access:&lt;/p&gt;
&lt;pre&gt;#include &amp;quot;memmapdevice.h&amp;quot;&lt;br /&gt;typedef dgpio::pin&amp;lt;dgpio::memmapdevice&amp;gt; pin;&lt;/pre&gt;
&lt;p&gt;For the rest the code is the same. Example:&lt;/p&gt;
&lt;pre&gt;int main() {&lt;br /&gt;  pin p21 = pin(21);&lt;br /&gt;  p21.init(pin::dir::out);&lt;br /&gt;  p21.set(pin::status::off);&lt;/pre&gt;
&lt;p&gt;I haven&amp;#39;t published the memmap code yet, but here is the current version:&lt;/p&gt;
&lt;p&gt;header:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:8bbd5984-6935-425b-9265-94ee0bef2fdd:type=c_cpp&amp;text=%23ifndef%20MEMMAPDEVICE_H_%0D%0A%23define%20MEMMAPDEVICE_H_%0D%0A%0D%0A%23include%20%3Cfcntl.h%3E%20%20%20%20%2F%2F%20for%20open%0D%0A%23include%20%3Csys%2Fmman.h%3E%20%2F%2F%20for%20mmap%0D%0A%23include%20%22pin.h%22%0D%0A%0D%0Anamespace%20dgpio%20%7B%0D%0A%0D%0Aclass%20memmapdevice%20%7B%0D%0Apublic%3A%0D%0A%09static%20void%20init%28unsigned%20gpio%2C%20pin%3Cmemmapdevice%3E%3A%3Adir%20dir%29%3B%0D%0A%09static%20void%20deinit%28unsigned%20gpio%29%20%7B%0D%0A%09%09%2F%2F%20https%3A%2F%2Flinux.die.net%2Fman%2F2%2Fclose%0D%0A%09%09%2F%2F%20It%20is%20probably%20unwise%20to%20close%20file%20descriptors%20while%20they%20may%0D%0A%09%09%2F%2F%20be%20in%20use%20by%20system%20calls%20in%20other%20threads%20in%20the%20same%20process%0D%0A%09%7D%0D%0A%09static%20pin%3Cmemmapdevice%3E%3A%3Astatus%20get%28unsigned%20gpio%29%20%7B%0D%0A%09%09return%20gpiomem%5B13%5D%20%26%20%281%20%3C%3C%20gpio%29%20%3F%20pin%3Cmemmapdevice%3E%3A%3Astatus%3A%3Aon%20%3A%20pin%3Cmemmapdevice%3E%3A%3Astatus%3A%3Aoff%3B%20%2F%2F%20GPLEV0%0D%0A%09%7D%0D%0A%09static%20void%20set%28unsigned%20gpio%2C%20pin%3Cmemmapdevice%3E%3A%3Astatus%20status%29%20%7B%0D%0A%09%09%2F%2F%20always%20set%20status%20first%0D%0A%09%09switch%20%28status%29%20%7B%0D%0A%09%09case%20pin%3Cmemmapdevice%3E%3A%3Astatus%3A%3Aon%3A%0D%0A%09%09%09gpiomem%5B7%5D%20%7C%3D%20%281%20%3C%3C%20gpio%29%3B%20%2F%2F%20GPSET0%0D%0A%09%09%09break%3B%0D%0A%09%09case%20pin%3Cmemmapdevice%3E%3A%3Astatus%3A%3Aoff%3A%0D%0A%09%09%09gpiomem%5B10%5D%20%7C%3D%20%281%20%3C%3C%20gpio%29%3B%20%2F%2F%20GPCLR0%0D%0A%09%09%09break%3B%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0Aprivate%3A%0D%0A%09static%20unsigned%20int%20%2Agpiomem%3B%0D%0A%09static%20int%20fdgpio%3B%0D%0A%7D%3B%0D%0A%0D%0A%7D%20%2F%2F%20namespace%20dgpio%0D%0A%0D%0A%23endif%20%2F%2A%20MEMMAPDEVICE_H_%20%2A%2F%0D%0A]&lt;/p&gt;
&lt;p&gt;cpp:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:2b31926c-3452-4abd-867d-3687a86ecfa2:type=c_cpp&amp;text=%23include%20%22memmapdevice.h%22%0D%0A%0D%0A%0D%0Anamespace%20dgpio%20%7B%0D%0A%0D%0A%2F%2F%20static%20members%20to%20be%20initialised%20in%20the%20cpp%20file%0D%0A%2F%2F%20memory%20mapped%20registers%0D%0Aunsigned%20int%20%2Amemmapdevice%3A%3Agpiomem%20%3D%20nullptr%3B%0D%0A%2F%2F%20gpio%20memory%20map%20file%20interface%0D%0Aint%20memmapdevice%3A%3Afdgpio%20%3D%20-1%3B%0D%0A%0D%0Avoid%20memmapdevice%3A%3Ainit%28unsigned%20gpio%2C%20pin%3Cmemmapdevice%3E%3A%3Adir%20dir%29%20%7B%0D%0A%09%2F%2F%20inspiration%0D%0A%09%2F%2F%20https%3A%2F%2Fwww.cs.uaf.edu%2F2016%2Ffall%2Fcs301%2Flecture%2F11_09_raspberry_pi.html%0D%0A%09if%20%28fdgpio%20%3E%200%29%20%7B%20%2F%2F%20memmap%20already%20initialised%3F%0D%0A%09%09return%3B%0D%0A%09%7D%0D%0A%09%2F%2F%20map%20gpio%20memory%20to%20gpiomem%0D%0A%09fdgpio%3Dopen%28%22%2Fdev%2Fgpiomem%22%2C%20O_RDWR%29%3B%0D%0A%09if%20%28fdgpio%20%3E%200%29%20%7B%0D%0A%09%09gpiomem%20%3D%20%28unsigned%20int%20%2A%29mmap%280%2C%204096%2C%20PROT_READ%2BPROT_WRITE%2C%20MAP_SHARED%2C%20fdgpio%2C0%29%3B%0D%0A%09%7D%0D%0A%09unsigned%20int%20index%20%3D%20gpio%20%2F%2010%3B%20%2F%2F%20GPFSELn%20register%20to%20set%20this%20pin%20direction%0D%0A%09unsigned%20int%20offset%20%3D%20%28gpio%20%25%2010%29%20%2A%203%3B%20%2F%2F%20lsb%20for%20this%20pin%27s%203%20bit%20status%20in%20that%20register%0D%0A%0D%0A%09unsigned%20int%20curval%20%3D%20gpiomem%5Bindex%5D%3B%0D%0A%09curval%20%26%3D%20~0b111%20%3C%3C%20%28offset%29%3B%0D%0A%0D%0A%09switch%20%28dir%29%20%7B%0D%0A%09case%20pin%3Cmemmapdevice%3E%3A%3Adir%3A%3Ain%3A%0D%0A%09%09break%3B%0D%0A%09case%20pin%3Cmemmapdevice%3E%3A%3Adir%3A%3Aout%3A%0D%0A%09%09curval%20%7C%3D%200b1%20%3C%3C%20%28offset%29%3B%0D%0A%09%09break%3B%0D%0A%09%7D%0D%0A%0D%0A%09gpiomem%5Bindex%5D%20%3D%20curval%3B%0D%0A%7D%0D%0A%0D%0A%7D%20%2F%2F%20namespace%20dgpio%0D%0A]&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll blog the details when I have time.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Wed, 03 Jan 2024 19:43:40 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;preparation for the register based driver:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:c7a5ac9b-213d-44f5-b380-56b7d0ec3280:type=c_cpp&amp;text=%09int%20fdgpio%3Dopen%28%22%2Fdev%2Fgpiomem%22%2CO_RDWR%29%3B%0D%0A%09if%20%28fdgpio%3C0%29%20%7B%20printf%28%22Error%20opening%20%2Fdev%2Fgpiomem%22%29%3B%20return%20-1%3B%20%7D%0D%0A%0D%0A%09unsigned%20int%20%2Agpio%3D%28unsigned%20int%20%2A%29mmap%280%2C4096%2C%0D%0A%09%09PROT_READ%2BPROT_WRITE%2C%20MAP_SHARED%2C%0D%0A%09%09fdgpio%2C0%29%3B%0D%0A%09printf%28%22mmap%27d%20gpiomem%20at%20pointer%20%25p%5Cn%22%2Cgpio%29%3B%0D%0A%0D%0A%09%2F%2F%20set%20pin%2021%20as%20output%2C%20by%20writing%20001%20to%205-3%20of%20GPFSEL2%0D%0A%09gpio%5B2%5D%20%7C%3D%20%281%3C%3C3%29%3B%20%2F%2F%20pin%2021%20output%0D%0A%0D%0A%09%2F%2F%09toggle%20pin%2021%0D%0A%09gpio%5B7%5D%20%7C%3D%20%281%3C%3C21%29%3B%20%2F%2F%20pin%2021%20high%0D%0A%09gpio%5B10%5D%20%7C%3D%20%281%3C%3C21%29%3B%20%2F%2F%20pin%2021%20low%0D%0A%0D%0A%0D%0A%09%2F%2F%20Read%20pin%208%2C%20by%20accessing%20bit%208%20of%20GPLEV0%0D%0A%09unsigned%20int%20retval%20%3D%20gpio%5B13%5D%26%281%3C%3C8%29%3B]&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This code first set pin 21 as output, then sets it high, then low.&amp;nbsp;Then it reads pin 8. All from the registers.&lt;/p&gt;
&lt;p&gt;See page 90 of the datasheet:&amp;nbsp;&lt;a id="" href="https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf" rel="noopener noreferrer nofollow" target="_blank" data-e14adj="t"&gt;https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;inspiration:&amp;nbsp;&lt;a id="" href="https://www.cs.uaf.edu/2016/fall/cs301/lecture/11_09_raspberry_pi.html" rel="noopener noreferrer nofollow" target="_blank" data-e14adj="t"&gt;https://www.cs.uaf.edu/2016/fall/cs301/lecture/11_09_raspberry_pi.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I tested, and my LED blunk.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Thu, 28 Dec 2023 20:01:15 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>DAB</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Nice post Jan.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/c-gpio-library-for-raspberry-pi</link><pubDate>Tue, 26 Dec 2023 16:02:59 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:3b8de625-9ddd-491b-871b-79412856c10f</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;If you&amp;#39;d like to use a different mechanism (e.g.: register access), you &amp;#39;d need to write a class with minimum this interface:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:d3809cad-b0dc-4799-9b05-30e8c7714390:type=c_cpp&amp;text=%23include%20%22pin.h%22%0A%0A%0Anamespace%20dgpio%20%7B%0A%0Aclass%20registerdevice%20%7B%0Apublic%3A%0A%09static%20void%20init%28unsigned%20gpio%2C%20pin%3Cregisterdevice%3E%3A%3Adir%20dir%2C%20pin%3Cregisterdevice%3E%3A%3Astatus%20status%29%3B%0A%09static%20void%20deinit%28unsigned%20gpio%29%3B%0A%09static%20pin%3Cregisterdevice%3E%3A%3Astatus%20get%28unsigned%20gpio%29%3B%0A%09static%20void%20set%28unsigned%20gpio%2C%20pin%3Cregisterdevice%3E%3A%3Astatus%20status%29%3B%0A%09%0A%7D%3B%0A]&lt;/p&gt;
&lt;p&gt;(I used the registerdevice classname from my original post).&lt;/p&gt;
&lt;pre&gt;typedef dgpio::pin&amp;lt;dgpio::registerdevice&amp;gt; pin;&lt;br /&gt;pin p21 = pin(21);&lt;/pre&gt;
&lt;p&gt;The implementation of the 4 members is responsible to access the underlying mechanism to set direction, set state, read state.&amp;nbsp;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27377&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>