LibDriver SPS30
Loading...
Searching...
No Matches
driver_sps30_register_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static sps30_handle_t gs_handle;
41
51{
52 uint8_t res;
53 uint32_t second, second_check;
54 char type[9];
55 char sn[17];
56 uint8_t major;
57 uint8_t minor;
58 uint32_t status;
59 sps30_info_t info;
60 sps30_interface_t interface_check;
62
63 /* link functions */
76
77 /* get information */
78 res = sps30_info(&info);
79 if (res != 0)
80 {
81 sps30_interface_debug_print("sps30: get info failed.\n");
82
83 return 1;
84 }
85 else
86 {
87 /* print chip info */
88 sps30_interface_debug_print("sps30: chip is %s.\n", info.chip_name);
89 sps30_interface_debug_print("sps30: manufacturer is %s.\n", info.manufacturer_name);
90 sps30_interface_debug_print("sps30: interface is %s.\n", info.interface);
91 sps30_interface_debug_print("sps30: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
92 sps30_interface_debug_print("sps30: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
93 sps30_interface_debug_print("sps30: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
94 sps30_interface_debug_print("sps30: max current is %0.2fmA.\n", info.max_current_ma);
95 sps30_interface_debug_print("sps30: max temperature is %0.1fC.\n", info.temperature_max);
96 sps30_interface_debug_print("sps30: min temperature is %0.1fC.\n", info.temperature_min);
97 }
98
99 /* start register test */
100 sps30_interface_debug_print("sps30: start register test.\n");
101
102 /* sps30_set_interface/sps30_get_interface test */
103 sps30_interface_debug_print("sps30: sps30_set_interface/sps30_get_interface test.\n");
104
105 /* set iic interface */
106 res = sps30_set_interface(&gs_handle, SPS30_INTERFACE_IIC);
107 if (res != 0)
108 {
109 sps30_interface_debug_print("sps30: set interface failed.\n");
110
111 return 1;
112 }
113 sps30_interface_debug_print("sps30: set interface iic.\n");
114 res = sps30_get_interface(&gs_handle, &interface_check);
115 if (res != 0)
116 {
117 sps30_interface_debug_print("sps30: get interface failed.\n");
118
119 return 1;
120 }
121 sps30_interface_debug_print("sps30: check interface %s.\n", interface_check == SPS30_INTERFACE_IIC ? "ok" : "error");
122
123 /* set uart interface */
124 res = sps30_set_interface(&gs_handle, SPS30_INTERFACE_UART);
125 if (res != 0)
126 {
127 sps30_interface_debug_print("sps30: set interface failed.\n");
128
129 return 1;
130 }
131 sps30_interface_debug_print("sps30: set interface uart.\n");
132 res = sps30_get_interface(&gs_handle, &interface_check);
133 if (res != 0)
134 {
135 sps30_interface_debug_print("sps30: get interface failed.\n");
136
137 return 1;
138 }
139 sps30_interface_debug_print("sps30: check interface %s.\n", interface_check == SPS30_INTERFACE_UART ? "ok" : "error");
140
141 /* set the interface */
142 res = sps30_set_interface(&gs_handle, interface);
143 if (res != 0)
144 {
145 sps30_interface_debug_print("sps30: set interface failed.\n");
146
147 return 1;
148 }
149
150 /* init the chip */
151 res = sps30_init(&gs_handle);
152 if (res != 0)
153 {
154 sps30_interface_debug_print("sps30: init failed.\n");
155
156 return 1;
157 }
158
159 /* sps30_set_auto_cleaning_interval/sps30_get_auto_cleaning_interval test */
160 sps30_interface_debug_print("sps30: sps30_set_auto_cleaning_interval/sps30_get_auto_cleaning_interval test.\n");
161
162 second = rand() % 65536 + 65536;
163 res = sps30_set_auto_cleaning_interval(&gs_handle, second);
164 if (res != 0)
165 {
166 sps30_interface_debug_print("sps30: set auto cleaning interval failed.\n");
167 (void)sps30_deinit(&gs_handle);
168
169 return 1;
170 }
171 sps30_interface_debug_print("sps30: set auto cleaning interval %d.\n", second);
172 res = sps30_get_auto_cleaning_interval(&gs_handle, (uint32_t *)&second_check);
173 if (res != 0)
174 {
175 sps30_interface_debug_print("sps30: get auto cleaning interval failed.\n");
176 (void)sps30_deinit(&gs_handle);
177
178 return 1;
179 }
180 sps30_interface_debug_print("sps30: check interval %s.\n", second_check == second ? "ok" : "error");
181
182 /* sps30_disable_auto_cleaning_interval test */
183 sps30_interface_debug_print("sps30: sps30_disable_auto_cleaning_interval test.\n");
184 res = sps30_disable_auto_cleaning_interval(&gs_handle);
185 if (res != 0)
186 {
187 sps30_interface_debug_print("sps30: disable auto cleaning interval failed.\n");
188 (void)sps30_deinit(&gs_handle);
189
190 return 1;
191 }
192 sps30_interface_debug_print("sps30: check disable cleaning %s.\n", res == 0 ? "ok" : "error");
193
194 /* sps30_start_fan_cleaning test */
195 sps30_interface_debug_print("sps30: sps30_start_fan_cleaning test.\n");
197 if (res != 0)
198 {
199 sps30_interface_debug_print("sps30: start measurement failed.\n");
200 (void)sps30_deinit(&gs_handle);
201
202 return 1;
203 }
204 res = sps30_start_fan_cleaning(&gs_handle);
205 if (res != 0)
206 {
207 sps30_interface_debug_print("sps30: start fan cleaning failed.\n");
208 (void)sps30_deinit(&gs_handle);
209
210 return 1;
211 }
212
213 /* delay 10s */
214 sps30_interface_delay_ms(1000 * 10);
215 sps30_interface_debug_print("sps30: check fan cleaning %s.\n", res == 0 ? "ok" : "error");
216 res = sps30_stop_measurement(&gs_handle);
217 if (res != 0)
218 {
219 sps30_interface_debug_print("sps30: stop measurement failed.\n");
220 (void)sps30_deinit(&gs_handle);
221
222 return 1;
223 }
224
225 /* sps30_get_product_type test */
226 sps30_interface_debug_print("sps30: sps30_get_product_type test.\n");
227 res = sps30_get_product_type(&gs_handle, (char *)type);
228 if (res != 0)
229 {
230 sps30_interface_debug_print("sps30: get product type failed.\n");
231 (void)sps30_deinit(&gs_handle);
232
233 return 1;
234 }
235 sps30_interface_debug_print("sps30: type is %s.\n", type);
236
237 /* sps30_get_serial_number test */
238 sps30_interface_debug_print("sps30: sps30_get_serial_number test.\n");
239 res = sps30_get_serial_number(&gs_handle, (char *)sn);
240 if (res != 0)
241 {
242 sps30_interface_debug_print("sps30: get serial number failed.\n");
243 (void)sps30_deinit(&gs_handle);
244
245 return 1;
246 }
247 sps30_interface_debug_print("sps30: serial number is %s.\n", sn);
248
249 /* sps30_get_version test */
250 sps30_interface_debug_print("sps30: sps30_get_version test.\n");
251 res = sps30_get_version(&gs_handle, (uint8_t *)&major, (uint8_t *)&minor);
252 if (res != 0)
253 {
254 sps30_interface_debug_print("sps30: get version failed.\n");
255 (void)sps30_deinit(&gs_handle);
256
257 return 1;
258 }
259 sps30_interface_debug_print("sps30: major is 0x%02X minor is 0x%02X .\n", major, minor);
260
261 /* sps30_get_device_status test */
262 sps30_interface_debug_print("sps30: sps30_get_device_status test.\n");
263 res = sps30_get_device_status(&gs_handle, (uint32_t *)&status);
264 if (res != 0)
265 {
266 sps30_interface_debug_print("sps30: sps30 get device status failed.\n");
267 (void)sps30_deinit(&gs_handle);
268
269 return 1;
270 }
271 sps30_interface_debug_print("sps30: status is 0x%X .\n", status);
272
273 /* sps30_clear_device_status test */
274 sps30_interface_debug_print("sps30: sps30_clear_device_status test.\n");
275 res = sps30_clear_device_status(&gs_handle);
276 if (res != 0)
277 {
278 sps30_interface_debug_print("sps30: clear device status failed.\n");
279 (void)sps30_deinit(&gs_handle);
280
281 return 1;
282 }
283 sps30_interface_debug_print("sps30: check clear status %s.\n", res == 0 ? "ok" : "error");
284
285 /* iic interface */
286 if (interface == SPS30_INTERFACE_IIC)
287 {
288 /* sps30_read_data_flag test */
289 sps30_interface_debug_print("sps30: sps30_read_data_flag test.\n");
290 res = sps30_read_data_flag(&gs_handle, &flag);
291 if (res != 0)
292 {
293 sps30_interface_debug_print("sps30: sps30 read data flag failed.\n");
294 (void)sps30_deinit(&gs_handle);
295
296 return 1;
297 }
298 sps30_interface_debug_print("sps30: flag is 0x%X .\n", flag);
299 }
300
301 /* sps30_start_measurement/sps30_stop_measurement test */
302 sps30_interface_debug_print("sps30: sps30_start_measurement/sps30_stop_measurement test.\n");
303
304 /* start measurement */
306 if (res != 0)
307 {
308 sps30_interface_debug_print("sps30: start measurement failed.\n");
309 (void)sps30_deinit(&gs_handle);
310
311 return 1;
312 }
313 sps30_interface_debug_print("sps30: check start measurement %s.\n", res == 0 ? "ok" : "error");
315
316 /* stop measurement */
317 res = sps30_stop_measurement(&gs_handle);
318 if (res != 0)
319 {
320 sps30_interface_debug_print("sps30: stop measurement failed.\n");
321 (void)sps30_deinit(&gs_handle);
322
323 return 1;
324 }
325 sps30_interface_debug_print("sps30: check stop measurement %s.\n", res == 0 ? "ok" : "error");
326
327 /* sps30_sleep test */
328 sps30_interface_debug_print("sps30: sps30_sleep test.\n");
329 res = sps30_sleep(&gs_handle);
330 if (res != 0)
331 {
332 sps30_interface_debug_print("sps30: sleep failed.\n");
333 (void)sps30_deinit(&gs_handle);
334
335 return 1;
336 }
337 sps30_interface_debug_print("sps30: check sleep %s.\n", res == 0 ? "ok" : "error");
338
339 /* sps30_wake_up test */
340 sps30_interface_debug_print("sps30: sps30_wake_up test.\n");
342 res = sps30_wake_up(&gs_handle);
343 if (res != 0)
344 {
345 sps30_interface_debug_print("sps30: wake up failed.\n");
346 (void)sps30_deinit(&gs_handle);
347
348 return 1;
349 }
350 sps30_interface_debug_print("sps30: check wake up %s.\n", res == 0 ? "ok" : "error");
351
352 /* sps30_reset test */
353 sps30_interface_debug_print("sps30: sps30_reset test.\n");
355 res = sps30_reset(&gs_handle);
356 if (res != 0)
357 {
358 sps30_interface_debug_print("sps30: reset failed.\n");
359 (void)sps30_deinit(&gs_handle);
360
361 return 1;
362 }
363 sps30_interface_debug_print("sps30: check reset %s.\n", res == 0 ? "ok" : "error");
364
365 /* finish register test */
366 sps30_interface_debug_print("sps30: finish register test.\n");
367 (void)sps30_deinit(&gs_handle);
368
369 return 0;
370}
driver sps30 register test header file
uint8_t sps30_get_interface(sps30_handle_t *handle, sps30_interface_t *interface)
get the chip interface
uint8_t sps30_set_auto_cleaning_interval(sps30_handle_t *handle, uint32_t second)
set the auto cleaning interval
sps30_data_ready_flag_t
sps30 data ready flag enumeration definition
uint8_t sps30_start_fan_cleaning(sps30_handle_t *handle)
start the fan cleaning
uint8_t sps30_get_auto_cleaning_interval(sps30_handle_t *handle, uint32_t *second)
get the auto cleaning interval
struct sps30_info_s sps30_info_t
sps30 information structure definition
uint8_t sps30_wake_up(sps30_handle_t *handle)
wake up the chip
sps30_interface_t
sps30 interface enumeration definition
uint8_t sps30_start_measurement(sps30_handle_t *handle, sps30_format_t format)
start the measurement
uint8_t sps30_stop_measurement(sps30_handle_t *handle)
stop the measurement
uint8_t sps30_get_product_type(sps30_handle_t *handle, char type[9])
get the product type
uint8_t sps30_get_device_status(sps30_handle_t *handle, uint32_t *status)
get the device status
uint8_t sps30_disable_auto_cleaning_interval(sps30_handle_t *handle)
disable the auto cleaning interval
uint8_t sps30_reset(sps30_handle_t *handle)
reset the chip
struct sps30_handle_s sps30_handle_t
sps30 handle structure definition
uint8_t sps30_read_data_flag(sps30_handle_t *handle, sps30_data_ready_flag_t *flag)
read the data read flag
uint8_t sps30_info(sps30_info_t *info)
get chip information
uint8_t sps30_deinit(sps30_handle_t *handle)
close the chip
uint8_t sps30_init(sps30_handle_t *handle)
initialize the chip
uint8_t sps30_sleep(sps30_handle_t *handle)
enter the sleep mode
uint8_t sps30_set_interface(sps30_handle_t *handle, sps30_interface_t interface)
set the chip interface
uint8_t sps30_clear_device_status(sps30_handle_t *handle)
clear the device status
uint8_t sps30_get_version(sps30_handle_t *handle, uint8_t *major, uint8_t *minor)
get the version
uint8_t sps30_get_serial_number(sps30_handle_t *handle, char sn[17])
get the serial number
@ SPS30_INTERFACE_IIC
@ SPS30_INTERFACE_UART
@ SPS30_FORMAT_IEEE754
uint8_t sps30_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write
uint16_t sps30_interface_uart_read(uint8_t *buf, uint16_t len)
interface uart read
uint8_t sps30_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t sps30_interface_iic_deinit(void)
interface iic bus deinit
void sps30_interface_debug_print(const char *const fmt,...)
interface print format data
void sps30_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sps30_interface_iic_init(void)
interface iic bus init
uint8_t sps30_interface_uart_flush(void)
interface uart flush
uint8_t sps30_interface_uart_write(uint8_t *buf, uint16_t len)
interface uart write
uint8_t sps30_interface_uart_init(void)
interface uart init
uint8_t sps30_interface_uart_deinit(void)
interface uart deinit
uint8_t sps30_register_test(sps30_interface_t interface)
register test
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
char interface[16]
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]