Exploring Edison - Beyond mraa
Written by Harry Fairhead   
Monday, 02 May 2016
Article Index
Exploring Edison - Beyond mraa
Controlling the GPIO Mode
Setting the Mode

Setting the Mode

We can use the  sys/kernel/debug/gpio_debug file system to set the mode. First we need some general function that read and write to the file system.

First we need a function that will read the current property of any pin: 

void getSetting(int pin,char prop[],char* buf){
 sprintf(buf, "/sys/kernel/debug/gpio_debug/

 int fd = open(buf, O_RDONLY);
 int count=read(fd, buf, 10);

Notice that the property is specified by prop[] and the result returned in buf[].


Similarly we need a function that will write a new value to any current property for the specified pin:

void putSetting(int pin,char prop[],
                                char value[]){

 char buf[200];
 sprintf(buf, "/sys/kernel/debug/gpio_debug/

 int fd = open(buf, O_WRONLY);
 write(fd, value, strlen(value));

Using the getSetting function it is now easy to write a function that will print the current mode status to the console;

void getMode(int pin){
 char buf[200];

 printf("pullmode= %s \n",buf);

 printf("pullstrength= %s \n",buf);

 printf("opendrain= %s \n",buf);


This is useful when you are debugging to see what mode is set and to make sure that mraa isn't changing it while you are not looking. 

The problem is that if you use mraa to set a pin to high or low then the use of Sysfs will reset the mode. The solution to this problem is to use memory mapped access which mraa supports and doesn't use Sysfs. 

For example:

int gpio = 44;

mraa_gpio_context pin = mraa_gpio_init(31);
mraa_gpio_dir(pin, MRAA_GPIO_OUT);
mraa_gpio_use_mmaped(pin, 1);

mraa_gpio_write(pin, 1);
mraa_gpio_write(pin, 0);


Recall that GPIO-44 is the same as mraa pin 31 so we are working with the same pin in all of the code. First we set the pin to output and memory mapped access using mraa functions. Next we set the output mode to 50K pullup and enable the opendrain mode. You can see from the output that the pin is written to and the set mode doesn't change. If you change use_mmaped to 

 mraa_gpio_use_mmaped(pin, 1);

then you will find that the mode changes between setting the pin high and low. 

You can check that the mode really has changed by connecting a multimeter to the pin and measuring its voltage.

In pushpull mode, i.e. with 


the output voltage will be a little over 1.8V, but with it enabled it drops to just over 1.7V. You can also put a 10K resistor from the pin to ground and you will discover that the voltage drops to around 2.7, which is correct for a voltage divider consisting of a 50K pullup and a 10K load resistor.

All of the pullstrength resistors, work but something strange happens if you specify 910ohms - the output voltage drops.

You can also try making pulldown work - it doesn't seem to. However, setting pullmode to nopull gives you the open drain output line without a pullup resistor. This allows you to add an external pull up resistor of your own choice between the output line and the 1.8V line.

To summarize:

You can work with two modes:

  • If current_opendrain is set to disable then you have a pushpull output stage - this is the default for all output pins.

  • if  current_opendrain is set to enable then you can set pullmode to pullup with built in resistors of 50K, 20K and 2K or you can set pullup pullmode to nopull and use your own external pull up resistors. 

There may well be other things that are useful in Sysfs and in /sys/kernel/debug/gpio_debug/gpio, but there is currently no documentation for it. 




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

  1. 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. 

  2. 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. 

  3. 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. 

  4. 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.

  5. 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.

  6. 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. 

  7. 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. 

  8. Sophisticated GPIO -  I2C 
    I2C is a simple communications bus that allows you to connect any of a very large range of sensors. 

  9. I2C - Measuring Temperature  
    After looking at the theory of using I2C here is a complete case study using the SparkFun HTU21D hardware and software. 
  10. 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.

  11. 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. 

  12. 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. 

  13. 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. 

  14. 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.

  15. 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. 



To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin. 


raspberry pi books



or email your comment to: comments@i-programmer.info



Last Updated ( Sunday, 08 May 2016 )