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 1.20f
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 if (handle == NULL) /* check handle */
259 {
260 return 2; /* return error */
261 }
262 if (handle->inited != 1) /* check handle initialization */
263 {
264 return 3; /* return error */
265 }
266 if ((address + len) > handle->id) /* check length */
267 {
268 handle->debug_print("mb85rcxx: read out of range.\n"); /* read out of range */
269
270 return 4; /* return error */
271 }
272
273 if (handle->id > (uint32_t)MB85RC16) /* choose id to set different address */
274 {
275 if (handle->iic_read_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
276 address % 65536, buf,
277 len) != 0) /* read page */
278 {
279 handle->debug_print("mb85rcxx: read failed.\n"); /* read failed */
280
281 return 1; /* return error */
282 }
283 }
284 else
285 {
286 if (handle->iic_read((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256,
287 buf, len) != 0) /* read page */
288 {
289 handle->debug_print("mb85rcxx: read failed.\n"); /* read failed */
290
291 return 1; /* return error */
292 }
293 }
294
295 return 0; /* success return 0 */
296}
297
312uint8_t mb85rcxx_write(mb85rcxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
313{
314 if (handle == NULL) /* check handle */
315 {
316 return 2; /* return error */
317 }
318 if (handle->inited != 1) /* check handle initialization */
319 {
320 return 3; /* return error */
321 }
322 if ((address + len) > handle->id) /* check length */
323 {
324 handle->debug_print("mb85rcxx: write out of range.\n"); /* write out of range */
325
326 return 4; /* return error */
327 }
328
329 if (handle->id > (uint32_t)MB85RC16) /* check id */
330 {
331 if (handle->iic_write_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
332 address % 65536, buf,
333 len) != 0) /* write page */
334 {
335 handle->debug_print("mb85rcxx: write failed.\n"); /* write failed */
336
337 return 1; /* return error */
338 }
339 }
340 else
341 {
342 if (handle->iic_write((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256,
343 buf, len) != 0) /* write page */
344 {
345 handle->debug_print("mb85rcxx: write failed.\n"); /* write failed */
346
347 return 1; /* return error */
348 }
349 }
350
351 return 0; /* success return 0 */
352}
353
363{
364 if (info == NULL) /* check handle */
365 {
366 return 2; /* return error */
367 }
368
369 memset(info, 0, sizeof(mb85rcxx_info_t)); /* initialize mb85rcxx info structure */
370 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
371 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
372 strncpy(info->interface, "IIC", 8); /* copy interface name */
373 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
374 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
375 info->max_current_ma = MAX_CURRENT; /* set maximum current */
376 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
377 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
378 info->driver_version = DRIVER_VERSION; /* set driver version */
379
380 return 0; /* success return 0 */
381}
#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]