Page 1 of 3 There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the mraa library. Sometimes you need this because you are working in a language for which mraa isn't available. It also lets you access features that mraa doesn't make available.
This is a chapter from our ebook on the Intel Edison. The full contents can be seen below. Notice this is a first draft and a work in progress. Use the comments or email harry.fairhead@i-programmer.info with your queries or suggestions.
Now On Sale!
You can now buy a print edition of Exploring Intel Edison.
You can buy it from:
USA and World Amazon.com Canada Amazon.ca UK Amazon.co.uk France Amazon.fr Germany Amazon.de Spain Amazon.es Brazil Amazon.br Italy Amazon.it Japan Amazon.co.jp Mexico Amazon.com.mx
Chapter List
-
Meet Edison In this chapter we consider the Edison's pros and cons and get an overview of its structure and the ways in which you can make use of it. If you have ever wondered if you need an Edison or an Arduino or even a Raspberry Pi then this is the place to start.
- First Contact
When you are prototyping with the Edison you are going to need to use one of the two main breakout boards - the Arduino or the mini. This chapter explains how to set up the Edison for both configurations.
- In C
You can program the Edison in Python, JavaScript or C/C+ but there are big advantages in choosing C. It is fast, almost as easy as the other languages and gives you direct access to everything. It is worth the effort and in this chapter we show you how to set up the IDE and get coding.
- Mraa GPIO
Using the mraa library is the direct way to work with the GPIO lines and you have to master it. Output is easy but you do need to be aware of how long everything takes. Input is also easy but using it can be more difficult. You can use polling or the Edison interrupt system which might not work exactly as you would expect.
- Fast Memory Mapped I/O
There is a faster way to work with GPIO lines - memory mapped I/O. Using this it is possible to generate pulses as short at 0.25 microsecond and read pulse widths of 5 microseconds. However getting things right can be tricky. We look at how to generate fast accurate pulses of a given width and how to measure pulse widths.
- Near Realtime Linux
You need to be aware how running your programs under a non-realtime operating system like Yocto Linux effects timings and how accurately you can create pulse trains and react to the outside world. In this chapter we look the realtime facilities in every version of Linux.
- Sophisticated GPIO - Pulse Width Modulation
Using the PWM mode of the GPIO lines is often the best way of solving control problems. PWM means you can dim an LED or position a servo and all using mraa.
- Sophisticated GPIO - I2C
I2C is a simple communications bus that allows you to connect any of a very large range of sensors.
- I2C - Measuring Temperature
After looking at the theory of using I2C here is a complete case study using the SparkFun HTU21D hardware and software.
- Life At 1.8V
How to convert a 1.8V input or output to work with 5V or 3.3V including how to deal with bidirectional pull-up buses.
- Using the DHT11/22 Temperature Humidity Sensor at 1.8V
In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in interfacing two logic families and implementing a protocol directly in C.
-
The DS18B20 1-Wire Temperature The Edison doesn't have built in support for the Maxim 1-Wire bus and this means you can't use the very popular DS18B20 temperature sensor. However with a little careful planning you can and you can do it from user rather than kernel space.
- Using the SPI Bus
The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so, if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are.
- SPI in Practice The MCP3008 AtoD
The SPI bus can be difficult to make work at first, but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12-bit AtoD using the MCP3008.
- Beyond mraa - Controlling the features mraa doesn't.
There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the mraa library. Sometimes you need this because you are working in a language for which mraa isn't available. It also lets you access features that mraa doesn't make available.
<ASIN:1871962447>
A key principle of Linux is that everything is a file or a folder. As much as is possible Linux deals with external hardware by treating it as if it was a file system. This is reasonable because external hardware either wants to receive data as commands or something to store or display or it want to send data as responses or user input. So most hardware interacts with Linux as a source or a sink of data and this is exactly what a file is all about.
This "everything is a file" approach only really fails when issues of performance enter the picture. Accessing a piece of hardware as if it was a file when it isn't can be slow. In normal use mraa accesses most of the hardware of the Edison using file commands, but when you use fast memory access it maps the hardware into user space and accesses it as a range of memory locations.
So file-based access to the hardware can be slow, but it has the huge advantage that it is language-independent. Every language has the facilities needed to open, read/write and close a file and so has the facilities needed to work with hardware via the file system
The big problem is that the details of how hardware is represented as a file system is poorly documented and you have to find out about it by guessing, trial and error, reverse engineering, or by reading code the makes use of it.
Working with Sysfs
Sysfs is a virtual file system that provides all sorts of access to hardware and the operation of the Linux kernel. You can spend a lot of time exploring Sysfs, but the only part we are particularly interested in is the gpio folder. Sysfs is usually mounted in the sys folder and the folder that corresponds to the gpio device is usually:
/sys/class/gpio
To see what is in the folder, simply list it:
ls /sys/class/gpio
These are the gpio lines that are already in use by some process or other. Notice that the gpio numbers are not mraa numbers, but SYSFS numbers.
The steps in using a line are always the same:
- Reserve or "export" the gpio line so that no other process can use it
- Set its direction and read or write it
- Unreserve it or unexport it
You can do these steps from any language that supports file operations including the shell.
To reserve a gpio line you have to write its number to the export folder and you can do this using the shell command. For example, assuming we want to work with gpio-44:
echo 44 > /sys/class/gpio/export
You can of course change 44 to any valid gpio number.
You can do the same job in C:
gpio=44; fd = open("/sys/class/gpio/export", O_WRONLY);
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
If you are not familiar with C file operations - the open function opens export for write only, the sprintf command creates a string with the number of the gpio line and then this is written to the file before it is closed.
Once you have the pin reserved you will see a new folder gpio44 corresponding to it in /sys/class/gpio.
Now that you have it reserved, you can set its direction and read or write it. To do this read or write to the appropriate sub folder of the new gpio folder, direction or value.
For example, to read the line use:
echo "in" > /sys/class/gpio/gpio44/direction
cat /sys/class/gpio/gpio44/value
and to set the line high and then low:
echo "out" > /sys/class/gpio/gpio44/direction
echo 1 > /sys/class/gpio/gpio44/value
echo 0 > /sys/class/gpio/gpio4/value
You can do the same things using C but it is slightly more verbose due to the need to open and close files and build the appropriate strings.
|