AnsweredAssumed Answered

IMX8M GPIO direction

Question asked by gao bo on Jul 10, 2020
Latest reply on Jul 12, 2020 by gao bo

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

 

程序运行结果,刚开始未设置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 }

 

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

谢谢!

Attachments

Outcomes