1-Wire and ENS210 driver stack
one_wire.c
Go to the documentation of this file.
1 /**
2  * @file one_wire.c
3  * @brief General 1-Wire API using the DS2485 1-Wire master.
4  *
5  * @par Return convention
6  * Functions return an error code. A value of 0 means no error occurred.
7  *
8  * @par Update history - Dave Nadler Cleanups...
9  * | Date | Author | Updates |
10  * | ----: | :----: | :---- |
11  * | 18-May-2023 | Dave Nadler | Fixed compilation errors such as true versus TRUE. |
12  * | 18-May-2023 | Dave Nadler | Deleted unused platform-specific defines. |
13  * | 13-March-2026 | Dave Nadler | Added a significant number of missing error checks. |
14  */
15 
16 /*******************************************************************************
17 * Copyright (C) Maxim Integrated Products, Inc., All rights Reserved.
18 *
19 * This software is protected by copyright laws of the United States and
20 * of foreign countries. This material may also be protected by patent laws
21 * and technology transfer regulations of the United States and of foreign
22 * countries. This software is furnished under a license agreement and/or a
23 * nondisclosure agreement and may only be used or reproduced in accordance
24 * with the terms of those agreements. Dissemination of this information to
25 * any party or parties not specified in the license agreement and/or
26 * nondisclosure agreement is expressly prohibited.
27 *
28 * The above copyright notice and this permission notice shall be included
29 * in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
35 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37 * OTHER DEALINGS IN THE SOFTWARE.
38 *
39 * Except as contained in this notice, the name of Maxim Integrated
40 * Products, Inc. shall not be used except as stated in the Maxim Integrated
41 * Products, Inc. Branding Policy.
42 *
43 * The mere transfer of this software does not imply any licenses
44 * of trade secrets, proprietary technology, copyrights, patents,
45 * trademarks, maskwork rights, or any other form of intellectual
46 * property whatsoever. Maxim Integrated Products, Inc. retains all
47 * ownership rights.
48 *******************************************************************************
49 */
50 
51 /* **** Includes **** */
52 #include <stdint.h>
53 #include <string.h>
54 #include "one_wire.h"
55 #include "DS2485.h"
56 
57 /* **** Definitions **** */
58 
59 /* **** Globals **** */
60 uint8_t oneWireScript[126];
61 uint8_t oneWireScript_length = 0;
62 double oneWireScript_accumulativeOneWireTime = 0;
63 uint8_t oneWireScript_commandsCount = 0;
64 uint8_t oneWireScriptResponse[126];
65 uint8_t oneWireScriptResponse_length = 0;
66 
67 /* **** Functions **** */
68 int OneWire_ResetPulse()
69 {
70  int error = 0; //if there is an error, make error == 1
71  uint8_t resetResponse_index;
72  one_wire_speeds speed;
73  uint8_t presence_pulse_detect;
74  uint8_t reset_status;
75 
76  OneWire_Script_Clear();
77 
78  error = OneWire_Get_OneWireMasterSpeed(&speed);
79  if(error) return error;
80  error = OneWire_Script_Add_OW_RESET(&resetResponse_index, speed, false);
81  if(error) return error;
82  // For some reason, the very first attempt at running the reset script fails with RB_COMMS_FAIL.
83  // This is the very first OneWire script run during initialization; maybe there's a 'first time' bug somewhere...
84  // Subsequent attempts work fine, hence the retry loop below
85  for(int i=0; i<3; i++) {
86  error = OneWire_Script_Execute();
87  if(error == 0) break;
88  }
89  if(error) return error;
90  reset_status = oneWireScriptResponse[resetResponse_index + 1];
91  presence_pulse_detect = reset_status & (1 << 1);
92  if (presence_pulse_detect)
93  {
94  error = 0;
95  }
96  else
97  {
98  error = 1;
99  }
100 
101  return error;
102 }
103 
104 int OneWire_WriteByte(unsigned char byte)
105 {
106  int error = 0;
107  uint8_t writeByteResponse_index;
108 
109  OneWire_Script_Clear();
110 
111  error = OneWire_Script_Add_OW_WRITE_BYTE(&writeByteResponse_index, byte);
112  if(error) return error;
113  error = OneWire_Script_Execute();
114  if(error) return error;
115  uint8_t writeByte_status = oneWireScriptResponse[writeByteResponse_index + 1];
116  if (byte == writeByte_status)
117  {
118  error = 0;
119  }
120  else
121  {
122  error = 1;
123  }
124 
125  return error;
126 }
127 
128 int OneWire_WriteBlock(unsigned char *data, int data_length)
129 {
130  int error = 0;
131  uint8_t writeBlockResponse_index;
132 
133  OneWire_Script_Clear();
134 
135  error = OneWire_Script_Add_OW_WRITE_BLOCK(&writeBlockResponse_index, data, data_length);
136  if(error) return error;
137  error = OneWire_Script_Execute();
138  if(error) return error;
139  uint8_t writeBlock_status = oneWireScriptResponse[writeBlockResponse_index + 1];
140  if (writeBlock_status == 0xAA)
141  {
142  error = 0;
143  }
144  else
145  {
146  error = 1;
147  }
148 
149  return error;
150 }
151 
152 uint8_t OneWire_ReadByte()
153 {
154  int error = 0;
155  uint8_t readByteResponse_index;
156 
157  OneWire_Script_Clear();
158 
159  error = OneWire_Script_Add_OW_READ_BYTE(&readByteResponse_index);
160  if(error) return -1; // don't return a valid byte count
161  error = OneWire_Script_Execute();
162  if(error) return -1; // don't return a valid byte count
163 
164  uint8_t readByte = oneWireScriptResponse[readByteResponse_index + 1];
165  return readByte;
166 }
167 
168 int OneWire_ReadBlock(unsigned char *data, int data_length)
169 {
170  int error = 0;
171  uint8_t readBlockResponse_index;
172 
173  OneWire_Script_Clear();
174 
175  error = OneWire_Script_Add_OW_READ_BLOCK(&readBlockResponse_index, data_length);
176  if(error) return error;
177  error = OneWire_Script_Execute();
178  if(error) return error;
179 
180  uint8_t readBlock_length = oneWireScriptResponse[readBlockResponse_index + 1];
181  uint8_t readBlock_index = readBlockResponse_index + 2;
182  memcpy(&data[0], &oneWireScriptResponse[readBlock_index], readBlock_length);
183  return 0;
184 }
185 
186 //--------------------------------------------------------------------------
187 /// Perform the 1-Wire Search Algorithm on the 1-Wire bus
188 /// parameter: search_reset: start a new search? (false: continue on to next device)
189 /// Return 0: no error
190 /// return parameter: last_device_found - True: no more devices
191 int OneWire_Search(OneWire_ROM_ID_T *romid, bool search_reset, bool *last_device_found)
192 {
193  return DS2485_OneWireSearch(romid->ID, /*search command code=*/0xF0, /*reset=*/true, /*ignore=*/false, search_reset, last_device_found);
194 }
195 
196 int OneWire_WriteBytePower(int send_byte)
197 {
198  int error = 0;
199  uint8_t writeByteResponse_index;
200 
201  OneWire_Script_Clear();
202 
203  OneWire_Script_Add_PRIME_SPU();
204  error = OneWire_Script_Add_OW_WRITE_BYTE(&writeByteResponse_index, send_byte);
205  if(error) return error;
206  error = OneWire_Script_Execute();
207  if(error) return error;
208  uint8_t writeByte_status = oneWireScriptResponse[writeByteResponse_index + 1];
209  if (send_byte == writeByte_status)
210  {
211  error = 0;
212  }
213  else
214  {
215  error = 1;
216  }
217 
218  return error;
219 }
220 
221 
222 /* **** Primitive Commands Functions **** */
223 void OneWire_Script_Clear(void)
224 {
225  oneWireScript_length = 0;
226  oneWireScript_accumulativeOneWireTime = 0;
227  oneWireScript_commandsCount = 0;
228  oneWireScriptResponse_length = 0;
229 }
230 int OneWire_Script_Execute(void)
231 {
232  int error = 0;
233 
234  if((error = DS2485_OneWireScript(oneWireScript, oneWireScript_length, oneWireScript_accumulativeOneWireTime, oneWireScript_commandsCount, oneWireScriptResponse, oneWireScriptResponse_length)) != 0)
235  {
236  return error;
237  }
238 
239  return error;
240 
241 }
242 int OneWire_Script_Add_OW_RESET(uint8_t *response_index, one_wire_speeds spd, bool ignore)
243 {
244  int error = 0;
245 
246  // Delay variables
247  one_wire_speeds master_speed;
248  double ow_rst_time;
249  double t_rstl;
250  double t_rsth;
251 
252  /***** Command code *****/
253  oneWireScript[oneWireScript_length++] = PC_OW_RESET;
254 
255  /***** Command parameter *****/
256  oneWireScript[oneWireScript_length++] = ((spd ^ 1) << 7) | (spd << 3) | (ignore << 1);
257 
258  /***** Add expected response size to total response length *****/
259  *response_index = oneWireScriptResponse_length;
260  oneWireScriptResponse_length += 2;
261 
262  /***** Add 1-Wire actions to total command count *****/
263  oneWireScript_commandsCount++;
264 
265  /***** Add accumulative 1-Wire time in us *****/
266  // Fetch timings
267  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
268  {
269  return error;
270  }
271 
272  if(master_speed != STANDARD)
273  {
274  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
275  {
276  return error;
277  }
278  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
279  {
280  return error;
281  }
282  }
283  else
284  {
285  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
286  {
287  return error;
288  }
289  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
290  {
291  return error;
292  }
293  }
294  //Calculate 1-Wire time
295  ow_rst_time = t_rstl + t_rsth;
296 
297  //Add to total 1-Wire time
298  oneWireScript_accumulativeOneWireTime += ow_rst_time;
299 
300  return error;
301 }
302 
303 int OneWire_Script_Add_OW_WRITE_BIT(uint8_t *response_index, bool bit_value)
304 {
305  int error = 0;
306 
307  // Delay variables
308  one_wire_speeds master_speed;
309  double t_slot;
310 
311  // Timings
312  double t_w0l;
313  double t_rec;
314 
315  /***** Command code *****/
316  oneWireScript[oneWireScript_length++] = PC_OW_WRITE_BIT;
317 
318  /***** Command parameter *****/
319  oneWireScript[oneWireScript_length++] = bit_value;
320 
321  /***** Add expected response size to total response length *****/
322  *response_index = oneWireScriptResponse_length;
323  oneWireScriptResponse_length += 2;
324 
325  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
326 // oneWireScript_commandsCount++; // omitted
327 
328  /***** Add accumulative 1-Wire time in us *****/
329  /***** Fetch timings *****/
330  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
331  {
332  return error;
333  }
334 
335  if(master_speed != STANDARD)
336  {
337  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
338  {
339  return error;
340  }
341  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
342  {
343  return error;
344  }
345  }
346  else
347  {
348  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
349  {
350  return error;
351  }
352  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
353  {
354  return error;
355  }
356  }
357 
358  //Calculate 1-Wire time
359  t_slot = t_w0l + t_rec;
360 
361  //Add to total 1-Wire time
362  oneWireScript_accumulativeOneWireTime += t_slot;
363 
364  return error;
365 }
366 
367 int OneWire_Script_Add_OW_READ_BIT(uint8_t *response_index)
368 {
369  int error = 0;
370 
371  // Delay variables
372  one_wire_speeds master_speed;
373  double t_slot;
374 
375  // Timings
376  double t_w0l;
377  double t_rec;
378 
379  /***** Command code *****/
380  oneWireScript[oneWireScript_length++] = PC_OW_READ_BIT;
381 
382  /***** Command parameter *****/
383  //No parameter
384 
385  /***** Add expected response size to total response length *****/
386  *response_index = oneWireScriptResponse_length;
387  oneWireScriptResponse_length += 2;
388 
389  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
390 // oneWireScript_commandsCount++; // omitted
391 
392  /***** Add accumulative 1-Wire time in us *****/
393  /***** Fetch timings *****/
394  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
395  {
396  return error;
397  }
398 
399  if(master_speed != STANDARD)
400  {
401  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
402  {
403  return error;
404  }
405  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
406  {
407  return error;
408  }
409  }
410  else
411  {
412  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
413  {
414  return error;
415  }
416  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
417  {
418  return error;
419  }
420  }
421 
422  //Calculate 1-Wire time
423  t_slot = t_w0l + t_rec;
424 
425  //Add to total 1-Wire time
426  oneWireScript_accumulativeOneWireTime += t_slot;
427 
428  return error;
429 }
430 
431 int OneWire_Script_Add_OW_WRITE_BYTE(uint8_t *response_index, uint8_t txByte)
432 {
433  int error = 0;
434 
435  // Delay variables
436  one_wire_speeds master_speed;
437  double t_slot;
438  double byte_time;
439 
440  // Timings
441  double t_w0l;
442  double t_rec;
443 
444  /***** Command code *****/
445  oneWireScript[oneWireScript_length++] = PC_OW_WRITE_BYTE;
446 
447  /***** Command parameter *****/
448  oneWireScript[oneWireScript_length++] = txByte;
449 
450  /***** Add expected response size to total response length *****/
451  *response_index = oneWireScriptResponse_length;
452  oneWireScriptResponse_length += 2;
453 
454  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
455  oneWireScript_commandsCount++;
456 
457  /***** Add accumulative 1-Wire time in us *****/
458  /***** Fetch timings *****/
459  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
460  {
461  return error;
462  }
463 
464  if(master_speed != STANDARD)
465  {
466  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
467  {
468  return error;
469  }
470  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
471  {
472  return error;
473  }
474  }
475  else
476  {
477  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
478  {
479  return error;
480  }
481  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
482  {
483  return error;
484  }
485  }
486 
487  //Calculate 1-Wire time
488  t_slot = t_w0l + t_rec;
489  byte_time = 8 * t_slot;
490 
491  //Add to total 1-Wire time
492  oneWireScript_accumulativeOneWireTime += byte_time;
493 
494  return error;
495 }
496 
497 int OneWire_Script_Add_OW_READ_BYTE(uint8_t *response_index)
498 {
499  int error = 0;
500 
501  // Delay variables
502  one_wire_speeds master_speed;
503  double t_slot;
504  double byte_time;
505 
506  // Timings
507  double t_w0l;
508  double t_rec;
509 
510  /***** Command code *****/
511  oneWireScript[oneWireScript_length++] = PC_OW_READ_BYTE;
512 
513  /***** Command parameter *****/
514  //no parameter
515 
516  /***** Add expected response size to total response length *****/
517  *response_index = oneWireScriptResponse_length;
518  oneWireScriptResponse_length += 2;
519 
520  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
521  oneWireScript_commandsCount++;
522 
523  /***** Add accumulative 1-Wire time in us *****/
524  /***** Fetch timings *****/
525  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
526  {
527  return error;
528  }
529 
530  if(master_speed != STANDARD)
531  {
532  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
533  {
534  return error;
535  }
536  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
537  {
538  return error;
539  }
540  }
541  else
542  {
543  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
544  {
545  return error;
546  }
547  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
548  {
549  return error;
550  }
551  }
552 
553  //Calculate 1-Wire time
554  t_slot = t_w0l + t_rec;
555  byte_time = 8 * t_slot;
556 
557  //Add to total 1-Wire time
558  oneWireScript_accumulativeOneWireTime += byte_time;
559 
560  return error;
561 }
562 
563 int OneWire_Script_Add_OW_TRIPLET(uint8_t *response_index, bool t_value)
564 {
565  int error = 0;
566 
567  // Delay variables
568  one_wire_speeds master_speed;
569  double t_slot;
570 
571  // Timings
572  double t_w0l;
573  double t_rec;
574 
575  /***** Command code *****/
576  oneWireScript[oneWireScript_length++] = PC_OW_TRIPLET;
577 
578  /***** Command parameter *****/
579  oneWireScript[oneWireScript_length++] = t_value;
580 
581  /***** Add expected response size to total response length *****/
582  *response_index = oneWireScriptResponse_length;
583  oneWireScriptResponse_length += 2;
584 
585  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
586 // oneWireScript_commandsCount++; // omitted
587 
588  /***** Add accumulative 1-Wire time in us *****/
589  /***** Fetch timings *****/
590  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
591  {
592  return error;
593  }
594 
595  if(master_speed != STANDARD)
596  {
597  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
598  {
599  return error;
600  }
601  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
602  {
603  return error;
604  }
605  }
606  else
607  {
608  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
609  {
610  return error;
611  }
612  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
613  {
614  return error;
615  }
616  }
617 
618  //Calculate 1-Wire time
619  t_slot = t_w0l + t_rec;
620 
621  //Add to total 1-Wire time
622  oneWireScript_accumulativeOneWireTime += t_slot;
623 
624  return error;
625 }
626 
627 int OneWire_Script_Add_OV_SKIP(uint8_t *response_index)
628 {
629  int error = 0;
630 
631  // Delay variables
632  double standard_ow_rst_time;
633  double overdrive_ow_rst_time;
634  double t_slot;
635  double byte_time;
636 
637  // Timings
638  double standard_t_w0l;
639  double standard_t_rec;
640  double standard_t_rstl;
641  double standard_t_rsth;
642  double overdrive_t_rstl;
643  double overdrive_t_rsth;
644 
645  /***** Command code *****/
646  oneWireScript[oneWireScript_length++] = PC_OW_OV_SKIP;
647 
648  /***** Command parameter *****/
649  //No parameter
650 
651  /***** Add expected response size to total response length *****/
652  *response_index = oneWireScriptResponse_length;
653  oneWireScriptResponse_length += 2;
654 
655  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
656  oneWireScript_commandsCount += 3; //1-Wire STD reset + Overdrive Skip Command + 1-Wire OV reset
657 
658  /***** Add accumulative 1-Wire time in us *****/
659  /***** Fetch timings *****/
660  if ((error = OneWire_Get_tRSTH(&overdrive_t_rsth, OVERDRIVE)) != 0)
661  {
662  return error;
663  }
664  if ((error = OneWire_Get_tRSTL(&overdrive_t_rstl, OVERDRIVE)) != 0)
665  {
666  return error;
667  }
668  if ((error = OneWire_Get_tW0L(&standard_t_w0l, STANDARD)) != 0)
669  {
670  return error;
671  }
672  if ((error = OneWire_Get_tREC(&standard_t_rec, STANDARD)) != 0)
673  {
674  return error;
675  }
676  if ((error = OneWire_Get_tRSTH(&standard_t_rsth, STANDARD)) != 0)
677  {
678  return error;
679  }
680  if ((error = OneWire_Get_tRSTL(&standard_t_rstl, STANDARD)) != 0)
681  {
682  return error;
683  }
684 
685  //Calculate 1-Wire time
686  t_slot = standard_t_w0l + standard_t_rec;
687  byte_time = 8 * t_slot;
688  standard_ow_rst_time = standard_t_rsth + standard_t_rstl;
689  overdrive_ow_rst_time = overdrive_t_rsth + overdrive_t_rstl;
690 
691  //Add to total 1-Wire time
692  oneWireScript_accumulativeOneWireTime += standard_ow_rst_time + byte_time + overdrive_ow_rst_time + 2000;
693 
694  return error;
695 }
696 
697 int OneWire_Script_Add_SKIP(uint8_t *response_index)
698 {
699  int error = 0;
700 
701  // Delay variables
702  double ow_rst_time;
703  double t_slot;
704  double byte_time;
705 
706  // Timings
707  double t_w0l;
708  double t_rec;
709  double t_rstl;
710  double t_rsth;
711 
712  /***** Command code *****/
713  oneWireScript[oneWireScript_length++] = PC_OW_SKIP;
714 
715  /***** Command parameter *****/
716  //No parameter
717 
718  /***** Add expected response size to total response length *****/
719  *response_index = oneWireScriptResponse_length;
720  oneWireScriptResponse_length += 2;
721 
722  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
723  oneWireScript_commandsCount += 2; //1-Wire STD reset + Skip ROM Command
724 
725  /***** Add accumulative 1-Wire time in us *****/
726  /***** Fetch timings *****/
727  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
728  {
729  return error;
730  }
731  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
732  {
733  return error;
734  }
735  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
736  {
737  return error;
738  }
739  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
740  {
741  return error;
742  }
743 
744  //Calculate 1-Wire time
745  t_slot = t_w0l + t_rec;
746  byte_time = 8 * t_slot;
747  ow_rst_time = t_rsth + t_rstl;
748 
749  //Add to total 1-Wire time
750  oneWireScript_accumulativeOneWireTime += ow_rst_time + byte_time;
751 
752  return error;
753 }
754 
755 int OneWire_Script_Add_OW_READ_BLOCK(uint8_t *response_index, uint8_t rxBytes)
756 {
757  int error = 0;
758 
759  // Delay variables
760  one_wire_speeds master_speed;
761  double t_slot;
762  double byte_time;
763 
764  // Timings
765  double t_w0l;
766  double t_rec;
767 
768  /***** Command code *****/
769  oneWireScript[oneWireScript_length++] = PC_OW_READ_BLOCK;
770 
771  /***** Command parameter *****/
772  oneWireScript[oneWireScript_length++] = rxBytes;
773 
774  /***** Add expected response size to total response length *****/
775  *response_index = oneWireScriptResponse_length;
776  oneWireScriptResponse_length += rxBytes + 2;
777 
778  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
779  oneWireScript_commandsCount += rxBytes;
780 
781  /***** Add accumulative 1-Wire time in us *****/
782  /***** Fetch timings *****/
783  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
784  {
785  return error;
786  }
787 
788  if(master_speed != STANDARD)
789  {
790  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
791  {
792  return error;
793  }
794  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
795  {
796  return error;
797  }
798  }
799  else
800  {
801  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
802  {
803  return error;
804  }
805  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
806  {
807  return error;
808  }
809  }
810 
811  //Calculate 1-Wire time
812  t_slot = t_w0l + t_rec;
813  byte_time = 8 * t_slot;
814 
815  //Add to total 1-Wire time
816  oneWireScript_accumulativeOneWireTime += byte_time * rxBytes;
817 
818  return error;
819 }
820 
821 int OneWire_Script_Add_OW_WRITE_BLOCK(uint8_t *response_index, const uint8_t *txData, uint8_t txData_length)
822 {
823  int error = 0;
824 
825  // Delay variables
826  one_wire_speeds master_speed;
827  double t_slot;
828  double byte_time;
829 
830  // Timings
831  double t_w0l;
832  double t_rec;
833 
834  /***** Command code *****/
835  oneWireScript[oneWireScript_length++] = PC_OW_WRITE_BLOCK;
836 
837  /***** Command parameter *****/
838  oneWireScript[oneWireScript_length++] = txData_length;
839  for(int i = 0; i < txData_length; i++)
840  {
841  oneWireScript[oneWireScript_length++] = txData[i];
842  }
843 
844  /***** Add expected response size to total response length *****/
845  *response_index = oneWireScriptResponse_length;
846  oneWireScriptResponse_length += 2;
847 
848  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
849  oneWireScript_commandsCount += txData_length;
850 
851  /***** Add accumulative 1-Wire time in us *****/
852  /***** Fetch timings *****/
853  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
854  {
855  return error;
856  }
857 
858  if(master_speed != STANDARD)
859  {
860  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
861  {
862  return error;
863  }
864  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
865  {
866  return error;
867  }
868  }
869  else
870  {
871  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
872  {
873  return error;
874  }
875  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
876  {
877  return error;
878  }
879  }
880 
881  //Calculate 1-Wire time
882  t_slot = t_w0l + t_rec;
883  byte_time = 8 * t_slot;
884 
885  //Add to total 1-Wire time
886  oneWireScript_accumulativeOneWireTime += byte_time * txData_length;
887 
888  return error;
889 }
890 
891 void OneWire_Script_Add_DELAY(uint8_t ms)
892 {
893  /***** Command code *****/
894  oneWireScript[oneWireScript_length++] = PC_DELAY;
895 
896  /***** Command parameter *****/
897  oneWireScript[oneWireScript_length++] = ms;
898 
899  /***** Add expected response size to total response length *****/
900 // *response_index = oneWireScriptResponse_length; //ommited
901 // oneWireScriptResponse_length += 2; //ommited
902 
903  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
904 
905  /***** Add accumulative 1-Wire time in us *****/
906  oneWireScript_accumulativeOneWireTime += ms * 1000;
907 }
908 
909 void OneWire_Script_Add_PRIME_SPU(void)
910 {
911  /***** Command code *****/
912  oneWireScript[oneWireScript_length++] = PC_PRIME_SPU;
913 
914  /***** Command parameter *****/
915  //ommited
916 
917  /***** Add expected response size to total response length *****/
918  //ommited
919 
920  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
921 
922  /***** Add accumulative 1-Wire time in us *****/
923  //ommited
924 }
925 
926 void OneWire_Script_Add_SPU_OFF(void)
927 {
928  /***** Command code *****/
929  oneWireScript[oneWireScript_length++] = PC_SPU_OFF;
930 
931  /***** Command parameter *****/
932  //ommited
933 
934  /***** Add expected response size to total response length *****/
935  //ommited
936 
937  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
938 
939  /***** Add accumulative 1-Wire time in us *****/
940  //ommited
941 }
942 
943 int OneWire_Script_Add_SPEED(one_wire_speeds spd, bool ignore)
944 {
945  int error = 0;
946 
947  // Delay variables
948  double ow_rst_time;
949 
950  // Timings
951  double t_rstl;
952  double t_rsth;
953 
954  /***** Command code *****/
955  oneWireScript[oneWireScript_length++] = PC_SPEED;
956 
957  /***** Command parameter *****/
958  oneWireScript[oneWireScript_length++] = ((spd ^ 1) << 7) | (spd << 3) | (ignore << 1);
959 
960  /***** Add expected response size to total response length *****/
961  //ommited
962 
963  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
964  oneWireScript_commandsCount++;
965 
966  /***** Add accumulative 1-Wire time in us *****/
967  /***** Fetch timings *****/
968  if(spd != STANDARD)
969  {
970  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
971  {
972  return error;
973  }
974  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
975  {
976  return error;
977  }
978  }
979  else
980  {
981  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
982  {
983  return error;
984  }
985  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
986  {
987  return error;
988  }
989  }
990 
991  //Calculate 1-Wire time
992  ow_rst_time = t_rstl + t_rsth;
993 
994  //Add to total 1-Wire time
995  oneWireScript_accumulativeOneWireTime += ow_rst_time;
996 
997  return error;
998 }
999 
1000 int OneWire_Script_Add_VERIFY_TOGGLE(uint8_t *response_index)
1001 {
1002  int error = 0;
1003 
1004  // Delay variables
1005  one_wire_speeds master_speed;
1006  double t_slot;
1007  double byte_time;
1008 
1009  // Timings
1010  double t_w0l;
1011  double t_rec;
1012 
1013  /***** Command code *****/
1014  oneWireScript[oneWireScript_length++] = PC_VERIFY_TOGGLE;
1015 
1016  /***** Command parameter *****/
1017  //ommited
1018 
1019  /***** Add expected response size to total response length *****/
1020  *response_index = oneWireScriptResponse_length;
1021  oneWireScriptResponse_length += 2;
1022 
1023  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1024  oneWireScript_commandsCount++;
1025 
1026  /***** Add accumulative 1-Wire time in us *****/
1027  /***** Fetch timings *****/
1028  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
1029  {
1030  return error;
1031  }
1032 
1033  if(master_speed != STANDARD)
1034  {
1035  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
1036  {
1037  return error;
1038  }
1039  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
1040  {
1041  return error;
1042  }
1043  }
1044  else
1045  {
1046  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
1047  {
1048  return error;
1049  }
1050  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
1051  {
1052  return error;
1053  }
1054  }
1055 
1056  //Calculate 1-Wire time
1057  t_slot = t_w0l + t_rec;
1058  byte_time = 8 * t_slot;
1059 
1060  //Add to total 1-Wire time
1061  oneWireScript_accumulativeOneWireTime += byte_time;
1062 
1063  return error;
1064 }
1065 
1066 int OneWire_Script_Add_VERIFY_BYTE(uint8_t *response_index, uint8_t byte)
1067 {
1068  int error = 0;
1069 
1070  // Delay variables
1071  one_wire_speeds master_speed;
1072  double t_slot;
1073  double byte_time;
1074 
1075  // Timings
1076  double t_w0l;
1077  double t_rec;
1078 
1079  /***** Command code *****/
1080  oneWireScript[oneWireScript_length++] = PC_VERIFY_BYTE;
1081 
1082  /***** Command parameter *****/
1083  oneWireScript[oneWireScript_length++] = byte;
1084 
1085  /***** Add expected response size to total response length *****/
1086  *response_index = oneWireScriptResponse_length;
1087  oneWireScriptResponse_length += 2;
1088 
1089  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1090  oneWireScript_commandsCount++;
1091 
1092  /***** Add accumulative 1-Wire time in us *****/
1093  /***** Fetch timings *****/
1094  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
1095  {
1096  return error;
1097  }
1098 
1099  if(master_speed != STANDARD)
1100  {
1101  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
1102  {
1103  return error;
1104  }
1105  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
1106  {
1107  return error;
1108  }
1109  }
1110  else
1111  {
1112  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
1113  {
1114  return error;
1115  }
1116  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
1117  {
1118  return error;
1119  }
1120  }
1121 
1122  //Calculate 1-Wire time
1123  t_slot = t_w0l + t_rec;
1124  byte_time = 8 * t_slot;
1125 
1126  //Add to total 1-Wire time
1127  oneWireScript_accumulativeOneWireTime += byte_time;
1128 
1129  return error;
1130 }
1131 
1132 void OneWire_Script_Add_CRC16_START(void)
1133 {
1134  /***** Command code *****/
1135  oneWireScript[oneWireScript_length++] = PC_CRC16_START;
1136 
1137  /***** Command parameter *****/
1138  //ommited
1139 
1140  /***** Add expected response size to total response length *****/
1141  //ommited
1142 
1143  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1144 
1145  /***** Add accumulative 1-Wire time in us *****/
1146  //ommited
1147 }
1148 
1149 void OneWire_Script_Add_VERIFY_CRC16(uint8_t *response_index, unsigned short hex_value)
1150 {
1151  /***** Command code *****/
1152  oneWireScript[oneWireScript_length++] = PC_VERIFY_CRC16;
1153 
1154  /***** Command parameter *****/
1155  oneWireScript[oneWireScript_length++] = (uint8_t)hex_value; //LSB
1156  oneWireScript[oneWireScript_length++] = (uint8_t)(hex_value >> 8); //MSB
1157 
1158  /***** Add expected response size to total response length *****/
1159  *response_index = oneWireScriptResponse_length;
1160  oneWireScriptResponse_length += 2;
1161 
1162  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1163 
1164  /***** Add accumulative 1-Wire time in us *****/
1165  //ommited
1166 }
1167 
1168 void OneWire_Script_Add_SET_GPIO(uint8_t *response_index, gpio_settings pioac)
1169 {
1170  /***** Command code *****/
1171  oneWireScript[oneWireScript_length++] = PC_SET_GPIO;
1172 
1173  /***** Command parameter *****/
1174  oneWireScript[oneWireScript_length++] = pioac;
1175 
1176  /***** Add expected response size to total response length *****/
1177  *response_index = oneWireScriptResponse_length;
1178  oneWireScriptResponse_length += 2;
1179 
1180  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1181 
1182  /***** Add accumulative 1-Wire time in us *****/
1183  //ommited
1184 }
1185 
1186 void OneWire_Script_Add_READ_GPIO(uint8_t *response_index)
1187 {
1188  /***** Command code *****/
1189  oneWireScript[oneWireScript_length++] = PC_READ_GPIO;
1190 
1191  /***** Command parameter *****/
1192  //ommited
1193 
1194  /***** Add expected response size to total response length *****/
1195  *response_index = oneWireScriptResponse_length;
1196  oneWireScriptResponse_length += 2;
1197 
1198  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1199 
1200  /***** Add accumulative 1-Wire time in us *****/
1201  //ommited
1202 }
1203 
1204 void OneWire_Script_Add_VERIFY_GPIO(uint8_t *response_index, gpio_verify_level_detection pioal)
1205 {
1206  /***** Command code *****/
1207  oneWireScript[oneWireScript_length++] = PC_VERIFY_GPIO;
1208 
1209  /***** Command parameter *****/
1210  oneWireScript[oneWireScript_length++] = pioal;
1211 
1212  /***** Add expected response size to total response length *****/
1213  *response_index = oneWireScriptResponse_length;
1214  oneWireScriptResponse_length += 2;
1215 
1216  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1217 
1218  /***** Add accumulative 1-Wire time in us *****/
1219  //ommited
1220 }
1221 
1222 void OneWire_Script_Add_CONFIG_RPUP_BUF(unsigned short hex_value)
1223 {
1224  /***** Command code *****/
1225  oneWireScript[oneWireScript_length++] = PC_CONFIG_RPUP_BUF;
1226 
1227  /***** Command parameter *****/
1228  oneWireScript[oneWireScript_length++] = (uint8_t)hex_value; //LSB
1229  oneWireScript[oneWireScript_length++] = (uint8_t)(hex_value >> 8); //MSB
1230 
1231  /***** Add expected response size to total response length *****/
1232  //ommited
1233 
1234  /***** Add 1-Wire actions to total command count (only applies to 1-Wire reset, and 1-Wire bytes) *****/
1235 
1236  /***** Add accumulative 1-Wire time in us *****/
1237  //ommited
1238 }
1239 
1240 /* **** High Level Functions **** */
1241 int OneWire_Enable_APU(bool apu)
1242 {
1243  int error = 0;
1244  uint8_t reg_data[2];
1245 
1246  if (apu)
1247  {
1248  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1249  {
1250  return error;
1251  }
1252 
1253  reg_data[0] = 0x00;
1254  reg_data[1] |= 0x10;
1255 
1256  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1257  {
1258  return error;
1259  }
1260  }
1261  else
1262  {
1263  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1264  {
1265  return error;
1266  }
1267 
1268  reg_data[0] = 0x00;
1269  reg_data[1] &= ~(0x10);
1270 
1271  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1272  {
1273  return error;
1274  }
1275  }
1276  return error;
1277 }
1278 
1279 int OneWire_Enable_SPU(bool spu)
1280 {
1281  int error = 0;
1282  uint8_t reg_data[2];
1283 
1284  if (spu)
1285  {
1286  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1287  {
1288  return error;
1289  }
1290 
1291  reg_data[0] = 0x00;
1292  reg_data[1] |= 0x20;
1293 
1294  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1295  {
1296  return error;
1297  }
1298 
1299  }
1300  else
1301  {
1302  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1303  {
1304  return error;
1305  }
1306 
1307  reg_data[0] = 0x00;
1308  reg_data[1] &= ~(0x20);
1309 
1310  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1311  {
1312  return error;
1313  }
1314  }
1315  return error;
1316 }
1317 
1318 int OneWire_Enable_OneWirePowerDown(bool pdn)
1319 {
1320  int error = 0;
1321  uint8_t reg_data[2];
1322 
1323  if (pdn)
1324  {
1325  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1326  {
1327  return error;
1328  }
1329 
1330  reg_data[0] = 0x00;
1331  reg_data[1] |= 0x40;
1332 
1333  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1334  {
1335  return error;
1336  }
1337 
1338  }
1339  else
1340  {
1341  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1342  {
1343  return error;
1344  }
1345 
1346  reg_data[0] = 0x00;
1347  reg_data[1] &= ~(0x40);
1348 
1349  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1350  {
1351  return error;
1352  }
1353  }
1354  return error;
1355 }
1356 
1357 int OneWire_Set_OneWireMasterSpeed(one_wire_speeds spd)
1358 {
1359  int error = 0;
1360  uint8_t reg_data[2];
1361 
1362  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1363  {
1364  return error;
1365  }
1366 
1367  reg_data[0] = 0x00;
1368  reg_data[1] = ((reg_data[1] & ~0x80) | (spd << 7));
1369 
1370  if((error = DS2485_WriteOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1371  {
1372  return error;
1373  }
1374 
1375  return error;
1376 }
1377 
1378 int OneWire_Get_OneWireMasterSpeed(one_wire_speeds *spd)
1379 {
1380  int error = 0;
1381  uint8_t reg_data[2];
1382 
1383  if((error = DS2485_ReadOneWirePortConfig(MASTER_CONFIGURATION, reg_data)) != 0)
1384  {
1385  return error;
1386  }
1387 
1388  if((reg_data[1] >> 7) != STANDARD)
1389  {
1390  *spd = OVERDRIVE;
1391  }
1392  else
1393  {
1394  *spd = STANDARD;
1395  }
1396 
1397  return error;
1398 }
1399 
1400 int OneWire_Set_Custom_RPUP_BUF(vth_values vth, viapo_values viapo, rwpu_values rwpu)
1401 {
1402  int error = 0;
1403  uint8_t reg_data[2];
1404 
1405  reg_data[1] = 0x80;
1406  reg_data[0] = (vth << 4) | (viapo << 2) | rwpu;
1407 
1408  if((error = DS2485_WriteOneWirePortConfig(RPUP_BUF, reg_data)) != 0)
1409  {
1410  return error;
1411  }
1412 
1413  return error;
1414 }
1415 int OneWire_Get_Custom_RPUP_BUF(vth_values *vth, viapo_values *viapo, rwpu_values *rwpu)
1416 {
1417  int error = 0;
1418  uint8_t reg_data[2];
1419 
1420  if((error = DS2485_ReadOneWirePortConfig(RPUP_BUF, reg_data)) != 0)
1421  {
1422  return error;
1423  }
1424 
1425  switch((reg_data[0] >> 4) & 0x03)
1426  {
1427  case VTH_LOW:
1428  *vth = VTH_LOW;
1429  break;
1430  case VTH_MEDIUM:
1431  *vth = VTH_MEDIUM;
1432  break;
1433  case VTH_HIGH:
1434  *vth = VTH_HIGH;
1435  break;
1436  case VTH_OFF:
1437  *vth = VTH_OFF;
1438  break;
1439  }
1440 
1441  switch((reg_data[0] >> 2) & 0x03)
1442  {
1443  case VIAPO_LOW:
1444  *viapo = VIAPO_LOW;
1445  break;
1446  case VIAPO_MEDIUM:
1447  *viapo = VIAPO_MEDIUM;
1448  break;
1449  case VIAPO_HIGH:
1450  *viapo = VIAPO_HIGH;
1451  break;
1452  case VIAPO_OFF:
1453  *viapo = VIAPO_OFF;
1454  break;
1455  }
1456 
1457  switch(reg_data[0] & 0x03)
1458  {
1459  case RWPU_EXTERNAL:
1460  *rwpu = RWPU_EXTERNAL;
1461  break;
1462  case RWPU_500:
1463  *rwpu = RWPU_500;
1464  break;
1465  case RWPU_1000:
1466  *rwpu = RWPU_1000;
1467  break;
1468  case RWPU_333:
1469  *rwpu = RWPU_333;
1470  break;
1471  }
1472 
1473  return error;
1474 }
1475 
1476 
1477 int OneWire_Set_tRSTL_Standard_Predefined(one_wire_timing_presets trstl)
1478 {
1479  int error = 0;
1480  uint8_t reg_data[2];
1481 
1482  reg_data[1] = 0x00;
1483  reg_data[0] = trstl;
1484 
1485  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tRSTL, reg_data)) != 0)
1486  {
1487  return error;
1488  }
1489 
1490  return error;
1491 }
1492 int OneWire_Set_tRSTL_Overdrive_Predefined(one_wire_timing_presets trstl)
1493 {
1494  int error = 0;
1495  uint8_t reg_data[2];
1496 
1497  reg_data[1] = 0x00;
1498  reg_data[0] = trstl;
1499 
1500  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tRSTL, reg_data)) != 0)
1501  {
1502  return error;
1503  }
1504 
1505  return error;
1506 }
1507 int OneWire_Set_tRSTL_Standard_Custom(double trstl)
1508 {
1509  int error = 0;
1510  uint8_t reg_data[2];
1511 
1512  if (trstl > 1020)
1513  {
1514  return error = RB_INVALID_PARAMETER;
1515  }
1516 
1517  trstl *= 1000; //us -> ns
1518  trstl /= (double)62.5;
1519  trstl = ((int)trstl | 0x8000);
1520  reg_data[0] = (uint8_t)trstl;
1521  reg_data[1] = ((int)trstl >> 8);
1522 
1523  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tRSTL, reg_data)) != 0)
1524  {
1525  return error;
1526  }
1527 
1528  return error;
1529 }
1530 int OneWire_Set_tRSTL_Overdrive_Custom(double trstl)
1531 {
1532  int error = 0;
1533  uint8_t reg_data[2];
1534 
1535  if (trstl > 126)
1536  {
1537  return error = RB_INVALID_PARAMETER;
1538  }
1539 
1540  trstl *= 1000; //us -> ns
1541  trstl /= (double)62.5;
1542  trstl = ((int)trstl | 0x8000);
1543  reg_data[0] = (uint8_t)trstl;
1544  reg_data[1] = ((int)trstl >> 8);
1545 
1546  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tRSTL, reg_data)) != 0)
1547  {
1548  return error;
1549  }
1550 
1551  return error;
1552 }
1553 int OneWire_Get_tRSTL(double *trstl, one_wire_speeds spd)
1554 {
1555  int error = 0;
1556  uint8_t reg_data[2];
1557  unsigned short value;
1558 
1559  if(spd != STANDARD) //Overdrive
1560  {
1561  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tRSTL, reg_data)) != 0)
1562  {
1563  return error;
1564  }
1565 
1566  if((reg_data[1] >> 7) != 0) //custom value
1567  {
1568 
1569  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
1570  *trstl = (double)value;
1571  *trstl *= (double)62.5;
1572  *trstl /= 1000;
1573  }
1574  else //predefined value
1575  {
1576  switch(reg_data[0])
1577  {
1578  case PRESET_0:
1579  *trstl = tRSTL_OVERDRIVE_PRESET_0;
1580  break;
1581 
1582  case PRESET_1:
1583  *trstl = tRSTL_OVERDRIVE_PRESET_1;
1584  break;
1585 
1586  case PRESET_2:
1587  *trstl = tRSTL_OVERDRIVE_PRESET_2;
1588  break;
1589 
1590  case PRESET_3:
1591  *trstl = tRSTL_OVERDRIVE_PRESET_3;
1592  break;
1593 
1594  case PRESET_4:
1595  *trstl = tRSTL_OVERDRIVE_PRESET_4;
1596  break;
1597 
1598  case PRESET_5:
1599  *trstl = tRSTL_OVERDRIVE_PRESET_5;
1600  break;
1601 
1602  case PRESET_6:
1603  *trstl = tRSTL_OVERDRIVE_PRESET_6;
1604  break;
1605 
1606  case PRESET_7:
1607  *trstl = tRSTL_OVERDRIVE_PRESET_7;
1608  break;
1609 
1610  case PRESET_8:
1611  *trstl = tRSTL_OVERDRIVE_PRESET_8;
1612  break;
1613 
1614  case PRESET_9:
1615  *trstl = tRSTL_OVERDRIVE_PRESET_9;
1616  break;
1617 
1618  case PRESET_A:
1619  *trstl = tRSTL_OVERDRIVE_PRESET_A;
1620  break;
1621 
1622  case PRESET_B:
1623  *trstl = tRSTL_OVERDRIVE_PRESET_B;
1624  break;
1625 
1626  case PRESET_C:
1627  *trstl = tRSTL_OVERDRIVE_PRESET_C;
1628  break;
1629 
1630  case PRESET_D:
1631  *trstl = tRSTL_OVERDRIVE_PRESET_D;
1632  break;
1633 
1634  case PRESET_E:
1635  *trstl = tRSTL_OVERDRIVE_PRESET_E;
1636  break;
1637 
1638  case PRESET_F:
1639  *trstl = tRSTL_OVERDRIVE_PRESET_F;
1640  break;
1641 
1642  default:
1643  *trstl = tRSTL_OVERDRIVE_PRESET_6;
1644  break;
1645  }
1646  }
1647  }
1648  else //Standard
1649  {
1650  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tRSTL, reg_data)) != 0)
1651  {
1652  return error;
1653  }
1654 
1655  if((reg_data[1] >> 7) != 0) //Custom Value
1656  {
1657  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
1658  *trstl = (double)value;
1659  *trstl *= (double)62.5;
1660  *trstl /= 1000;
1661  }
1662  else //Predefined value
1663  {
1664  switch(reg_data[0])
1665  {
1666  case PRESET_0:
1667  *trstl = tRSTL_STANDARD_PRESET_0;
1668  break;
1669 
1670  case PRESET_1:
1671  *trstl = tRSTL_STANDARD_PRESET_1;
1672  break;
1673 
1674  case PRESET_2:
1675  *trstl = tRSTL_STANDARD_PRESET_2;
1676  break;
1677 
1678  case PRESET_3:
1679  *trstl = tRSTL_STANDARD_PRESET_3;
1680  break;
1681 
1682  case PRESET_4:
1683  *trstl = tRSTL_STANDARD_PRESET_4;
1684  break;
1685 
1686  case PRESET_5:
1687  *trstl = tRSTL_STANDARD_PRESET_5;
1688  break;
1689 
1690  case PRESET_6:
1691  *trstl = tRSTL_STANDARD_PRESET_6;
1692  break;
1693 
1694  case PRESET_7:
1695  *trstl = tRSTL_STANDARD_PRESET_7;
1696  break;
1697 
1698  case PRESET_8:
1699  *trstl = tRSTL_STANDARD_PRESET_8;
1700  break;
1701 
1702  case PRESET_9:
1703  *trstl = tRSTL_STANDARD_PRESET_9;
1704  break;
1705 
1706  case PRESET_A:
1707  *trstl = tRSTL_STANDARD_PRESET_A;
1708  break;
1709 
1710  case PRESET_B:
1711  *trstl = tRSTL_STANDARD_PRESET_B;
1712  break;
1713 
1714  case PRESET_C:
1715  *trstl = tRSTL_STANDARD_PRESET_C;
1716  break;
1717 
1718  case PRESET_D:
1719  *trstl = tRSTL_STANDARD_PRESET_D;
1720  break;
1721 
1722  case PRESET_E:
1723  *trstl = tRSTL_STANDARD_PRESET_E;
1724  break;
1725 
1726  case PRESET_F:
1727  *trstl = tRSTL_STANDARD_PRESET_F;
1728  break;
1729 
1730  default:
1731  *trstl = tRSTL_STANDARD_PRESET_6;
1732  break;
1733  }
1734  }
1735  }
1736 
1737  return error;
1738 }
1739 int OneWire_Set_tRSTH_Standard_Predefined(one_wire_timing_presets trsth)
1740 {
1741  int error = 0;
1742  uint8_t reg_data[2];
1743 
1744  reg_data[1] = 0x00;
1745  reg_data[0] = trsth;
1746 
1747  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tRSTH, reg_data)) != 0)
1748  {
1749  return error;
1750  }
1751 
1752  return error;
1753 }
1754 int OneWire_Set_tRSTH_Overdrive_Predefined(one_wire_timing_presets trsth)
1755 {
1756  int error = 0;
1757  uint8_t reg_data[2];
1758 
1759  reg_data[1] = 0x00;
1760  reg_data[0] = trsth;
1761 
1762  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tRSTH, reg_data)) != 0)
1763  {
1764  return error;
1765  }
1766 
1767  return error;
1768 }
1769 int OneWire_Set_tRSTH_Standard_Custom(double trsth)
1770 {
1771  int error = 0;
1772  uint8_t reg_data[2];
1773 
1774  if (trsth > 1020)
1775  {
1776  return error = RB_INVALID_PARAMETER;
1777  }
1778 
1779  trsth *= 1000; //us -> ns
1780  trsth /= (double)62.5;
1781  trsth = ((int)trsth | 0x8000);
1782  reg_data[0] = (uint8_t)trsth;
1783  reg_data[1] = ((int)trsth >> 8);
1784 
1785  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tRSTH, reg_data)) != 0)
1786  {
1787  return error;
1788  }
1789 
1790  return error;
1791 }
1792 int OneWire_Set_tRSTH_Overdrive_Custom(double trsth)
1793 {
1794  int error = 0;
1795  uint8_t reg_data[2];
1796 
1797  if (trsth > 126)
1798  {
1799  return error = RB_INVALID_PARAMETER;
1800  }
1801 
1802  trsth *= 1000; //us -> ns
1803  trsth /= (double)62.5;
1804  trsth = ((int)trsth | 0x8000);
1805  reg_data[0] = (uint8_t)trsth;
1806  reg_data[1] = ((int)trsth >> 8);
1807 
1808  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tRSTH, reg_data)) != 0)
1809  {
1810  return error;
1811  }
1812 
1813  return error;
1814 }
1815 int OneWire_Get_tRSTH(double *trsth, one_wire_speeds spd)
1816 {
1817  int error = 0;
1818  uint8_t reg_data[2];
1819  unsigned short value;
1820 
1821  if(spd != STANDARD) //Overdrive
1822  {
1823  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tRSTH, reg_data)) != 0)
1824  {
1825  return error;
1826  }
1827 
1828  if((reg_data[1] >> 7) != 0) //custom value
1829  {
1830 
1831  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
1832  *trsth = (double)value;
1833  *trsth *= (double)62.5;
1834  *trsth /= 1000;
1835  }
1836  else //predefined value
1837  {
1838  switch(reg_data[0])
1839  {
1840  case PRESET_0:
1841  *trsth = tRSTH_OVERDRIVE_PRESET_0;
1842  break;
1843 
1844  case PRESET_1:
1845  *trsth = tRSTH_OVERDRIVE_PRESET_1;
1846  break;
1847 
1848  case PRESET_2:
1849  *trsth = tRSTH_OVERDRIVE_PRESET_2;
1850  break;
1851 
1852  case PRESET_3:
1853  *trsth = tRSTH_OVERDRIVE_PRESET_3;
1854  break;
1855 
1856  case PRESET_4:
1857  *trsth = tRSTH_OVERDRIVE_PRESET_4;
1858  break;
1859 
1860  case PRESET_5:
1861  *trsth = tRSTH_OVERDRIVE_PRESET_5;
1862  break;
1863 
1864  case PRESET_6:
1865  *trsth = tRSTH_OVERDRIVE_PRESET_6;
1866  break;
1867 
1868  case PRESET_7:
1869  *trsth = tRSTH_OVERDRIVE_PRESET_7;
1870  break;
1871 
1872  case PRESET_8:
1873  *trsth = tRSTH_OVERDRIVE_PRESET_8;
1874  break;
1875 
1876  case PRESET_9:
1877  *trsth = tRSTH_OVERDRIVE_PRESET_9;
1878  break;
1879 
1880  case PRESET_A:
1881  *trsth = tRSTH_OVERDRIVE_PRESET_A;
1882  break;
1883 
1884  case PRESET_B:
1885  *trsth = tRSTH_OVERDRIVE_PRESET_B;
1886  break;
1887 
1888  case PRESET_C:
1889  *trsth = tRSTH_OVERDRIVE_PRESET_C;
1890  break;
1891 
1892  case PRESET_D:
1893  *trsth = tRSTH_OVERDRIVE_PRESET_D;
1894  break;
1895 
1896  case PRESET_E:
1897  *trsth = tRSTH_OVERDRIVE_PRESET_E;
1898  break;
1899 
1900  case PRESET_F:
1901  *trsth = tRSTH_OVERDRIVE_PRESET_F;
1902  break;
1903 
1904  default:
1905  *trsth = tRSTH_OVERDRIVE_PRESET_6;
1906  break;
1907  }
1908  }
1909  }
1910  else //Standard
1911  {
1912  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tRSTH, reg_data)) != 0)
1913  {
1914  return error;
1915  }
1916 
1917  if((reg_data[1] >> 7) != 0) //Custom Value
1918  {
1919  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
1920  *trsth = (double)value;
1921  *trsth *= (double)62.5;
1922  *trsth /= 1000;
1923  }
1924  else //Predefined value
1925  {
1926  switch(reg_data[0])
1927  {
1928  case PRESET_0:
1929  *trsth = tRSTH_STANDARD_PRESET_0;
1930  break;
1931 
1932  case PRESET_1:
1933  *trsth = tRSTH_STANDARD_PRESET_1;
1934  break;
1935 
1936  case PRESET_2:
1937  *trsth = tRSTH_STANDARD_PRESET_2;
1938  break;
1939 
1940  case PRESET_3:
1941  *trsth = tRSTH_STANDARD_PRESET_3;
1942  break;
1943 
1944  case PRESET_4:
1945  *trsth = tRSTH_STANDARD_PRESET_4;
1946  break;
1947 
1948  case PRESET_5:
1949  *trsth = tRSTH_STANDARD_PRESET_5;
1950  break;
1951 
1952  case PRESET_6:
1953  *trsth = tRSTH_STANDARD_PRESET_6;
1954  break;
1955 
1956  case PRESET_7:
1957  *trsth = tRSTH_STANDARD_PRESET_7;
1958  break;
1959 
1960  case PRESET_8:
1961  *trsth = tRSTH_STANDARD_PRESET_8;
1962  break;
1963 
1964  case PRESET_9:
1965  *trsth = tRSTH_STANDARD_PRESET_9;
1966  break;
1967 
1968  case PRESET_A:
1969  *trsth = tRSTH_STANDARD_PRESET_A;
1970  break;
1971 
1972  case PRESET_B:
1973  *trsth = tRSTH_STANDARD_PRESET_B;
1974  break;
1975 
1976  case PRESET_C:
1977  *trsth = tRSTH_STANDARD_PRESET_C;
1978  break;
1979 
1980  case PRESET_D:
1981  *trsth = tRSTH_STANDARD_PRESET_D;
1982  break;
1983 
1984  case PRESET_E:
1985  *trsth = tRSTH_STANDARD_PRESET_E;
1986  break;
1987 
1988  case PRESET_F:
1989  *trsth = tRSTH_STANDARD_PRESET_F;
1990  break;
1991 
1992  default:
1993  *trsth = tRSTH_STANDARD_PRESET_6;
1994  break;
1995  }
1996  }
1997  }
1998 
1999  return error;
2000 }
2001 int OneWire_Set_tW0L_Standard_Predefined(one_wire_timing_presets tw0l)
2002 {
2003  int error = 0;
2004  uint8_t reg_data[2];
2005 
2006  reg_data[1] = 0x00;
2007  reg_data[0] = tw0l;
2008 
2009  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tW0L, reg_data)) != 0)
2010  {
2011  return error;
2012  }
2013 
2014  return error;
2015 }
2016 int OneWire_Set_tW0L_Overdrive_Predefined(one_wire_timing_presets tw0l)
2017 {
2018  int error = 0;
2019  uint8_t reg_data[2];
2020 
2021  reg_data[1] = 0x00;
2022  reg_data[0] = tw0l;
2023 
2024  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tW0L, reg_data)) != 0)
2025  {
2026  return error;
2027  }
2028 
2029  return error;
2030 }
2031 int OneWire_Set_tW0L_Standard_Custom(double tw0l)
2032 {
2033  int error = 0;
2034  uint8_t reg_data[2];
2035 
2036  if (tw0l > 126)
2037  {
2038  return error = RB_INVALID_PARAMETER;
2039  }
2040 
2041  tw0l *= 1000; //us -> ns
2042  tw0l /= (double)62.5;
2043  tw0l = ((int)tw0l | 0x8000);
2044  reg_data[0] = (uint8_t)tw0l;
2045  reg_data[1] = ((int)tw0l >> 8);
2046 
2047  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tW0L, reg_data)) != 0)
2048  {
2049  return error;
2050  }
2051 
2052  return error;
2053 }
2054 int OneWire_Set_tW0L_Overdrive_Custom(double tw0l)
2055 {
2056  int error = 0;
2057  uint8_t reg_data[2];
2058 
2059  if (tw0l > (double)31.5)
2060  {
2061  return error = RB_INVALID_PARAMETER;
2062  }
2063 
2064  tw0l *= 1000; //us -> ns
2065  tw0l /= (double)62.5;
2066  tw0l = ((int)tw0l | 0x8000);
2067  reg_data[0] = (uint8_t)tw0l;
2068  reg_data[1] = ((int)tw0l >> 8);
2069 
2070  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tW0L, reg_data)) != 0)
2071  {
2072  return error;
2073  }
2074 
2075  return error;
2076 }
2077 int OneWire_Get_tW0L(double *tw0l, one_wire_speeds spd)
2078 {
2079  int error = 0;
2080  uint8_t reg_data[2];
2081  unsigned short value;
2082 
2083  if(spd != STANDARD) //Overdrive
2084  {
2085  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tW0L, reg_data)) != 0)
2086  {
2087  return error;
2088  }
2089 
2090  if((reg_data[1] >> 7) != 0) //custom value
2091  {
2092 
2093  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2094  *tw0l = (double)value;
2095  *tw0l *= (double)62.5;
2096  *tw0l /= 1000;
2097  }
2098  else //predefined value
2099  {
2100  switch(reg_data[0])
2101  {
2102  case PRESET_0:
2103  *tw0l = tW0L_OVERDRIVE_PRESET_0;
2104  break;
2105 
2106  case PRESET_1:
2107  *tw0l = tW0L_OVERDRIVE_PRESET_1;
2108  break;
2109 
2110  case PRESET_2:
2111  *tw0l = tW0L_OVERDRIVE_PRESET_2;
2112  break;
2113 
2114  case PRESET_3:
2115  *tw0l = tW0L_OVERDRIVE_PRESET_3;
2116  break;
2117 
2118  case PRESET_4:
2119  *tw0l = tW0L_OVERDRIVE_PRESET_4;
2120  break;
2121 
2122  case PRESET_5:
2123  *tw0l = tW0L_OVERDRIVE_PRESET_5;
2124  break;
2125 
2126  case PRESET_6:
2127  *tw0l = tW0L_OVERDRIVE_PRESET_6;
2128  break;
2129 
2130  case PRESET_7:
2131  *tw0l = tW0L_OVERDRIVE_PRESET_7;
2132  break;
2133 
2134  case PRESET_8:
2135  *tw0l = tW0L_OVERDRIVE_PRESET_8;
2136  break;
2137 
2138  case PRESET_9:
2139  *tw0l = tW0L_OVERDRIVE_PRESET_9;
2140  break;
2141 
2142  case PRESET_A:
2143  *tw0l = tW0L_OVERDRIVE_PRESET_A;
2144  break;
2145 
2146  case PRESET_B:
2147  *tw0l = tW0L_OVERDRIVE_PRESET_B;
2148  break;
2149 
2150  case PRESET_C:
2151  *tw0l = tW0L_OVERDRIVE_PRESET_C;
2152  break;
2153 
2154  case PRESET_D:
2155  *tw0l = tW0L_OVERDRIVE_PRESET_D;
2156  break;
2157 
2158  case PRESET_E:
2159  *tw0l = tW0L_OVERDRIVE_PRESET_E;
2160  break;
2161 
2162  case PRESET_F:
2163  *tw0l = tW0L_OVERDRIVE_PRESET_F;
2164  break;
2165 
2166  default:
2167  *tw0l = tW0L_OVERDRIVE_PRESET_6;
2168  break;
2169  }
2170  }
2171  }
2172  else //Standard
2173  {
2174  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tW0L, reg_data)) != 0)
2175  {
2176  return error;
2177  }
2178 
2179  if((reg_data[1] >> 7) != 0) //Custom Value
2180  {
2181  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2182  *tw0l = (double)value;
2183  *tw0l *= (double)62.5;
2184  *tw0l /= 1000;
2185  }
2186  else //Predefined value
2187  {
2188  switch(reg_data[0])
2189  {
2190  case PRESET_0:
2191  *tw0l = tW0L_STANDARD_PRESET_0;
2192  break;
2193 
2194  case PRESET_1:
2195  *tw0l = tW0L_STANDARD_PRESET_1;
2196  break;
2197 
2198  case PRESET_2:
2199  *tw0l = tW0L_STANDARD_PRESET_2;
2200  break;
2201 
2202  case PRESET_3:
2203  *tw0l = tW0L_STANDARD_PRESET_3;
2204  break;
2205 
2206  case PRESET_4:
2207  *tw0l = tW0L_STANDARD_PRESET_4;
2208  break;
2209 
2210  case PRESET_5:
2211  *tw0l = tW0L_STANDARD_PRESET_5;
2212  break;
2213 
2214  case PRESET_6:
2215  *tw0l = tW0L_STANDARD_PRESET_6;
2216  break;
2217 
2218  case PRESET_7:
2219  *tw0l = tW0L_STANDARD_PRESET_7;
2220  break;
2221 
2222  case PRESET_8:
2223  *tw0l = tW0L_STANDARD_PRESET_8;
2224  break;
2225 
2226  case PRESET_9:
2227  *tw0l = tW0L_STANDARD_PRESET_9;
2228  break;
2229 
2230  case PRESET_A:
2231  *tw0l = tW0L_STANDARD_PRESET_A;
2232  break;
2233 
2234  case PRESET_B:
2235  *tw0l = tW0L_STANDARD_PRESET_B;
2236  break;
2237 
2238  case PRESET_C:
2239  *tw0l = tW0L_STANDARD_PRESET_C;
2240  break;
2241 
2242  case PRESET_D:
2243  *tw0l = tW0L_STANDARD_PRESET_D;
2244  break;
2245 
2246  case PRESET_E:
2247  *tw0l = tW0L_STANDARD_PRESET_E;
2248  break;
2249 
2250  case PRESET_F:
2251  *tw0l = tW0L_STANDARD_PRESET_F;
2252  break;
2253 
2254  default:
2255  *tw0l = tW0L_STANDARD_PRESET_6;
2256  break;
2257  }
2258  }
2259  }
2260 
2261  return error;
2262 }
2263 int OneWire_Set_tREC_Standard_Predefined(one_wire_timing_presets trec)
2264 {
2265  int error = 0;
2266  uint8_t reg_data[2];
2267 
2268  reg_data[1] = 0x00;
2269  reg_data[0] = trec;
2270 
2271  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tREC, reg_data)) != 0)
2272  {
2273  return error;
2274  }
2275 
2276  return error;
2277 }
2278 int OneWire_Set_tREC_Overdrive_Predefined(one_wire_timing_presets trec)
2279 {
2280  int error = 0;
2281  uint8_t reg_data[2];
2282 
2283  reg_data[1] = 0x00;
2284  reg_data[0] = trec;
2285 
2286  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tREC, reg_data)) != 0)
2287  {
2288  return error;
2289  }
2290 
2291  return error;
2292 }
2293 int OneWire_Set_tREC_Standard_Custom(double trec)
2294 {
2295  int error = 0;
2296  uint8_t reg_data[2];
2297 
2298  if (trec > (double)255.5)
2299  {
2300  return error = RB_INVALID_PARAMETER;
2301  }
2302 
2303  trec *= 1000; //us -> ns
2304  trec /= (double)62.5;
2305  trec = ((int)trec | 0x8000);
2306  reg_data[0] = (uint8_t)trec;
2307  reg_data[1] = ((int)trec >> 8);
2308 
2309  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tREC, reg_data)) != 0)
2310  {
2311  return error;
2312  }
2313 
2314  return error;
2315 }
2316 int OneWire_Set_tREC_Overdrive_Custom(double trec)
2317 {
2318  int error = 0;
2319  uint8_t reg_data[2];
2320 
2321  if (trec > (double)255.5)
2322  {
2323  return error = RB_INVALID_PARAMETER;
2324  }
2325 
2326  trec *= 1000; //us -> ns
2327  trec /= (double)62.5;
2328  trec = ((int)trec | 0x8000);
2329  reg_data[0] = (uint8_t)trec;
2330  reg_data[1] = ((int)trec >> 8);
2331 
2332  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tREC, reg_data)) != 0)
2333  {
2334  return error;
2335  }
2336 
2337  return error;
2338 }
2339 int OneWire_Get_tREC(double *trec, one_wire_speeds spd)
2340 {
2341  int error = 0;
2342  uint8_t reg_data[2];
2343  unsigned short value;
2344 
2345  if(spd != STANDARD) //Overdrive
2346  {
2347  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tREC, reg_data)) != 0)
2348  {
2349  return error;
2350  }
2351 
2352  if((reg_data[1] >> 7) != 0) //custom value
2353  {
2354 
2355  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2356  *trec = (double)value;
2357  *trec *= (double)62.5;
2358  *trec /= 1000;
2359  }
2360  else //predefined value
2361  {
2362  switch(reg_data[0])
2363  {
2364  case PRESET_0:
2365  *trec = tREC_OVERDRIVE_PRESET_0;
2366  break;
2367 
2368  case PRESET_1:
2369  *trec = tREC_OVERDRIVE_PRESET_1;
2370  break;
2371 
2372  case PRESET_2:
2373  *trec = tREC_OVERDRIVE_PRESET_2;
2374  break;
2375 
2376  case PRESET_3:
2377  *trec = tREC_OVERDRIVE_PRESET_3;
2378  break;
2379 
2380  case PRESET_4:
2381  *trec = tREC_OVERDRIVE_PRESET_4;
2382  break;
2383 
2384  case PRESET_5:
2385  *trec = tREC_OVERDRIVE_PRESET_5;
2386  break;
2387 
2388  case PRESET_6:
2389  *trec = tREC_OVERDRIVE_PRESET_6;
2390  break;
2391 
2392  case PRESET_7:
2393  *trec = tREC_OVERDRIVE_PRESET_7;
2394  break;
2395 
2396  case PRESET_8:
2397  *trec = tREC_OVERDRIVE_PRESET_8;
2398  break;
2399 
2400  case PRESET_9:
2401  *trec = tREC_OVERDRIVE_PRESET_9;
2402  break;
2403 
2404  case PRESET_A:
2405  *trec = tREC_OVERDRIVE_PRESET_A;
2406  break;
2407 
2408  case PRESET_B:
2409  *trec = tREC_OVERDRIVE_PRESET_B;
2410  break;
2411 
2412  case PRESET_C:
2413  *trec = tREC_OVERDRIVE_PRESET_C;
2414  break;
2415 
2416  case PRESET_D:
2417  *trec = tREC_OVERDRIVE_PRESET_D;
2418  break;
2419 
2420  case PRESET_E:
2421  *trec = tREC_OVERDRIVE_PRESET_E;
2422  break;
2423 
2424  case PRESET_F:
2425  *trec = tREC_OVERDRIVE_PRESET_F;
2426  break;
2427 
2428  default:
2429  *trec = tREC_OVERDRIVE_PRESET_6;
2430  break;
2431  }
2432  }
2433  }
2434  else //Standard
2435  {
2436  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tREC, reg_data)) != 0)
2437  {
2438  return error;
2439  }
2440 
2441  if((reg_data[1] >> 7) != 0) //Custom Value
2442  {
2443  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2444  *trec = (double)value;
2445  *trec *= (double)62.5;
2446  *trec /= 1000;
2447  }
2448  else //Predefined value
2449  {
2450  switch(reg_data[0])
2451  {
2452  case PRESET_0:
2453  *trec = tREC_STANDARD_PRESET_0;
2454  break;
2455 
2456  case PRESET_1:
2457  *trec = tREC_STANDARD_PRESET_1;
2458  break;
2459 
2460  case PRESET_2:
2461  *trec = tREC_STANDARD_PRESET_2;
2462  break;
2463 
2464  case PRESET_3:
2465  *trec = tREC_STANDARD_PRESET_3;
2466  break;
2467 
2468  case PRESET_4:
2469  *trec = tREC_STANDARD_PRESET_4;
2470  break;
2471 
2472  case PRESET_5:
2473  *trec = tREC_STANDARD_PRESET_5;
2474  break;
2475 
2476  case PRESET_6:
2477  *trec = tREC_STANDARD_PRESET_6;
2478  break;
2479 
2480  case PRESET_7:
2481  *trec = tREC_STANDARD_PRESET_7;
2482  break;
2483 
2484  case PRESET_8:
2485  *trec = tREC_STANDARD_PRESET_8;
2486  break;
2487 
2488  case PRESET_9:
2489  *trec = tREC_STANDARD_PRESET_9;
2490  break;
2491 
2492  case PRESET_A:
2493  *trec = tREC_STANDARD_PRESET_A;
2494  break;
2495 
2496  case PRESET_B:
2497  *trec = tREC_STANDARD_PRESET_B;
2498  break;
2499 
2500  case PRESET_C:
2501  *trec = tREC_STANDARD_PRESET_C;
2502  break;
2503 
2504  case PRESET_D:
2505  *trec = tREC_STANDARD_PRESET_D;
2506  break;
2507 
2508  case PRESET_E:
2509  *trec = tREC_STANDARD_PRESET_E;
2510  break;
2511 
2512  case PRESET_F:
2513  *trec = tREC_STANDARD_PRESET_F;
2514  break;
2515 
2516  default:
2517  *trec = tREC_STANDARD_PRESET_6;
2518  break;
2519  }
2520  }
2521  }
2522 
2523  return error;
2524 }
2525 
2526 int OneWire_Set_tMSI_Standard_Predefined(one_wire_timing_presets tmsi)
2527 {
2528  int error = 0;
2529  uint8_t reg_data[2];
2530 
2531  reg_data[1] = 0x00;
2532  reg_data[0] = tmsi;
2533 
2534  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSI, reg_data)) != 0)
2535  {
2536  return error;
2537  }
2538 
2539  return error;
2540 }
2541 int OneWire_Set_tMSI_Overdrive_Predefined(one_wire_timing_presets tmsi)
2542 {
2543  int error = 0;
2544  uint8_t reg_data[2];
2545 
2546  reg_data[1] = 0x00;
2547  reg_data[0] = tmsi;
2548 
2549  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSI, reg_data)) != 0)
2550  {
2551  return error;
2552  }
2553 
2554  return error;
2555 }
2556 int OneWire_Set_tMSI_Standard_Custom(double tmsi)
2557 {
2558  int error = 0;
2559  uint8_t reg_data[2];
2560 
2561  if (tmsi > (double)255.5)
2562  {
2563  return error = RB_INVALID_PARAMETER;
2564  }
2565 
2566  tmsi *= 1000; //us -> ns
2567  tmsi /= (double)62.5;
2568  tmsi = ((int)tmsi | 0x8000);
2569  reg_data[0] = (uint8_t)tmsi;
2570  reg_data[1] = ((int)tmsi >> 8);
2571 
2572  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSI, reg_data)) != 0)
2573  {
2574  return error;
2575  }
2576 
2577  return error;
2578 }
2579 int OneWire_Set_tMSI_Overdrive_Custom(double tmsi)
2580 {
2581  int error = 0;
2582  uint8_t reg_data[2];
2583 
2584  if (tmsi > (double)255.5)
2585  {
2586  return error = RB_INVALID_PARAMETER;
2587  }
2588 
2589  tmsi *= 1000; //us -> ns
2590  tmsi /= (double)62.5;
2591  tmsi = ((int)tmsi | 0x8000);
2592  reg_data[0] = (uint8_t)tmsi;
2593  reg_data[1] = ((int)tmsi >> 8);
2594 
2595  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSI, reg_data)) != 0)
2596  {
2597  return error;
2598  }
2599 
2600  return error;
2601 }
2602 int OneWire_Get_tMSI(double *tmsi, one_wire_speeds spd)
2603 {
2604  int error = 0;
2605  uint8_t reg_data[2];
2606  unsigned short value;
2607 
2608  if(spd != STANDARD) //Overdrive
2609  {
2610  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tMSI, reg_data)) != 0)
2611  {
2612  return error;
2613  }
2614 
2615  if((reg_data[1] >> 7) != 0) //custom value
2616  {
2617 
2618  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2619  *tmsi = (double)value;
2620  *tmsi *= (double)62.5;
2621  *tmsi /= 1000;
2622  }
2623  else //predefined value
2624  {
2625  switch(reg_data[0])
2626  {
2627  case PRESET_0:
2628  *tmsi = tMSI_OVERDRIVE_PRESET_0;
2629  break;
2630 
2631  case PRESET_1:
2632  *tmsi = tMSI_OVERDRIVE_PRESET_1;
2633  break;
2634 
2635  case PRESET_2:
2636  *tmsi = tMSI_OVERDRIVE_PRESET_2;
2637  break;
2638 
2639  case PRESET_3:
2640  *tmsi = tMSI_OVERDRIVE_PRESET_3;
2641  break;
2642 
2643  case PRESET_4:
2644  *tmsi = tMSI_OVERDRIVE_PRESET_4;
2645  break;
2646 
2647  case PRESET_5:
2648  *tmsi = tMSI_OVERDRIVE_PRESET_5;
2649  break;
2650 
2651  case PRESET_6:
2652  *tmsi = tMSI_OVERDRIVE_PRESET_6;
2653  break;
2654 
2655  case PRESET_7:
2656  *tmsi = tMSI_OVERDRIVE_PRESET_7;
2657  break;
2658 
2659  case PRESET_8:
2660  *tmsi = tMSI_OVERDRIVE_PRESET_8;
2661  break;
2662 
2663  case PRESET_9:
2664  *tmsi = tMSI_OVERDRIVE_PRESET_9;
2665  break;
2666 
2667  case PRESET_A:
2668  *tmsi = tMSI_OVERDRIVE_PRESET_A;
2669  break;
2670 
2671  case PRESET_B:
2672  *tmsi = tMSI_OVERDRIVE_PRESET_B;
2673  break;
2674 
2675  case PRESET_C:
2676  *tmsi = tMSI_OVERDRIVE_PRESET_C;
2677  break;
2678 
2679  case PRESET_D:
2680  *tmsi = tMSI_OVERDRIVE_PRESET_D;
2681  break;
2682 
2683  case PRESET_E:
2684  *tmsi = tMSI_OVERDRIVE_PRESET_E;
2685  break;
2686 
2687  case PRESET_F:
2688  *tmsi = tMSI_OVERDRIVE_PRESET_F;
2689  break;
2690 
2691  default:
2692  *tmsi = tMSI_OVERDRIVE_PRESET_6;
2693  break;
2694  }
2695  }
2696  }
2697  else //Standard
2698  {
2699  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tMSI, reg_data)) != 0)
2700  {
2701  return error;
2702  }
2703 
2704  if((reg_data[1] >> 7) != 0) //Custom Value
2705  {
2706  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2707  *tmsi = (double)value;
2708  *tmsi *= (double)62.5;
2709  *tmsi /= 1000;
2710  }
2711  else //Predefined value
2712  {
2713  switch(reg_data[0])
2714  {
2715  case PRESET_0:
2716  *tmsi = tMSI_STANDARD_PRESET_0;
2717  break;
2718 
2719  case PRESET_1:
2720  *tmsi = tMSI_STANDARD_PRESET_1;
2721  break;
2722 
2723  case PRESET_2:
2724  *tmsi = tMSI_STANDARD_PRESET_2;
2725  break;
2726 
2727  case PRESET_3:
2728  *tmsi = tMSI_STANDARD_PRESET_3;
2729  break;
2730 
2731  case PRESET_4:
2732  *tmsi = tMSI_STANDARD_PRESET_4;
2733  break;
2734 
2735  case PRESET_5:
2736  *tmsi = tMSI_STANDARD_PRESET_5;
2737  break;
2738 
2739  case PRESET_6:
2740  *tmsi = tMSI_STANDARD_PRESET_6;
2741  break;
2742 
2743  case PRESET_7:
2744  *tmsi = tMSI_STANDARD_PRESET_7;
2745  break;
2746 
2747  case PRESET_8:
2748  *tmsi = tMSI_STANDARD_PRESET_8;
2749  break;
2750 
2751  case PRESET_9:
2752  *tmsi = tMSI_STANDARD_PRESET_9;
2753  break;
2754 
2755  case PRESET_A:
2756  *tmsi = tMSI_STANDARD_PRESET_A;
2757  break;
2758 
2759  case PRESET_B:
2760  *tmsi = tMSI_STANDARD_PRESET_B;
2761  break;
2762 
2763  case PRESET_C:
2764  *tmsi = tMSI_STANDARD_PRESET_C;
2765  break;
2766 
2767  case PRESET_D:
2768  *tmsi = tMSI_STANDARD_PRESET_D;
2769  break;
2770 
2771  case PRESET_E:
2772  *tmsi = tMSI_STANDARD_PRESET_E;
2773  break;
2774 
2775  case PRESET_F:
2776  *tmsi = tMSI_STANDARD_PRESET_F;
2777  break;
2778 
2779  default:
2780  *tmsi = tMSI_STANDARD_PRESET_6;
2781  break;
2782  }
2783  }
2784  }
2785 
2786  return error;
2787 }
2788 
2789 int OneWire_Set_tMSP_Standard_Predefined(one_wire_timing_presets tmsp)
2790 {
2791  int error = 0;
2792  uint8_t reg_data[2];
2793 
2794  reg_data[1] = 0x00;
2795  reg_data[0] = tmsp;
2796 
2797  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSP, reg_data)) != 0)
2798  {
2799  return error;
2800  }
2801 
2802  return error;
2803 }
2804 int OneWire_Set_tMSP_Overdrive_Predefined(one_wire_timing_presets tmsp)
2805 {
2806  int error = 0;
2807  uint8_t reg_data[2];
2808 
2809  reg_data[1] = 0x00;
2810  reg_data[0] = tmsp;
2811 
2812  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSP, reg_data)) != 0)
2813  {
2814  return error;
2815  }
2816 
2817  return error;
2818 }
2819 int OneWire_Set_tMSP_Standard_Custom(double tmsp)
2820 {
2821  int error = 0;
2822  uint8_t reg_data[2];
2823 
2824  if (tmsp > (double)255.5)
2825  {
2826  return error = RB_INVALID_PARAMETER;
2827  }
2828 
2829  tmsp *= 1000; //us -> ns
2830  tmsp /= (double)62.5;
2831  tmsp = ((int)tmsp | 0x8000);
2832  reg_data[0] = (uint8_t)tmsp;
2833  reg_data[1] = ((int)tmsp >> 8);
2834 
2835  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSP, reg_data)) != 0)
2836  {
2837  return error;
2838  }
2839 
2840  return error;
2841 }
2842 int OneWire_Set_tMSP_Overdrive_Custom(double tmsp)
2843 {
2844  int error = 0;
2845  uint8_t reg_data[2];
2846 
2847  if (tmsp > (double)255.5)
2848  {
2849  return error = RB_INVALID_PARAMETER;
2850  }
2851 
2852  tmsp *= 1000; //us -> ns
2853  tmsp /= (double)62.5;
2854  tmsp = ((int)tmsp | 0x8000);
2855  reg_data[0] = (uint8_t)tmsp;
2856  reg_data[1] = ((int)tmsp >> 8);
2857 
2858  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSP, reg_data)) != 0)
2859  {
2860  return error;
2861  }
2862 
2863  return error;
2864 }
2865 int OneWire_Get_tMSP(double *tmsp, one_wire_speeds spd)
2866 {
2867  int error = 0;
2868  uint8_t reg_data[2];
2869  unsigned short value;
2870 
2871  if(spd != STANDARD) //Overdrive
2872  {
2873  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tMSP, reg_data)) != 0)
2874  {
2875  return error;
2876  }
2877 
2878  if((reg_data[1] >> 7) != 0) //custom value
2879  {
2880 
2881  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2882  *tmsp = (double)value;
2883  *tmsp *= (double)62.5;
2884  *tmsp /= 1000;
2885  }
2886  else //predefined value
2887  {
2888  switch(reg_data[0])
2889  {
2890  case PRESET_0:
2891  *tmsp = tMSP_OVERDRIVE_PRESET_0;
2892  break;
2893 
2894  case PRESET_1:
2895  *tmsp = tMSP_OVERDRIVE_PRESET_1;
2896  break;
2897 
2898  case PRESET_2:
2899  *tmsp = tMSP_OVERDRIVE_PRESET_2;
2900  break;
2901 
2902  case PRESET_3:
2903  *tmsp = tMSP_OVERDRIVE_PRESET_3;
2904  break;
2905 
2906  case PRESET_4:
2907  *tmsp = tMSP_OVERDRIVE_PRESET_4;
2908  break;
2909 
2910  case PRESET_5:
2911  *tmsp = tMSP_OVERDRIVE_PRESET_5;
2912  break;
2913 
2914  case PRESET_6:
2915  *tmsp = tMSP_OVERDRIVE_PRESET_6;
2916  break;
2917 
2918  case PRESET_7:
2919  *tmsp = tMSP_OVERDRIVE_PRESET_7;
2920  break;
2921 
2922  case PRESET_8:
2923  *tmsp = tMSP_OVERDRIVE_PRESET_8;
2924  break;
2925 
2926  case PRESET_9:
2927  *tmsp = tMSP_OVERDRIVE_PRESET_9;
2928  break;
2929 
2930  case PRESET_A:
2931  *tmsp = tMSP_OVERDRIVE_PRESET_A;
2932  break;
2933 
2934  case PRESET_B:
2935  *tmsp = tMSP_OVERDRIVE_PRESET_B;
2936  break;
2937 
2938  case PRESET_C:
2939  *tmsp = tMSP_OVERDRIVE_PRESET_C;
2940  break;
2941 
2942  case PRESET_D:
2943  *tmsp = tMSP_OVERDRIVE_PRESET_D;
2944  break;
2945 
2946  case PRESET_E:
2947  *tmsp = tMSP_OVERDRIVE_PRESET_E;
2948  break;
2949 
2950  case PRESET_F:
2951  *tmsp = tMSP_OVERDRIVE_PRESET_F;
2952  break;
2953 
2954  default:
2955  *tmsp = tMSP_OVERDRIVE_PRESET_6;
2956  break;
2957  }
2958  }
2959  }
2960  else //Standard
2961  {
2962  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tMSP, reg_data)) != 0)
2963  {
2964  return error;
2965  }
2966 
2967  if((reg_data[1] >> 7) != 0) //Custom Value
2968  {
2969  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
2970  *tmsp = (double)value;
2971  *tmsp *= (double)62.5;
2972  *tmsp /= 1000;
2973  }
2974  else //Predefined value
2975  {
2976  switch(reg_data[0])
2977  {
2978  case PRESET_0:
2979  *tmsp = tMSP_STANDARD_PRESET_0;
2980  break;
2981 
2982  case PRESET_1:
2983  *tmsp = tMSP_STANDARD_PRESET_1;
2984  break;
2985 
2986  case PRESET_2:
2987  *tmsp = tMSP_STANDARD_PRESET_2;
2988  break;
2989 
2990  case PRESET_3:
2991  *tmsp = tMSP_STANDARD_PRESET_3;
2992  break;
2993 
2994  case PRESET_4:
2995  *tmsp = tMSP_STANDARD_PRESET_4;
2996  break;
2997 
2998  case PRESET_5:
2999  *tmsp = tMSP_STANDARD_PRESET_5;
3000  break;
3001 
3002  case PRESET_6:
3003  *tmsp = tMSP_STANDARD_PRESET_6;
3004  break;
3005 
3006  case PRESET_7:
3007  *tmsp = tMSP_STANDARD_PRESET_7;
3008  break;
3009 
3010  case PRESET_8:
3011  *tmsp = tMSP_STANDARD_PRESET_8;
3012  break;
3013 
3014  case PRESET_9:
3015  *tmsp = tMSP_STANDARD_PRESET_9;
3016  break;
3017 
3018  case PRESET_A:
3019  *tmsp = tMSP_STANDARD_PRESET_A;
3020  break;
3021 
3022  case PRESET_B:
3023  *tmsp = tMSP_STANDARD_PRESET_B;
3024  break;
3025 
3026  case PRESET_C:
3027  *tmsp = tMSP_STANDARD_PRESET_C;
3028  break;
3029 
3030  case PRESET_D:
3031  *tmsp = tMSP_STANDARD_PRESET_D;
3032  break;
3033 
3034  case PRESET_E:
3035  *tmsp = tMSP_STANDARD_PRESET_E;
3036  break;
3037 
3038  case PRESET_F:
3039  *tmsp = tMSP_STANDARD_PRESET_F;
3040  break;
3041 
3042  default:
3043  *tmsp = tMSP_STANDARD_PRESET_6;
3044  break;
3045  }
3046  }
3047  }
3048 
3049  return error;
3050 }
3051 
3052 int OneWire_Set_tW1L_Standard_Predefined(one_wire_timing_presets tw1l)
3053 {
3054  int error = 0;
3055  uint8_t reg_data[2];
3056 
3057  reg_data[1] = 0x00;
3058  reg_data[0] = tw1l;
3059 
3060  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tW1L, reg_data)) != 0)
3061  {
3062  return error;
3063  }
3064 
3065  return error;
3066 }
3067 int OneWire_Set_tW1L_Overdrive_Predefined(one_wire_timing_presets tw1l)
3068 {
3069  int error = 0;
3070  uint8_t reg_data[2];
3071 
3072  reg_data[1] = 0x00;
3073  reg_data[0] = tw1l;
3074 
3075  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tW1L, reg_data)) != 0)
3076  {
3077  return error;
3078  }
3079 
3080  return error;
3081 }
3082 int OneWire_Set_tW1L_Standard_Custom(double tw1l)
3083 {
3084  int error = 0;
3085  uint8_t reg_data[2];
3086 
3087  if (tw1l > (double)255.5)
3088  {
3089  return error = RB_INVALID_PARAMETER;
3090  }
3091 
3092  tw1l *= 1000; //us -> ns
3093  tw1l /= (double)62.5;
3094  tw1l = ((int)tw1l | 0x8000);
3095  reg_data[0] = (uint8_t)tw1l;
3096  reg_data[1] = ((int)tw1l >> 8);
3097 
3098  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tW1L, reg_data)) != 0)
3099  {
3100  return error;
3101  }
3102 
3103  return error;
3104 }
3105 int OneWire_Set_tW1L_Overdrive_Custom(double tw1l)
3106 {
3107  int error = 0;
3108  uint8_t reg_data[2];
3109 
3110  if (tw1l > (double)255.5)
3111  {
3112  return error = RB_INVALID_PARAMETER;
3113  }
3114 
3115  tw1l *= 1000; //us -> ns
3116  tw1l /= (double)62.5;
3117  tw1l = ((int)tw1l | 0x8000);
3118  reg_data[0] = (uint8_t)tw1l;
3119  reg_data[1] = ((int)tw1l >> 8);
3120 
3121  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tW1L, reg_data)) != 0)
3122  {
3123  return error;
3124  }
3125 
3126  return error;
3127 }
3128 int OneWire_Get_tW1L(double *tw1l, one_wire_speeds spd)
3129 {
3130  int error = 0;
3131  uint8_t reg_data[2];
3132  unsigned short value;
3133 
3134  if(spd != STANDARD) //Overdrive
3135  {
3136  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tW1L, reg_data)) != 0)
3137  {
3138  return error;
3139  }
3140 
3141  if((reg_data[1] >> 7) != 0) //custom value
3142  {
3143 
3144  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
3145  *tw1l = (double)value;
3146  *tw1l *= (double)62.5;
3147  *tw1l /= 1000;
3148  }
3149  else //predefined value
3150  {
3151  switch(reg_data[0])
3152  {
3153  case PRESET_0:
3154  *tw1l = tW1L_OVERDRIVE_PRESET_0;
3155  break;
3156 
3157  case PRESET_1:
3158  *tw1l = tW1L_OVERDRIVE_PRESET_1;
3159  break;
3160 
3161  case PRESET_2:
3162  *tw1l = tW1L_OVERDRIVE_PRESET_2;
3163  break;
3164 
3165  case PRESET_3:
3166  *tw1l = tW1L_OVERDRIVE_PRESET_3;
3167  break;
3168 
3169  case PRESET_4:
3170  *tw1l = tW1L_OVERDRIVE_PRESET_4;
3171  break;
3172 
3173  case PRESET_5:
3174  *tw1l = tW1L_OVERDRIVE_PRESET_5;
3175  break;
3176 
3177  case PRESET_6:
3178  *tw1l = tW1L_OVERDRIVE_PRESET_6;
3179  break;
3180 
3181  case PRESET_7:
3182  *tw1l = tW1L_OVERDRIVE_PRESET_7;
3183  break;
3184 
3185  case PRESET_8:
3186  *tw1l = tW1L_OVERDRIVE_PRESET_8;
3187  break;
3188 
3189  case PRESET_9:
3190  *tw1l = tW1L_OVERDRIVE_PRESET_9;
3191  break;
3192 
3193  case PRESET_A:
3194  *tw1l = tW1L_OVERDRIVE_PRESET_A;
3195  break;
3196 
3197  case PRESET_B:
3198  *tw1l = tW1L_OVERDRIVE_PRESET_B;
3199  break;
3200 
3201  case PRESET_C:
3202  *tw1l = tW1L_OVERDRIVE_PRESET_C;
3203  break;
3204 
3205  case PRESET_D:
3206  *tw1l = tW1L_OVERDRIVE_PRESET_D;
3207  break;
3208 
3209  case PRESET_E:
3210  *tw1l = tW1L_OVERDRIVE_PRESET_E;
3211  break;
3212 
3213  case PRESET_F:
3214  *tw1l = tW1L_OVERDRIVE_PRESET_F;
3215  break;
3216 
3217  default:
3218  *tw1l = tW1L_OVERDRIVE_PRESET_6;
3219  break;
3220  }
3221  }
3222  }
3223  else //Standard
3224  {
3225  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tW1L, reg_data)) != 0)
3226  {
3227  return error;
3228  }
3229 
3230  if((reg_data[1] >> 7) != 0) //Custom Value
3231  {
3232  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
3233  *tw1l = (double)value;
3234  *tw1l *= (double)62.5;
3235  *tw1l /= 1000;
3236  }
3237  else //Predefined value
3238  {
3239  switch(reg_data[0])
3240  {
3241  case PRESET_0:
3242  *tw1l = tW1L_STANDARD_PRESET_0;
3243  break;
3244 
3245  case PRESET_1:
3246  *tw1l = tW1L_STANDARD_PRESET_1;
3247  break;
3248 
3249  case PRESET_2:
3250  *tw1l = tW1L_STANDARD_PRESET_2;
3251  break;
3252 
3253  case PRESET_3:
3254  *tw1l = tW1L_STANDARD_PRESET_3;
3255  break;
3256 
3257  case PRESET_4:
3258  *tw1l = tW1L_STANDARD_PRESET_4;
3259  break;
3260 
3261  case PRESET_5:
3262  *tw1l = tW1L_STANDARD_PRESET_5;
3263  break;
3264 
3265  case PRESET_6:
3266  *tw1l = tW1L_STANDARD_PRESET_6;
3267  break;
3268 
3269  case PRESET_7:
3270  *tw1l = tW1L_STANDARD_PRESET_7;
3271  break;
3272 
3273  case PRESET_8:
3274  *tw1l = tW1L_STANDARD_PRESET_8;
3275  break;
3276 
3277  case PRESET_9:
3278  *tw1l = tW1L_STANDARD_PRESET_9;
3279  break;
3280 
3281  case PRESET_A:
3282  *tw1l = tW1L_STANDARD_PRESET_A;
3283  break;
3284 
3285  case PRESET_B:
3286  *tw1l = tW1L_STANDARD_PRESET_B;
3287  break;
3288 
3289  case PRESET_C:
3290  *tw1l = tW1L_STANDARD_PRESET_C;
3291  break;
3292 
3293  case PRESET_D:
3294  *tw1l = tW1L_STANDARD_PRESET_D;
3295  break;
3296 
3297  case PRESET_E:
3298  *tw1l = tW1L_STANDARD_PRESET_E;
3299  break;
3300 
3301  case PRESET_F:
3302  *tw1l = tW1L_STANDARD_PRESET_F;
3303  break;
3304 
3305  default:
3306  *tw1l = tW1L_STANDARD_PRESET_6;
3307  break;
3308  }
3309  }
3310  }
3311 
3312  return error;
3313 }
3314 
3315 int OneWire_Set_tMSR_Standard_Predefined(one_wire_timing_presets tmsr)
3316 {
3317  int error = 0;
3318  uint8_t reg_data[2];
3319 
3320  reg_data[1] = 0x00;
3321  reg_data[0] = tmsr;
3322 
3323  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSR, reg_data)) != 0)
3324  {
3325  return error;
3326  }
3327 
3328  return error;
3329 }
3330 int OneWire_Set_tMSR_Overdrive_Predefined(one_wire_timing_presets tmsr)
3331 {
3332  int error = 0;
3333  uint8_t reg_data[2];
3334 
3335  reg_data[1] = 0x00;
3336  reg_data[0] = tmsr;
3337 
3338  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSR, reg_data)) != 0)
3339  {
3340  return error;
3341  }
3342 
3343  return error;
3344 }
3345 int OneWire_Set_tMSR_Standard_Custom(double tmsr)
3346 {
3347  int error = 0;
3348  uint8_t reg_data[2];
3349 
3350  if (tmsr > (double)255.5)
3351  {
3352  return error = RB_INVALID_PARAMETER;
3353  }
3354 
3355  tmsr *= 1000; //us -> ns
3356  tmsr /= (double)62.5;
3357  tmsr = ((int)tmsr | 0x8000);
3358  reg_data[0] = (uint8_t)tmsr;
3359  reg_data[1] = ((int)tmsr >> 8);
3360 
3361  if((error = DS2485_WriteOneWirePortConfig(STANDARD_SPEED_tMSR, reg_data)) != 0)
3362  {
3363  return error;
3364  }
3365 
3366  return error;
3367 }
3368 int OneWire_Set_tMSR_Overdrive_Custom(double tmsr)
3369 {
3370  int error = 0;
3371  uint8_t reg_data[2];
3372 
3373  if (tmsr > (double)255.5)
3374  {
3375  return error = RB_INVALID_PARAMETER;
3376  }
3377 
3378  tmsr *= 1000; //us -> ns
3379  tmsr /= (double)62.5;
3380  tmsr = ((int)tmsr | 0x8000);
3381  reg_data[0] = (uint8_t)tmsr;
3382  reg_data[1] = ((int)tmsr >> 8);
3383 
3384  if((error = DS2485_WriteOneWirePortConfig(OVERDRIVE_SPEED_tMSR, reg_data)) != 0)
3385  {
3386  return error;
3387  }
3388 
3389  return error;
3390 }
3391 int OneWire_Get_tMSR(double *tmsr, one_wire_speeds spd)
3392 {
3393  int error = 0;
3394  uint8_t reg_data[2];
3395  unsigned short value;
3396 
3397  if(spd != STANDARD) //Overdrive
3398  {
3399  if((error = DS2485_ReadOneWirePortConfig(OVERDRIVE_SPEED_tMSR, reg_data)) != 0)
3400  {
3401  return error;
3402  }
3403 
3404  if((reg_data[1] >> 7) != 0) //custom value
3405  {
3406 
3407  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
3408  *tmsr = (double)value;
3409  *tmsr *= (double)62.5;
3410  *tmsr /= 1000;
3411  }
3412  else //predefined value
3413  {
3414  switch(reg_data[0])
3415  {
3416  case PRESET_0:
3417  *tmsr = tMSR_OVERDRIVE_PRESET_0;
3418  break;
3419 
3420  case PRESET_1:
3421  *tmsr = tMSR_OVERDRIVE_PRESET_1;
3422  break;
3423 
3424  case PRESET_2:
3425  *tmsr = tMSR_OVERDRIVE_PRESET_2;
3426  break;
3427 
3428  case PRESET_3:
3429  *tmsr = tMSR_OVERDRIVE_PRESET_3;
3430  break;
3431 
3432  case PRESET_4:
3433  *tmsr = tMSR_OVERDRIVE_PRESET_4;
3434  break;
3435 
3436  case PRESET_5:
3437  *tmsr = tMSR_OVERDRIVE_PRESET_5;
3438  break;
3439 
3440  case PRESET_6:
3441  *tmsr = tMSR_OVERDRIVE_PRESET_6;
3442  break;
3443 
3444  case PRESET_7:
3445  *tmsr = tMSR_OVERDRIVE_PRESET_7;
3446  break;
3447 
3448  case PRESET_8:
3449  *tmsr = tMSR_OVERDRIVE_PRESET_8;
3450  break;
3451 
3452  case PRESET_9:
3453  *tmsr = tMSR_OVERDRIVE_PRESET_9;
3454  break;
3455 
3456  case PRESET_A:
3457  *tmsr = tMSR_OVERDRIVE_PRESET_A;
3458  break;
3459 
3460  case PRESET_B:
3461  *tmsr = tMSR_OVERDRIVE_PRESET_B;
3462  break;
3463 
3464  case PRESET_C:
3465  *tmsr = tMSR_OVERDRIVE_PRESET_C;
3466  break;
3467 
3468  case PRESET_D:
3469  *tmsr = tMSR_OVERDRIVE_PRESET_D;
3470  break;
3471 
3472  case PRESET_E:
3473  *tmsr = tMSR_OVERDRIVE_PRESET_E;
3474  break;
3475 
3476  case PRESET_F:
3477  *tmsr = tMSR_OVERDRIVE_PRESET_F;
3478  break;
3479 
3480  default:
3481  *tmsr = tMSR_OVERDRIVE_PRESET_6;
3482  break;
3483  }
3484  }
3485  }
3486  else //Standard
3487  {
3488  if((error = DS2485_ReadOneWirePortConfig(STANDARD_SPEED_tMSR, reg_data)) != 0)
3489  {
3490  return error;
3491  }
3492 
3493  if((reg_data[1] >> 7) != 0) //Custom Value
3494  {
3495  value = ((reg_data[1] & ~(0x80)) << 8) | reg_data[0];
3496  *tmsr = (double)value;
3497  *tmsr *= (double)62.5;
3498  *tmsr /= 1000;
3499  }
3500  else //Predefined value
3501  {
3502  switch(reg_data[0])
3503  {
3504  case PRESET_0:
3505  *tmsr = tMSR_STANDARD_PRESET_0;
3506  break;
3507 
3508  case PRESET_1:
3509  *tmsr = tMSR_STANDARD_PRESET_1;
3510  break;
3511 
3512  case PRESET_2:
3513  *tmsr = tMSR_STANDARD_PRESET_2;
3514  break;
3515 
3516  case PRESET_3:
3517  *tmsr = tMSR_STANDARD_PRESET_3;
3518  break;
3519 
3520  case PRESET_4:
3521  *tmsr = tMSR_STANDARD_PRESET_4;
3522  break;
3523 
3524  case PRESET_5:
3525  *tmsr = tMSR_STANDARD_PRESET_5;
3526  break;
3527 
3528  case PRESET_6:
3529  *tmsr = tMSR_STANDARD_PRESET_6;
3530  break;
3531 
3532  case PRESET_7:
3533  *tmsr = tMSR_STANDARD_PRESET_7;
3534  break;
3535 
3536  case PRESET_8:
3537  *tmsr = tMSR_STANDARD_PRESET_8;
3538  break;
3539 
3540  case PRESET_9:
3541  *tmsr = tMSR_STANDARD_PRESET_9;
3542  break;
3543 
3544  case PRESET_A:
3545  *tmsr = tMSR_STANDARD_PRESET_A;
3546  break;
3547 
3548  case PRESET_B:
3549  *tmsr = tMSR_STANDARD_PRESET_B;
3550  break;
3551 
3552  case PRESET_C:
3553  *tmsr = tMSR_STANDARD_PRESET_C;
3554  break;
3555 
3556  case PRESET_D:
3557  *tmsr = tMSR_STANDARD_PRESET_D;
3558  break;
3559 
3560  case PRESET_E:
3561  *tmsr = tMSR_STANDARD_PRESET_E;
3562  break;
3563 
3564  case PRESET_F:
3565  *tmsr = tMSR_STANDARD_PRESET_F;
3566  break;
3567 
3568  default:
3569  *tmsr = tMSR_STANDARD_PRESET_6;
3570  break;
3571  }
3572  }
3573  }
3574 
3575  return error;
3576 }
3577 
3578 int OneWire_Init(void)
3579 {
3580  int error = 0;
3581 
3582  //Set standard speed 1-Wire timings
3583  if ((error = OneWire_Set_tRSTL_Standard_Predefined(PRESET_6)) != 0)
3584  {
3585  return error;
3586  }
3587  if ((error = OneWire_Set_tMSI_Standard_Predefined(PRESET_6)) != 0)
3588  {
3589  return error;
3590  }
3591  if ((error = OneWire_Set_tMSP_Standard_Predefined(PRESET_6)) != 0)
3592  {
3593  return error;
3594  }
3595  if ((error = OneWire_Set_tRSTH_Standard_Predefined(PRESET_6)) != 0)
3596  {
3597  return error;
3598  }
3599  if ((error = OneWire_Set_tW0L_Standard_Predefined(PRESET_6)) != 0)
3600  {
3601  return error;
3602  }
3603  if ((error = OneWire_Set_tW1L_Standard_Predefined(PRESET_6)) != 0)
3604  {
3605  return error;
3606  }
3607  if ((error = OneWire_Set_tMSR_Standard_Predefined(PRESET_6)) != 0)
3608  {
3609  return error;
3610  }
3611  if ((error = OneWire_Set_tREC_Standard_Predefined(PRESET_6)) != 0)
3612  {
3613  return error;
3614  }
3615  //Set overdrive speed 1-Wire timings
3616  if ((error = OneWire_Set_tRSTL_Overdrive_Predefined(PRESET_6)) != 0)
3617  {
3618  return error;
3619  }
3620  if ((error = OneWire_Set_tMSI_Overdrive_Predefined(PRESET_6)) != 0)
3621  {
3622  return error;
3623  }
3624  if ((error = OneWire_Set_tMSP_Overdrive_Predefined(PRESET_6)) != 0)
3625  {
3626  return error;
3627  }
3628  if ((error = OneWire_Set_tRSTH_Overdrive_Predefined(PRESET_6)) != 0)
3629  {
3630  return error;
3631  }
3632  if ((error = OneWire_Set_tW0L_Overdrive_Predefined(PRESET_6)) != 0)
3633  {
3634  return error;
3635  }
3636  if ((error = OneWire_Set_tW1L_Overdrive_Predefined(PRESET_6)) != 0)
3637  {
3638  return error;
3639  }
3640  if ((error = OneWire_Set_tMSR_Overdrive_Predefined(PRESET_6)) != 0)
3641  {
3642  return error;
3643  }
3644  if ((error = OneWire_Set_tREC_Overdrive_Predefined(PRESET_6)) != 0)
3645  {
3646  return error;
3647  }
3648 
3649  //Set 1-Wire master speed to Standard
3650  error = OneWire_Set_OneWireMasterSpeed(STANDARD);
3651  if(error) return error;
3652 
3653  //Set RPUP/BUF
3654  error = OneWire_Set_Custom_RPUP_BUF(VTH_MEDIUM, VIAPO_LOW, RWPU_1000);
3655  if(error) return error;
3656 
3657  // DRN: Add SPU (strong pull-up) which might be needed before trying reset pulse?
3658  // DS28E18 VDD_SENS (DS28E18 power to the sensor) requires 'Strong Pull-Up' 'SPU' on 1-Wire bus.
3659  // That's turned on with DS2485 1-Wire Master Configuration (Register 0) Bit 13: Strong Pullup (SPU).
3660  // But, we don't need this yet to talk to the DS28E18, as the sensor is not yet powered up.
3661  // error = OneWire_Enable_SPU(true); // DS2485 must provide strong power to 1-Wire bus
3662  // if(error) return error;
3663 
3664  //Perform a 1-Wire Reset - This is very first script executed on the one-wire bus during initialization.
3665  error = OneWire_ResetPulse();
3666  if(error) return error;
3667 
3668  return error;
3669 }
General library for the DS2485, supporting the higher-level one_wire.c/.h API.
OneWire_ROM_ID_T type stores a 1-Wire address.
int OneWire_Search(OneWire_ROM_ID_T *romid, bool search_reset, bool *last_device_found)
Definition: one_wire.c:191
General 1-Wire API using the DS2485 1-Wire master.