版主: wanchong , wangyu , eepwwp , firedom

分享到:
共9条 1/1 1   

Linux native applications - config as GPIO output to drive a led

    [您是本帖的第1740位阅读者]
100201452
我是MM
普通会员

最后登陆时间:2015-01-13 22:06:31

直达楼层
1# 发表于 2015-04-14 22:46:46
参考文档:
    Intel Edison Native Applicaton Guide  
    Intel Edison Kit for Arduino
根据文档Intel Edison Native Application Guide,宿主机是ubuntu 12.04, 编写一个简单的Linux native applicaton,即将一个external IO5配置为GPIO output high来驱动一个led,使它亮。
直接上代码, C语言代码在宿主机ubuntu12.04上,需要先在宿主机上编译成可执行的目标文件,然后通过usb传到Edison板上,运行即可。
xiaoy@xiaoy-ProLiant:~/sample$ vim led_blink.c

/* A Sample Program to set GPIO pin 5 direction OUT and value as HIGH
**
** Author: xiaoyang
**
*/

#include <stdio.h>
#include <fcntl.h>
#define GPIO_DIRECTION_PATH "/sys/class/gpio/gpio%d/direction"
#define GPIO_VALUE_PATH "/sys/class/gpio/gpio%d/value"
#define GPIO_EXPORT_PATH "/sys/class/gpio/export"
#define PINMUX_DIRECTION_PATH "/sys/kernel/debug/gpio_debug/gpio%d/current_pinmux"
#define PLUP_DIRECTION_PATH "/sys/class/gpio/gpio%d/direction"
#define TRIG_ALL_DIRECTION_PATH "/sys/class/gpio/gpio%d/direction"
#define BUFOUT_DIRECTION_PATH "/sys/class/gpio/gpio%d/direction"
#define BUFFER 60

int main()
{
//GPIO Pin 13
        int gpio_pin = 13;
        int bufout_pin = 253;
        int plup_pin = 221;
//Path Variables
        char gpio_exp_path[BUFFER];
        char gpio_direction_path[BUFFER];
        char gpio_value_path[BUFFER];
        char pinmux_direction_path[BUFFER];
        char bufout_direction_path[BUFFER];
        char plup_direction_path[BUFFER];
        char trig_all_direction_path[BUFFER];
//Files
        int fd_export, fd_val, fd_dir, fd_mux;
        int err = 0;
//Set GPIO Paths
        snprintf(gpio_exp_path, BUFFER, GPIO_EXPORT_PATH, gpio_pin); //
        snprintf(gpio_direction_path, BUFFER, GPIO_DIRECTION_PATH, gpio_pin);
        snprintf(gpio_value_path, BUFFER, GPIO_VALUE_PATH, gpio_pin);
        snprintf(pinmux_direction_path, BUFFER, PINMUX_DIRECTION_PATH, gpio_pin);
        snprintf(bufout_direction_path, BUFFER, BUFOUT_DIRECTION_PATH, bufout_pin);
        snprintf(plup_direction_path, BUFFER, PLUP_DIRECTION_PATH, plup_pin);
        snprintf(trig_all_direction_path, BUFFER, TRIG_ALL_DIRECTION_PATH, trig_all_pin);
// Export GPIO
        fd_export = open(gpio_exp_path, O_WRONLY);
        if(fd_export < 0){
                perror("Can't Open Export File");
                return -1;
        }else
        {
                char buf[15];
                sprintf(buf,"%d",gpio_pin);
                write(fd_export, buf, sizeof(buf));
                sprintf(buf,"%d",bufout_pin);
                write(fd_export, buf, sizeof(buf));
                sprintf(buf,"%d",plup_pin);
                write(fd_export, buf, sizeof(buf));
                sprintf(buf,"%d",trig_all_pin);
                write(fd_export, buf, sizeof(buf));
                close(fd_export);
        }
//Set Direction
        fd_dir = open(trig_all_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open TRIG_ALL Direction File");
        }else{
                write(fd_dir,"low", sizeof("low"));
                printf("Set trig_all_direction %d value as LOW\n", trig_all_pin);
                close(fd_dir);
        }
        fd_dir = open(bufout_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open BUFOUT Direction File");
        }else{
                write(fd_dir,"high", sizeof("high"));
                printf("Set bufout_direction %d value as High\n", bufout_pin);
                close(fd_dir);
        }
        fd_dir = open(plup_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open PLUP Direction File");
        }else{
                write(fd_dir,"in", sizeof("in"));
                printf("Set plup_direction %d value as in\n", plup_pin);
                close(fd_dir);
        }

//Set mux
        fd_mux = open(pinmux_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open PINMUX Direction File");
        }else{
                write(fd_mux,"mode0", sizeof("mode0"));
                printf("Set pinmux_direction value as mode0, output.\n");
                close(fd_mux);
        }
//Set direction
        fd_dir = open(gpio_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open GPIO Direction File");
        }else{
                write(fd_dir,"out", sizeof("out"));
                printf("Set gpio_direction %d value as out\n", gpio_pin);
                close(fd_dir);
        }

        fd_dir = open(trig_all_direction_path, O_WRONLY);
        if(fd_dir < 0){
                perror("Can't Open TRIG_ALL Direction File");
        }else{
                write(fd_dir,"high", sizeof("high"));
                printf("Set trig_all_direction %d value as High\n", trig_all_pin);
                close(fd_dir);
        }
//Set value
        fd_val = open(gpio_value_path, O_WRONLY);
        if (fd_val < 0) {
                perror("Can't Open GPIO Value File");
                return -1;
        }else{
                write(fd_val, "1", sizeof("1"));
                printf("Set gpio %d value as HIGH\n", gpio_pin);
                close(fd_val);
        }
// Close the Files
        return 0;

}
/** End of GPIO Program **/
xiaoy@xiaoy-ProLiant:~/sample$ source /opt/poky-edison/1.6.1/environment-setup-core2-32-poky-linux
xiaoy@xiaoy-ProLiant:~/sample$ $CC -o  led_blink1 led_blink.c

xiaoy@xiaoy-ProLiant:~/sample$ scp led_blink1 root@192.168.2.15:/home/root/sample
root@xiaoy_eds:~/sample#./led_blink1
就可以看到led亮起来了。

附录:a Ubuntu host to compile .c files, copy executable files to Edison board to run 
Download the Edison Yocto image, & flashing Intel Edison

http://www.intel.com/support/maker/edison.htm

http://www.intel.com/support/edison/sb/CS-035280.htm


Intel Edison Application Guide

2 Native Application Development

On the host:

    Install the required packages

        sudo apt-get install make automake gcc g++ build-essential gcc-multilib

    Set up pocky and install the Intel Edison toolchain

        source /opt/poky-edison/1.6/environment-setup-core2-32-poky-linux


https://software.intel.com/iot/getting-started

Step3 get your board online -> Via Ethernet

https://software.intel.com/en-us/articles/intel-edison-connecting-ethernet-over-usb#linux

Use Terminal and ifconfig to forward connections to IP address 192.168.2.2

    $sudo ifconfig usb0 192.168.2.2
    $ifdown usb0

    $ifup usb0


  $scp hello_cpp root@192.168.2.15:/home/root/sample

RE:by zpeng

changyan
我是MM
普通会员

最后登陆时间:2015-01-14 10:49:00

2# 发表于 2015-04-16 09:30:59
大神啊!

RE:by zpeng

wuchangjing
我是MM
普通会员

最后登陆时间:2015-01-14 11:05:15

3# 发表于 2015-04-17 08:37:56
我说看不懂你会打我吗?

RE:by zpeng

luodingyue
我是MM
普通会员

最后登陆时间:2015-01-14 11:06:58

4# 发表于 2015-04-17 11:58:14
顶大神

RE:by zpeng

springrose
我是MM
普通会员

最后登陆时间:2015-01-14 10:51:01

5# 发表于 2015-04-20 13:56:45
我说看不懂你会打我吗?

RE:by zpeng

handys
我是MM
普通会员

最后登陆时间:2015-01-14 11:05:35

6# 发表于 2015-04-23 12:51:45
来顶一个 准备入手一个。。

RE:by zpeng

miaojinjie
我是MM
普通会员

最后登陆时间:2015-01-14 11:04:28

7# 发表于 2015-04-27 20:41:35
机械工程小白默默路过。

RE:by zpeng

mengkecoo
我是MM
普通会员

最后登陆时间:2015-01-14 11:08:14

8# 发表于 2015-04-30 07:16:02
继续更啊

RE:by zpeng

weihonlei
我是MM
普通会员

最后登陆时间:2015-01-14 10:40:01

9# 发表于 2015-05-11 19:05:29
继续更啊
共9条 1/1 1   
快速回复主题
  • 匿名不能发帖!请先 [ 登陆 注册 ]