0

I need a simple program that will write to gpio. I can't find one anywhere. The example in the mmra documentation does not work. I picked gpio14 because the Sprakfun example that writes to this pin using system calls works just fine. But my program does not work.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mraa.h>
#include <math.h>
#include <mraa/gpio.h>

int main(int argc, char **argv)
{

mraa_gpio_context gpio;

gpio = mraa_gpio_init(14);  <--- to get gpio14 to toggle change this to 36
mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
int value = 0;
for (;;) {
    if(value == 0)value = 1;
    else value = 0;
    mraa_gpio_write(gpio,value);
    printf("output is %d\n",value);
    sleep(1);
}
mraa_gpio_close(gpio);
return 0;
}

The loop runs and prints out output is 1 then output is 0. I have an oscilloscope on the pin and it stays low.

Using this Sparkfun tutorial I can move the pin high so I know my setup is correct. If someone can just give me an example of code that works with all the includes and such that would be very helpful.

Allen Edwards
  • 1,488
  • 1
  • 27
  • 44
  • What? You have an example that works? Why can you not convert one into the other, line-by-line, and so find out what is going wrong? – Martin James Feb 22 '15 at 00:14
  • @MartinJames: this "example" is actually uses sysfs access to GPIO, just via shell. He needs example written in C – Sam Protsenko Feb 22 '15 at 00:19
  • Just google for a datasheet and do what it says – Simon Feb 22 '15 at 00:20
  • @Simon: datasheet has nothing to do with this issue. He says that GPIO works when accessing via sysfs. Means GPIO driver works just fine. He has issues with "mraa/gpio.h" framework. – Sam Protsenko Feb 22 '15 at 00:23
  • he probably failed at setting up the gpio's, in the datasheet youll find how to. Thats what I'd do. – Simon Feb 22 '15 at 00:25
  • @Allen, if you are able to use GPIO via sysfs, it means you can just open the same sysfs file in your C application and read/write to it, just like you did from shell. Your MRAA framework do it for you: https://github.com/intel-iot-devkit/mraa/blob/master/src/gpio/gpio.c#L127 . So it may be some error in that framework, you can try to get rid of it and write your own code that will do just the same you did from shell (using tutorial). Another option is to write kernel module, it would be very easy to do what you want: see http://lwn.net/Articles/532714/ – Sam Protsenko Feb 22 '15 at 00:32
  • @Simon: he managed to setup and to use the same GPIO via sysfs. Now he is doing it via some MRAA framework in C code, and that framework does just the same: it opens sysfs files and writes to them. His code has the same sequence as he did it via sysfs. So either MRAA framework is broken in some way, or this framework requires some additional (or different) code. I think datasheet is not gonna help here, because everything you can get from datasheet is already implemented in GPIO driver (in Linux kernel) for his platform. – Sam Protsenko Feb 22 '15 at 00:52
  • Do you run your program as regular user or as root? – Sam Protsenko Feb 22 '15 at 00:54
  • I run the program as root. I should also say that I successfully run a program that uses i2c and the UART. – Allen Edwards Feb 22 '15 at 01:08
  • I have looked at the the links and basically you initialize, set the direction, and set the value. I am doing all of these. In addition, I used as a template the example in the official documentation found here http://iotdk.intel.com/docs/master/mraa/gpio_8h.html and changed the direction and made it write instead of read. I just need someone who has done this to tell me what I left out. – Allen Edwards Feb 22 '15 at 01:26

1 Answers1

0

It turns out that the code I posted is correct and works. The problem is that gpio14 is mraa 36 so to toggle gpio14, I need to change the number in init to 36. Here is the defining document.

mraa decoder ring

Allen Edwards
  • 1,488
  • 1
  • 27
  • 44