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/soc/gpio.h>
25
27xwer_t xwds_gpio_req(struct xwds_soc * soc, xwid_t port, xwsq_t pinmask)
28{
29 const struct xwds_soc_driver * drv;
30 xwer_t rc;
31
32 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
33 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
34
35 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
36 rc = xwds_soc_grab(soc);
37 if (rc < 0) {
38 goto err_soc_grab;
39 }
40 rc = xwaop_t0ma_then_s1m(xwsq_t, &soc->gpio.pins[port], pinmask, NULL, NULL);
41 if (rc < 0) {
42 rc = -EBUSY;
43 goto err_set_pin;
44 }
45 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
46 if ((drv) && (drv->gpio_req)) {
47 rc = drv->gpio_req(soc, port, pinmask);
48 if (rc < 0) {
49 goto err_drv_gpio_req;
50 }
51 }
52 return XWOK;
53
54err_drv_gpio_req:
55 xwaop_c0m(xwsq_t, &soc->gpio.pins[port], pinmask, NULL, NULL);
56err_set_pin:
57 xwds_soc_put(soc);
58err_soc_grab:
59 return rc;
60}
61
63xwer_t xwds_gpio_rls(struct xwds_soc * soc, xwid_t port, xwsq_t pinmask)
64{
65 const struct xwds_soc_driver * drv;
66 xwsq_t pinsts;
67 xwer_t rc;
68
69 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
70 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
71
72 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
73 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
74 if (pinmask & (~pinsts)) {
75 rc = -EPERM;
76 goto err_pinsts;
77 }
78 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
79 if ((drv) && (drv->gpio_rls)) {
80 rc = drv->gpio_rls(soc, port, pinmask);
81 if (rc < 0) {
82 goto err_drv_gpio_rls;
83 }
84 }
85 xwaop_c0m(xwsq_t, &soc->gpio.pins[port], pinmask, NULL, NULL);
86
87 xwds_soc_put(soc);
88 return XWOK;
89
90err_drv_gpio_rls:
91err_pinsts:
92 return rc;
93}
94
97 xwid_t port, xwsq_t pinmask,
98 void * cfg)
99{
100 const struct xwds_soc_driver * drv;
101 xwer_t rc;
102
103 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
104 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
105 XWDS_VALIDATE(cfg, "nullptr", -EFAULT);
106
107 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
108 rc = xwds_soc_grab(soc);
109 if (rc < 0) {
110 goto err_soc_grab;
111 }
112 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
113 if ((drv) && (drv->gpio_cfg)) {
114 rc = drv->gpio_cfg(soc, port, pinmask, cfg);
115 } else {
116 rc = -ENOSYS;
117 }
118 if (rc < 0) {
119 goto err_drv_gpio_cfg;
120 }
121 xwds_soc_put(soc);
122 return XWOK;
123
124err_drv_gpio_cfg:
125 xwds_soc_put(soc);
126err_soc_grab:
127 return rc;
128}
129
131xwer_t xwds_gpio_set(struct xwds_soc * soc, xwid_t port, xwsq_t pinmask)
132{
133 const struct xwds_soc_driver * drv;
134 xwsq_t pinsts;
135 xwer_t rc;
136
137 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
138 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
139
140 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
141 rc = xwds_soc_grab(soc);
142 if (rc < 0) {
143 goto err_soc_grab;
144 }
145 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
146 if (pinmask & (~pinsts)) {
147 rc = -EPERM;
148 goto err_pinsts;
149 }
150 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
151 if ((drv) && (drv->gpio_set)) {
152 rc = drv->gpio_set(soc, port, pinmask);
153 } else {
154 rc = -ENOSYS;
155 }
156 if (rc < 0) {
157 goto err_drv_set;
158 }
159 xwds_soc_put(soc);
160 return XWOK;
161
162err_drv_set:
163err_pinsts:
164 xwds_soc_put(soc);
165err_soc_grab:
166 return rc;
167}
168
170xwer_t xwds_gpio_reset(struct xwds_soc * soc, xwid_t port, xwsq_t pinmask)
171{
172 const struct xwds_soc_driver * drv;
173 xwsq_t pinsts;
174 xwer_t rc;
175
176 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
177 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
178
179 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
180 rc = xwds_soc_grab(soc);
181 if (rc < 0) {
182 goto err_soc_grab;
183 }
184 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
185 if (pinmask & (~pinsts)) {
186 rc = -EPERM;
187 goto err_pinsts;
188 }
189 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
190 if ((drv) && (drv->gpio_reset)) {
191 rc = drv->gpio_reset(soc, port, pinmask);
192 } else {
193 rc = -ENOSYS;
194 }
195 if (rc < 0) {
196 goto err_drv_reset;
197 }
198 xwds_soc_put(soc);
199 return XWOK;
200
201err_drv_reset:
202err_pinsts:
203 xwds_soc_put(soc);
204err_soc_grab:
205 return rc;
206}
207
209xwer_t xwds_gpio_toggle(struct xwds_soc * soc, xwid_t port, xwsq_t pinmask)
210{
211 const struct xwds_soc_driver * drv;
212 xwsq_t pinsts;
213 xwer_t rc;
214
215 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
216 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
217
218 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
219 rc = xwds_soc_grab(soc);
220 if (rc < 0) {
221 goto err_soc_grab;
222 }
223 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
224 if (pinmask & (~pinsts)) {
225 rc = -EPERM;
226 goto err_pinsts;
227 }
228 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
229 if ((drv) && (drv->gpio_toggle)) {
230 rc = drv->gpio_toggle(soc, port, pinmask);
231 } else {
232 rc = -ENOSYS;
233 }
234 if (rc < 0) {
235 goto err_drv_toggle;
236 }
237 xwds_soc_put(soc);
238 return XWOK;
239
240err_drv_toggle:
241err_pinsts:
242 xwds_soc_put(soc);
243err_soc_grab:
244 return rc;
245}
246
249 xwid_t port, xwsq_t pinmask,
250 xwsq_t out)
251{
252 const struct xwds_soc_driver * drv;
253 xwsq_t pinsts;
254 xwer_t rc;
255
256 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
257 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
258
259 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
260 rc = xwds_soc_grab(soc);
261 if (rc < 0) {
262 goto err_soc_grab;
263 }
264 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
265 if (pinmask & (~pinsts)) {
266 rc = -EPERM;
267 goto err_pinsts;
268 }
269 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
270 if ((drv) && (drv->gpio_output)) {
271 rc = drv->gpio_output(soc, port, pinmask, out);
272 } else {
273 rc = -ENOSYS;
274 }
275 if (rc < 0) {
276 goto err_drv_out;
277 }
278 xwds_soc_put(soc);
279 return XWOK;
280
281err_drv_out:
282err_pinsts:
283 xwds_soc_put(soc);
284err_soc_grab:
285 return rc;
286}
287
290 xwid_t port, xwsq_t pinmask,
291 xwsq_t * inbuf)
292{
293 const struct xwds_soc_driver * drv;
294 xwsq_t pinsts;
295 xwer_t rc;
296
297 XWDS_VALIDATE(soc, "nullptr", -EFAULT);
298 XWDS_VALIDATE((port < soc->gpio.port_num), "out-of-range", -ERANGE);
299
300 pinmask &= XWDS_GPIO_PIN_MASK(soc->gpio.pin_num);
301 rc = xwds_soc_grab(soc);
302 if (rc < 0) {
303 goto err_soc_grab;
304 }
305 pinsts = xwaop_load(xwsq_t, &soc->gpio.pins[port], xwaop_mo_relaxed);
306 if (pinmask & (~pinsts)) {
307 rc = -EPERM;
308 goto err_pinsts;
309 }
310 drv = xwds_cast(const struct xwds_soc_driver *, soc->dev.drv);
311 if ((drv) && (drv->gpio_input)) {
312 rc = drv->gpio_input(soc, port, pinmask, inbuf);
313 } else {
314 rc = -ENOSYS;
315 }
316 if (rc < 0) {
317 goto err_drv_in;
318 }
319 xwds_soc_put(soc);
320 return XWOK;
321
322err_drv_in:
323err_pinsts:
324 xwds_soc_put(soc);
325err_soc_grab:
326 return rc;
327}
#define XWDS_GPIO_PIN_MASK(n)
Definition gpio.h:33
xwer_t xwds_soc_grab(struct xwds_soc *soc)
XWDS API:增加对象的引用计数
Definition chip.c:71
xwer_t xwds_soc_put(struct xwds_soc *soc)
XWDS API:减少对象的引用计数
Definition chip.c:77
#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
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_gpio_reset(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:将SOC的GPIO设置为低电平
Definition gpio.c:170
xwer_t xwds_gpio_input(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask, xwsq_t *inbuf)
XWDS API:并行读取多个SOC的GPIO
Definition gpio.c:289
xwer_t xwds_gpio_cfg(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask, void *cfg)
XWDS API:配置SOC的GPIO
Definition gpio.c:96
xwer_t xwds_gpio_rls(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:释放SOC的GPIO
Definition gpio.c:63
xwer_t xwds_gpio_toggle(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:翻转SOC的GPIO电平
Definition gpio.c:209
xwer_t xwds_gpio_set(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:将SOC的GPIO设置为高电平
Definition gpio.c:131
xwer_t xwds_gpio_output(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask, xwsq_t out)
XWDS API:并行输出多个SOC的GPIO
Definition gpio.c:248
xwer_t xwds_gpio_req(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:申请SOC的GPIO
Definition gpio.c:27
玄武设备栈:SOC:GPIO
const struct xwds_driver * drv
Definition device.h:133
BSP中需要提供的SOC设备驱动函数表
Definition chip.h:51
xwer_t(* gpio_output)(struct xwds_soc *, xwid_t, xwsq_t, xwsq_t)
Definition chip.h:88
xwer_t(* gpio_req)(struct xwds_soc *, xwid_t, xwsq_t)
Definition chip.h:78
xwer_t(* gpio_rls)(struct xwds_soc *, xwid_t, xwid_t)
Definition chip.h:81
xwer_t(* gpio_toggle)(struct xwds_soc *, xwid_t, xwsq_t)
Definition chip.h:98
xwer_t(* gpio_cfg)(struct xwds_soc *, xwid_t, xwsq_t, void *)
Definition chip.h:84
xwer_t(* gpio_set)(struct xwds_soc *, xwid_t, xwsq_t)
Definition chip.h:92
xwer_t(* gpio_input)(struct xwds_soc *, xwid_t, xwsq_t, xwsq_t *)
Definition chip.h:101
xwer_t(* gpio_reset)(struct xwds_soc *, xwid_t, xwsq_t)
Definition chip.h:95
SOC设备
Definition chip.h:161
struct xwds_device dev
Definition chip.h:162
struct xwds_soc::@23 gpio
xwsz_t pin_num
Definition chip.h:171
atomic_xwsq_t * pins
Definition chip.h:169
XWOS通用库:原子操作
玄武设备栈:顶级头文件