LibDriver AFS01
Loading...
Searching...
No Matches
driver_afs01.c
Go to the documentation of this file.
1
36
37#include "driver_afs01.h"
38
42#define CHIP_NAME "ASAIR AFS01"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 4.75f
45#define SUPPLY_VOLTAGE_MAX 5.25f
46#define MAX_CURRENT 10.0f
47#define TEMPERATURE_MIN 25.0f
48#define TEMPERATURE_MAX 50.0f
49#define DRIVER_VERSION 1000
50
54#define AFS01_ADDRESS 0x80
55
59#define AFS01_COMMAND_READ_DATA 0x1000U
60#define AFS01_COMMAND_READ_ID 0x31AEU
61
73static uint8_t a_afs01_read(afs01_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
74{
75 if (handle->iic_read_address16(AFS01_ADDRESS, reg, data, len) != 0) /* read the register */
76 {
77 return 1; /* return error */
78 }
79
80 return 0; /* success return 0 */
81}
82
90static uint8_t a_afs01_crc(uint8_t *data, uint16_t len)
91{
92 const uint8_t POLYNOMIAL = 0x31;
93 uint8_t crc = 0x00;
94 uint16_t i, j;
95
96 for (j = len; j != 0; --j) /* length-- */
97 {
98 crc ^= *data++; /* xor */
99 for (i = 8; i != 0; --i) /* 8 times */
100 {
101 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); /* calculate crc */
102 }
103 }
104
105 return crc; /* return crc */
106}
107
119{
120 if (handle == NULL) /* check handle */
121 {
122 return 2; /* return error */
123 }
124 if (handle->debug_print == NULL) /* check debug_print */
125 {
126 return 3; /* return error */
127 }
128 if (handle->iic_init == NULL) /* check iic_init */
129 {
130 handle->debug_print("afs01: iic_init is null.\n"); /* iic_init is null */
131
132 return 3; /* return error */
133 }
134 if (handle->iic_deinit == NULL) /* check iic_deinit */
135 {
136 handle->debug_print("afs01: iic_deinit is null.\n"); /* iic_deinit is null */
137
138 return 3; /* return error */
139 }
140 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
141 {
142 handle->debug_print("afs01: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
143
144 return 3; /* return error */
145 }
146 if (handle->delay_ms == NULL) /* check delay_ms */
147 {
148 handle->debug_print("afs01: delay_ms is null.\n"); /* delay_ms is null */
149
150 return 3; /* return error */
151 }
152
153 if (handle->iic_init() != 0) /* iic init */
154 {
155 handle->debug_print("afs01: iic init failed.\n"); /* iic init failed */
156
157 return 1; /* return error */
158 }
159 handle->inited = 1; /* flag finish initialization */
160
161 return 0; /* success return 0 */
162}
163
175{
176 if (handle == NULL) /* check handle */
177 {
178 return 2; /* return error */
179 }
180 if (handle->inited != 1) /* check handle initialization */
181 {
182 return 3; /* return error */
183 }
184
185 if (handle->iic_deinit() != 0) /* iic deinit */
186 {
187 handle->debug_print("afs01: iic deinit failed.\n"); /* iic deinit failed */
188
189 return 1; /* return error */
190 }
191 handle->inited = 0; /* flag close */
192
193 return 0; /* success return 0 */
194}
195
209uint8_t afs01_read(afs01_handle_t *handle, uint16_t *raw, float *sccm)
210{
211 uint8_t res;
212 uint8_t buf[3];
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 res = a_afs01_read(handle, AFS01_COMMAND_READ_DATA, buf, 3); /* read data */
224 if (res != 0) /* check the result */
225 {
226 handle->debug_print("afs01: read failed.\n"); /* read failed */
227
228 return 1; /* return error */
229 }
230 if (a_afs01_crc(buf, 2) != buf[2]) /* check crc */
231 {
232 handle->debug_print("afs01: crc error.\n"); /* crc error */
233
234 return 4; /* return error */
235 }
236 *raw = (uint16_t)(((uint16_t)buf[0]) << 8) | buf[1]; /* set raw data */
237 *sccm = ((float)(*raw) - 32000.0f) / 140.0f; /* set sccm */
238
239 return 0; /* success return 0 */
240}
241
254uint8_t afs01_get_chip_id(afs01_handle_t *handle, uint8_t id[4])
255{
256 uint8_t res;
257 uint8_t buf[6];
258
259 if (handle == NULL) /* check handle */
260 {
261 return 2; /* return error */
262 }
263 if (handle->inited != 1) /* check handle initialization */
264 {
265 return 3; /* return error */
266 }
267
268 res = a_afs01_read(handle, AFS01_COMMAND_READ_ID, buf, 6); /* read id */
269 if (res != 0) /* check the result */
270 {
271 handle->debug_print("afs01: read id failed.\n"); /* read id failed */
272
273 return 1; /* return error */
274 }
275 if (a_afs01_crc(buf, 2) != buf[2]) /* check crc */
276 {
277 handle->debug_print("afs01: crc error.\n"); /* crc error */
278
279 return 4; /* return error */
280 }
281 if (a_afs01_crc(buf + 3, 2) != buf[5]) /* check crc */
282 {
283 handle->debug_print("afs01: crc error.\n"); /* crc error */
284
285 return 4; /* return error */
286 }
287 id[0] = buf[0]; /* set id part 0 */
288 id[1] = buf[1]; /* set id part 1 */
289 id[2] = buf[3]; /* set id part 2 */
290 id[3] = buf[4]; /* set id part 3 */
291
292 return 0; /* success return 0 */
293}
294
308uint8_t si7021_get_reg(afs01_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
309{
310 if (handle == NULL) /* check handle */
311 {
312 return 2; /* return error */
313 }
314 if (handle->inited != 1) /* check handle initialization */
315 {
316 return 3; /* return error */
317 }
318
319 if (a_afs01_read(handle, reg, buf, len) != 0) /* read data */
320 {
321 return 1; /* return error */
322 }
323
324 return 0; /* success return 0 */
325}
326
336{
337 if (info == NULL) /* check handle */
338 {
339 return 2; /* return error */
340 }
341
342 memset(info, 0, sizeof(afs01_info_t)); /* initialize afs01 info structure */
343 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
344 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
345 strncpy(info->interface, "IIC", 8); /* copy interface name */
346 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
347 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
348 info->max_current_ma = MAX_CURRENT; /* set maximum current */
349 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
350 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
351 info->driver_version = DRIVER_VERSION; /* set driver version */
352
353 return 0; /* success return 0 */
354}
#define MAX_CURRENT
#define AFS01_COMMAND_READ_ID
#define AFS01_COMMAND_READ_DATA
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AFS01_ADDRESS
chip address definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver afs01 header file
uint8_t afs01_info(afs01_info_t *info)
get chip's information
struct afs01_info_s afs01_info_t
afs01 information structure definition
uint8_t afs01_get_chip_id(afs01_handle_t *handle, uint8_t id[4])
get chip id
uint8_t afs01_read(afs01_handle_t *handle, uint16_t *raw, float *sccm)
read data
uint8_t afs01_deinit(afs01_handle_t *handle)
close the chip
uint8_t afs01_init(afs01_handle_t *handle)
initialize the chip
struct afs01_handle_s afs01_handle_t
afs01 handle structure definition
uint8_t si7021_get_reg(afs01_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
get chip register
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_deinit)(void)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]