LibDriver MB85RCXX
Loading...
Searching...
No Matches
driver_mb85rcxx.c
Go to the documentation of this file.
1
36
37#include "driver_mb85rcxx.h"
38
42#define CHIP_NAME "RAMXEED MB85RCXX"
43#define MANUFACTURER_NAME "RAMXEED"
44#define SUPPLY_VOLTAGE_MIN 1.7f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.02f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
62{
63 if (handle == NULL) /* check handle */
64 {
65 return 2; /* return error */
66 }
67 if (handle->debug_print == NULL) /* check debug_print */
68 {
69 return 3; /* return error */
70 }
71 if (handle->iic_init == NULL) /* check iic_init */
72 {
73 handle->debug_print("mb85rcxx: iic_init is null.\n"); /* iic_init is null */
74
75 return 3; /* return error */
76 }
77 if (handle->iic_deinit == NULL) /* check iic_deinit */
78 {
79 handle->debug_print("mb85rcxx: iic_deinit is null.\n"); /* iic_deinit is null */
80
81 return 3; /* return error */
82 }
83 if (handle->iic_read == NULL) /* check iic_read */
84 {
85 handle->debug_print("mb85rcxx: iic_read is null.\n"); /* iic_read is null */
86
87 return 3; /* return error */
88 }
89 if (handle->iic_write == NULL) /* check iic_write */
90 {
91 handle->debug_print("mb85rcxx: iic_write is null.\n"); /* iic_write is null */
92
93 return 3; /* return error */
94 }
95 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
96 {
97 handle->debug_print("mb85rcxx: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
98
99 return 3; /* return error */
100 }
101 if (handle->iic_write_address16 == NULL) /* check iic_write_address16 */
102 {
103 handle->debug_print("mb85rcxx: iic_write_address16 is null.\n"); /* iic_write_address16 is null */
104
105 return 3; /* return error */
106 }
107 if (handle->delay_ms == NULL) /* check delay_ms */
108 {
109 handle->debug_print("mb85rcxx: delay_ms is null.\n"); /* delay_ms is null */
110
111 return 3; /* return error */
112 }
113
114 if (handle->iic_init() != 0) /* iic init */
115 {
116 handle->debug_print("mb85rcxx: iic init failed.\n"); /* iic init failed */
117
118 return 1; /* return error */
119 }
120 handle->inited = 1; /* flag finish initialization */
121
122 return 0; /* success return 0 */
123}
124
136{
137 if (handle == NULL) /* check handle */
138 {
139 return 2; /* return error */
140 }
141 if (handle->inited != 1) /* check handle initialization */
142 {
143 return 3; /* return error */
144 }
145
146 if (handle->iic_deinit() != 0) /* iic deinit */
147 {
148 handle->debug_print("mb85rcxx: iic deinit failed.\n"); /* iic deinit failed */
149
150 return 1; /* return error */
151 }
152 handle->inited = 0; /* flag close */
153
154 return 0; /* success return 0 */
155}
156
167{
168 if (handle == NULL) /* check handle */
169 {
170 return 2; /* return error */
171 }
172
173 handle->id = (uint32_t)type; /* set id */
174
175 return 0; /* success return 0 */
176}
177
188{
189 if (handle == NULL) /* check handle */
190 {
191 return 2; /* return error */
192 }
193
194 *type = (mb85rcxx_t)(handle->id); /* get id */
195
196 return 0; /* success return 0 */
197}
198
209{
210 if (handle == NULL) /* check handle */
211 {
212 return 2; /* return error */
213 }
214
215 handle->iic_addr = 0xA0; /* set iic addr */
216 handle->iic_addr |= addr_pin << 1; /* set iic address */
217
218 return 0; /* success return 0 */
219}
220
231{
232 if (handle == NULL) /* check handle */
233 {
234 return 2; /* return error */
235 }
236
237 *addr_pin = (mb85rcxx_address_t)((handle->iic_addr & (~0xA0)) >> 1); /* get iic address */
238
239 return 0; /* success return 0 */
240}
241
256uint8_t mb85rcxx_read(mb85rcxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
257{
258 uint8_t page_remain;
259
260 if (handle == NULL) /* check handle */
261 {
262 return 2; /* return error */
263 }
264 if (handle->inited != 1) /* check handle initialization */
265 {
266 return 3; /* return error */
267 }
268
269 if ((address + len) > handle->id) /* check length */
270 {
271 handle->debug_print("mb85rcxx: read out of range.\n"); /* read out of range */
272
273 return 4; /* return error */
274 }
275 page_remain = (uint8_t)(8 - address % 8); /* get page remain */
276 if (len <= page_remain) /* page remain */
277 {
278 page_remain = (uint8_t)len; /* set page remain */
279 }
280 if (handle->id > (uint32_t)MB85RC16) /* choose id to set different address */
281 {
282 while (1)
283 {
284 if (handle->iic_read_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
285 address % 65536, buf,
286 page_remain) != 0) /* read page */
287 {
288 handle->debug_print("mb85rcxx: read failed.\n"); /* read failed */
289
290 return 1; /* return error */
291 }
292 if (page_remain == len) /* check break */
293 {
294 break; /* break loop */
295 }
296 else
297 {
298 address += page_remain; /* address increase */
299 buf += page_remain; /* buffer point increase */
300 len -= page_remain; /* length decrease */
301 if (len < 8) /* check length */
302 {
303 page_remain = (uint8_t)len; /* set the reset length */
304 }
305 else
306 {
307 page_remain = 8; /* set page */
308 }
309 }
310 }
311 }
312 else
313 {
314 while (1)
315 {
316 if (handle->iic_read((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256, buf,
317 page_remain) != 0) /* read page */
318 {
319 handle->debug_print("mb85rcxx: read failed.\n"); /* read failed */
320
321 return 1; /* return error */
322 }
323 if (page_remain == len) /* check break */
324 {
325 break; /* break loop */
326 }
327 else
328 {
329 address += page_remain; /* address increase */
330 buf += page_remain; /* buffer point increase */
331 len -= page_remain; /* length decrease */
332 if (len < 8) /* check length */
333 {
334 page_remain = (uint8_t)len; /* set the reset length */
335 }
336 else
337 {
338 page_remain = 8; /* set page */
339 }
340 }
341 }
342 }
343
344 return 0; /* success return 0 */
345}
346
361uint8_t mb85rcxx_write(mb85rcxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
362{
363 uint8_t page_remain;
364
365 if (handle == NULL) /* check handle */
366 {
367 return 2; /* return error */
368 }
369 if (handle->inited != 1) /* check handle initialization */
370 {
371 return 3; /* return error */
372 }
373
374 if ((address + len) > handle->id) /* check length */
375 {
376 handle->debug_print("mb85rcxx: write out of range.\n"); /* write out of range */
377
378 return 1; /* return error */
379 }
380 page_remain = (uint8_t)(8 - address % 8); /* set page remain */
381 if (len <= page_remain) /* check length */
382 {
383 page_remain = (uint8_t)len; /* set page remain */
384 }
385 if (handle->id > (uint32_t)MB85RC16) /* check id */
386 {
387 while (1)
388 {
389 if (handle->iic_write_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
390 address % 65536, buf,
391 page_remain) != 0) /* write page */
392 {
393 handle->debug_print("mb85rcxx: write failed.\n"); /* write failed */
394
395 return 1; /* return error */
396 }
397 if (page_remain == len) /* check break */
398 {
399 break; /* break */
400 }
401 else
402 {
403 address += page_remain; /* address increase */
404 buf += page_remain; /* buffer point increase */
405 len -= page_remain; /* length decrease */
406 if (len < 8) /* check length */
407 {
408 page_remain = (uint8_t)len; /* set the rest length */
409 }
410 else
411 {
412 page_remain = 8; /* set page */
413 }
414 }
415 }
416 }
417 else
418 {
419 while (1)
420 {
421 if (handle->iic_write((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256, buf,
422 page_remain) != 0) /* write page */
423 {
424 handle->debug_print("mb85rcxx: write failed.\n"); /* write failed */
425
426 return 1; /* return error */
427 }
428 if (page_remain == len) /* check break */
429 {
430 break; /* break */
431 }
432 else
433 {
434 address += page_remain; /* address increase */
435 buf += page_remain; /* buffer point increase */
436 len -= page_remain; /* length decrease */
437 if (len < 8) /* check length */
438 {
439 page_remain = (uint8_t)len; /* set the rest length */
440 }
441 else
442 {
443 page_remain = 8; /* set page */
444 }
445 }
446 }
447 }
448
449 return 0; /* success return 0 */
450}
451
461{
462 if (info == NULL) /* check handle */
463 {
464 return 2; /* return error */
465 }
466
467 memset(info, 0, sizeof(mb85rcxx_info_t)); /* initialize mb85rcxx info structure */
468 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
469 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
470 strncpy(info->interface, "IIC", 8); /* copy interface name */
471 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
472 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
473 info->max_current_ma = MAX_CURRENT; /* set maximum current */
474 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
475 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
476 info->driver_version = DRIVER_VERSION; /* set driver version */
477
478 return 0; /* success return 0 */
479}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver mb85rcxx header file
uint8_t mb85rcxx_deinit(mb85rcxx_handle_t *handle)
close the chip
uint8_t mb85rcxx_get_type(mb85rcxx_handle_t *handle, mb85rcxx_t *type)
get the chip type
uint8_t mb85rcxx_set_addr_pin(mb85rcxx_handle_t *handle, mb85rcxx_address_t addr_pin)
set the chip address pin
mb85rcxx_address_t
mb85rcxx address enumeration definition
mb85rcxx_t
mb85rcxx type enumeration definition
struct mb85rcxx_info_s mb85rcxx_info_t
mb85rcxx information structure definition
uint8_t mb85rcxx_write(mb85rcxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
write bytes to the chip
uint8_t mb85rcxx_info(mb85rcxx_info_t *info)
get chip's information
uint8_t mb85rcxx_get_addr_pin(mb85rcxx_handle_t *handle, mb85rcxx_address_t *addr_pin)
get the chip address pin
uint8_t mb85rcxx_set_type(mb85rcxx_handle_t *handle, mb85rcxx_t type)
set the chip type
uint8_t mb85rcxx_init(mb85rcxx_handle_t *handle)
initialize the chip
uint8_t mb85rcxx_read(mb85rcxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
read bytes from the chip
struct mb85rcxx_handle_s mb85rcxx_handle_t
mb85rcxx handle structure definition
@ MB85RC16
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
char manufacturer_name[32]