LibDriver JED1XX
Loading...
Searching...
No Matches
driver_jed1xx.c
Go to the documentation of this file.
1
36
37#include "driver_jed1xx.h"
38
42#define CHIP_NAME "JXCT JED1XX"
43#define MANUFACTURER_NAME "JXCT"
44#define SUPPLY_VOLTAGE_MIN 3.3f
45#define SUPPLY_VOLTAGE_MAX 5.0f
46#define MAX_CURRENT 25.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define JED1XX_ADDRESS 0x54
55
67static uint8_t a_jed1xx_iic_read(jed1xx_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
68{
69 if (handle->iic_read(JED1XX_ADDRESS, reg, data, len) != 0) /* read the register */
70 {
71 return 1; /* return error */
72 }
73
74 return 0; /* success return 0 */
75}
76
88static uint8_t a_jed1xx_iic_write(jed1xx_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
89{
90 if (handle->iic_write(JED1XX_ADDRESS, reg, data, len) != 0) /* write the register */
91 {
92 return 1; /* return error */
93 }
94
95 return 0; /* success return 0 */
96}
97
108{
109 if (handle == NULL) /* check handle */
110 {
111 return 2; /* return error */
112 }
113
114 handle->type = (uint32_t)(type); /* set type */
115
116 return 0; /* success return 0 */
117}
118
129{
130 if (handle == NULL) /* check handle */
131 {
132 return 2; /* return error */
133 }
134
135 *type = (jed1xx_type_t)(handle->type); /* get type */
136
137 return 0; /* success return 0 */
138}
139
151{
152 if (handle == NULL) /* check handle */
153 {
154 return 2; /* return error */
155 }
156 if (handle->debug_print == NULL) /* check debug_print */
157 {
158 return 3; /* return error */
159 }
160 if (handle->iic_init == NULL) /* check iic_init */
161 {
162 handle->debug_print("jed1xx: iic_init is null.\n"); /* iic_init is null */
163
164 return 3; /* return error */
165 }
166 if (handle->iic_deinit == NULL) /* check iic_deinit */
167 {
168 handle->debug_print("jed1xx: iic_deinit is null.\n"); /* iic_deinit is null */
169
170 return 3; /* return error */
171 }
172 if (handle->iic_read == NULL) /* check iic_read */
173 {
174 handle->debug_print("jed1xx: iic_read is null.\n"); /* iic_read is null */
175
176 return 3; /* return error */
177 }
178 if (handle->iic_write == NULL) /* check iic_write */
179 {
180 handle->debug_print("jed1xx: iic_write is null.\n"); /* iic_write is null */
181
182 return 3; /* return error */
183 }
184 if (handle->delay_ms == NULL) /* check delay_ms */
185 {
186 handle->debug_print("jed1xx: delay_ms is null.\n"); /* delay_ms is null */
187
188 return 3; /* return error */
189 }
190
191 if (handle->iic_init() != 0) /* iic init */
192 {
193 handle->debug_print("jed1xx: iic init failed.\n"); /* iic init failed */
194
195 return 1; /* return error */
196 }
197 handle->inited = 1; /* flag finish initialization */
198
199 return 0; /* success return 0 */
200}
201
213{
214 if (handle == NULL) /* check handle */
215 {
216 return 2; /* return error */
217 }
218 if (handle->inited != 1) /* check handle initialization */
219 {
220 return 3; /* return error */
221 }
222
223 if (handle->iic_deinit() != 0) /* iic deinit */
224 {
225 handle->debug_print("jed1xx: iic deinit failed.\n"); /* iic deinit failed */
226
227 return 1; /* return error */
228 }
229 handle->inited = 0; /* set closed flag */
230
231 return 0; /* success return 0 */
232}
233
246uint8_t jed1xx_read(jed1xx_handle_t *handle, uint16_t *raw, float *ppm)
247{
248 uint8_t res;
249 uint8_t buf[2];
250
251 if (handle == NULL) /* check handle */
252 {
253 return 2; /* return error */
254 }
255 if (handle->inited != 1) /* check handle initialization */
256 {
257 return 3; /* return error */
258 }
259
260 res = a_jed1xx_iic_read(handle, 0xA1, buf, 2); /* read data */
261 if (res != 0) /* check the result */
262 {
263 handle->debug_print("jed1xx: read data failed.\n"); /* read data failed */
264
265 return 1; /* return error */
266 }
267 *raw = (((uint16_t)buf[0]) << 8) | buf[1]; /* get raw data */
268 *ppm = (float)(*raw) / 10000.0f * (float)(handle->type); /* convert ppm */
269
270 return 0; /* success return 0 */
271}
272
286uint8_t jed1xx_set_reg(jed1xx_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
287{
288 if (handle == NULL) /* check handle */
289 {
290 return 2; /* return error */
291 }
292 if (handle->inited != 1) /* check handle initialization */
293 {
294 return 3; /* return error */
295 }
296
297 if (a_jed1xx_iic_write(handle, reg, buf, len) != 0) /* write data */
298 {
299 return 1; /* return error */
300 }
301
302 return 0; /* success return 0 */
303
304}
305
319uint8_t jed1xx_get_reg(jed1xx_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
320{
321 if (handle == NULL) /* check handle */
322 {
323 return 2; /* return error */
324 }
325 if (handle->inited != 1) /* check handle initialization */
326 {
327 return 3; /* return error */
328 }
329
330 if (a_jed1xx_iic_read(handle, reg, buf, len) != 0) /* read data */
331 {
332 return 1; /* return error */
333 }
334
335 return 0; /* success return 0 */
336}
337
347{
348 if (info == NULL) /* check handle */
349 {
350 return 2; /* return error */
351 }
352
353 memset(info, 0, sizeof(jed1xx_info_t)); /* initialize jed1xx info structure */
354 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
355 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
356 strncpy(info->interface, "IIC", 8); /* copy interface name */
357 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
358 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
359 info->max_current_ma = MAX_CURRENT; /* set maximum current */
360 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
361 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
362 info->driver_version = DRIVER_VERSION; /* set driver version */
363
364 return 0; /* success return 0 */
365}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define JED1XX_ADDRESS
chip address definition
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver jed1xx header file
uint8_t jed1xx_deinit(jed1xx_handle_t *handle)
close the chip
struct jed1xx_handle_s jed1xx_handle_t
jed1xx handle structure definition
struct jed1xx_info_s jed1xx_info_t
jed1xx information structure definition
uint8_t jed1xx_set_type(jed1xx_handle_t *handle, jed1xx_type_t type)
set the chip type
uint8_t jed1xx_read(jed1xx_handle_t *handle, uint16_t *raw, float *ppm)
read data
jed1xx_type_t
jed1xx type enumeration definition
uint8_t jed1xx_info(jed1xx_info_t *info)
get chip's information
uint8_t jed1xx_init(jed1xx_handle_t *handle)
initialize the chip
uint8_t jed1xx_get_type(jed1xx_handle_t *handle, jed1xx_type_t *type)
get the chip type
uint8_t jed1xx_get_reg(jed1xx_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t jed1xx_set_reg(jed1xx_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
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)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]