XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
driver.c
浏览该文件的文档.
1
21#include <xwos/standard.h>
22#include <xwcd/ds/soc/gpio.h>
26
27/******** ******** base driver ******** ********/
28static __xwbsp_code
30{
31 const struct xwds_eeprom_parameter * parameter;
34 xwer_t rc;
35
36 parameter = &eeprom->parameter;
37 page_size = parameter->page_size;
38 total = parameter->total;
39 if (total % page_size) {
40 rc = -EINVAL;
41 } else {
42 rc = XWOK;
43 }
44 return rc;
45}
46
49{
50 struct xwds_eeprom * eeprom;
51 const struct xwds_resource_gpio * gpiorsc;
52 xwer_t rc;
53
54 eeprom = xwds_cast(struct xwds_eeprom *, dev);
55 rc = xwds_eeprom_check_desc(eeprom);
56 if (rc < 0) {
57 goto err_chk_desc;
58 }
59 /* request GPIO resources */
60 gpiorsc = eeprom->pwr_gpiorsc;
61 if (NULL != gpiorsc) {
62 rc = xwds_gpio_req(gpiorsc->soc, gpiorsc->port, gpiorsc->pinmask);
63 if (rc < 0) {
64 goto err_pwrgpio_req;
65 }
66 }
67 gpiorsc = eeprom->wp_gpiorsc;
68 if (NULL != gpiorsc) {
69 rc = xwds_gpio_req(gpiorsc->soc, gpiorsc->port, gpiorsc->pinmask);
70 if (rc < 0) {
71 goto err_wpgpio_req;
72 }
73 }
74 return XWOK;
75
76err_wpgpio_req:
77 gpiorsc = eeprom->pwr_gpiorsc;
78 if (NULL != gpiorsc) {
79 xwds_gpio_rls(gpiorsc->soc, gpiorsc->port, gpiorsc->pinmask);
80 }
81err_pwrgpio_req:
82err_chk_desc:
83 return rc;
84}
85
88{
89 struct xwds_eeprom * eeprom;
90 const struct xwds_resource_gpio * gpiorsc;
91
92 eeprom = xwds_cast(struct xwds_eeprom *, dev);
93 /* release GPIO resources */
94 gpiorsc = eeprom->wp_gpiorsc;
95 if (NULL != gpiorsc) {
96 xwds_gpio_rls(gpiorsc->soc, gpiorsc->port, gpiorsc->pinmask);
97 }
98 gpiorsc = eeprom->pwr_gpiorsc;
99 if (NULL != gpiorsc) {
100 xwds_gpio_rls(gpiorsc->soc, gpiorsc->port, gpiorsc->pinmask);
101 }
102
103 return XWOK;
104}
105
106#if defined(XWCDCFG_ds_PM) && (1 == XWCDCFG_ds_PM)
109{
110 return xwds_eeprom_drv_stop(dev);
111}
112
115{
116 return xwds_eeprom_drv_start(dev);
117}
118#endif
119
120/******** ******** I2C EEPROM APIs ******** ********/
123{
124 xwer_t rc;
125
126 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
127
128 if (eeprom->pwr_gpiorsc) {
129 const struct xwds_resource_gpio * gpiorsc = eeprom->pwr_gpiorsc;
130 rc = xwds_gpio_set(gpiorsc->soc,
131 gpiorsc->port,
132 gpiorsc->pinmask);
133 } else {
134 rc = -ENOSYS;
135 }
136 return rc;
137}
138
141{
142 xwer_t rc;
143
144 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
145
146 if (eeprom->pwr_gpiorsc) {
147 const struct xwds_resource_gpio * gpiorsc = eeprom->pwr_gpiorsc;
148 rc = xwds_gpio_reset(gpiorsc->soc,
149 gpiorsc->port,
150 gpiorsc->pinmask);
151 } else {
152 rc = -ENOSYS;
153 }
154 return rc;
155}
156
159{
160 xwer_t rc;
161
162 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
163
164 if (eeprom->wp_gpiorsc) {
165 const struct xwds_resource_gpio * gpiorsc = eeprom->wp_gpiorsc;
166 rc = xwds_gpio_set(gpiorsc->soc,
167 gpiorsc->port,
168 gpiorsc->pinmask);
169 } else {
170 rc = -ENOSYS;
171 }
172 return rc;
173}
174
177{
178 xwer_t rc;
179
180 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
181
182 if (eeprom->wp_gpiorsc) {
183 const struct xwds_resource_gpio * gpiorsc = eeprom->wp_gpiorsc;
184 rc = xwds_gpio_reset(gpiorsc->soc,
185 gpiorsc->port,
186 gpiorsc->pinmask);
187 } else {
188 rc = -ENOSYS;
189 }
190 return rc;
191}
192
195 xwu8_t data, xwsq_t addr,
196 xwtm_t to)
197{
198 const struct xwds_eeprom_driver * drv;
199 xwer_t rc;
200
201 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
202
203 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
204 if ((drv) && (drv->putc)) {
205 rc = drv->putc(eeprom, data, addr, to);
206 } else {
207 rc = -ENOSYS;
208 }
209 return rc;
210}
211
214 xwu8_t * buf, xwsq_t addr,
215 xwtm_t to)
216{
217 const struct xwds_eeprom_driver * drv;
218 xwer_t rc;
219
220 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
221 XWDS_VALIDATE(buf, "nullptr", -EFAULT);
222
223 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
224 if ((drv) && (drv->getc)) {
225 rc = drv->getc(eeprom, buf, addr, to);
226 } else {
227 rc = -ENOSYS;
228 }
229 return rc;
230}
231
234 xwu8_t * data, xwsz_t * size, xwsq_t pgidx,
235 xwtm_t to)
236{
237 const struct xwds_eeprom_parameter * param;
238 const struct xwds_eeprom_driver * drv;
239 xwer_t rc;
240
241 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
242 XWDS_VALIDATE(data, "nullptr", -EFAULT);
243 XWDS_VALIDATE(size, "nullptr", -EFAULT);
244
245 param = &eeprom->parameter;
246 if (*size > param->page_size) {
247 *size = param->page_size;
248 }
249 if (pgidx < param->total) {
250 drv = xwds_cast(const struct xwds_eeprom_driver *,
251 eeprom->i2cp.dev.drv);
252 if ((drv) && (drv->pgwrite)) {
253 rc = drv->pgwrite(eeprom, data, size, pgidx, to);
254 } else {
255 rc = -ENOSYS;
256 }
257 } else {
258 rc = -ENXIO;
259 }
260 return rc;
261}
262
265 xwu8_t * buf, xwsz_t * size, xwsq_t pgidx,
266 xwtm_t to)
267{
268 const struct xwds_eeprom_parameter * param;
269 const struct xwds_eeprom_driver * drv;
270 xwer_t rc;
271
272 XWDS_VALIDATE(eeprom, "nullptr", -EFAULT);
273 XWDS_VALIDATE(data, "nullptr", -EFAULT);
274 XWDS_VALIDATE(size, "nullptr", -EFAULT);
275
276 param = &eeprom->parameter;
277 if (*size > param->page_size) {
278 *size = param->page_size;
279 }
280 if (pgidx < param->total) {
281 drv = xwds_cast(const struct xwds_eeprom_driver *,
282 eeprom->i2cp.dev.drv);
283 if ((drv) && (drv->pgread)) {
284 rc = drv->pgread(eeprom, buf, size, pgidx, to);
285 } else {
286 rc = -ENOSYS;
287 }
288 } else {
289 rc = -ENXIO;
290 }
291 return rc;
292}
#define xwds_cast(type, dev)
Definition standard.h:40
#define XWDS_VALIDATE(exp, errstr,...)
Definition standard.h:51
#define __xwbsp_code
Definition compiler.h:231
#define __xwbsp_api
Definition compiler.h:235
#define ENXIO
No such device or address
Definition errno.h:36
#define EINVAL
Invalid argument
Definition errno.h:52
#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
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 xwsz_t
Definition type.h:339
uint8_t xwu8_t
Definition type.h:194
unsigned long xwsq_t
Definition type.h:445
xwer_t xwds_eeprom_drv_start(struct xwds_device *dev)
EEPROM基本驱动:启动设备
Definition driver.c:48
xwer_t xwds_eeprom_putc(struct xwds_eeprom *eeprom, xwu8_t data, xwsq_t addr, xwtm_t to)
EEPROM API:写一个字节到EEPROM
Definition driver.c:194
xwer_t xwds_eeprom_drv_suspend(struct xwds_device *dev)
EEPROM基本驱动:继续设备
Definition driver.c:108
xwer_t xwds_eeprom_pgwrite(struct xwds_eeprom *eeprom, xwu8_t *data, xwsz_t *size, xwsq_t pgidx, xwtm_t to)
EEPROM API:写一页数据到EEPROM
Definition driver.c:233
xwer_t xwds_eeprom_getc(struct xwds_eeprom *eeprom, xwu8_t *buf, xwsq_t addr, xwtm_t to)
EEPROM API:从EEPROM中读取一个字节
Definition driver.c:213
xwer_t xwds_eeprom_pgread(struct xwds_eeprom *eeprom, xwu8_t *buf, xwsz_t *size, xwsq_t pgidx, xwtm_t to)
EEPROM API:从EEPROM读一页数据
Definition driver.c:264
static xwer_t xwds_eeprom_check_desc(struct xwds_eeprom *eeprom)
Definition driver.c:29
xwer_t xwds_eeprom_wp_enable(struct xwds_eeprom *eeprom)
EEPROM API:开启EEPROM的写保护
Definition driver.c:158
xwer_t xwds_eeprom_drv_stop(struct xwds_device *dev)
EEPROM基本驱动:停止设备
Definition driver.c:87
xwer_t xwds_eeprom_wp_disable(struct xwds_eeprom *eeprom)
EEPROM API:关闭EEPROM的写保护
Definition driver.c:176
xwer_t xwds_eeprom_power_off(struct xwds_eeprom *eeprom)
EEPROM API:关闭EEPROM的电源
Definition driver.c:140
xwer_t xwds_eeprom_power_on(struct xwds_eeprom *eeprom)
EEPROM API:开启EEPROM的电源
Definition driver.c:122
xwer_t xwds_eeprom_drv_resume(struct xwds_device *dev)
EEPROM基本驱动:暂停设备
Definition driver.c:114
I2C EEPROM 驱动
玄武设备栈:I2C:外设
I2C EEPROM 设备
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_rls(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:释放SOC的GPIO
Definition gpio.c:63
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_req(struct xwds_soc *soc, xwid_t port, xwsq_t pinmask)
XWDS API:申请SOC的GPIO
Definition gpio.c:27
玄武设备栈:SOC:GPIO
设备(所有设备的基类)
Definition device.h:127
const struct xwds_driver * drv
Definition device.h:133
EEPROM驱动函数表
Definition driver.h:38
xwer_t(* pgread)(struct xwds_eeprom *, xwu8_t *, xwsz_t *, xwsq_t, xwtm_t)
Definition driver.h:49
xwer_t(* getc)(struct xwds_eeprom *, xwu8_t *, xwptr_t, xwtm_t)
Definition driver.h:43
xwer_t(* pgwrite)(struct xwds_eeprom *, xwu8_t *, xwsz_t *, xwsq_t, xwtm_t)
Definition driver.h:46
xwer_t(* putc)(struct xwds_eeprom *, xwu8_t, xwptr_t, xwtm_t)
Definition driver.h:40
const struct xwds_resource_gpio * wp_gpiorsc
Definition device.h:42
const struct xwds_resource_gpio * pwr_gpiorsc
Definition device.h:41
const struct xwds_eeprom_parameter parameter
Definition device.h:40
struct xwds_i2cp i2cp
Definition device.h:39
struct xwds_device dev
Definition peripheral.h:49
设备栈GPIO资源
Definition standard.h:98
struct xwds_soc * soc
Definition standard.h:99
XWOS的标准头文件