XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
gpio.c
浏览该文件的文档.
1
21#include <xwcd/ds/standard.h>
22#include <string.h>
23#include <xwos/lib/xwaop.h>
24#include <xwcd/ds/iochip/gpio.h>
25
28 xwid_t port, xwsq_t pinmask,
29 xwtm_t to)
30{
31 const struct xwds_iochip_driver * drv;
32 xwer_t rc;
33
34 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
35 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
36
37 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
38 rc = xwds_iochip_grab(iochip);
39 if (rc < 0) {
40 goto err_iochip_grab;
41 }
42 rc = xwaop_t0ma_then_s1m(xwsq_t, &iochip->gpio.pins[port], pinmask, NULL, NULL);
43 if (rc < 0) {
44 rc = -EBUSY;
45 goto err_set_pin;
46 }
47 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
48 if ((drv) && (drv->gpio_req)) {
49 rc = drv->gpio_req(iochip, port, pinmask, to);
50 if (rc < 0) {
51 goto err_drv_gpio_req;
52 }
53 }
54 return XWOK;
55
56err_drv_gpio_req:
57 xwaop_c0m(xwsq_t, &iochip->gpio.pins[port], pinmask, NULL, NULL);
58err_set_pin:
59 xwds_iochip_put(iochip);
60err_iochip_grab:
61 return rc;
62}
63
66 xwid_t port, xwsq_t pinmask,
67 xwtm_t to)
68{
69 const struct xwds_iochip_driver * drv;
70 xwsq_t pinsts;
71 xwer_t rc;
72
73 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
74 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
75
76 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
77 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
78 if (pinmask & (~pinsts)) {
79 rc = -EPERM;
80 goto err_pinsts;
81 }
82 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
83 if ((drv) && (drv->gpio_rls)) {
84 rc = drv->gpio_rls(iochip, port, pinmask, to);
85 if (rc < 0) {
86 goto err_drv_gpio_rls;
87 }
88 }
89 xwaop_c0m(xwsq_t, &iochip->gpio.pins[port], pinmask, NULL, NULL);
90
91 xwds_iochip_put(iochip);
92 return XWOK;
93
94err_drv_gpio_rls:
95err_pinsts:
96 return rc;
97}
98
101 xwid_t port, xwsq_t pinmask, void * cfg,
102 xwtm_t to)
103{
104 const struct xwds_iochip_driver * drv;
105 xwer_t rc;
106
107 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
108 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
109 XWDS_VALIDATE(cfg, "nullptr", -EFAULT);
110
111 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
112 rc = xwds_iochip_grab(iochip);
113 if (rc < 0) {
114 goto err_iochip_grab;
115 }
116 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
117 if ((drv) && (drv->gpio_cfg)) {
118 rc = drv->gpio_cfg(iochip, port, pinmask, cfg, to);
119 } else {
120 rc = -ENOSYS;
121 }
122 if (rc < 0) {
123 goto err_drv_gpio_cfg;
124 }
125 xwds_iochip_put(iochip);
126 return XWOK;
127
128err_drv_gpio_cfg:
129 xwds_iochip_put(iochip);
130err_iochip_grab:
131 return rc;
132}
133
136 xwid_t port, xwsq_t pinmask,
137 xwtm_t to)
138{
139 const struct xwds_iochip_driver * drv;
140 xwsq_t pinsts;
141 xwer_t rc;
142
143 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
144 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
145
146 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
147 rc = xwds_iochip_grab(iochip);
148 if (rc < 0) {
149 goto err_iochip_grab;
150 }
151 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
152 if (pinmask & (~pinsts)) {
153 rc = -EPERM;
154 goto err_pinsts;
155 }
156 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
157 if ((drv) && (drv->gpio_set)) {
158 rc = drv->gpio_set(iochip, port, pinmask, to);
159 } else {
160 rc = -ENOSYS;
161 }
162 if (rc < 0) {
163 goto err_drv_set;
164 }
165 xwds_iochip_put(iochip);
166 return XWOK;
167
168err_drv_set:
169err_pinsts:
170 xwds_iochip_put(iochip);
171err_iochip_grab:
172 return rc;
173}
174
177 xwid_t port, xwsq_t pinmask,
178 xwtm_t to)
179{
180 const struct xwds_iochip_driver * drv;
181 xwsq_t pinsts;
182 xwer_t rc;
183
184 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
185 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
186
187 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
188 rc = xwds_iochip_grab(iochip);
189 if (rc < 0) {
190 goto err_iochip_grab;
191 }
192 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
193 if (pinmask & (~pinsts)) {
194 rc = -EPERM;
195 goto err_pinsts;
196 }
197 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
198 if ((drv) && (drv->gpio_reset)) {
199 rc = drv->gpio_reset(iochip, port, pinmask, to);
200 } else {
201 rc = -ENOSYS;
202 }
203 if (rc < 0) {
204 goto err_drv_reset;
205 }
206 xwds_iochip_put(iochip);
207 return XWOK;
208
209err_drv_reset:
210err_pinsts:
211 xwds_iochip_put(iochip);
212err_iochip_grab:
213 return rc;
214}
215
218 xwid_t port, xwsq_t pinmask,
219 xwtm_t to)
220{
221 const struct xwds_iochip_driver * drv;
222 xwsq_t pinsts;
223 xwer_t rc;
224
225 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
226 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
227
228 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
229 rc = xwds_iochip_grab(iochip);
230 if (rc < 0) {
231 goto err_iochip_grab;
232 }
233 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
234 if (pinmask & (~pinsts)) {
235 rc = -EPERM;
236 goto err_pinsts;
237 }
238 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
239 if ((drv) && (drv->gpio_toggle)) {
240 rc = drv->gpio_toggle(iochip, port, pinmask, to);
241 } else {
242 rc = -ENOSYS;
243 }
244 if (rc < 0) {
245 goto err_drv_toggle;
246 }
247 xwds_iochip_put(iochip);
248 return XWOK;
249
250err_drv_toggle:
251err_pinsts:
252 xwds_iochip_put(iochip);
253err_iochip_grab:
254 return rc;
255}
256
259 xwid_t port, xwsq_t pinmask, xwsq_t out,
260 xwtm_t to)
261{
262 const struct xwds_iochip_driver * drv;
263 xwsq_t pinsts;
264 xwer_t rc;
265
266 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
267 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
268
269 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
270 rc = xwds_iochip_grab(iochip);
271 if (rc < 0) {
272 goto err_iochip_grab;
273 }
274 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
275 if (pinmask & (~pinsts)) {
276 rc = -EPERM;
277 goto err_pinsts;
278 }
279 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
280 if ((drv) && (drv->gpio_output)) {
281 rc = drv->gpio_output(iochip, port, pinmask, out, to);
282 } else {
283 rc = -ENOSYS;
284 }
285 if (rc < 0) {
286 goto err_drv_out;
287 }
288 xwds_iochip_put(iochip);
289 return XWOK;
290
291err_drv_out:
292err_pinsts:
293 xwds_iochip_put(iochip);
294err_iochip_grab:
295 return rc;
296}
297
300 xwid_t port, xwsq_t pinmask, xwsq_t * inbuf,
301 xwtm_t to)
302{
303 const struct xwds_iochip_driver * drv;
304 xwsq_t pinsts;
305 xwer_t rc;
306
307 XWDS_VALIDATE(iochip, "nullptr", -EFAULT);
308 XWDS_VALIDATE((port < iochip->gpio.port_num), "out-of-range", -ERANGE);
309
310 pinmask &= XWDS_IOCHIP_GPIO_PIN_MASK(iochip->gpio.pin_num);
311 rc = xwds_iochip_grab(iochip);
312 if (rc < 0) {
313 goto err_iochip_grab;
314 }
315 pinsts = xwaop_load(xwsq_t, &iochip->gpio.pins[port], xwaop_mo_relaxed);
316 if (pinmask & (~pinsts)) {
317 rc = -EPERM;
318 goto err_pinsts;
319 }
320 drv = xwds_cast(const struct xwds_iochip_driver *, iochip->bc.dev.drv);
321 if ((drv) && (drv->gpio_input)) {
322 rc = drv->gpio_input(iochip, port, pinmask, inbuf, to);
323 } else {
324 rc = -ENOSYS;
325 }
326 if (rc < 0) {
327 goto err_drv_in;
328 }
329 xwds_iochip_put(iochip);
330 return XWOK;
331
332err_drv_in:
333err_pinsts:
334 xwds_iochip_put(iochip);
335err_iochip_grab:
336 return rc;
337}
xwer_t xwds_iochip_grab(struct xwds_iochip *iochip)
XWDS API:增加对象的引用计数
Definition chip.c:71
xwer_t xwds_iochip_put(struct xwds_iochip *iochip)
XWDS API:减少对象的引用计数
Definition chip.c:77
#define XWDS_IOCHIP_GPIO_PIN_MASK(n)
Definition gpio.h:35
#define xwds_cast(type, dev)
Definition standard.h:40
#define __xwds_api
Definition standard.h:33
#define XWDS_VALIDATE(exp, errstr,...)
Definition standard.h:51
#define EFAULT
Bad address
Definition errno.h:44
#define ENOSYS
Function not implemented
Definition errno.h:110
#define XWOK
No error
Definition errno.h:182
#define EBUSY
Device or resource busy
Definition errno.h:46
#define ERANGE
Result too large
Definition errno.h:64
#define EPERM
Operation not permitted
Definition errno.h:31
xws64_t xwtm_t
XWOS系统时间 (有符号)
Definition type.h:742
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
unsigned long xwid_t
Definition type.h:481
unsigned long xwsq_t
Definition type.h:445
@ xwaop_mo_relaxed
Definition type.h:628
#define xwaop_c0m(type, a, m, nv, ov)
对原子变量进行原子操作:读取-位清0操作-回写
Definition xwaop.h:1190
#define xwaop_load(type, a, memorder)
对原子变量进行原子操作:加载
Definition xwaop.h:45
#define xwaop_t0ma_then_s1m(type, a, m, nv, ov)
对原子变量进行原子操作:读取-位测试-位置1操作-回写
Definition xwaop.h:1277
xwer_t xwds_iochip_gpio_input(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwsq_t *inbuf, xwtm_t to)
XWDS API:并行读取多个IO扩展芯片的GPIO
Definition gpio.c:299
xwer_t xwds_iochip_gpio_set(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwtm_t to)
XWDS API:将IO扩展芯片的GPIO设置为高电平
Definition gpio.c:135
xwer_t xwds_iochip_gpio_cfg(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, void *cfg, xwtm_t to)
XWDS API:配置IO扩展芯片的GPIO
Definition gpio.c:100
xwer_t xwds_iochip_gpio_toggle(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwtm_t to)
XWDS API:翻转IO扩展芯片的GPIO电平
Definition gpio.c:217
xwer_t xwds_iochip_gpio_req(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwtm_t to)
XWDS API:申请IO扩展芯片的GPIO
Definition gpio.c:27
xwer_t xwds_iochip_gpio_reset(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwtm_t to)
XWDS API:将IO扩展芯片的GPIO设置为低电平
Definition gpio.c:176
xwer_t xwds_iochip_gpio_rls(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwtm_t to)
XWDS API:释放IO扩展芯片的GPIO
Definition gpio.c:65
xwer_t xwds_iochip_gpio_output(struct xwds_iochip *iochip, xwid_t port, xwsq_t pinmask, xwsq_t out, xwtm_t to)
XWDS API:并行输出多个IO扩展芯片的GPIO
Definition gpio.c:258
玄武设备栈:IO扩展芯片:GPIO
const struct xwds_driver * drv
Definition device.h:133
BSP中需要提供的IO扩展芯片设备驱动函数表
Definition chip.h:52
xwer_t(* gpio_req)(struct xwds_iochip *, xwid_t, xwsq_t, xwtm_t)
Definition chip.h:55
xwer_t(* gpio_cfg)(struct xwds_iochip *, xwid_t, xwsq_t, void *, xwtm_t)
Definition chip.h:63
xwer_t(* gpio_rls)(struct xwds_iochip *, xwid_t, xwid_t, xwtm_t)
Definition chip.h:59
xwer_t(* gpio_toggle)(struct xwds_iochip *, xwid_t, xwsq_t, xwtm_t)
Definition chip.h:81
xwer_t(* gpio_set)(struct xwds_iochip *, xwid_t, xwsq_t, xwtm_t)
Definition chip.h:73
xwer_t(* gpio_reset)(struct xwds_iochip *, xwid_t, xwsq_t, xwtm_t)
Definition chip.h:77
xwer_t(* gpio_input)(struct xwds_iochip *, xwid_t, xwsq_t, xwsq_t *, xwtm_t)
Definition chip.h:85
xwer_t(* gpio_output)(struct xwds_iochip *, xwid_t, xwsq_t, xwsq_t, xwtm_t)
Definition chip.h:68
IO扩展芯片
Definition chip.h:95
xwsz_t pin_num
Definition chip.h:111
struct xwds_device dev
Definition chip.h:97
union xwds_iochip::@19 bc
struct xwds_iochip::@20 gpio
atomic_xwsq_t * pins
Definition chip.h:109
XWOS通用库:原子操作
玄武设备栈:顶级头文件