Exploring Edison - Mraa GPIO
Written by Harry Fairhead   
Monday, 13 July 2015
Article Index
Exploring Edison - Mraa GPIO
Phased pulses
Interrupts
Program and Summary

 

The complete program, including debounce, is: 

#include "mraa.h" 
#include <stdio.h>
#include <unistd.h> 
#include <time.h> 

#define BILLION 1000000000L 
struct timespec btime, etime;__time_t i;

void switchPressed(void* pin);
 

int main()
{
 
 
mraa_init();
 
mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_IN);
 mraa_gpio_isr(pin31, MRAA_GPIO_EDGE_BOTH,
                       &switchPressed, pin31);
 for (;;) {}; 
 return MRAA_SUCCESS; 
}

void switchPressed(void* pin) {
 struct timespec ttime;
 clock_gettime(CLOCK_REALTIME, &ttime);
 usleep(500);
 int s = mraa_gpio_read((mraa_gpio_context) pin);
 if (s == 0)
 {
   
btime=ttime; 
 }
 else
 {
   double nseconds = (double)(
    (ttime.tv_sec-btime.tv_sec)*BILLION)+
    (double)(ttime.tv_nsec-btime.tv_nsec ) ;
   printf("time = %f (s)  \n ", nseconds/BILLION);
 }
}

Summary

  1. Intel Edison pin numberings can be confusing. You can mostly ignore Arduino pin numbering when working with the mini-breakout board - use either mraa pin numbers or SYSFS GPIO numbers.
     
  2. To initialize a pin use either:
    mraa_gpio_init (mraa_pin)
    mraa_gpio_init_raw (SYSFS_gpiopin)

  3. Set the direction of the pin with:
    mraa_gpio_dir (pin,dir)

  4. Set the mode if you need to:
    mraa_gpio_mode (pin, mode) 
    but this only works with the Arduino board. The default for the mini board is pullup with a 50K resistor. This cannot be changed using mraa.

  5. For output write a 0 or a 1 using:
    mraa_gpio_write (pin, value)

  6. Mraa uses SYSFS to control the state of a GPIO line. This, and the fact that Linux can suspend the operation of your program at any time, means that you cannot rely on how fast you can change an I/O line. In particular, if you set multiple lines don't expect the change to happen at the same time. 

  7. For input use:
    mraa_gpio_read (pin)

  8. You can either read the state of a GPIO line using a polling loop or you can use an interrupt. 

  9. To associate a function with an interrupt use:
    mraa_gpio_isr (pin,edge,
          pointerToFunction,pointerToArgs)

  10. It is important to know that Edison interrupts are not hardware interrupts but software simulated interrupts. What happens is that when you associate an interrupt function with a pin event a new thread is created which polls the pin until the event occurs when it then calls your function. The interrupt system makes use of SYSFS for all pin interactions. 

  11. Software simulated interrupts have a number of drawbacks. You can only have a single interrupt handler per pin. You cannot change or remove and interrupt handler within an interrupt handler. Most importantly, the interrupt handler thread is scheduled by Linux in the usual way and as such you cannot rely on a speedy or timely response. 

  12. If you need the fastest response time then use a polling loop and some or all of the techniques discussed in the next chapter. 

  

Where Next?

Now you know how to perform basic I/O operations using mraa and this is all you need to know if speed isn't an issue. You can write programs that read data from human interface devices such as switches and you can send data to display devices such as LEDs.

The problems start when you need Edison to change a line's state very quickly or respond to an input change quickly. In this context "quickly" means less than a millisecond and there are lots of tasks that require timings at the microsecond level. 

It is usually said that as the Intel Edison's main Atom CPU is running Linux it isn't capable of such realtime operations. This is not the case. With only a little effort you can improve the speed to the point where you can consider the system to be nearly realtime. This is the topic of the next chapter. 

More Information

The starting point for finding out about all Intel's Internet of Things resources, including Edison, is the Intel IoT Developer Zone.

 

Exploring Edison

ExploringEdison

 

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. 
 

<ASIN:1871962447>

More Information

Intel Edison

Edison Board Installer

Intel IoT Developer Kit

Intel IoT Developer Zone

Intel IoT Support

Sensors for Iot Projects

mrra on GitHub 

Related Articles

Powerful New Features For Edison 

Intel Announces IoT Platform       

 

 

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

 

kotlin book

 

Comments




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

 

 

<ASIN:B00ND1KH42@COM>

<ASIN:B00ND1KNXM>

<ASIN:B00ND1KH10>


 



Last Updated ( Thursday, 26 November 2015 )