LibDriver SGP30
Loading...
Searching...
No Matches
driver_sgp30_register_test.c
Go to the documentation of this file.
1
37
39#include <stdlib.h>
40
41static sgp30_handle_t gs_handle;
42
51{
52 uint8_t res;
53 uint16_t id[3];
54 uint8_t product_type;
55 uint8_t product_version;
56 uint16_t tvoc, tvoc_check;
57 uint16_t co2_eq, co2_eq_check;
58 uint16_t humidity;
59 float temp;
60 float rh;
61 sgp30_info_t info;
62
63 /* link functions */
71
72 /* sgp30 info */
73 res = sgp30_info(&info);
74 if (res != 0)
75 {
76 sgp30_interface_debug_print("sgp30: get info failed.\n");
77
78 return 1;
79 }
80 else
81 {
82 /* print chip information */
83 sgp30_interface_debug_print("sgp30: chip is %s.\n", info.chip_name);
84 sgp30_interface_debug_print("sgp30: manufacturer is %s.\n", info.manufacturer_name);
85 sgp30_interface_debug_print("sgp30: interface is %s.\n", info.interface);
86 sgp30_interface_debug_print("sgp30: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
87 sgp30_interface_debug_print("sgp30: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
88 sgp30_interface_debug_print("sgp30: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
89 sgp30_interface_debug_print("sgp30: max current is %0.2fmA.\n", info.max_current_ma);
90 sgp30_interface_debug_print("sgp30: max temperature is %0.1fC.\n", info.temperature_max);
91 sgp30_interface_debug_print("sgp30: min temperature is %0.1fC.\n", info.temperature_min);
92 }
93
94 /* start register test */
95 sgp30_interface_debug_print("sgp30: start register test.\n");
96
97 /* sgp30 init */
98 res = sgp30_init(&gs_handle);
99 if (res != 0)
100 {
101 sgp30_interface_debug_print("sgp30: init failed.\n");
102
103 return 1;
104 }
105
106 /* sgp30 soft reset test */
107 sgp30_interface_debug_print("sgp30: sgp30_soft_reset test.\n");
108
109 /* soft reset */
110 res = sgp30_soft_reset(&gs_handle);
111 if (res != 0)
112 {
113 sgp30_interface_debug_print("sgp30: soft failed.\n");
114 (void)sgp30_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* delay 100 ms */
121 sgp30_interface_debug_print("sgp30: check soft reset %s.\n", res==0?"ok":"error");
122
123 /* iaq init */
124 res = sgp30_iaq_init(&gs_handle);
125 if (res != 0)
126 {
127 sgp30_interface_debug_print("sgp30: iaq init failed.\n");
128 (void)sgp30_deinit(&gs_handle);
129
130 return 1;
131 }
132
133 /* sgp30 get serial id test */
134 sgp30_interface_debug_print("sgp30: sgp30_get_serial_id test.\n");
135
136 /* sgp30 get serial id */
137 res = sgp30_get_serial_id(&gs_handle, (uint16_t *)id);
138 if (res != 0)
139 {
140 sgp30_interface_debug_print("sgp30: get serial id failed.\n");
141 (void)sgp30_deinit(&gs_handle);
142
143 return 1;
144 }
145 sgp30_interface_debug_print("sgp30: check get serial id %s.\n", res==0?"ok":"error");
146 sgp30_interface_debug_print("sgp30: serial id 0x%04X 0x%04X 0x%04X.\n", (uint16_t)(id[0]), (uint16_t)(id[1]), (uint16_t)(id[2]));
147
148 /* sgp30_get_feature_set test */
149 sgp30_interface_debug_print("sgp30: sgp30_get_feature_set test.\n");
150 res = sgp30_get_feature_set(&gs_handle, (uint8_t *)&product_type, (uint8_t *)&product_version);
151 if (res != 0)
152 {
153 sgp30_interface_debug_print("sgp30: get feature set failed.\n");
154 (void)sgp30_deinit(&gs_handle);
155
156 return 1;
157 }
158 sgp30_interface_debug_print("sgp30: check get feature set %s.\n", res==0?"ok":"error");
159 sgp30_interface_debug_print("sgp30: product type is 0x%02X.\n", product_type);
160 sgp30_interface_debug_print("sgp30: product version is 0x%02X.\n", product_version);
161
162 /* sgp30_set_tvoc_baseline/sgp30_get_tvoc_inceptive_baseline test */
163 sgp30_interface_debug_print("sgp30: sgp30_set_tvoc_baseline/sgp30_get_tvoc_inceptive_baseline test.\n");
164 tvoc = (uint16_t)(rand() % 65535);
165 res = sgp30_set_tvoc_baseline(&gs_handle, tvoc);
166 if (res != 0)
167 {
168 sgp30_interface_debug_print("sgp30: set tvoc baseline failed.\n");
169 (void)sgp30_deinit(&gs_handle);
170
171 return 1;
172 }
173 sgp30_interface_debug_print("sgp30: set tvoc baseline 0x%04X.\n", (uint16_t)(tvoc));
174 res = sgp30_get_tvoc_inceptive_baseline(&gs_handle, (uint16_t *)&tvoc_check);
175 if (res != 0)
176 {
177 sgp30_interface_debug_print("sgp30: get tvoc inceptive baseline failed.\n");
178 (void)sgp30_deinit(&gs_handle);
179
180 return 1;
181 }
182 sgp30_interface_debug_print("sgp30: get tvoc inceptive baseline 0x%04X.\n", tvoc_check);
183
184 /* sgp30_set_iaq_baseline/sgp30_get_iaq_baseline test */
185 sgp30_interface_debug_print("sgp30: sgp30_set_iaq_baseline/sgp30_get_iaq_baseline test.\n");
186 tvoc = (uint16_t)(rand() % 65535);
187 co2_eq = (uint16_t)(rand() % 65535);
188 res = sgp30_set_iaq_baseline(&gs_handle, tvoc, co2_eq);
189 if (res != 0)
190 {
191 sgp30_interface_debug_print("sgp30: set iaq baseline failed.\n");
192 (void)sgp30_deinit(&gs_handle);
193
194 return 1;
195 }
196 sgp30_interface_debug_print("sgp30: set tvoc baseline 0x%04X.\n", (uint16_t)(tvoc));
197 sgp30_interface_debug_print("sgp30: set co2 eq baseline 0x%04X.\n", (uint16_t)(co2_eq));
198 res = sgp30_get_iaq_baseline(&gs_handle, (uint16_t *)&tvoc_check, (uint16_t *)&co2_eq_check);
199 if (res != 0)
200 {
201 sgp30_interface_debug_print("sgp30: get iaq baseline failed.\n");
202 (void)sgp30_deinit(&gs_handle);
203
204 return 1;
205 }
206 sgp30_interface_debug_print("sgp30: check iaq baseline %s.\n", ((tvoc==tvoc_check)&&(co2_eq==co2_eq_check))?"ok":"error");
207
208 /* sgp30_set_absolute_humidity test */
209 sgp30_interface_debug_print("sgp30: sgp30_set_absolute_humidity test.\n");
210 temp = (float)(rand() % 3000) / 100.0f;
211 rh = (float)(rand() % 10000) / 100.0f;
212 res = sgp30_absolute_humidity_convert_to_register(&gs_handle, temp, rh, (uint16_t *)&humidity);
213 if (res != 0)
214 {
215 sgp30_interface_debug_print("sgp30: absolute humidity convert to register failed.\n");
216 (void)sgp30_deinit(&gs_handle);
217
218 return 1;
219 }
220 sgp30_interface_debug_print("sgp30: set temp %0.1fC.\n", temp);
221 sgp30_interface_debug_print("sgp30: set rh %0.2f%%.\n", rh);
222 sgp30_interface_debug_print("sgp30: set register value 0x%04X.\n", (uint16_t)humidity);
223 res = sgp30_set_absolute_humidity(&gs_handle, humidity);
224 if (res != 0)
225 {
226 sgp30_interface_debug_print("sgp30: set absolute humidity failed.\n");
227 (void)sgp30_deinit(&gs_handle);
228
229 return 1;
230 }
231 sgp30_interface_debug_print("sgp30: check absolute humidity %s.\n", (res==0)?"ok":"error");
232
233 /* finish register test */
234 sgp30_interface_debug_print("sgp30: finish register test.\n");
235 (void)sgp30_deinit(&gs_handle);
236
237 return 0;
238}
driver sgp30 register test header file
uint8_t sgp30_get_iaq_baseline(sgp30_handle_t *handle, uint16_t *tvoc, uint16_t *co2_eq)
get the chip iaq baseline
uint8_t sgp30_set_tvoc_baseline(sgp30_handle_t *handle, uint16_t tvoc_baseline)
set the chip tvoc baseline
struct sgp30_handle_s sgp30_handle_t
sgp30 handle structure definition
uint8_t sgp30_init(sgp30_handle_t *handle)
initialize the chip
uint8_t sgp30_absolute_humidity_convert_to_register(sgp30_handle_t *handle, float temp, float rh, uint16_t *reg)
convert the absolute humidity to the register data
uint8_t sgp30_deinit(sgp30_handle_t *handle)
close the chip
struct sgp30_info_s sgp30_info_t
sgp30 information structure definition
uint8_t sgp30_get_serial_id(sgp30_handle_t *handle, uint16_t id[3])
get the chip serial id
uint8_t sgp30_set_iaq_baseline(sgp30_handle_t *handle, uint16_t tvoc, uint16_t co2_eq)
set the chip iaq baseline
uint8_t sgp30_set_absolute_humidity(sgp30_handle_t *handle, uint16_t humidity)
set the chip absolute_humidity
uint8_t sgp30_get_feature_set(sgp30_handle_t *handle, uint8_t *product_type, uint8_t *product_version)
get the chip feature
uint8_t sgp30_info(sgp30_info_t *info)
get chip information
uint8_t sgp30_soft_reset(sgp30_handle_t *handle)
soft reset the chip
uint8_t sgp30_iaq_init(sgp30_handle_t *handle)
initialize the chip iaq
uint8_t sgp30_get_tvoc_inceptive_baseline(sgp30_handle_t *handle, uint16_t *tvoc_baseline)
get the chip tvoc inceptive baseline
uint8_t sgp30_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t sgp30_interface_iic_init(void)
interface iic bus init
uint8_t sgp30_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void sgp30_interface_delay_ms(uint32_t ms)
interface delay ms
void sgp30_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sgp30_interface_iic_deinit(void)
interface iic bus deinit
uint8_t sgp30_register_test(void)
register test
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]