LibDriver MB85RSXX
Loading...
Searching...
No Matches
driver_mb85rsxx.c
Go to the documentation of this file.
1
36
37#include "driver_mb85rsxx.h"
38
42#define CHIP_NAME "RAMXEED MB85RSXX"
43#define MANUFACTURER_NAME "RAMXEED"
44#define SUPPLY_VOLTAGE_MIN 1.8f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 2.6f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define MB85RSXX_COMMAND_WREN 0x06
55#define MB85RSXX_COMMAND_WRDI 0x04
56#define MB85RSXX_COMMAND_RDSR 0x05
57#define MB85RSXX_COMMAND_WRSR 0x01
58#define MB85RSXX_COMMAND_READ 0x03
59#define MB85RSXX_COMMAND_WRITE 0x02
60#define MB85RSXX_COMMAND_RDID 0x9F
61
73{
74 uint8_t res;
75 uint8_t buf[1];
76
77 if (handle == NULL) /* check handle */
78 {
79 return 2; /* return error */
80 }
81 if (handle->inited != 1) /* check handle initialization */
82 {
83 return 3; /* return error */
84 }
85
86 buf[0] = MB85RSXX_COMMAND_WREN; /* set command */
87 res = handle->spi_read_write(buf, 1, NULL, 0); /* write command */
88 if (res != 0) /* check the result */
89 {
90 handle->debug_print("mb85rsxx: enable write failed.\n"); /* enable write failed */
91
92 return 1; /* return error */
93 }
94
95 return 0; /* success return 0 */
96}
97
109{
110 uint8_t res;
111 uint8_t buf[1];
112
113 if (handle == NULL) /* check handle */
114 {
115 return 2; /* return error */
116 }
117 if (handle->inited != 1) /* check handle initialization */
118 {
119 return 3; /* return error */
120 }
121
122 buf[0] = MB85RSXX_COMMAND_WRDI; /* set command */
123 res = handle->spi_read_write(buf, 1, NULL, 0); /* write command */
124 if (res != 0) /* check the result */
125 {
126 handle->debug_print("mb85rsxx: disable write failed.\n"); /* disable write failed */
127
128 return 1; /* return error */
129 }
130
131 return 0; /* success return 0 */
132}
133
145uint8_t mb85rsxx_get_status(mb85rsxx_handle_t *handle, uint8_t *status)
146{
147 uint8_t res;
148 uint8_t buf[1];
149
150 if (handle == NULL) /* check handle */
151 {
152 return 2; /* return error */
153 }
154 if (handle->inited != 1) /* check handle initialization */
155 {
156 return 3; /* return error */
157 }
158
159 buf[0] = MB85RSXX_COMMAND_RDSR; /* set command */
160 res = handle->spi_read_write(buf, 1, status, 1); /* write command */
161 if (res != 0) /* check the result */
162 {
163 handle->debug_print("mb85rsxx: get status failed.\n"); /* get status failed */
164
165 return 1; /* return error */
166 }
167
168 return 0; /* success return 0 */
169}
170
182uint8_t mb85rsxx_set_status(mb85rsxx_handle_t *handle, uint8_t status)
183{
184 uint8_t res;
185 uint8_t buf[2];
186
187 if (handle == NULL) /* check handle */
188 {
189 return 2; /* return error */
190 }
191 if (handle->inited != 1) /* check handle initialization */
192 {
193 return 3; /* return error */
194 }
195
196 buf[0] = MB85RSXX_COMMAND_WREN; /* set command */
197 res = handle->spi_read_write(buf, 1, NULL, 0); /* write command */
198 if (res != 0) /* check the result */
199 {
200 handle->debug_print("mb85rsxx: enable write failed.\n"); /* enable write failed */
201
202 return 1; /* return error */
203 }
204
205 buf[0] = MB85RSXX_COMMAND_WRSR; /* set command */
206 buf[1] = status; /* set status */
207 res = handle->spi_read_write(buf, 2, NULL, 0); /* write command */
208 if (res != 0) /* check the result */
209 {
210 handle->debug_print("mb85rsxx: set status failed.\n"); /* set status failed */
211
212 return 1; /* return error */
213 }
214
215 buf[0] = MB85RSXX_COMMAND_WRDI; /* set command */
216 res = handle->spi_read_write(buf, 1, NULL, 0); /* write command */
217 if (res != 0) /* check the result */
218 {
219 handle->debug_print("mb85rsxx: disable write failed.\n"); /* disable write failed */
220
221 return 1; /* return error */
222 }
223
224 return 0; /* success return 0 */
225}
226
239{
240 uint8_t res;
241 uint8_t ind;
242 uint8_t buf[1];
243 uint8_t number[4];
244 const uint32_t density[] = {1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288};
245
246 if (handle == NULL) /* check handle */
247 {
248 return 2; /* return error */
249 }
250 if (handle->debug_print == NULL) /* check debug_print */
251 {
252 return 3; /* return error */
253 }
254 if (handle->spi_init == NULL) /* check spi_init */
255 {
256 handle->debug_print("mb85rsxx: spi_init is null.\n"); /* spi_init is null */
257
258 return 3; /* return error */
259 }
260 if (handle->spi_deinit == NULL) /* check spi_deinit */
261 {
262 handle->debug_print("mb85rsxx: spi_deinit is null.\n"); /* spi_deinit is null */
263
264 return 3; /* return error */
265 }
266 if (handle->spi_read_write == NULL) /* check spi_read_write */
267 {
268 handle->debug_print("mb85rsxx: spi_read_write is null.\n"); /* spi_read_write is null */
269
270 return 3; /* return error */
271 }
272 if (handle->delay_ms == NULL) /* check delay_ms */
273 {
274 handle->debug_print("mb85rsxx: delay_ms is null.\n"); /* delay_ms is null */
275
276 return 3; /* return error */
277 }
278
279 if (handle->spi_init() != 0) /* spi init */
280 {
281 handle->debug_print("mb85rsxx: spi init failed.\n"); /* spi init failed */
282
283 return 1; /* return error */
284 }
285#if (MB85RSXX_USE_DEVICE_ID != 0)
286 buf[0] = MB85RSXX_COMMAND_RDID; /* set command */
287 res = handle->spi_read_write(buf, 1, number, 4); /* write command */
288 if (res != 0) /* check the result */
289 {
290 handle->debug_print("mb85rsxx: get id failed.\n"); /* get id failed */
291 (void)handle->spi_deinit(); /* spi deinit */
292
293 return 4; /* return error */
294 }
295 if ((number[0] != 0x04) || (number[1] != 0x7F)) /* check id */
296 {
297 handle->debug_print("mb85rsxx: id is invalid.\n"); /* id is invalid */
298 (void)handle->spi_deinit(); /* spi deinit */
299
300 return 4; /* return error */
301 }
302 ind = number[2] & 0x1F; /* set index */
303 if (ind > 9) /* check index range */
304 {
305 handle->debug_print("mb85rsxx: id is invalid.\n"); /* id is invalid */
306 (void)handle->spi_deinit(); /* spi deinit */
307
308 return 4; /* return error */
309 }
310 if (density[ind] != handle->id) /* check density */
311 {
312 handle->debug_print("mb85rsxx: id is invalid.\n"); /* id is invalid */
313 (void)handle->spi_deinit(); /* spi deinit */
314
315 return 4; /* return error */
316 }
317#endif
318 handle->inited = 1; /* flag finish initialization */
319
320 return 0; /* success return 0 */
321}
322
334{
335 if (handle == NULL) /* check handle */
336 {
337 return 2; /* return error */
338 }
339 if (handle->inited != 1) /* check handle initialization */
340 {
341 return 3; /* return error */
342 }
343
344 if (handle->spi_deinit() != 0) /* spi deinit */
345 {
346 handle->debug_print("mb85rsxx: spi deinit failed.\n"); /* spi deinit failed */
347
348 return 1; /* return error */
349 }
350 handle->inited = 0; /* flag close */
351
352 return 0; /* success return 0 */
353}
354
365{
366 if (handle == NULL) /* check handle */
367 {
368 return 2; /* return error */
369 }
370
371 handle->id = (uint32_t)type; /* set id */
372
373 return 0; /* success return 0 */
374}
375
386{
387 if (handle == NULL) /* check handle */
388 {
389 return 2; /* return error */
390 }
391
392 *type = (mb85rsxx_t)(handle->id); /* get id */
393
394 return 0; /* success return 0 */
395}
396
411uint8_t mb85rsxx_read(mb85rsxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
412{
413 uint8_t res;
414 uint8_t reg[4];
415
416 if (handle == NULL) /* check handle */
417 {
418 return 2; /* return error */
419 }
420 if (handle->inited != 1) /* check handle initialization */
421 {
422 return 3; /* return error */
423 }
424 if ((address + len) > handle->id) /* check length */
425 {
426 handle->debug_print("mb85rsxx: read out of range.\n"); /* read out of range */
427
428 return 4; /* return error */
429 }
430
431 if (handle->id > (uint32_t)MB85RS512) /* choose id to set different address */
432 {
433 reg[0] = MB85RSXX_COMMAND_READ; /* set command */
434 reg[1] = (address >> 16) & 0xFF; /* address msb */
435 reg[2] = (address >> 8) & 0xFF; /* address mmsb */
436 reg[3] = (address >> 0) & 0xFF; /* address lsb */
437 res = handle->spi_read_write(reg, 4, buf, len); /* read data */
438 if (res != 0) /* check the result */
439 {
440 handle->debug_print("mb85rsxx: read failed.\n"); /* read failed */
441
442 return 1; /* return error */
443 }
444 }
445 else
446 {
447 reg[0] = MB85RSXX_COMMAND_READ; /* set command */
448 reg[1] = (address >> 8) & 0xFF; /* address msb */
449 reg[2] = (address >> 0) & 0xFF; /* address lsb */
450 res = handle->spi_read_write(reg, 3, buf, len); /* read data */
451 if (res != 0) /* check the result */
452 {
453 handle->debug_print("mb85rsxx: read failed.\n"); /* read failed */
454
455 return 1; /* return error */
456 }
457 }
458
459 return 0; /* success return 0 */
460}
461
477uint8_t mb85rsxx_write(mb85rsxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
478{
479 uint8_t res;
480 uint8_t reg[1];
481
482 if (handle == NULL) /* check handle */
483 {
484 return 2; /* return error */
485 }
486 if (handle->inited != 1) /* check handle initialization */
487 {
488 return 3; /* return error */
489 }
490 if ((address + len) > handle->id) /* check length */
491 {
492 handle->debug_print("mb85rsxx: write out of range.\n"); /* write out of range */
493
494 return 4; /* return error */
495 }
496 if (len > 512) /* check length */
497 {
498 handle->debug_print("mb85rsxx: len > 512.\n"); /* len > 512 */
499
500 return 5; /* return error */
501 }
502
503 reg[0] = MB85RSXX_COMMAND_WREN; /* set command */
504 res = handle->spi_read_write(reg, 1, NULL, 0); /* write command */
505 if (res != 0) /* check the result */
506 {
507 handle->debug_print("mb85rsxx: enable write failed.\n"); /* enable write failed */
508
509 return 1; /* return error */
510 }
511
512 if (handle->id > (uint32_t)MB85RS512) /* choose id to set different address */
513 {
514 handle->buf[0] = MB85RSXX_COMMAND_WRITE; /* set command */
515 handle->buf[1] = (address >> 16) & 0xFF; /* address msb */
516 handle->buf[2] = (address >> 8) & 0xFF; /* address mmsb */
517 handle->buf[3] = (address >> 0) & 0xFF; /* address lsb */
518 memcpy((uint8_t *)(handle->buf + 4), (uint8_t *)buf, len); /* copy data */
519 res = handle->spi_read_write(handle->buf, 4 + len, NULL, 0); /* write data */
520 if (res != 0) /* check the result */
521 {
522 handle->debug_print("mb85rsxx: write failed.\n"); /* write failed */
523
524 return 1; /* return error */
525 }
526 }
527 else
528 {
529 handle->buf[0] = MB85RSXX_COMMAND_WRITE; /* set command */
530 handle->buf[1] = (address >> 8) & 0xFF; /* address msb */
531 handle->buf[2] = (address >> 0) & 0xFF; /* address lsb */
532 memcpy((uint8_t *)(handle->buf + 3), (uint8_t *)buf, len); /* copy data */
533 res = handle->spi_read_write(handle->buf, 3 + len, NULL, 0); /* write data */
534 if (res != 0) /* check the result */
535 {
536 handle->debug_print("mb85rsxx: write failed.\n"); /* write failed */
537
538 return 1; /* return error */
539 }
540 }
541
542 reg[0] = MB85RSXX_COMMAND_WRDI; /* set command */
543 res = handle->spi_read_write(reg, 1, NULL, 0); /* write command */
544 if (res != 0) /* check the result */
545 {
546 handle->debug_print("mb85rsxx: disable write failed.\n"); /* disable write failed */
547
548 return 1; /* return error */
549 }
550
551 return 0; /* success return 0 */
552}
553
568uint8_t mb85rsxx_get_device_id(mb85rsxx_handle_t *handle, uint8_t *manufacturer_id, uint8_t *continuation_code,
569 uint8_t *product_id1, uint8_t *product_id2)
570{
571#if (MB85RSXX_USE_DEVICE_ID != 0)
572 uint8_t res;
573 uint8_t buf[1];
574 uint8_t number[4];
575
576 if (handle == NULL) /* check handle */
577 {
578 return 2; /* return error */
579 }
580 if (handle->inited != 1) /* check handle initialization */
581 {
582 return 3; /* return error */
583 }
584
585 buf[0] = MB85RSXX_COMMAND_RDID; /* set command */
586 res = handle->spi_read_write(buf, 1, number, 4); /* write command */
587 if (res != 0) /* check the result */
588 {
589 handle->debug_print("mb85rsxx: get id failed.\n"); /* get id failed */
590
591 return 1; /* return error */
592 }
593 *manufacturer_id = number[0]; /* set manufacturer id */
594 *continuation_code = number[1]; /* set continuation code */
595 *product_id1 = number[2]; /* set product id1 */
596 *product_id2 = number[3]; /* set product id2 */
597
598 return 0; /* success return 0 */
599#else
600 if (handle == NULL) /* check handle */
601 {
602 return 2; /* return error */
603 }
604 if (handle->inited != 1) /* check handle initialization */
605 {
606 return 3; /* return error */
607 }
608
609 handle->debug_print("mb85rsxx: this function is disable.\n"); /* this function is disable */
610
611 return 1; /* return error */
612#endif
613}
614
624{
625 if (info == NULL) /* check handle */
626 {
627 return 2; /* return error */
628 }
629
630 memset(info, 0, sizeof(mb85rsxx_info_t)); /* initialize mb85rsxx info structure */
631 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
632 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
633 strncpy(info->interface, "SPI", 8); /* copy interface name */
634 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
635 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
636 info->max_current_ma = MAX_CURRENT; /* set maximum current */
637 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
638 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
639 info->driver_version = DRIVER_VERSION; /* set driver version */
640
641 return 0; /* success return 0 */
642}
#define MB85RSXX_COMMAND_WRITE
#define MAX_CURRENT
#define MB85RSXX_COMMAND_WRDI
#define MB85RSXX_COMMAND_READ
#define MB85RSXX_COMMAND_RDSR
#define MB85RSXX_COMMAND_WREN
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MB85RSXX_COMMAND_RDID
#define MB85RSXX_COMMAND_WRSR
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver mb85rsxx header file
uint8_t mb85rsxx_info(mb85rsxx_info_t *info)
get chip's information
uint8_t mb85rsxx_enable_write(mb85rsxx_handle_t *handle)
enable write
uint8_t mb85rsxx_get_device_id(mb85rsxx_handle_t *handle, uint8_t *manufacturer_id, uint8_t *continuation_code, uint8_t *product_id1, uint8_t *product_id2)
get device id
uint8_t mb85rsxx_get_type(mb85rsxx_handle_t *handle, mb85rsxx_t *type)
get the chip type
uint8_t mb85rsxx_write(mb85rsxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
write bytes to the chip
uint8_t mb85rsxx_read(mb85rsxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
read bytes from the chip
struct mb85rsxx_info_s mb85rsxx_info_t
mb85rsxx information structure definition
uint8_t mb85rsxx_set_status(mb85rsxx_handle_t *handle, uint8_t status)
set status
uint8_t mb85rsxx_set_type(mb85rsxx_handle_t *handle, mb85rsxx_t type)
set the chip type
struct mb85rsxx_handle_s mb85rsxx_handle_t
mb85rsxx handle structure definition
uint8_t mb85rsxx_deinit(mb85rsxx_handle_t *handle)
close the chip
mb85rsxx_t
mb85rsxx type enumeration definition
uint8_t mb85rsxx_get_status(mb85rsxx_handle_t *handle, uint8_t *status)
get status
uint8_t mb85rsxx_disable_write(mb85rsxx_handle_t *handle)
disable write
uint8_t mb85rsxx_init(mb85rsxx_handle_t *handle)
initialize the chip
@ MB85RS512
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t buf[512+4]
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* spi_read_write)(uint8_t *in_buf, uint32_t in_len, uint8_t *out_buf, uint32_t out_len)
char manufacturer_name[32]