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 if (eeprom->pwr_gpiorsc) {
127 const struct xwds_resource_gpio * gpiorsc = eeprom->pwr_gpiorsc;
128 rc = xwds_gpio_set(gpiorsc->soc,
129 gpiorsc->port,
130 gpiorsc->pinmask);
131 } else {
132 rc = -ENOSYS;
133 }
134 return rc;
135}
136
139{
140 xwer_t rc;
141
142 if (eeprom->pwr_gpiorsc) {
143 const struct xwds_resource_gpio * gpiorsc = eeprom->pwr_gpiorsc;
144 rc = xwds_gpio_reset(gpiorsc->soc,
145 gpiorsc->port,
146 gpiorsc->pinmask);
147 } else {
148 rc = -ENOSYS;
149 }
150 return rc;
151}
152
155{
156 xwer_t rc;
157
158 if (eeprom->wp_gpiorsc) {
159 const struct xwds_resource_gpio * gpiorsc = eeprom->wp_gpiorsc;
160 rc = xwds_gpio_set(gpiorsc->soc,
161 gpiorsc->port,
162 gpiorsc->pinmask);
163 } else {
164 rc = -ENOSYS;
165 }
166 return rc;
167}
168
171{
172 xwer_t rc;
173
174 if (eeprom->wp_gpiorsc) {
175 const struct xwds_resource_gpio * gpiorsc = eeprom->wp_gpiorsc;
176 rc = xwds_gpio_reset(gpiorsc->soc,
177 gpiorsc->port,
178 gpiorsc->pinmask);
179 } else {
180 rc = -ENOSYS;
181 }
182 return rc;
183}
184
187 xwu8_t data, xwsq_t addr,
188 xwtm_t to)
189{
190 const struct xwds_eeprom_driver * drv;
191 xwer_t rc;
192
193 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
194 if ((drv) && (drv->putc)) {
195 rc = drv->putc(eeprom, data, addr, to);
196 } else {
197 rc = -ENOSYS;
198 }
199 return rc;
200}
201
204 xwu8_t * buf, xwsq_t addr,
205 xwtm_t to)
206{
207 const struct xwds_eeprom_driver * drv;
208 xwer_t rc;
209
210 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
211 if ((drv) && (drv->getc)) {
212 rc = drv->getc(eeprom, buf, addr, to);
213 } else {
214 rc = -ENOSYS;
215 }
216 return rc;
217}
218
221 xwu8_t * data, xwsz_t * size, xwsq_t pgidx,
222 xwtm_t to)
223{
224 const struct xwds_eeprom_driver * drv;
225 xwer_t rc;
226
227 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
228 if ((drv) && (drv->pgwrite)) {
229 rc = drv->pgwrite(eeprom, data, size, pgidx, to);
230 } else {
231 rc = -ENOSYS;
232 }
233 return rc;
234}
235
238 xwu8_t * buf, xwsz_t * size, xwsq_t pgidx,
239 xwtm_t to)
240{
241 const struct xwds_eeprom_driver * drv;
242 xwer_t rc;
243
244 drv = xwds_cast(const struct xwds_eeprom_driver *, eeprom->i2cp.dev.drv);
245 if ((drv) && (drv->pgread)) {
246 rc = drv->pgread(eeprom, buf, size, pgidx, to);
247 } else {
248 rc = -ENOSYS;
249 }
250 return rc;
251}
252
255{
256 struct xwds_i2c_msg msg;
257 xwu8_t dummy;
258 xwer_t rc;
259
260 dummy = 0xFF;
261 msg.addr = 0xFE;
263 msg.data = &dummy;
264 msg.size = 1;
265 rc = xwds_i2cm_xfer(eeprom->i2cp.bus, &msg, to);
266 if (-EADDRNOTAVAIL == rc) {
268 msg.data = NULL;
269 msg.size = 0;
270 rc = xwds_i2cm_xfer(eeprom->i2cp.bus, &msg, to);
271 if (-EADDRNOTAVAIL == rc) {
272 rc = XWOK;
273 }
274 }
275 return rc;
276}
xwer_t xwds_i2cm_xfer(struct xwds_i2cm *i2cm, struct xwds_i2c_msg *msg, xwtm_t to)
XWDS API:传输I2C消息
Definition master.c:201
@ XWDS_I2C_F_WR
Definition common.h:43
@ XWDS_I2C_F_START
Definition common.h:46
@ XWDS_I2C_F_STOP
Definition common.h:56
#define xwds_cast(type, dev)
Definition standard.h:40
#define __xwbsp_code
Definition compiler.h:231
#define __xwbsp_api
Definition compiler.h:235
#define EINVAL
Invalid argument
Definition errno.h:52
#define ENOSYS
Function not implemented
Definition errno.h:110
#define EADDRNOTAVAIL
Address not available
Definition errno.h:138
#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:186
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:220
xwer_t xwds_eeprom_getc(struct xwds_eeprom *eeprom, xwu8_t *buf, xwsq_t addr, xwtm_t to)
EEPROM API:从EEPROM中读取一个字节
Definition driver.c:203
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:237
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:154
xwer_t xwds_eeprom_reset(struct xwds_eeprom *eeprom, xwtm_t to)
EEPROM API:复位EEPROM的I2C总线
Definition driver.c:254
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:170
xwer_t xwds_eeprom_power_off(struct xwds_eeprom *eeprom)
EEPROM API:关闭EEPROM的电源
Definition driver.c:138
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
I2C消息
Definition common.h:62
xwsz_t size
Definition common.h:68
xwu8_t * data
Definition common.h:67
xwu16_t addr
Definition common.h:63
xwu16_t flag
Definition common.h:66
struct xwds_device dev
Definition peripheral.h:49
struct xwds_i2cm * bus
Definition peripheral.h:52
设备栈GPIO资源
Definition standard.h:98
struct xwds_soc * soc
Definition standard.h:99
XWOS的标准头文件