[vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Introduce” tab_id=”1394070611-1-58″][vc_column_text]
Now I operate pcduino basically through arduinoIDE.
For I’m not particularly understand the Linux application programming,
but I’m very curious about it, and want to put arduinoIDE aside to operating hardware,
after a period of time I finally find out the way .
So I build a project and write a simple test code.
[/vc_column_text][/vc_tab][vc_tab title=”Code instructions” tab_id=”1394070611-2-53″][vc_column_text][/vc_column_text][vc_column_text]
voidGpoiInitial(int*fd_pin,int*fd_mode,intpinNum,intmode);
This function is used to initialize the GPIO, the first two are the two file object point,
once initialize well , we could operate the corresponding GPIO through the pointer.
PinNum need the specific GPIO number, it was the same with arduino, mode has the
OUTPUT and INPUT mode.
intSetGpio(intfd,bool High);
This function is used to set the GPIO state, fd is initial file pointer,
H is referred to whether set the GPIO to high level, set high level for the truth,
and set low level for the false.
Makefile is a common makefile, and you can use it to manage other project.
[/vc_column_text][/vc_tab][vc_tab title=”Test code” tab_id=”1394128685441-2-1″][vc_column_text]
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include"delay.h"
#include"typedef.h"
#define TEST_PIN_NUM 2
int WriteToGpioFile(int fd,char* buf,int len)
{
lseek(fd,0,SEEK_SET);
return write(fd,buf,len);
}
void GpoiInitial(int *fd_pin,int *fd_mode,int pinNum,int mode)
{
char *Str;
Str = (char*)malloc(sizeof("/sys/devices/virtual/misc/gpio/mode/gpio1"));
sprintf(Str, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, pinNum);
*fd_pin = open(Str,O_RDWR);
if(*fd_pin < 0)
{
printf("Faild to open gpio pin%dn",pinNum);
free(Str);
return;
}
sprintf(Str, "%s%s%d", GPIO_MODE_DIR, GPIO_IF_PREFIX, pinNum);
*fd_mode = open(Str,O_RDWR);
if(*fd_mode < 0)
{
printf("Failed to open gpio mode%dn",pinNum);
free(Str);
return;
}
if(0>WriteToGpioFile(*fd_mode,(char*)((mode & 1)?"1":"0"),sizeof("1")))
{
printf("Write to gpio mode failedn");
free(Str);
return;
}
printf("gpio initial successn");
free(Str);
}
int SetGpio(int fd,BOOL High)
{
if(0>WriteToGpioFile(fd,(char*)(High ? "1" : "0" ),sizeof("x")))
{
printf("Write to gpio pin failedn");
return -1;
}
return 0;
}
int main(int arg)
{
int fd_pin_led;
int fd_mode_led;
BOOL led_on;
led_on = FALSE;
GpoiInitial(&fd_pin_led,&fd_mode_led,TEST_PIN_NUM,OUTPUT);
while(1)
{
delay_ms(500);
led_on = !led_on;
SetGpio(fd_pin_led,led_on);
}
return 1;
}
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Leave a Reply
You must be logged in to post a comment.