42#define CHIP_NAME "Sensirion SFA30"
43#define MANUFACTURER_NAME "Sensirion"
44#define SUPPLY_VOLTAGE_MIN 3.15f
45#define SUPPLY_VOLTAGE_MAX 5.50f
46#define MAX_CURRENT 5.0f
47#define TEMPERATURE_MIN 0.0f
48#define TEMPERATURE_MAX 40.0f
49#define DRIVER_VERSION 1000
54#define SFA30_ADDRESS (0x5D << 1)
59#define SFA30_IIC_COMMAND_START_MEASUREMENT 0x0006U
60#define SFA30_IIC_COMMAND_STOP_MEASUREMENT 0x0104U
61#define SFA30_IIC_COMMAND_READ_MEASURED_VALUES 0x0327U
62#define SFA30_IIC_COMMAND_READ_DEVICE_INFORMATION 0xD060U
63#define SFA30_IIC_COMMAND_RESET 0xD304U
64#define SFA30_UART_COMMAND_START_MEASUREMENT 0x00
65#define SFA30_UART_COMMAND_STOP_MEASUREMENT 0x01
66#define SFA30_UART_COMMAND_READ_MEASURED_VALUES 0x03
67#define SFA30_UART_COMMAND_READ_DEVICE_INFORMATION 0xD0
68#define SFA30_UART_COMMAND_RESET 0xD3
78static uint8_t a_sfa30_generate_crc(
sfa30_handle_t *handle, uint8_t* data, uint8_t count)
86 for (current_byte = 0; current_byte < count; ++current_byte)
88 crc ^= (data[current_byte]);
89 for (crc_bit = 8; crc_bit > 0; --crc_bit)
91 if ((crc & 0x80) != 0)
93 crc = (crc << 1) ^ 0x31;
109 for (i = 0; i < count; i++)
114 return (uint8_t)(~(crc & 0xFF));
131static uint8_t a_sfa30_iic_read(
sfa30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len, uint16_t delay_ms)
135 buf[0] = (reg >> 8) & 0xFF;
136 buf[1] = (reg >> 0) & 0xFF;
142 if (handle->
iic_read_cmd(addr, (uint8_t *)data, len) != 0)
163static uint8_t a_sfa30_iic_write(
sfa30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len, uint16_t delay_ms)
171 buf[0] = (reg >> 8) & 0xFF;
172 buf[1] = (reg >> 0) & 0xFF;
173 memcpy((uint8_t *)&buf[2], data, len);
174 if (handle->
iic_write_cmd(addr, (uint8_t *)buf, len + 2) != 0)
194static uint8_t a_sfa30_uart_get_rx_frame(
sfa30_handle_t *handle, uint16_t len, uint8_t *output, uint16_t out_len)
198 output[0] = handle->
buf[0];
200 for (i = 1; i < (len - 1); i++)
202 if (point >= (out_len - 1))
206 if (((handle->
buf[i] == 0x7D) && (handle->
buf[i + 1]) == 0x5E) ||
207 ((handle->
buf[i] == 0x7D) && (handle->
buf[i + 1]) == 0x5D) ||
208 ((handle->
buf[i] == 0x7D) && (handle->
buf[i + 1]) == 0x31) ||
209 ((handle->
buf[i] == 0x7D) && (handle->
buf[i + 1]) == 0x33)
212 switch (handle->
buf[i + 1])
216 output[point] = 0x7E;
224 output[point] = 0x7D;
232 output[point] = 0x11;
240 output[point] = 0x13;
254 output[point] = handle->
buf[i];
258 output[point] = handle->
buf[len - 1];
260 if (point != out_len)
279static uint8_t a_sfa30_uart_set_tx_frame(
sfa30_handle_t *handle, uint8_t *input, uint16_t in_len, uint16_t *out_len)
283 memset(handle->
buf, 0,
sizeof(uint8_t) * 256);
284 handle->
buf[0] = input[0];
286 for (i = 1; i < (in_len - 1); i++)
288 if ((*out_len) >= 255)
292 if ((input[i] == 0x7E) || (input[i] == 0x7D) ||
293 (input[i] == 0x11) || (input[i] == 0x13)
300 handle->
buf[*out_len] = 0x7D;
302 handle->
buf[*out_len] = 0x5E;
309 handle->
buf[*out_len] = 0x7D;
311 handle->
buf[*out_len] = 0x5D;
318 handle->
buf[*out_len] = 0x7D;
320 handle->
buf[*out_len] = 0x31;
327 handle->
buf[*out_len] = 0x7D;
329 handle->
buf[*out_len] = 0x33;
342 handle->
buf[*out_len] = input[i];
346 handle->
buf[*out_len] = input[in_len - 1];
365static uint8_t a_sfa30_uart_write_read(
sfa30_handle_t *handle, uint8_t *input, uint16_t in_len,
366 uint16_t delay_ms, uint8_t *output, uint16_t out_len)
370 if (a_sfa30_uart_set_tx_frame(handle, input, in_len, (uint16_t *)&len) != 0)
384 if (a_sfa30_uart_get_rx_frame(handle, len, output, out_len) != 0)
399static uint8_t a_sfa30_uart_error(
sfa30_handle_t *handle, uint8_t e)
409 handle->
debug_print(
"sfa30: wrong data length for this command error.\n");
421 handle->
debug_print(
"sfa30: no access right for command.\n");
427 handle->
debug_print(
"sfa30: illegal command parameter or parameter "
428 "out of allowed range.\n");
434 handle->
debug_print(
"sfa30: no measurement data available.\n");
440 handle->
debug_print(
"sfa30: command not allowed in current state.\n");
483 handle->
iic_uart = (uint8_t)interface;
534 uint8_t input_buf[6 + 1];
542 input_buf[5] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 4);
544 memset(out_buf, 0,
sizeof(uint8_t) * 7);
545 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 7, 10, (uint8_t *)out_buf, 7);
548 handle->
debug_print(
"sfa30: write read failed.\n");
552 if (out_buf[5] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 4))
558 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
568 handle->
debug_print(
"sfa30: start measurement failed.\n");
602 uint8_t input_buf[6 + 0];
609 input_buf[4] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 3);
611 memset(out_buf, 0,
sizeof(uint8_t) * 7);
612 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 6, 10, (uint8_t *)out_buf, 7);
615 handle->
debug_print(
"sfa30: write read failed.\n");
619 if (out_buf[5] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 4))
625 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
635 handle->
debug_print(
"sfa30: stop measurement failed.\n");
671 uint8_t input_buf[6 + 1];
672 uint8_t out_buf[7 + 17];
679 input_buf[5] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 4);
681 memset(info, 0,
sizeof(
char) * 32);
682 memset(out_buf, 0,
sizeof(uint8_t) * 24);
683 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 7, 10, (uint8_t *)out_buf, 24);
686 handle->
debug_print(
"sfa30: write read failed.\n");
690 if (out_buf[21] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 22))
696 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
700 memcpy((uint8_t *)info, (uint8_t *)&out_buf[5], 17);
707 memset(info, 0,
sizeof(
char) * 32);
708 memset(buf, 0,
sizeof(uint8_t) * 48);
713 handle->
debug_print(
"sfa30: read measured values failed.\n");
717 for (i = 0; i < 16; i++)
719 if (buf[i * 3 + 2] != a_sfa30_generate_crc(handle, (uint8_t *)&buf[i * 3], 2))
726 for (i = 0; i < 16; i++)
728 info[i * 2 + 0] = (char)buf[i * 3 + 0];
729 info[i * 2 + 1] = (char)buf[i * 3 + 1];
761 uint8_t input_buf[6];
768 input_buf[4] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 3);
770 memset(out_buf, 0,
sizeof(uint8_t) * 7);
771 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 6, 200, (uint8_t *)out_buf, 7);
774 handle->
debug_print(
"sfa30: write read failed.\n");
778 if (out_buf[5] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 4))
784 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
818 if ((handle == NULL) || (data == NULL))
829 uint8_t input_buf[6 + 1];
830 uint8_t out_buf[7 + 6];
837 input_buf[5] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 4);
839 memset(out_buf, 0,
sizeof(uint8_t) * 13);
840 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 7, 100, (uint8_t *)out_buf, 13);
843 handle->
debug_print(
"sfa30: write read failed.\n");
847 if (out_buf[11] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 10))
853 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
857 data->
formaldehyde_raw = (uint16_t)(((uint16_t)(out_buf[5 + 0]) << 8) | ((uint16_t)(out_buf[5 + 1]) << 0));
858 data->
humidity_raw = (uint16_t)(((uint16_t)(out_buf[5 + 2]) << 8) | ((uint16_t)(out_buf[5 + 3]) << 0));
859 data->
temperature_raw = (uint16_t)(((uint16_t)(out_buf[5 + 4]) << 8) | ((uint16_t)(out_buf[5 + 5]) << 0));
869 memset(buf, 0,
sizeof(uint8_t) * 9);
873 handle->
debug_print(
"sfa30: read measured values failed.\n");
877 for (i = 0; i < 3; i++)
879 if (buf[i * 3 + 2] != a_sfa30_generate_crc(handle, (uint8_t *)&buf[i * 3], 2))
886 data->
formaldehyde_raw = (uint16_t)(((uint16_t)(buf[0]) << 8) | ((uint16_t)(buf[1]) << 0));
887 data->
humidity_raw = (uint16_t)(((uint16_t)(buf[3]) << 8) | ((uint16_t)(buf[4]) << 0));
888 data->
temperature_raw = (uint16_t)(((uint16_t)(buf[6]) << 8) | ((uint16_t)(buf[7]) << 0));
928 handle->
debug_print(
"sfa30: iic_deinit is null.\n");
934 handle->
debug_print(
"sfa30: iic_write_cmd is null.\n");
940 handle->
debug_print(
"sfa30: iic_read_cmd is null.\n");
946 handle->
debug_print(
"sfa30: uart_init is null.\n");
952 handle->
debug_print(
"sfa30: uart_deinit is null.\n");
958 handle->
debug_print(
"sfa30: uart_read is null.\n");
964 handle->
debug_print(
"sfa30: uart_write is null.\n");
970 handle->
debug_print(
"sfa30: uart_flush is null.\n");
983 uint8_t input_buf[6];
996 input_buf[4] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 3);
998 memset(out_buf, 0,
sizeof(uint8_t) * 7);
999 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 6, 100, (uint8_t *)out_buf, 7);
1002 handle->
debug_print(
"sfa30: write read failed.\n");
1007 if (out_buf[5] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 4))
1014 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
1069 uint8_t input_buf[6];
1072 input_buf[0] = 0x7E;
1073 input_buf[1] = 0x00;
1075 input_buf[3] = 0x00;
1076 input_buf[4] = a_sfa30_generate_crc(handle, (uint8_t *)&input_buf[1], 3);
1077 input_buf[5] = 0x7E;
1078 memset(out_buf, 0,
sizeof(uint8_t) * 7);
1079 res = a_sfa30_uart_write_read(handle, (uint8_t *)input_buf, 6, 100, (uint8_t *)out_buf, 7);
1082 handle->
debug_print(
"sfa30: write read failed.\n");
1086 if (out_buf[5] != a_sfa30_generate_crc(handle, (uint8_t *)&out_buf[1], 4))
1092 if (a_sfa30_uart_error(handle, out_buf[3]) != 0)
1098 handle->
debug_print(
"sfa30: uart deinit failed.\n");
1115 handle->
debug_print(
"sfa30: iic deinit failed.\n");
1151 return a_sfa30_uart_write_read(handle, input, in_len, 20, output, out_len);
1155 handle->
debug_print(
"sfa30: iic interface is invalid.\n");
1187 handle->
debug_print(
"sfa30: uart interface is invalid.\n");
1193 return a_sfa30_iic_write(handle,
SFA30_ADDRESS, reg, buf, len, 20);
1223 handle->
debug_print(
"sfa30: uart interface is invalid.\n");
1229 return a_sfa30_iic_read(handle,
SFA30_ADDRESS, reg, buf, len, 20);
1251 strncpy(info->
interface,
"UART IIC", 16);
#define SFA30_UART_COMMAND_RESET
#define SFA30_UART_COMMAND_START_MEASUREMENT
#define SFA30_UART_COMMAND_READ_MEASURED_VALUES
#define SFA30_UART_COMMAND_STOP_MEASUREMENT
#define SFA30_IIC_COMMAND_READ_DEVICE_INFORMATION
#define SFA30_UART_COMMAND_READ_DEVICE_INFORMATION
#define SFA30_IIC_COMMAND_STOP_MEASUREMENT
#define SUPPLY_VOLTAGE_MAX
#define SFA30_IIC_COMMAND_RESET
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define SFA30_ADDRESS
chip address definition
#define SFA30_IIC_COMMAND_READ_MEASURED_VALUES
#define SFA30_IIC_COMMAND_START_MEASUREMENT
chip command definition
uint8_t sfa30_info(sfa30_info_t *info)
get chip information
sfa30_interface_t
sfa30 interface enumeration definition
struct sfa30_handle_s sfa30_handle_t
sfa30 handle structure definition
uint8_t sfa30_set_interface(sfa30_handle_t *handle, sfa30_interface_t interface)
set the chip interface
uint8_t sfa30_init(sfa30_handle_t *handle)
initialize the chip
uint8_t sfa30_get_interface(sfa30_handle_t *handle, sfa30_interface_t *interface)
get the chip interface
uint8_t sfa30_deinit(sfa30_handle_t *handle)
close the chip
uint8_t sfa30_get_device_information(sfa30_handle_t *handle, char info[32])
get device information
uint8_t sfa30_reset(sfa30_handle_t *handle)
reset the chip
uint8_t sfa30_read(sfa30_handle_t *handle, sfa30_data_t *data)
read the result
struct sfa30_data_s sfa30_data_t
sfa30 data structure definition
uint8_t sfa30_stop_measurement(sfa30_handle_t *handle)
stop the measurement
uint8_t sfa30_start_measurement(sfa30_handle_t *handle)
start the measurement
struct sfa30_info_s sfa30_info_t
sfa30 information structure definition
uint8_t sfa30_get_reg_iic(sfa30_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
get the chip register with iic interface
uint8_t sfa30_set_reg_iic(sfa30_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
set the chip register with iic interface
uint8_t sfa30_set_get_reg_uart(sfa30_handle_t *handle, uint8_t *input, uint16_t in_len, uint8_t *output, uint16_t out_len)
set and get the chip register with uart interface
uint8_t(* uart_flush)(void)
uint8_t(* uart_write)(uint8_t *buf, uint16_t len)
void(* delay_ms)(uint32_t ms)
uint8_t(* uart_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* uart_init)(void)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v