Dawn Framework 1.0
Universal data acquisition framework for embedded systems
gpio.cxx
1// dawn/src/porting/nuttx/gpio.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include <errno.h>
7#include <fcntl.h>
8#include <nuttx/ioexpander/gpio.h>
9#include <sys/ioctl.h>
10#include <unistd.h>
11
12#include "dawn/debug.hxx"
13#include "dawn/porting/config.hxx"
14
15//***************************************************************************
16// Public Functions
17//***************************************************************************
18
19//***************************************************************************
20// Name: gpi_open
21//***************************************************************************
22
23int gpi_open(const char *path)
24{
25 int fd;
26
27 fd = open(path, O_RDWR);
28 DAWNINFO("GPI: open %s %d\n", path, fd);
29 if (fd < 0)
30 {
31 DAWNERR("Failed to open GPI file %s (error %d)\n", path, fd);
32 return -EIO;
33 }
34 return fd;
35}
36
37//***************************************************************************
38// Name: gpi_close
39//***************************************************************************
40
41void gpi_close(int fd)
42{
43 if (fd >= 0)
44 {
45 close(fd);
46 }
47}
48
49//***************************************************************************
50// Name: gpi_init
51//***************************************************************************
52
53int gpi_init(int fd)
54{
55 int pintype = 0;
56 int ret;
57
58 // Make sure that this is input pin
59
60 ret = ioctl(fd, GPIOC_PINTYPE, reinterpret_cast<unsigned long>(&pintype));
61 if (ret < 0)
62 {
63 DAWNERR("GPI ioctl GPIOC_PINTYPE failed (error %d)\n", ret);
64 return -EIO;
65 }
66
67 if (pintype == GPIO_OUTPUT_PIN)
68 {
69 DAWNERR("GPIO pin is output, expected input (pintype %d)\n", pintype);
70 return -EINVAL;
71 }
72
73 return OK;
74}
75
76//***************************************************************************
77// Name: gpi_read
78//***************************************************************************
79
80int gpi_read(int fd, bool *invalue)
81{
82 int ret;
83
84 ret = ioctl(fd, GPIOC_READ, reinterpret_cast<unsigned long>(invalue));
85 if (ret < 0)
86 {
87 DAWNERR("GPIOC_READ failed %d\n", -errno);
88 return -errno;
89 }
90
91 return ret;
92}
93
94//***************************************************************************
95// Name: gpi_notify
96//***************************************************************************
97
98bool gpi_notify(int fd)
99{
100 return ioctl(fd, GPIOC_REGISTER, nullptr) < 0 ? false : true;
101}
102
103//***************************************************************************
104// Name: gpo_open
105//***************************************************************************
106
107int gpo_open(const char *path)
108{
109 int fd;
110
111 fd = open(path, O_RDWR);
112 DAWNINFO("GPO: open %s %d\n", path, fd);
113 if (fd < 0)
114 {
115 DAWNERR("Failed to open GPO file %s (error %d)\n", path, fd);
116 return -EIO;
117 }
118 return fd;
119}
120
121//***************************************************************************
122// Name: gpo_close
123//***************************************************************************
124
125void gpo_close(int fd)
126{
127 if (fd >= 0)
128 {
129 close(fd);
130 }
131}
132
133//***************************************************************************
134// Name: gpo_init
135//***************************************************************************
136
137int gpo_init(int fd)
138{
139 int pintype = 0;
140 int ret;
141
142 // Make sure that this is output pin
143
144 ret = ioctl(fd, GPIOC_PINTYPE, reinterpret_cast<unsigned long>(&pintype));
145 if (ret < 0)
146 {
147 DAWNERR("GPO ioctl GPIOC_PINTYPE failed (error %d)\n", ret);
148 return -EIO;
149 }
150
151 if (pintype != GPIO_OUTPUT_PIN)
152 {
153 DAWNERR("GPIO pin is input, expected output (pintype %d)\n", pintype);
154 return -EINVAL;
155 }
156
157 return OK;
158}
159
160//***************************************************************************
161// Name: gpo_read
162//***************************************************************************
163
164int gpo_read(int fd, bool *invalue)
165{
166 int ret;
167
168 ret = ioctl(fd, GPIOC_READ, reinterpret_cast<unsigned long>(invalue));
169 if (ret < 0)
170 {
171 DAWNERR("GPIOC_READ failed %d\n", -errno);
172 return -errno;
173 }
174
175 return ret;
176}
177
178//***************************************************************************
179// Name: gpo_write
180//***************************************************************************
181
182int gpo_write(int fd, bool invalue)
183{
184 int ret;
185
186 ret = ioctl(fd, GPIOC_WRITE, static_cast<unsigned long>(invalue));
187 if (ret < 0)
188 {
189 DAWNERR("GPIOC_WRITE failed %d\n", -errno);
190 return -errno;
191 }
192
193 return ret;
194}