参考文档:
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
|