1-Wire and ENS210 driver stack
DS2485.c
Go to the documentation of this file.
1 /**
2  * @file DS2485.c
3  * @brief General library for the DS2485, supporting the higher-level one_wire.c/.h API.
4  *
5  * @par DS2485 functions return error
6  * 0 means no error occurred. Usually an error is just 1, but sometimes a
7  * lower-level error is propagated upward.
8  *
9  * @par Update history
10  * - 18-May-2023 Dave Nadler Modified for platform independence.
11  */
12 
13 /*******************************************************************************
14 * Copyright (C) Maxim Integrated Products, Inc., All rights Reserved.
15 *
16 * This software is protected by copyright laws of the United States and
17 * of foreign countries. This material may also be protected by patent laws
18 * and technology transfer regulations of the United States and of foreign
19 * countries. This software is furnished under a license agreement and/or a
20 * nondisclosure agreement and may only be used or reproduced in accordance
21 * with the terms of those agreements. Dissemination of this information to
22 * any party or parties not specified in the license agreement and/or
23 * nondisclosure agreement is expressly prohibited.
24 *
25 * The above copyright notice and this permission notice shall be included
26 * in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
32 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 *
36 * Except as contained in this notice, the name of Maxim Integrated
37 * Products, Inc. shall not be used except as stated in the Maxim Integrated
38 * Products, Inc. Branding Policy.
39 *
40 * The mere transfer of this software does not imply any licenses
41 * of trade secrets, proprietary technology, copyrights, patents,
42 * trademarks, maskwork rights, or any other form of intellectual
43 * property whatsoever. Maxim Integrated Products, Inc. retains all
44 * ownership rights.
45 *******************************************************************************
46 */
47 
48 /* **** Includes **** */
49 #include <stdint.h>
50 #include <string.h> // memcpy
51 
52 #include "DS2485.h"
53 
54 #include "one_wire.h" // one_wire_speeds...
55 
56 /* **** Device Function Commands **** */
57 int DS2485_WriteMemory(DS2485_memory_page_T pgNumber, const uint8_t *pgData)
58 {
59  int error = 0;
60 
61  //Command specific variables
62  const int txLength = 35;
63  const int delay_msec = tWM_MSEC;
64  const int rxLength = 2;
65 
66  uint8_t packet[txLength];
67  uint8_t response[rxLength];
68 
69  //Build command packet
70  packet[0] = DFC_WRITE_MEMORY; // Command
71  packet[1] = sizeof(packet) - 2; // Command length byte
72  packet[2] = pgNumber; // Parameter
73  memcpy(&packet[3], &pgData[0], 32); // Data
74 
75  //Execute Command
76  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_msec*1000, response, sizeof(response))) != 0)
77  {
78  return error;
79  }
80 
81  switch (response[1]) {
82  case 0xAA:
83  error = RB_SUCCESS;
84  break;
85 
86  case 0x55:
87  error = RB_WRITE_PROTECTED;
88  break;
89 
90  case 0x77:
91  error = RB_INVALID_PARAMETER;
92  break;
93 
94  default:
95  error = RB_UNKNOWN;
96  break;
97  }
98 
99  return error;
100 }
101 
102 int DS2485_ReadMemory(DS2485_memory_page_T pgNumber, uint8_t *pgData)
103 {
104  int error = 0;
105 
106  //Command specific variables
107  const int txLength = 3;
108  const int delay_msec = tRM_MSEC;
109  const int rxLength = 34;
110 
111  uint8_t packet[txLength];
112  uint8_t response[rxLength];
113 
114  //Build command packet
115  packet[0] = DFC_READ_MEMORY; // Command
116  packet[1] = sizeof(packet) - 2; // Command length byte
117  packet[2] = pgNumber; // Parameter
118 
119  //Execute Command
120  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_msec*1000, response, sizeof(response))) != 0)
121  {
122  return error;
123  }
124 
125  //Fetch page data from response
126  memcpy(&pgData[0], &response[2], sizeof(response) - 2);
127 
128  switch (response[1]) {
129  case 0xAA:
130  error = RB_SUCCESS;
131  break;
132 
133  case 0x77:
134  error = RB_INVALID_PARAMETER;
135  break;
136 
137  default:
138  error = RB_UNKNOWN;
139  break;
140  }
141 
142  return error;
143 }
144 
145 int DS2485_ReadStatus(DS2485_status_outputs_T output, uint8_t *status)
146 {
147  int error = 0;
148 
149  //Command specific variables
150  const int txLength = 3;
151  const int delay_msec = tRM_MSEC;
152  int rxLength;
153 
154  switch (output){
155  case PAGE_PROTECTIONS:
156  rxLength = 8;
157  break;
158  default:
159  rxLength = 4;
160  break;
161  }
162 
163  uint8_t packet[txLength];
164  uint8_t response[rxLength];
165 
166  //Build command packet
167  packet[0] = DFC_READ_STATUS; // Command
168  packet[1] = sizeof(packet) - 2; // Command length byte
169  packet[2] = output; // Parameter
170 
171  //Execute Command
172  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_msec*1000, response, sizeof(response))) != 0)
173  {
174  return error;
175  }
176 
177  //Fetch status data from response
178  memcpy(&status[0], &response[2], sizeof(response) - 2);
179 
180  switch (response[1]) {
181  case 0xAA:
182  error = RB_SUCCESS;
183  break;
184 
185  case 0x77:
186  error = RB_INVALID_PARAMETER;
187  break;
188 
189  default:
190  error = RB_UNKNOWN;
191  break;
192  }
193 
194  return error;
195 }
196 
197 int DS2485_SetI2cAddress(uint8_t newAddress)
198 {
199  int error = 0;
200 
201  //Command specific variables
202  const int txLength = 3;
203  const int delay_msec = tWS_MSEC;
204  const int rxLength = 2;
205 
206  uint8_t packet[txLength];
207  uint8_t response[rxLength];
208 
209  //Build command packet
210  packet[0] = DFC_SET_I2C_ADDRESS; // Command
211  packet[1] = sizeof(packet) - 2; // Command length byte
212  packet[2] = newAddress << 1; // Parameter
213 
214  //Execute Command
215  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_msec*1000, response, sizeof(response))) != 0)
216  {
217  return error;
218  }
219 
220  switch (response[1]) {
221  case 0xAA:
222  error = RB_SUCCESS;
223  break;
224 
225  case 0x55:
226  error = RB_SET_ADDRESS_FAIL;
227  break;
228 
229  default:
230  error = RB_UNKNOWN;
231  break;
232  }
233 
234  return error;
235 }
236 
237 int DS2485_SetPageProtection(DS2485_memory_page_T pgNumber, DS2485_page_protection_T protection)
238 {
239  int error = 0;
240 
241  //Command specific variables
242  const int txLength = 4;
243  const int delay_msec = tWS_MSEC;
244  const int rxLength = 2;
245 
246  uint8_t packet[txLength];
247  uint8_t response[rxLength];
248 
249  //Build command packet
250  packet[0] = DFC_SET_PAGE_PROTECTION; // Command
251  packet[1] = sizeof(packet) - 2; // Command length byte
252  packet[2] = pgNumber; // Parameter
253  packet[3] = protection; // Parameter
254 
255  //Execute Command
256  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_msec*1000, response, sizeof(response))) != 0)
257  {
258  return error;
259  }
260 
261  switch (response[1]) {
262  case 0xAA:
263  error = RB_SUCCESS;
264  break;
265 
266  case 0x55:
267  error = RB_ALREADY_PROTECTED;
268  break;
269 
270  case 0x77:
271  error = RB_INVALID_PARAMETER;
272  break;
273 
274  default:
275  error = RB_UNKNOWN;
276  break;
277  }
278 
279  return error;
280 }
281 
282 int DS2485_ReadOneWirePortConfig(DS2485_configuration_register_address_T reg, uint8_t *regData)
283 {
284  int error = 0;
285 
286  //Command specific variables
287  const int txLength = 3;
288  const int delay_usec = tOP_USEC;
289  int rxLength;
290 
291  if (reg > 0x13)
292  {
293  rxLength = 42;
294  }
295  else
296  {
297  rxLength = 4;
298  }
299 
300  uint8_t packet[txLength];
301  uint8_t response[rxLength];
302 
303  //Build command packet
304  packet[0] = DFC_READ_ONE_WIRE_PORT_CONFIG; // Command
305  packet[1] = sizeof(packet) - 2; // Command length byte
306  packet[2] = reg; // Parameter
307 
308  //Execute Command
309  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
310  {
311  return error;
312  }
313 
314  //Fetch status data from response
315  memcpy(&regData[0], &response[2], sizeof(response) - 2);
316 
317  switch (response[1]) {
318  case 0xAA:
319  error = RB_SUCCESS;
320  break;
321 
322  default:
323  error = RB_UNKNOWN;
324  break;
325  }
326 
327  return error;
328 }
329 
330 int DS2485_WriteOneWirePortConfig(DS2485_configuration_register_address_T reg, const uint8_t *regData)
331 {
332  int error = 0;
333 
334  //Command specific variables
335  const int txLength = 5;
336  const int delay_usec = tOP_USEC + 1000;
337  const int rxLength = 2;
338 
339  uint8_t packet[txLength];
340  uint8_t response[rxLength];
341 
342  //Build command packet
343  packet[0] = DFC_WRITE_ONE_WIRE_PORT_CONFIG; // Command
344  packet[1] = sizeof(packet) - 2; // Command length byte
345  packet[2] = reg; // Parameter
346  packet[3] = regData[0]; // Data
347  packet[4] = regData[1]; // Data
348 
349 
350  //Execute Command
351  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
352  {
353  return error;
354  }
355 
356  switch (response[1]) {
357  case 0xAA:
358  error = RB_SUCCESS;
359  break;
360 
361  case 0x77:
362  error = RB_INVALID_PARAMETER;
363  break;
364 
365  default:
366  error = RB_UNKNOWN;
367  break;
368  }
369 
370  return error;
371 }
372 
373 int DS2485_MasterReset(void)
374 {
375  int error = 0;
376 
377  //Command specific variables
378  const int txLength = 1;
379  const int delay_usec = tOP_USEC;
380  const int rxLength = 2;
381 
382  uint8_t packet[txLength];
383  uint8_t response[rxLength];
384 
385  //Build command packet
386  packet[0] = DFC_MASTER_RESET; // Command
387 
388  //Execute Command
389  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
390  {
391  return error;
392  }
393 
394  switch (response[1]) {
395  case 0xAA:
396  error = RB_SUCCESS;
397  break;
398 
399  case 0x22:
400  error = RB_MASTER_RESET_FAIL;
401  break;
402 
403  default:
404  error = RB_UNKNOWN;
405  break;
406  }
407 
408  return error;
409 }
410 
411 int DS2485_OneWireScript(const uint8_t *script, uint8_t script_length, double accumulativeOneWireTime, uint8_t commandsCount, uint8_t *scriptResponse, uint8_t scriptResponse_length)
412 {
413  int error = 0;
414 
415  //Command specific variables
416  const int txLength = script_length + 2;
417  const int delay_usec = tOP_USEC + (tSEQ_USEC*(commandsCount)) + accumulativeOneWireTime + 1000;
418  const int rxLength = scriptResponse_length + 2;
419 
420  uint8_t packet[txLength];
421  uint8_t response[rxLength];
422 
423  //Build command packet
424  packet[0] = DFC_ONE_WIRE_SCRIPT; // Command
425  packet[1] = sizeof(packet) - 2; // Command length byte
426  memcpy(&packet[2], &script[0], script_length); // Primitive commands + data + parameters = script
427 
428  //Execute Command
429  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
430  {
431  return error;
432  }
433 
434  //Fetch page CRC16 from response
435  memcpy(&scriptResponse[0], &response[2], sizeof(response) - 2);
436 
437  switch (response[1]) {
438  case 0xAA:
439  error = RB_SUCCESS;
440  break;
441 
442  case 0x77:
443  error = RB_INVALID_PARAMETER;
444  break;
445 
446  case 0x22:
447  error = RB_COMMS_FAIL;
448  break;
449 
450  default:
451  error = RB_UNKNOWN;
452  break;
453  }
454 
455  return error;
456 }
457 
458 int DS2485_OneWireBlock(const uint8_t *blockData, int blockData_Length, uint8_t *ow_data, bool ow_reset, bool ignore, bool spu, bool pe)
459 {
460  int error = 0;
461  one_wire_speeds master_speed;
462 
463  // Delay variables
464  double one_wire_time;
465  double ow_rst_time;
466  double t_slot;
467 
468  // Timings
469  double t_rstl;
470  double t_rsth;
471  double t_w0l;
472  double t_rec;
473 
474  /***** Fetch timings *****/
475  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
476  {
477  return error;
478  }
479 
480  if(master_speed != STANDARD)
481  {
482  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
483  {
484  return error;
485  }
486  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
487  {
488  return error;
489  }
490  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
491  {
492  return error;
493  }
494  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
495  {
496  return error;
497  }
498  }
499  else
500  {
501  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
502  {
503  return error;
504  }
505  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
506  {
507  return error;
508  }
509  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
510  {
511  return error;
512  }
513  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
514  {
515  return error;
516  }
517  }
518 
519  //'1-Wire time'
520  t_slot = t_w0l + t_rec; //Time it takes to complete a 1-Wire Write/Read bit time slot
521  ow_rst_time = t_rstl + t_rsth; //Time it takes to complete a 1-Wire Reset slot
522  one_wire_time = ((t_slot * 8) * (blockData_Length));
523  if(ow_reset)
524  {
525  one_wire_time += ow_rst_time;
526  }
527 
528  //Command specific variables
529  const int txLength = blockData_Length + 3;
530  const int delay_usec = tOP_USEC + (tSEQ_USEC*(blockData_Length + ow_reset)) + one_wire_time;
531  const int rxLength = blockData_Length + 2;
532 
533  uint8_t packet[txLength];
534  uint8_t response[rxLength];
535 
536  //Build command packet
537  packet[0] = DFC_ONE_WIRE_BLOCK; // Command
538  packet[1] = sizeof(packet) - 2; // Command length byte
539  packet[2] = (pe << 3) | (spu << 2) | (ignore << 1) | (ow_reset << 0); // Parameter byte
540  memcpy(&packet[3], &blockData[0], blockData_Length); // Data
541 
542  //Execute Command
543  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
544  {
545  return error;
546  }
547 
548  //Fetch page CRC16 from response
549  memcpy(&ow_data[0], &response[2], sizeof(response) - 2);
550 
551  switch (response[1]) {
552  case 0xAA:
553  error = RB_SUCCESS;
554  break;
555 
556  case 0x77:
557  error = RB_INVALID_PARAMETER;
558  break;
559 
560  case 0x22:
561  error = RB_COMMS_FAIL;
562  break;
563 
564  case 0x33:
565  error = RB_NOT_DETECTED;
566  break;
567 
568  default:
569  error = RB_UNKNOWN;
570  break;
571  }
572 
573  return error;
574 }
575 
576 int DS2485_OneWireWriteBlock(const uint8_t *writeData, int writeData_Length, bool ow_reset, bool ignore, bool spu)
577 {
578  int error = 0;
579  one_wire_speeds master_speed;
580 
581  // Delay variables
582  double one_wire_time;
583  double ow_rst_time;
584  double t_slot;
585 
586  // Timings
587  double t_rstl;
588  double t_rsth;
589  double t_w0l;
590  double t_rec;
591 
592  /***** Fetch timings *****/
593  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
594  {
595  return error;
596  }
597 
598  if(master_speed != STANDARD)
599  {
600  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
601  {
602  return error;
603  }
604  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
605  {
606  return error;
607  }
608  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
609  {
610  return error;
611  }
612  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
613  {
614  return error;
615  }
616  }
617  else
618  {
619  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
620  {
621  return error;
622  }
623  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
624  {
625  return error;
626  }
627  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
628  {
629  return error;
630  }
631  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
632  {
633  return error;
634  }
635  }
636 
637  //'1-Wire time'
638  t_slot = t_w0l + t_rec; //Time it takes to complete a 1-Wire Write/Read bit time slot
639  ow_rst_time = t_rstl + t_rsth; //Time it takes to complete a 1-Wire Reset slot
640  one_wire_time = ((t_slot * 8) * (writeData_Length));
641  if(ow_reset)
642  {
643  one_wire_time += ow_rst_time;
644  }
645 
646  //Command specific variables
647  const int txLength = writeData_Length + 3;
648  const int delay_usec = tOP_USEC + (tSEQ_USEC*(writeData_Length + ow_reset)) + one_wire_time;
649  const int rxLength = 2;
650 
651  uint8_t packet[txLength];
652  uint8_t response[rxLength];
653 
654  //Build command packet
655  packet[0] = DFC_ONE_WIRE_WRITE_BLOCK; // Command
656  packet[1] = sizeof(packet) - 2; // Command length byte
657  packet[2] = (spu << 2) | (ignore << 1) | (ow_reset << 0); // Parameter byte
658  memcpy(&packet[3], &writeData[0], writeData_Length); // Data
659 
660  //Execute Command
661  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
662  {
663  return error;
664  }
665 
666  switch (response[1]) {
667  case 0xAA:
668  error = RB_SUCCESS;
669  break;
670 
671  case 0x22:
672  error = RB_COMMS_FAIL;
673  break;
674 
675  case 0x33:
676  error = RB_NO_PRESENCE;
677  break;
678 
679  case 0x00:
680  error = RB_NO_MATCH_WRITES;
681  break;
682 
683  case 0x77:
684  error = RB_INVALID_PARAMETER;
685  break;
686 
687  default:
688  error = RB_UNKNOWN;
689  break;
690  }
691 
692  return error;
693 }
694 
695 int DS2485_OneWireReadBlock(uint8_t *readData, uint8_t bytes)
696 {
697  int error = 0;
698  one_wire_speeds master_speed;
699 
700  // Delay variables
701  double one_wire_time;
702  double t_slot;
703 
704  // Timings
705  double t_rstl;
706  double t_rsth;
707  double t_w0l;
708  double t_rec;
709 
710  /***** Fetch timings *****/
711  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
712  {
713  return error;
714  }
715 
716  if(master_speed != STANDARD)
717  {
718  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
719  {
720  return error;
721  }
722  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
723  {
724  return error;
725  }
726  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
727  {
728  return error;
729  }
730  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
731  {
732  return error;
733  }
734  }
735  else
736  {
737  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
738  {
739  return error;
740  }
741  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
742  {
743  return error;
744  }
745  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
746  {
747  return error;
748  }
749  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
750  {
751  return error;
752  }
753  }
754 
755  //'1-Wire time'
756  t_slot = t_w0l + t_rec; //Time it takes to complete a 1-Wire Write/Read bit time slot
757  one_wire_time = ((t_slot * 8) * (bytes));
758 
759 
760  //Command specific variables
761  const int txLength = 3;
762  const int delay_usec = tOP_USEC + (tSEQ_USEC*(bytes)) + one_wire_time;
763  const int rxLength = bytes + 2;
764 
765  uint8_t packet[txLength];
766  uint8_t response[rxLength];
767 
768  //Build command packet
769  packet[0] = DFC_ONE_WIRE_READ_BLOCK; // Command
770  packet[1] = sizeof(packet) - 2; // Command length byte
771  packet[2] = bytes; // Parameter Byte
772 
773  //Execute Command
774  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
775  {
776  return error;
777  }
778 
779  //Fetch read data from response
780  memcpy(&readData[0], &response[2], sizeof(response) - 2);
781 
782  switch (response[1]) {
783  case 0xAA:
784  error = RB_SUCCESS;
785  break;
786 
787  case 0x22:
788  error = RB_COMMS_FAIL;
789  break;
790 
791  case 0x77:
792  error = RB_INVALID_LENGTH;
793  break;
794 
795  default:
796  error = RB_UNKNOWN;
797  break;
798  }
799 
800  return error;
801 }
802 
803 int DS2485_OneWireSearch(uint8_t *romId, uint8_t code, bool ow_reset, bool ignore, bool search_rst, bool *flag)
804 {
805  int error = 0;
806  one_wire_speeds master_speed;
807 
808  // Delay variables
809  double one_wire_time;
810  double ow_rst_time;
811  double t_slot;
812 
813  // Timings
814  double t_rstl;
815  double t_rsth;
816  double t_w0l;
817  double t_rec;
818 
819  /***** Fetch timings *****/
820  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
821  {
822  return error;
823  }
824 
825  if(master_speed != STANDARD)
826  {
827  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
828  {
829  return error;
830  }
831  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
832  {
833  return error;
834  }
835  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
836  {
837  return error;
838  }
839  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
840  {
841  return error;
842  }
843  }
844  else
845  {
846  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
847  {
848  return error;
849  }
850  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
851  {
852  return error;
853  }
854  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
855  {
856  return error;
857  }
858  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
859  {
860  return error;
861  }
862  }
863 
864  //'1-Wire time'
865  t_slot = t_w0l + t_rec; //Time it takes to complete a 1-Wire Write/Read bit time slot
866  ow_rst_time = t_rstl + t_rsth; //Time it takes to complete a 1-Wire Reset slot
867  one_wire_time = ((t_slot * 8) * (64));
868  if(ow_reset)
869  {
870  one_wire_time += ow_rst_time;
871  }
872 
873  //Command specific variables
874  const int txLength = 4;
875  const int delay_usec = tOP_USEC + (tSEQ_USEC*(64 + ow_reset)) + one_wire_time;
876  const int rxLength = 11;
877 
878  uint8_t packet[txLength];
879  uint8_t response[rxLength];
880 
881  // Search command (from datasheet):
882  // Table 60. 1-Wire Search Parameter Byte
883  // Bit 0: 1-Wire Reset (OW_RST).
884  // (1b) transmit a 1-Wire reset before the block and verify presence pulse—if presence is not detected, and IGNORE = 0b then stop operation;
885  // (0b) no 1-Wire reset.
886  // Bit 1: Ignore Presence Pulse (IGNORE).
887  // (1b) ignore the presence pulse result from OW_RST
888  // (0b) do not ignore presence on optional 1-Wire reset.
889  // Bit 2: Search Reset (SEARCH_RST).
890  // (1b) reset the search state to find the "first" device ROMID
891  // (0b) do not reset the search state, find the "next" device ROMID.
892 
893  //Build command packet
894  packet[0] = DFC_ONE_WIRE_SEARCH; // Command
895  packet[1] = sizeof(packet) - 2; // Command length byte
896  packet[2] = (search_rst << 2) | (ignore << 1) | (ow_reset << 0); // Parameter byte
897  packet[3] = code; // Search command code
898 
899  //Execute Command
900  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
901  {
902  return error;
903  }
904 
905  //Fetch ROM ID + last device flag from response
906  memcpy(&romId[0], &response[2], 8);
907  *flag = response[10];
908 
909  switch (response[1]) {
910  case 0xAA:
911  error = RB_SUCCESS;
912  break;
913 
914  case 0x00:
915  error = RB_NOT_DETECTED;
916  break;
917 
918  case 0x33:
919  error = RB_NO_PRESENCE;
920  break;
921 
922  case 0x77:
923  error = RB_INVALID_PARAMETER;
924  break;
925 
926  default:
927  error = RB_UNKNOWN;
928  break;
929  }
930 
931  return error;
932 }
933 
934 int DS2485_FullCommandSequence(const uint8_t *owData, int owData_Length, uint8_t *rom_id, DS2485_full_command_sequence_delays_msecs_T ow_delay_msec, uint8_t *ow_rslt_data, uint8_t ow_rslt_len)
935 {
936  int error = 0;
937  one_wire_speeds master_speed;
938 
939  // Delay variables
940  double one_wire_time;
941  double ow_rst_time;
942  double t_slot;
943 
944  // Timings
945  double t_rstl;
946  double t_rsth;
947  double t_w0l;
948  double t_rec;
949 
950  /***** Fetch timings *****/
951  if ((error = OneWire_Get_OneWireMasterSpeed(&master_speed)) != 0)
952  {
953  return error;
954  }
955 
956  if(master_speed != STANDARD)
957  {
958  if ((error = OneWire_Get_tRSTL(&t_rstl, OVERDRIVE)) != 0)
959  {
960  return error;
961  }
962  if ((error = OneWire_Get_tRSTH(&t_rsth, OVERDRIVE)) != 0)
963  {
964  return error;
965  }
966  if ((error = OneWire_Get_tW0L(&t_w0l, OVERDRIVE)) != 0)
967  {
968  return error;
969  }
970  if ((error = OneWire_Get_tREC(&t_rec, OVERDRIVE)) != 0)
971  {
972  return error;
973  }
974  }
975  else
976  {
977  if ((error = OneWire_Get_tRSTL(&t_rstl, STANDARD)) != 0)
978  {
979  return error;
980  }
981  if ((error = OneWire_Get_tRSTH(&t_rsth, STANDARD)) != 0)
982  {
983  return error;
984  }
985  if ((error = OneWire_Get_tW0L(&t_w0l, STANDARD)) != 0)
986  {
987  return error;
988  }
989  if ((error = OneWire_Get_tREC(&t_rec, STANDARD)) != 0)
990  {
991  return error;
992  }
993  }
994 
995  //'1-Wire time'
996  t_slot = t_w0l + t_rec; //Time it takes to complete a 1-Wire Write/Read bit time slot
997  ow_rst_time = t_rstl + t_rsth; //Time it takes to complete a 1-Wire Reset slot
998  one_wire_time = ow_rst_time + ((t_slot * 8) * (18 + owData_Length + ow_rslt_len));
999 
1000  //Command specific variables
1001  const int txLength = owData_Length + 11;
1002  const int delay_usec = tOP_USEC + (tSEQ_USEC*(19 + owData_Length + ow_rslt_len)) + one_wire_time + (ow_delay_msec * 2000);
1003  const int rxLength = ow_rslt_len + 3;
1004 
1005  uint8_t packet[txLength];
1006  uint8_t response[rxLength];
1007 
1008  //Build command packet
1009  packet[0] = DFC_FULL_COMMAND_SEQUENCE; // Command
1010  packet[1] = sizeof(packet) - 2; // Command length byte
1011  packet[2] = ow_delay_msec; // Parameter byte
1012  memcpy(&packet[3], &rom_id[0], 8); // ROM ID for Match ROM
1013  memcpy(&packet[11], &owData[0], owData_Length); // 1-Wire Data
1014 
1015  //Execute Command
1016  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
1017  {
1018  return error;
1019  }
1020 
1021  //Fetch OW_RSLT_DATA from response
1022  memcpy(&ow_rslt_data[0], &response[3], sizeof(response) - 3);
1023 
1024  switch (response[1]) {
1025  case 0xAA:
1026  error = RB_SUCCESS;
1027  break;
1028 
1029  case 0x00:
1030  error = RB_INCORRECT_CRC;
1031  break;
1032 
1033  default:
1034  error = RB_UNKNOWN;
1035  break;
1036  }
1037 
1038  return error;
1039 }
1040 
1041 int DS2485_ComputeCrc16(const uint8_t *crcData, int crcData_Length, uint8_t *crc16)
1042 {
1043  int error = 0;
1044 
1045  //Command specific variables
1046  const int txLength = crcData_Length + 2;
1047  const int delay_usec = tOP_USEC;
1048  const int rxLength = 4;
1049 
1050  uint8_t packet[txLength];
1051  uint8_t response[rxLength];
1052 
1053  //Build command packet
1054  packet[0] = DFC_COMPUTE_CRC16; // Command
1055  packet[1] = sizeof(packet) - 2; // Command length byte
1056  memcpy(&packet[2], &crcData[0], crcData_Length); // Data
1057 
1058  //Execute Command
1059  if ((error = DS2485_ExecuteCommand(packet, sizeof(packet), delay_usec, response, sizeof(response))) != 0)
1060  {
1061  return error;
1062  }
1063 
1064  //Fetch CRC16 from response
1065  memcpy(&crc16[0], &response[2], sizeof(response) - 2);
1066 
1067  switch (response[1]) {
1068  case 0xAA:
1069  error = RB_SUCCESS;
1070  break;
1071 
1072  case 0x77:
1073  error = RB_INVALID_LENGTH;
1074  break;
1075 
1076  case 0xFF:
1077  error = RB_LENGTH_MISMATCH;
1078  break;
1079 
1080  default:
1081  error = RB_UNKNOWN;
1082  break;
1083  }
1084 
1085  return error;
1086 }
int DS2485_ExecuteCommand(const uint8_t *packet, int packetSize, int delay_uSec, uint8_t *response, int responseSize)
Platform-specific I2C command interface implemented in DS2485_port_xxxx.c Returns &#39;error&#39; (0 if compl...
General library for the DS2485, supporting the higher-level one_wire.c/.h API.
General 1-Wire API using the DS2485 1-Wire master.