LibDriver DS18B20
Loading...
Searching...
No Matches
driver_ds18b20_register_test.c
Go to the documentation of this file.
1
37
39#include <stdlib.h>
40
41static ds18b20_handle_t gs_handle;
42
51{
52 uint8_t res, i;
53 float temp, temp_check;
54 int8_t reg;
55 int8_t threshold_high, threshold_low;
56 int8_t threshold_high_check, threshold_low_check;
57 uint8_t rom[8], rom_check[8];
58 ds18b20_info_t info;
59 ds18b20_mode_t mode;
60 ds18b20_power_mode_t power_mode;
61 ds18b20_resolution_t resolution;
62
63 /* link interface function */
74
75 /* get ds18b20 info */
76 res = ds18b20_info(&info);
77 if (res != 0)
78 {
79 ds18b20_interface_debug_print("ds18b20: get info failed.\n");
80
81 return 1;
82 }
83 else
84 {
85 /* print ds18b20 info */
86 ds18b20_interface_debug_print("ds18b20: chip is %s.\n", info.chip_name);
87 ds18b20_interface_debug_print("ds18b20: manufacturer is %s.\n", info.manufacturer_name);
88 ds18b20_interface_debug_print("ds18b20: interface is %s.\n", info.interface);
89 ds18b20_interface_debug_print("ds18b20: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
90 ds18b20_interface_debug_print("ds18b20: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
91 ds18b20_interface_debug_print("ds18b20: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
92 ds18b20_interface_debug_print("ds18b20: max current is %0.2fmA.\n", info.max_current_ma);
93 ds18b20_interface_debug_print("ds18b20: max temperature is %0.1fC.\n", info.temperature_max);
94 ds18b20_interface_debug_print("ds18b20: min temperature is %0.1fC.\n", info.temperature_min);
95 }
96
97 /* start register test */
98 ds18b20_interface_debug_print("ds18b20: start register test.\n");
99
100 /* ds18b20 init */
101 res = ds18b20_init(&gs_handle);
102 if (res != 0)
103 {
104 ds18b20_interface_debug_print("ds18b20: init failed.\n");
105
106 return 1;
107 }
108
109 /* ds18b20_set_mode/ds18b20_get_mode test */
110 ds18b20_interface_debug_print("ds18b20: ds18b20_set_mode/ds18b20_get_mode test.\n");
111
112 /* skip rom */
113 res = ds18b20_set_mode(&gs_handle, DS18B20_MODE_SKIP_ROM);
114 if (res != 0)
115 {
116 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
117 (void)ds18b20_deinit(&gs_handle);
118
119 return 1;
120 }
121 ds18b20_interface_debug_print("ds18b20: set skip rom mode.\n");
122 res = ds18b20_get_mode(&gs_handle, &mode);
123 if (res != 0)
124 {
125 ds18b20_interface_debug_print("ds18b20: get mode failed.\n");
126 (void)ds18b20_deinit(&gs_handle);
127
128 return 1;
129 }
130 ds18b20_interface_debug_print("ds18b20: check mode %s.\n", mode==DS18B20_MODE_SKIP_ROM?"ok":"error");
131
132 /* match rom */
133 res = ds18b20_set_mode(&gs_handle, DS18B20_MODE_MATCH_ROM);
134 if (res != 0)
135 {
136 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
137 (void)ds18b20_deinit(&gs_handle);
138
139 return 1;
140 }
141 ds18b20_interface_debug_print("ds18b20: set match rom mode.\n");
142 res = ds18b20_get_mode(&gs_handle, &mode);
143 if (res != 0)
144 {
145 ds18b20_interface_debug_print("ds18b20: get mode failed.\n");
146 (void)ds18b20_deinit(&gs_handle);
147
148 return 1;
149 }
150 ds18b20_interface_debug_print("ds18b20: check mode %s.\n", mode==DS18B20_MODE_MATCH_ROM?"ok":"error");
151
152 /* ds18b20_set_rom/ds18b20_get_rom test */
153 ds18b20_interface_debug_print("ds18b20: ds18b20_set_rom/ds18b20_get_rom test.\n");
154 res = ds18b20_get_rom(&gs_handle, (uint8_t *)rom);
155 if (res != 0)
156 {
157 ds18b20_interface_debug_print("ds18b20: get rom failed.\n");
158 (void)ds18b20_deinit(&gs_handle);
159
160 return 1;
161 }
162 res = ds18b20_set_rom(&gs_handle, (uint8_t *)rom);
163 if (res != 0)
164 {
165 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
166 (void)ds18b20_deinit(&gs_handle);
167
168 return 1;
169 }
170 ds18b20_interface_debug_print("ds18b20: set rom.\n");
171 res = ds18b20_get_rom(&gs_handle, (uint8_t *)rom_check);
172 if (res != 0)
173 {
174 ds18b20_interface_debug_print("ds18b20: get rom failed.\n");
175 (void)ds18b20_deinit(&gs_handle);
176
177 return 1;
178 }
179 for (i=0; i<8; i++)
180 {
181 if (rom[i] != rom_check[i])
182 {
183 res = 1;
184 }
185 }
186 ds18b20_interface_debug_print("ds18b20: check rom %s.\n", (res==0)?"ok":"error");
187
188 /* ds18b20_get_power_mode test */
189 ds18b20_interface_debug_print("ds18b20: ds18b20_get_power_mode test.\n");
190 res = ds18b20_get_power_mode(&gs_handle, &power_mode);
191 if (res != 0)
192 {
193 ds18b20_interface_debug_print("ds18b20: get power mode failed.\n");
194 (void)ds18b20_deinit(&gs_handle);
195
196 return 1;
197 }
198 ds18b20_interface_debug_print("ds18b20: check power mode %s.\n",
199 power_mode == DS18B20_POWER_MODE_EXTERNALLY ? "externally power" : "parasite power");
200
201 /* ds18b20_scrachpad_set_resolution/ds18b20_scrachpad_get_resolution test */
202 ds18b20_interface_debug_print("ds18b20: ds18b20_scrachpad_set_resolution/ds18b20_scrachpad_get_resolution test.\n");
203
204 /* RESOLUTION_9BIT */
206 if (res != 0)
207 {
208 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
209 (void)ds18b20_deinit(&gs_handle);
210
211 return 1;
212 }
213 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 9bit.\n");
214 res = ds18b20_scratchpad_get_resolution(&gs_handle, &resolution);
215 if (res != 0)
216 {
217 ds18b20_interface_debug_print("ds18b20: scratchpad get resolution failed.\n");
218 (void)ds18b20_deinit(&gs_handle);
219
220 return 1;
221 }
222 ds18b20_interface_debug_print("ds18b20: check resolution %s.\n", resolution==DS18B20_RESOLUTION_9BIT?"ok":"error");
223
224 /* RESOLUTION_10BIT */
226 if (res != 0)
227 {
228 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
229 (void)ds18b20_deinit(&gs_handle);
230
231 return 1;
232 }
233 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 10bit.\n");
234 res = ds18b20_scratchpad_get_resolution(&gs_handle, &resolution);
235 if (res != 0)
236 {
237 ds18b20_interface_debug_print("ds18b20: scratchpad get resolution failed.\n");
238 (void)ds18b20_deinit(&gs_handle);
239
240 return 1;
241 }
242 ds18b20_interface_debug_print("ds18b20: check resolution %s.\n", resolution==DS18B20_RESOLUTION_10BIT?"ok":"error");
243
244 /* RESOLUTION_11BIT */
246 if (res != 0)
247 {
248 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
249 (void)ds18b20_deinit(&gs_handle);
250
251 return 1;
252 }
253 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 11bit.\n");
254 res = ds18b20_scratchpad_get_resolution(&gs_handle, &resolution);
255 if (res != 0)
256 {
257 ds18b20_interface_debug_print("ds18b20: scratchpad get resolution failed.\n");
258 (void)ds18b20_deinit(&gs_handle);
259
260 return 1;
261 }
262 ds18b20_interface_debug_print("ds18b20: check resolution %s.\n", resolution==DS18B20_RESOLUTION_11BIT?"ok":"error");
263
264 /* RESOLUTION_12BIT */
266 if (res != 0)
267 {
268 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
269 (void)ds18b20_deinit(&gs_handle);
270
271 return 1;
272 }
273 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 12bit.\n");
274 res = ds18b20_scratchpad_get_resolution(&gs_handle, &resolution);
275 if (res != 0)
276 {
277 ds18b20_interface_debug_print("ds18b20: scratchpad get resolution failed.\n");
278 (void)ds18b20_deinit(&gs_handle);
279
280 return 1;
281 }
282 ds18b20_interface_debug_print("ds18b20: check resolution %s.\n", resolution==DS18B20_RESOLUTION_12BIT?"ok":"error");
283
284 /* ds18b20_scrachpad_set_alarm_threshold/ds18b20_scrachpad_get_alarm_threshold test */
285 ds18b20_interface_debug_print("ds18b20: ds18b20_scrachpad_set_alarm_threshold/ds18b20_scrachpad_get_alarm_threshold test.\n");
286 threshold_high = (int8_t)(rand()%128);
287 threshold_low = (int8_t)(-(rand()%128));
288 res = ds18b20_scratchpad_set_alarm_threshold(&gs_handle, threshold_high, threshold_low);
289 if (res != 0)
290 {
291 ds18b20_interface_debug_print("ds18b20: scratchpad set alarm threshold failed.\n");
292 (void)ds18b20_deinit(&gs_handle);
293
294 return 1;
295 }
296 ds18b20_interface_debug_print("ds18b20: scratchpad set alarm high threshold %d.\n", threshold_high);
297 ds18b20_interface_debug_print("ds18b20: scratchpad set alarm low threshold %d.\n", threshold_low);
298 res = ds18b20_scrachpad_get_alarm_threshold(&gs_handle, (int8_t *)&threshold_high_check, (int8_t *)&threshold_low_check);
299 if (res != 0)
300 {
301 ds18b20_interface_debug_print("ds18b20: scratchpad get alarm threshold failed.\n");
302 (void)ds18b20_deinit(&gs_handle);
303
304 return 1;
305 }
306 ds18b20_interface_debug_print("ds18b20: check alarm high threshold %s.\n", threshold_high==threshold_high_check?"ok":"error");
307 ds18b20_interface_debug_print("ds18b20: check alarm low threshold %s.\n", threshold_low==threshold_low_check?"ok":"error");
308
309 /* ds18b20_copy_scratchpad_to_eeprom/ds18b20_copy_eeprom_to_scratchpad test */
310 ds18b20_interface_debug_print("ds18b20: ds18b20_copy_scratchpad_to_eeprom/ds18b20_copy_eeprom_to_scratchpad test.\n");
312 if (res != 0)
313 {
314 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
315 (void)ds18b20_deinit(&gs_handle);
316
317 return 1;
318 }
319 res = ds18b20_copy_scratchpad_to_eeprom(&gs_handle);
320 if (res != 0)
321 {
322 ds18b20_interface_debug_print("ds18b20: copy scratchpad to eeprom failed.\n");
323 (void)ds18b20_deinit(&gs_handle);
324
325 return 1;
326 }
327 ds18b20_interface_debug_print("ds18b20: copy scratchpad to eeprom.\n");
328 res = ds18b20_copy_eeprom_to_scratchpad(&gs_handle);
329 if (res != 0)
330 {
331 ds18b20_interface_debug_print("ds18b20: ds18b20 copy eeprom to scratchpad failed.\n");
332 (void)ds18b20_deinit(&gs_handle);
333
334 return 1;
335 }
336 res = ds18b20_scratchpad_get_resolution(&gs_handle, &resolution);
337 if (res != 0)
338 {
339 ds18b20_interface_debug_print("ds18b20: scratchpad get resolution failed.\n");
340 (void)ds18b20_deinit(&gs_handle);
341
342 return 1;
343 }
344 ds18b20_interface_debug_print("ds18b20: check scratchpad eeprom %s.\n", resolution==DS18B20_RESOLUTION_9BIT?"ok":"error");
345
346 /* ds18b20_alarm_convert_to_register/ds18b20_alarm_convert_to_data test */
347 ds18b20_interface_debug_print("ds18b20: ds18b20_alarm_convert_to_register/ds18b20_alarm_convert_to_data test.\n");
348 temp = (float)(rand()%36000)/1000.0f;
349 res = ds18b20_alarm_convert_to_register(&gs_handle, temp, (int8_t *)&reg);
350 if (res != 0)
351 {
352 ds18b20_interface_debug_print("ds18b20: alarm convert to register failed.\n");
353 (void)ds18b20_deinit(&gs_handle);
354
355 return 1;
356 }
357 ds18b20_interface_debug_print("ds18b20: set alarm convert to register %0.2f.\n", temp);
358 res = ds18b20_alarm_convert_to_data(&gs_handle, reg, (float *)&temp_check);
359 if (res != 0)
360 {
361 ds18b20_interface_debug_print("ds18b20: alarm convert to data failed.\n");
362 (void)ds18b20_deinit(&gs_handle);
363
364 return 1;
365 }
366 ds18b20_interface_debug_print("ds18b20: get convert to data %0.2f.\n", temp_check);
367
368 /* finish register test */
369 ds18b20_interface_debug_print("ds18b20: finish register test.\n");
370 (void)ds18b20_deinit(&gs_handle);
371
372 return 0;
373}
driver ds18b20 register test header file
uint8_t ds18b20_alarm_convert_to_data(ds18b20_handle_t *handle, int8_t reg, float *temp)
convert the register data to the alarm temperature
uint8_t ds18b20_get_power_mode(ds18b20_handle_t *handle, ds18b20_power_mode_t *power_mode)
get the power mode
uint8_t ds18b20_scrachpad_get_alarm_threshold(ds18b20_handle_t *handle, int8_t *threshold_high, int8_t *threshold_low)
get the alarm threshold in the scratchpad
uint8_t ds18b20_scratchpad_set_alarm_threshold(ds18b20_handle_t *handle, int8_t threshold_high, int8_t threshold_low)
set the alarm threshold in the scratchpad
uint8_t ds18b20_alarm_convert_to_register(ds18b20_handle_t *handle, float temp, int8_t *reg)
convert the alarm temperature to the register data
uint8_t ds18b20_scratchpad_get_resolution(ds18b20_handle_t *handle, ds18b20_resolution_t *resolution)
get the resolution in the scratchpad
ds18b20_power_mode_t
ds18b20 power mode enumeration definition
uint8_t ds18b20_init(ds18b20_handle_t *handle)
initialize the chip
uint8_t ds18b20_set_rom(ds18b20_handle_t *handle, uint8_t rom[8])
set the handle rom
uint8_t ds18b20_copy_eeprom_to_scratchpad(ds18b20_handle_t *handle)
copy the eeprom content to the scratchpad
uint8_t ds18b20_copy_scratchpad_to_eeprom(ds18b20_handle_t *handle)
copy the scratchpad content to the eeprom
ds18b20_resolution_t
ds18b20 resolution enumeration definition
uint8_t ds18b20_info(ds18b20_info_t *info)
get chip's information
uint8_t ds18b20_scratchpad_set_resolution(ds18b20_handle_t *handle, ds18b20_resolution_t resolution)
set the resolution in the scratchpad
struct ds18b20_handle_s ds18b20_handle_t
ds18b20 handle structure definition
ds18b20_mode_t
ds18b20 mode enumeration definition
uint8_t ds18b20_deinit(ds18b20_handle_t *handle)
close the chip
uint8_t ds18b20_get_mode(ds18b20_handle_t *handle, ds18b20_mode_t *mode)
get the chip mode
uint8_t ds18b20_set_mode(ds18b20_handle_t *handle, ds18b20_mode_t mode)
set the chip mode
struct ds18b20_info_s ds18b20_info_t
ds18b20 info structure definition
uint8_t ds18b20_get_rom(ds18b20_handle_t *handle, uint8_t rom[8])
get the chip rom
@ DS18B20_POWER_MODE_EXTERNALLY
@ DS18B20_RESOLUTION_11BIT
@ DS18B20_RESOLUTION_9BIT
@ DS18B20_RESOLUTION_10BIT
@ DS18B20_RESOLUTION_12BIT
@ DS18B20_MODE_SKIP_ROM
@ DS18B20_MODE_MATCH_ROM
uint8_t ds18b20_interface_deinit(void)
interface bus deinit
void ds18b20_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ds18b20_interface_write(uint8_t value)
interface bus write
uint8_t ds18b20_interface_init(void)
interface bus init
uint8_t ds18b20_interface_read(uint8_t *value)
interface bus read
void ds18b20_interface_debug_print(const char *const fmt,...)
interface print format data
void ds18b20_interface_disable_irq(void)
interface disable the interrupt
void ds18b20_interface_enable_irq(void)
interface enable the interrupt
void ds18b20_interface_delay_us(uint32_t us)
interface delay us
uint8_t ds18b20_register_test(void)
register test
uint32_t driver_version
char manufacturer_name[32]