LibDriver HDC302X
Loading...
Searching...
No Matches
driver_hdc302x_interrupt_test.c
Go to the documentation of this file.
1
36
38
39static hdc302x_handle_t gs_handle;
40
55 float temperature_low_deg, float temperature_high_deg,
56 float humidity_low_percentage, float humidity_high_percentage,
57 uint32_t times)
58{
59 uint8_t res;
60 uint16_t threshold;
61 uint32_t i;
62 hdc302x_info_t info;
63
64 /* link functions */
73
74 /* hdc302x info */
75 res = hdc302x_info(&info);
76 if (res != 0)
77 {
78 hdc302x_interface_debug_print("hdc302x: get info failed.\n");
79
80 return 1;
81 }
82 else
83 {
84 /* print chip information */
85 hdc302x_interface_debug_print("hdc302x: chip is %s.\n", info.chip_name);
86 hdc302x_interface_debug_print("hdc302x: manufacturer is %s.\n", info.manufacturer_name);
87 hdc302x_interface_debug_print("hdc302x: interface is %s.\n", info.interface);
88 hdc302x_interface_debug_print("hdc302x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
89 hdc302x_interface_debug_print("hdc302x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
90 hdc302x_interface_debug_print("hdc302x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
91 hdc302x_interface_debug_print("hdc302x: max current is %0.2fmA.\n", info.max_current_ma);
92 hdc302x_interface_debug_print("hdc302x: max temperature is %0.1fC.\n", info.temperature_max);
93 hdc302x_interface_debug_print("hdc302x: min temperature is %0.1fC.\n", info.temperature_min);
94 }
95
96 /* start interrupt test */
97 hdc302x_interface_debug_print("hdc302x: start interrupt test.\n");
98
99 /* set address */
100 res = hdc302x_set_addr(&gs_handle, addr);
101 if (res != 0)
102 {
103 hdc302x_interface_debug_print("hdc302x: set addr failed.\n");
104
105 return 1;
106 }
107
108 /* hdc302x init */
109 res = hdc302x_init(&gs_handle);
110 if (res != 0)
111 {
112 hdc302x_interface_debug_print("hdc302x: init failed.\n");
113
114 return 1;
115 }
116
117 /* alert threshold convert to register */
118 res = hdc302x_alert_threshold_convert_to_register(&gs_handle, temperature_low_deg, humidity_low_percentage, &threshold);
119 if (res != 0)
120 {
121 hdc302x_interface_debug_print("hdc302x: alert threshold convert to register failed.\n");
122 (void)hdc302x_deinit(&gs_handle);
123
124 return 1;
125 }
126
127 /* write set low alert */
128 res = hdc302x_write_set_low_alert(&gs_handle, threshold);
129 if (res != 0)
130 {
131 hdc302x_interface_debug_print("hdc302x: write set low alert failed.\n");
132 (void)hdc302x_deinit(&gs_handle);
133
134 return 1;
135 }
136
137 /* alert threshold convert to register */
138 res = hdc302x_alert_threshold_convert_to_register(&gs_handle, temperature_low_deg + 1.0f, humidity_low_percentage + 1.0f, &threshold);
139 if (res != 0)
140 {
141 hdc302x_interface_debug_print("hdc302x: alert threshold convert to register failed.\n");
142 (void)hdc302x_deinit(&gs_handle);
143
144 return 1;
145 }
146
147 /* write clear low alert */
148 res = hdc302x_write_clear_low_alert(&gs_handle, threshold);
149 if (res != 0)
150 {
151 hdc302x_interface_debug_print("hdc302x: write clear low alert failed.\n");
152 (void)hdc302x_deinit(&gs_handle);
153
154 return 1;
155 }
156
157 /* alert threshold convert to register */
158 res = hdc302x_alert_threshold_convert_to_register(&gs_handle, temperature_high_deg, humidity_high_percentage, &threshold);
159 if (res != 0)
160 {
161 hdc302x_interface_debug_print("hdc302x: alert threshold convert to register failed.\n");
162 (void)hdc302x_deinit(&gs_handle);
163
164 return 1;
165 }
166
167 /* write set high alert */
168 res = hdc302x_write_set_high_alert(&gs_handle, threshold);
169 if (res != 0)
170 {
171 hdc302x_interface_debug_print("hdc302x: write set high alert failed.\n");
172 (void)hdc302x_deinit(&gs_handle);
173
174 return 1;
175 }
176
177 /* check humidity high percentage */
178 if (humidity_high_percentage - 1.0f < 0.0f)
179 {
180 hdc302x_interface_debug_print("hdc302x: humidity_high_percentage < 1.0f.\n");
181 (void)hdc302x_deinit(&gs_handle);
182
183 return 1;
184 }
185
186 /* alert threshold convert to register */
187 res = hdc302x_alert_threshold_convert_to_register(&gs_handle, temperature_high_deg - 1.0f, humidity_high_percentage - 1.0f, &threshold);
188 if (res != 0)
189 {
190 hdc302x_interface_debug_print("hdc302x: alert threshold convert to register failed.\n");
191 (void)hdc302x_deinit(&gs_handle);
192
193 return 1;
194 }
195
196 /* write clear high alert */
197 res = hdc302x_write_clear_high_alert(&gs_handle, threshold);
198 if (res != 0)
199 {
200 hdc302x_interface_debug_print("hdc302x: write clear high alert failed.\n");
201 (void)hdc302x_deinit(&gs_handle);
202
203 return 1;
204 }
205
206 /* set low power mode 0 */
208 if (res != 0)
209 {
210 hdc302x_interface_debug_print("hdc302x: set low power mode failed.\n");
211 (void)hdc302x_deinit(&gs_handle);
212
213 return 1;
214 }
215
216 /* set auto mode 1Hz */
218 if (res != 0)
219 {
220 hdc302x_interface_debug_print("hdc302x: set auto mode failed.\n");
221 (void)hdc302x_deinit(&gs_handle);
222
223 return 1;
224 }
225
226 /* clear status */
227 res = hdc302x_clear_status(&gs_handle);
228 if (res != 0)
229 {
230 hdc302x_interface_debug_print("hdc302x: clear status failed.\n");
231 (void)hdc302x_deinit(&gs_handle);
232
233 return 1;
234 }
235
236 /* start continuous read */
237 res = hdc302x_start_continuous_read(&gs_handle);
238 if (res != 0)
239 {
240 hdc302x_interface_debug_print("hdc302x: start continuous read failed.\n");
241 (void)hdc302x_deinit(&gs_handle);
242
243 return 1;
244 }
245
246 /* wait 500 ms */
248
249 /* loop */
250 for (i = 0; i < times; i++)
251 {
252 uint16_t temperature_raw;
253 float temperature_s;
254 uint16_t humidity_raw;
255 float humidity_s;
256
257 /* read data */
258 res = hdc302x_continuous_read(&gs_handle, &temperature_raw, &temperature_s, &humidity_raw, &humidity_s);
259 if (res != 0)
260 {
261 hdc302x_interface_debug_print("hdc302x: read failed.\n");
262 (void)hdc302x_deinit(&gs_handle);
263
264 return 1;
265 }
266 hdc302x_interface_debug_print("hdc302x: temperature is %0.2fC.\n", temperature_s);
267 hdc302x_interface_debug_print("hdc302x: humidity is %0.2f%%.\n", humidity_s);
268
269 /* run the handler */
270 res = hdc302x_irq_handler(&gs_handle);
271 if (res != 0)
272 {
273 hdc302x_interface_debug_print("hdc302x: irq run failed.\n");
274 (void)hdc302x_deinit(&gs_handle);
275
276 return 1;
277 }
278
279 /* wait 1000 ms */
281 }
282
283 /* stop continuous read */
284 res = hdc302x_stop_continuous_read(&gs_handle);
285 if (res != 0)
286 {
287 hdc302x_interface_debug_print("hdc302x: stop continuous read failed.\n");
288 (void)hdc302x_deinit(&gs_handle);
289
290 return 1;
291 }
292
293 /* finish interrupt test */
294 hdc302x_interface_debug_print("hdc302x: finish interrupt test.\n");
295 (void)hdc302x_deinit(&gs_handle);
296
297 return 0;
298}
driver hdc302x interrupt test header file
uint8_t hdc302x_continuous_read(hdc302x_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
continuous read
struct hdc302x_info_s hdc302x_info_t
hdc302x information structure definition
uint8_t hdc302x_set_addr(hdc302x_handle_t *handle, hdc302x_address_t addr)
set the iic address
uint8_t hdc302x_deinit(hdc302x_handle_t *handle)
close the chip
hdc302x_address_t
hdc302x address enumeration definition
uint8_t hdc302x_irq_handler(hdc302x_handle_t *handle)
irq handler
uint8_t hdc302x_set_low_power_mode(hdc302x_handle_t *handle, hdc302x_low_power_mode_t mode)
set low power mode
uint8_t hdc302x_set_auto_mode(hdc302x_handle_t *handle, hdc302x_auto_mode_t mode)
set auto mode
uint8_t hdc302x_alert_threshold_convert_to_register(hdc302x_handle_t *handle, float temperature_deg, float humidity_percentage, uint16_t *reg)
convert the alert threshold to the register raw data
uint8_t hdc302x_init(hdc302x_handle_t *handle)
initialize the chip
uint8_t hdc302x_write_clear_low_alert(hdc302x_handle_t *handle, uint16_t threshold)
write clear low alert
uint8_t hdc302x_clear_status(hdc302x_handle_t *handle)
clear status
uint8_t hdc302x_write_set_low_alert(hdc302x_handle_t *handle, uint16_t threshold)
write set low alert
uint8_t hdc302x_stop_continuous_read(hdc302x_handle_t *handle)
stop continuous read
struct hdc302x_handle_s hdc302x_handle_t
hdc302x handle structure definition
uint8_t hdc302x_info(hdc302x_info_t *info)
get chip's information
uint8_t hdc302x_start_continuous_read(hdc302x_handle_t *handle)
start continuous read
uint8_t hdc302x_write_clear_high_alert(hdc302x_handle_t *handle, uint16_t threshold)
write clear high alert
uint8_t hdc302x_write_set_high_alert(hdc302x_handle_t *handle, uint16_t threshold)
write set high alert
@ HDC302X_AUTO_MODE_1_HZ
@ HDC302X_LOW_POWER_MODE_0
uint8_t hdc302x_interface_iic_write_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus write address16
void hdc302x_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t hdc302x_interface_iic_init(void)
interface iic bus init
uint8_t hdc302x_interface_iic_read_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus read address16
void hdc302x_interface_receive_callback(uint16_t type)
interface receive callback
void hdc302x_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t hdc302x_interface_iic_deinit(void)
interface iic bus deinit
uint8_t hdc302x_interrupt_test(hdc302x_address_t addr, float temperature_low_deg, float temperature_high_deg, float humidity_low_percentage, float humidity_high_percentage, uint32_t times)
interrupt test
uint32_t driver_version
char manufacturer_name[32]