Overview
Online Support
FAQ
  Atmel Series  (29) TOP10
  MYS-SAM9X5 (3)
  MYS-SAM9G45 (1)
  MYD-SAM9X5 (7)
  MYD-SAM9X5-V2 (2)
  MYD-JA5D2X (0)
  MYD-SAMA5D3X (11)
  MYD-SAMA5D3X-C (0)
  MYD-JA5D4X (0)
  MYC-SAM9X5 (4)
  MYC-SAM9X5-V2 (1)
  MYC-JA5D2X (0)
  MYC-SAMA5D3X (0)
  MCC-SAMA5D3X-C (0)
  MYC-JA5D4X (0)
  NXP Series  (23) TOP10
  MYS-6ULX (4)
  MYD-Y6ULX (3)
  MYD-Y6ULX-HMI (1)
  MYD-IMX28X (2)
  MYD-LPC435X (2)
  MYD-LPC185X (1)
  MYD-LPC1788 (0)
  MYC-Y6ULX (4)
  MYC-IMX28X (0)
  i.MX 6UL/6ULL (6)
  TI Series  (24) TOP10
  MYD-AM335X (12)
  MYD-AM335X-Y (0)
  MYD-AM335X-J (0)
  MYD-C437X (0)
  MYD-C437X-PRU (5)
  Rico Board (6)
  MYC-AM335X (1)
  MCC-AM335X-Y (0)
  MCC-AM335X-J (0)
  MYC-C437X (0)
  Xilinx Series  (20) TOP10
  Z-turn Board (9)
  Z-turn Lite (2)
  MYD-C7Z010/20 (0)
  MYD-C7Z015 (7)
  MYD-Y7Z010/007S (0)
  MYC-C7Z010/20 (0)
  MYC-C7Z015 (0)
  MYC-Y7Z010/007S (0)
  HMI Solutions  (0) TOP10
  MYD-Y6ULX-CHMI (0)
  MY-EVC5100S-HMI (0)
  Others  (1) TOP10
  MY-WF003U (1)
 
  Home >  Support > TI Series > MYD-AM335X
 
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 */
                        //gpio_set_value(GPIO_LED, 1); 
                    
                           
                    if (fdset->revents & POLLPRI) { 
                        len = read(fdset->fd, buf, MAX_BUF); 
                        printf("\nGPIO %d interrupt occurred\n", gpio);
                        /* when GPIO interrupt occurred, LED turn on */
                        //gpio_set_value(GPIO_LED, 0); 
                    }
                    fflush(stdout);  
                }
                 
                gpio_fd_close(gpio_fd);
                            
    } else if (strcmp(cmd, "out") == 0) {
             
                gpio_set_dir(gpio, OUT);
                         
                  if (argc = 4) {
                          gpio_set_value(gpio, atoi(argv[3]));
                          printf("gpio%d is set to %d\n", gpio, atoi(argv[3]));       
                  }
          } else if (strcmp(cmd, "in") == 0) {
                   
                  gpio_set_dir(gpio, IN);
                  printf("\n");
                   
                  while (1) {
                          gpio_get_value(gpio, &value);
                          printf("\rvalue:%d", value);
                  }
                   
          } else if (strcmp(cmd, "unexport") == 0) {
                  gpio_unexport(gpio);       
          }
           
          else {
                  printf("Usage: %s <gpio-pin> <direction> [value]\n\n", argv[0]);       
        exit(-1);       
          }                   
      
    return 0; 
}


Testing
MYD-IMX28X
Execute below command to configure the PD14 to be interruption function on the development board. Use a  falling edge trigger for this pin, relative information will be printed out when the program detected the falling edge.
1
2
3
4
5
6
7
root@freescale ~$ ./gpio 99 interrupt
 
****************************GPIO interrupt*****************************
 
GPIO 99 interrupt occurred
.......
GPIO 99 interrupt occurred
Execute below command to configure gpio as output and set corresponding values: 
1
2
3
4
root@freescale ~$ ./gpio 99 out 0
gpio99 is set to 0
root@freescale ~$ ./gpio 99 out 1
gpio99 is set to 1
Execute below command to configure gpio as input and get input value:
1
2
3
root@freescale ~$ ./gpio 99 in
 
value:1
Execute below command to export gpio:
1
root@freescale ~$ ./gpio 99 unexport

MYD-AM335X
Execute below command to configure the gpio3_5 pin on the board to be interruption function. Use a falling edge trigger for this pin, it will print out relative information when the program detected the failing edge.
1
2
3
4
5
6
7
root@MYD-AM335X:~# ./gpio 101 interrupt
 
****************************GPIO interrupt*****************************
 
GPIO 101 interrupt occurred
....
GPIO 101 interrupt occurred
Execute below command to configure gpio as output and set corresponding values:
1
2
3
4
root@MYD-AM335X:~# ./gpio 101 out 0
gpio101 is set to 0
root@MYD-AM335X:~# ./gpio 101 out 1
gpio101 is set to 1
Execute below command to configure gpio as input and get input value:
1
2
3
root@MYD-AM335X:~# ./gpio 101 in  
 
value:0
Execute below command to export gpio:
1
root@MYD-AM335X:~# ./gpio 101 unexport