IMX8M GPIO direction

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

IMX8M GPIO direction

1,203 Views
gaobo
Contributor I

BSP:4.14.78

Board:IMX8mq-evk

我通过软件无法改变gpio109的方向。首先我在设备树里把SAI1_TXD1复用成了GPIO4_IO13,然后通过软件操作gpio的方向寄存器和数据寄存器。当通过sys把gpio设置成输出方向,可以改变gpio的高低电平。当通过软件时就无法改变gpio的方向。

设备树的配置如下:

添加了MX8MQ_IOMUXC_SAI1_TXD1_GPIO4_IO13,注释了MX8MQ_IOMUXC_SAI1_TXD1_SAI1_TX_DATA1

pastedImage_1.png

pastedImage_2.png

程序运行结果,刚开始未设置gpio寄存器,test为软件的名称

root@imx8mqevk:/home/iqi# cat /sys/class/gpio/gpio109/value
0
root@imx8mqevk:/home/iqi# cat /sys/class/gpio/gpio109/direction
in
root@imx8mqevk:/home/iqi# ./test
input start
output stop
root@imx8mqevk:/home/iqi# cat /sys/class/gpio/gpio109/direction
in
root@imx8mqevk:/home/iqi# cat /sys/class/gpio/gpio109/value
0
root@imx8mqevk:/home/iqi# echo output > /sys/class/gpio/gpio109/direction
bash: echo: write error: Invalid argument
root@imx8mqevk:/home/iqi# echo out > /sys/class/gpio/gpio109/direction
root@imx8mqevk:/home/iqi# ./test
input start
output stop
root@imx8mqevk:/home/iqi# cat /sys/class/gpio/gpio109/value
1

代码工程如下:

1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6 #include <poll.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <time.h>
11 #include <fcntl.h>
12 #include <pthread.h>
13 #include <sys/time.h>
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <sys/ioctl.h>
18 #include <asm/ioctl.h>
19
20
21 #define BLOCK_SIZE (4*1024)

22 static volatile unsigned int GPIO4_DR ;
23 static volatile unsigned int GPIO4_GDIR ;
24 static volatile unsigned int *gpio4_dr ;
25 static volatile unsigned int *gpio4_gdir ;
26 #define INPUT 0
27 #define OUTPUT 1
28 #define LOW 0
29 #define HIGH 1
30 /*
31 * digitalWrite:
32 * Set an output bit
33 *********************************************************************************
34 */
35
36 void digitalWrite (int pin, int value)
37 {
38
39 if (value == 0 )
40 *(gpio4_dr) = (*(gpio4_dr) & (0 << 13)) ;
41 else
42 *(gpio4_dr) = (*(gpio4_dr) | (1 << 13 ));
43 }
44
45
46
47 void pinMode (int pin, int mode)
48 {
49 printf("input start \n");
50 if (mode == INPUT)
51 *(gpio4_dr + 4) = (*(gpio4_dr + 4) & (0 << 13)) ; // Sets bits to zero = input}
52 else
53 *(gpio4_dr + 4) = (*(gpio4_dr + 4) | (1 << 13)) ;
54 printf("output stop \n");
55
56 }
57
58 int wiringPiSetup (void)
59 {
60 int fd ;
61
62 GPIO4_DR = 0x30230000 ;
63 GPIO4_GDIR = 0x30230004 ;
64
65 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
66 {
67 printf("Unable to open /dev/mem: %s\n", strerror(errno));
68 }
69
70 gpio4_dr = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO4_DR) ;
71
72 return 0 ;
73 }
74 int main (void)
75 {
76 if(wiringPiSetup() < 0)return 1;
77 pinMode (0,OUTPUT) ;
78 digitalWrite(0, HIGH ) ;
79 return 0 ;
80 }

请帮忙查看一下是设备树配置出现问题,还是代码出现问题(附件为工程代码)。

谢谢!

0 Kudos
2 Replies

1,119 Views
b36401
NXP Employee
NXP Employee

In the sysfs, each GPIO has its own directory entry which can be found under /sys/class/gpio/gpioN. Here N is GPIO number.
In GPIO directory can be found its attributes. Here are most important:

direction (in/out)
value (0 - low / 1 - high)
edge  (none/rising/falling)

0 Kudos

1,119 Views
gaobo
Contributor I

我知道这个的。我们根本的目的是想开发gpio的底层库,需要通过操控寄存器来实现相应的功能。类似于树莓派的WiringPi库。我想知道为什么我通过代码没有实现我想得到的结果。

谢谢!

0 Kudos