This is a comprehensive and easy to read example C code designed to work alone or included as a library for dealing with general purpose I/O via the sysfs interface in Linux. It can easily be applied to any computer which utilizes the GPIO sysfs interface (both pro and maker boards). Getting started is a breeze. You just need to decide if you’re going to use it as a library, including it into your existing code, or as a stand-alone utility. We’ll talk about both in this article, but first and foremost, here’s the source code we’ll be working with:
Let’s take a quick look at what it takes to read from the ADC inputs of a i.MX28 based embedded system using example C code provided by Technologic Systems. Now, while this can be generically applied to many i.MX28 based embedded systems, we’ll be working with a TS-7680. Right, let’s get started!
The purpose of this example script, which we call grow-sd , is to demonstrate a use case for using sfdisk, or what I like to call ‘scripted fdisk’, to modify a partition on an SD card provided by Technologic Systems. This is useful when your production SD card image, originally copied from a 512 MB SD card, is being written to a much larger capacity SD card, and you want the additional storage space. Keep in mind, the script itself is specific to Technologic Systems’ embedded boards, and only applies to those images which are shipped with four partitions (ie. TS-7350). However, this script also generically demonstrates and example usage of sfdisk . We thought it’d be helpful to publish in hopes it would be useful. Feel free to modify it to fit your requirements.
Here’s an example program our engineers might find useful. Kris Bahnsen, a long time engineer for Technologic Systems, wrote this simple program to get the voltage input (Vin) on the 8 – 28 VDC power rail on the TS-7670 (Rev. D or later) or TS-7400-V2 (Rev. B or later). Without going into too much detail about implementation of the on-board supervisory microcontroller, there is a register which is used to store various ADC values, including Vin. This example program basically polls this 4 byte register via I2C interface, accounts for the voltage divider (see TS-7670 schematic or TS-7400-V2 schematic), and spits out the Vin value. So, without further ado, here’s the code:
Let’s take a quick look at an example C++ program which reads CPU temperature and controls an LED using sysfs. This example is a bit specific in that it’s only been tested on our NXP i.MX6 powered TS-4900 or TS-7970 running Yocto Linux, but the principles could be applied to other embedded systems as well. If you’re interested in the nitty gritty details about sysfs, take a look at The sysfs Filesystem by Patrick Mochel. Suffice it to say for our purposes, sysfs makes it easy for us to interact with system hardware using plain text files located in the /sys/ directory. The file to control the red LED is /sys/class/leds/red-led/brightness. The file to read the CPU temperature is /sys/class/thermal/thermal_zone0/temp. If we want to turn the red LED on, we simply write a ‘1’ to the file, and not surprisingly, writing a ‘0’ will turn it off. If you’ve booted up your TS-4900 or TS-7970, you can see this by running the shell commands:
Here’s an example program our engineers might find useful. Jesse Off, our lead engineer, wrote this simple program to get the voltage input (Vin) on the 8 – 28 VDC power rail on the TS-7250-V2 (Rev. B only). Without going into too much detail about implementation of the SiLabs microcontroller, there is a register which is used to store various ADC values, including Vin. This example program basically polls this 19 byte register via I2C interface, accounts for the voltage divider (see TS-7250-V2 schematic), and spits out the Vin value. So, without further ado, here’s the code: