Shopping Cart | 中文网站
 
 
Overview
Online Support
FAQ
  Atmel Boards  (37) TOP10
  MYD-SAM9X5 (9)
  MYS-SAM9X5 (2)
  MYS-SAM9G45 (1)
  MYD-SAMA5D3X (14)
  MYD-SAM9X5-V2 (2)
  MYC-SAM9X5 (4)
  MYC-SAM9X5-V2 (1)
  MYC-SAMA5D3X (1)
  MYD-JA5D4X (3)
  NXP Boards  (6) TOP10
  MYD-LPC435X (2)
  MYD-LPC185X (1)
  MYD-LPC1788 (0)
  MYS-6ULX (3)
  Samsung Boards  (0) TOP10
  TI Boards  (21) TOP10
  MYD-AM335X (9)
  MYC-AM335X (1)
  Rico Board (6)
  MYD-C437X-PRU (5)
  Other  (3) TOP10
  MY-WF003U (1)
  Freescale Boards  (2) TOP10
  MYD-IMX28X (2)
  Xilinx Boards  (25) TOP10
  Z-turn Board (10)
  MYD-C7Z010/20 (7)
  MYD-C7Z015 (7)
 
  Home >  Support > Other
 
ARM Linux GPIO sample code
 
Question
Please download the sample code package from below link: http://www.myirtech.com/download/code/gpio.tar.gz (include source code, Makefile and image which is compiled using arm-none-linux-gnueabi-gcc)
Answer

Source code


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/********************************************************************
*                 copyright (C) 2014 all rights reserved
*                         @file: gpio.c
*                   @Created: 2014-8-6 15:00
*                      @Author: conway chen
*           @Description: test gpios interrupt
*          @Modify Date: 2014-8-6 15:00
*********************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include <poll.h>
 
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define GPIO_LED 41
#define MAX_BUF 60
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define OUT 1
#define IN 0
 
/**
 * brief: export the GPIO to user space
 * @Param: gpio: the GPIO number
 */
int gpio_export(unsigned int gpio)
{
        int fd ,len;
        char buf[MAX_BUF];
 
        fd = open(SYSFS_GPIO_DIR "/export" ,O_WRONLY);
        if (fd < 0) {
                perror("gpio/export");
                return fd;
        }
        len = snprintf(buf ,sizeof(buf) ,"%d" ,gpio);
        write(fd ,buf ,len);
        close(fd);
        return 0;
}
 
/**
 * brief: unexport the GPIO to user space
 * @Param: gpio: the GPIO number
 */ 
int gpio_unexport(unsigned int gpio)
{
        int fd ,len;
        char buf[MAX_BUF];
 
        fd = open(SYSFS_GPIO_DIR "/unexport" ,O_WRONLY);
        if (fd < 0) {
                perror("gpio/unexport");
                return fd;
        }
        len = snprintf(buf ,sizeof(buf) ,"%d" ,gpio);
        write(fd ,buf ,len);
        close(fd);
        return 0;
}
 
/**
 * brief: configure GPIO for input or output
 * @Param: gpio: the GPIO number
 * @Param: out_flag: the flag of output or input.It's value can be 1 or 0. 
 */
int gpio_set_dir(unsigned int gpio ,int out_flag)
{
        int fd ,len;
        char buf[MAX_BUF];
 
        len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
        fd = open(buf ,O_WRONLY);
        if (fd < 0) {
                perror(buf);
                return fd;
        }
        if (out_flag)
                write(fd ,"out" ,4);
        else
                write(fd ,"in" ,3);
        close(fd);
        return 0;
}
 
/**
 * brief: Set the value of GPIO
 * @Param: gpio: the GPIO number
 * @Param: value: the value of GPIO. Supports values of 0 and 1.
 */ 
int gpio_set_value(unsigned int gpio, unsigned int value) 
    int fd, len; 
    char buf[MAX_BUF]; 
    
    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);     
    fd = open(buf, O_WRONLY);
        if (fd < 0) { 
        perror("gpio/set-value"); 
        return fd; 
    
    
    if (value) 
        write(fd, "1", 2); 
    else 
        write(fd, "0", 2); 
    
    close(fd); 
    return 0; 
}
 
/**
 * brief: get the value of GPIO
 * @Param: gpio: the GPIO number
 * @Param: value: pointer to the value of GPIO
 */
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
        int fd, len;
        char ch;
        char buf[MAX_BUF];
 
        len = snprintf(buf ,sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value" ,gpio);
        fd = open(buf ,O_RDONLY);
        if (fd < 0) {
                perror("gpio_get_value");
                return fd;
        }
        read(fd ,&ch ,1);
        if (ch == '1')
                *value = 1;
        else if(ch == '0')
                        *value = 0;
        close(fd);
        return 0;
}
 
/**
 * brief: set the edge that trigger interrupt
 * @Param: gpio: the GPIO number
 * @Param: edge:  edge that trigger interrupt
 */
int gpio_set_edge(unsigned int gpio ,char *edge)
{
        int fd ,len;
        char buf[MAX_BUF];
 
        len = snprintf(buf ,sizeof(buf) ,SYSFS_GPIO_DIR "/gpio%d/edge" ,gpio);
        fd = open(buf ,O_WRONLY);
        if (fd < 0) {
                perror("gpio_set_edge");
                return fd;
        }
        write(fd ,edge ,strlen(edge) + 1);
        close(fd);
        return 0;
}
 
/**
 * brief: open gpio device and return the file descriptor
 * @Param: gpio: the GPIO number
 */ 
int gpio_fd_open(unsigned int gpio) 
    int fd, len; 
    char buf[MAX_BUF]; 
   
    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); 
    
    fd = open(buf, O_RDONLY | O_NONBLOCK ); 
    if (fd < 0) { 
        perror("gpio/fd_open"); 
    
    return fd; 
   
/**
 * brief: close gpio device
 * @Param: fd: the file descriptor of gpio
 */
int gpio_fd_close(int fd) 
    return close(fd); 
   
/**
 * @brief: main function
 * @Param: argc: number of parameters
 * @Param: argv: parameters list
 */ 
int main(int argc, char **argv) 
    struct pollfd *fdset; 
    int nfds = 1; 
    int gpio_fd, timeout, rc; 
    char *buf[MAX_BUF]; 
    unsigned int gpio; 
    int len;
    char *cmd;
    unsigned int value;
       
          fdset = (struct pollfd*)malloc(sizeof(struct pollfd)); 
   
    if (argc < 3) { 
        printf("Usage: %s <gpio-pin> <direction> [value]\n\n", argv[0]);       
        exit(-1); 
    }
    cmd = argv[2];
    gpio = atoi(argv[1]);
        gpio_export(gpio);
         
    if (strcmp(cmd, "interrupt") == 0) {
            printf("\n**************************GPIO interrupt***************************\n");
                   
                gpio_set_dir(gpio, IN); 
                gpio_set_edge(gpio, "rising"); 
                gpio_fd = gpio_fd_open(gpio);
                 
                /* GPIO_LED configure */
                //gpio_export(GPIO_LED); 
                //gpio_set_dir(GPIO_LED, OUT);
                 
                timeout = POLL_TIMEOUT; 
            
                while (1) { 
                    memset((void*)fdset, 0, sizeof(fdset));
                    fdset->fd = gpio_fd; 
                    fdset->events = POLLPRI; 
           
                    rc = poll(fdset, nfds, timeout);       
           
                    if (rc < 0) { 
                        printf("\npoll() failed!\n"); 
                        return -1; 
                    
                     
                    if (rc == 0) { 
                        printf(".");
                        /* LED off */