1-Wire and ENS210 driver stack
DS2485.h
Go to the documentation of this file.
1 /*!
2  * @file DS2485.h
3  * @brief General library for the DS2485, supports the higher-level one_wire.c/.h API.
4  */
5 
6 /*******************************************************************************
7 * Copyright (C) Maxim Integrated Products, Inc., All rights Reserved.
8 *
9 * This software is protected by copyright laws of the United States and
10 * of foreign countries. This material may also be protected by patent laws
11 * and technology transfer regulations of the United States and of foreign
12 * countries. This software is furnished under a license agreement and/or a
13 * nondisclosure agreement and may only be used or reproduced in accordance
14 * with the terms of those agreements. Dissemination of this information to
15 * any party or parties not specified in the license agreement and/or
16 * nondisclosure agreement is expressly prohibited.
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
25 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 *
29 * Except as contained in this notice, the name of Maxim Integrated
30 * Products, Inc. shall not be used except as stated in the Maxim Integrated
31 * Products, Inc. Branding Policy.
32 *
33 * The mere transfer of this software does not imply any licenses
34 * of trade secrets, proprietary technology, copyrights, patents,
35 * trademarks, maskwork rights, or any other form of intellectual
36 * property whatsoever. Maxim Integrated Products, Inc. retains all
37 * ownership rights.
38 *******************************************************************************
39 */
40 
41  /* Define to prevent redundant inclusion */
42 #ifndef DS2485_H_INCLUDED
43 #define DS2485_H_INCLUDED
44 
45 /* **** Includes **** */
46 #include <stdint.h>
47 #include <stdbool.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /* **** Definitions **** */
54 /* I2C Address */
55 #define DS2485_I2C_7BIT_ADDRESS 0x40U ///< will be shifted left one bit, then or'd with RW (R=1, W=0) bit on I2C
56 #define DS2485_I2C_CLOCKRATE 1000000U ///< 1MHz
57 
58 /* Device Function Commands */
59 #define DFC_WRITE_MEMORY 0x96;
60 #define DFC_READ_MEMORY 0x44;
61 #define DFC_READ_STATUS 0xAA;
62 #define DFC_SET_I2C_ADDRESS 0x75;
63 #define DFC_SET_PAGE_PROTECTION 0xC3;
64 #define DFC_READ_ONE_WIRE_PORT_CONFIG 0x52;
65 #define DFC_WRITE_ONE_WIRE_PORT_CONFIG 0x99;
66 #define DFC_MASTER_RESET 0x62;
67 #define DFC_ONE_WIRE_SCRIPT 0x88;
68 #define DFC_ONE_WIRE_BLOCK 0xAB;
69 #define DFC_ONE_WIRE_READ_BLOCK 0x50;
70 #define DFC_ONE_WIRE_WRITE_BLOCK 0x68;
71 #define DFC_ONE_WIRE_SEARCH 0x11;
72 #define DFC_FULL_COMMAND_SEQUENCE 0x57;
73 #define DFC_COMPUTE_CRC16 0xCC;
74 
75 /* Result Bytes */
76 #define RB_SUCCESS 0 // No Failure
77 #define RB_ALREADY_PROTECTED -100 // Command failed because the protection for the page has already been set
78 #define RB_INVALID_PARAMETER -101 // Invalid input or parameter
79 #define RB_SET_ADDRESS_FAIL -102 // Command failed because I2C address has already been set
80 #define RB_MASTER_RESET_FAIL -103 // Master reset fail
81 #define RB_COMMS_FAIL -104 // Communication failure
82 #define RB_NO_PRESENCE -105 // 1-Wire presence pulse not detected
83 #define RB_NO_MATCH_WRITES -106 // Non-matching 1-Wire writes
84 #define RB_NOT_DETECTED -107 // Device not detected in search
85 #define RB_INCORRECT_CRC -108 // CRC16 incorrect
86 #define RB_INVALID_LENGTH -109 // Invalid length of data (0)
87 #define RB_LENGTH_MISMATCH -110 // Length byte does not match actual length of data (Rx Length Byte will be 0)
88 #define RB_WRITE_PROTECTED -111 // The command failed because destination page is protected (WP)
89 #define RB_UNKNOWN -112 // Unknown error
90 
91 /* Operation Times */
92 #define tOP_USEC 40
93 #define tSEQ_USEC 20
94 #define tRM_MSEC 50
95 #define tWM_MSEC 100
96 #define tWS_MSEC 15
97 
98 /* Memory Pages*/
99 typedef enum {
100  PAGE_0,
101  PAGE_1,
102  PAGE_2,
103  PAGE_3,
104  PAGE_4,
105  PAGE_5,
106 } DS2485_memory_page_T;
107 
108 /* Device Status Outputs*/
109 typedef enum {
110  PAGE_PROTECTIONS,
111  MAN_ID,
112  DEVICE_VERSION,
113 } DS2485_status_outputs_T;
114 
115 /* Page Protections*/
116 typedef enum {
117  WRITE_PROTECTION = 0x02,
118  NONE_PROTECTION = 0x20,
119 } DS2485_page_protection_T;
120 
121 /* Write 1-Wire Port Configuration Registers */
122 typedef enum {
123  MASTER_CONFIGURATION,
124  STANDARD_SPEED_tRSTL,
125  STANDARD_SPEED_tMSI,
126  STANDARD_SPEED_tMSP,
127  STANDARD_SPEED_tRSTH,
128  STANDARD_SPEED_tW0L,
129  STANDARD_SPEED_tW1L,
130  STANDARD_SPEED_tMSR,
131  STANDARD_SPEED_tREC,
132  OVERDRIVE_SPEED_tRSTL,
133  OVERDRIVE_SPEED_tMSI,
134  OVERDRIVE_SPEED_tMSP,
135  OVERDRIVE_SPEED_tRSTH,
136  OVERDRIVE_SPEED_tW0L,
137  OVERDRIVE_SPEED_tW1L,
138  OVERDRIVE_SPEED_tMSR,
139  OVERDRIVE_SPEED_tREC,
140  RPUP_BUF,
141  PDSLEW,
142  RESERVED,
143  ALL,
144 } DS2485_configuration_register_address_T;
145 
146 /* 1-Wire Delays*/
147 typedef enum {
148  ms_0,
149  ms_2,
150  ms_4,
151  ms_6,
152  ms_8,
153  ms_10,
154  ms_12,
155  ms_14,
156  ms_16,
157  ms_18,
158  ms_20,
159  ms_22,
160  ms_24,
161  ms_26,
162  ms_28,
163  ms_30,
164  ms_32,
165  ms_34,
166  ms_36,
167  ms_38,
168  ms_40,
169  ms_42,
170  ms_44,
171  ms_46,
172  ms_48,
173  ms_50,
174  ms_52,
175  ms_54,
176  ms_56,
177  ms_58,
178  ms_60,
179  ms_62,
180  ms_64,
181  ms_66,
182  ms_68,
183  ms_70,
184  ms_72,
185  ms_74,
186  ms_76,
187  ms_78,
188  ms_80,
189  ms_82,
190  ms_84,
191  ms_86,
192  ms_88,
193  ms_90,
194  ms_92,
195  ms_94,
196  ms_96,
197  ms_98,
198  ms_100,
199  ms_102,
200  ms_104,
201  ms_106,
202  ms_108,
203  ms_110,
204  ms_112,
205  ms_114,
206  ms_116,
207  ms_118,
208  ms_120,
209  ms_122,
210  ms_124,
211  ms_126,
212  ms_128,
213  ms_130,
214  ms_132,
215  ms_134,
216  ms_136,
217  ms_138,
218  ms_140,
219  ms_142,
220  ms_144,
221  ms_146,
222  ms_148,
223  ms_150,
224  ms_152,
225  ms_154,
226  ms_156,
227  ms_158,
228  ms_160,
229  ms_162,
230  ms_164,
231  ms_166,
232  ms_168,
233  ms_170,
234  ms_172,
235  ms_174,
236  ms_176,
237  ms_178,
238  ms_180,
239  ms_182,
240  ms_184,
241  ms_186,
242  ms_188,
243  ms_190,
244  ms_192,
245  ms_194,
246  ms_196,
247  ms_198,
248  ms_200,
249  ms_202,
250  ms_204,
251  ms_206,
252  ms_208,
253  ms_210,
254  ms_212,
255  ms_214,
256  ms_216,
257  ms_218,
258  ms_220,
259  ms_222,
260  ms_224,
261  ms_226,
262  ms_228,
263  ms_230,
264  ms_232,
265  ms_234,
266  ms_236,
267  ms_238,
268  ms_240,
269  ms_242,
270  ms_244,
271  ms_246,
272  ms_248,
273  ms_250,
274  ms_252,
275  ms_254,
276  ms_256,
277  ms_258,
278  ms_260,
279  ms_262,
280  ms_264,
281  ms_266,
282  ms_268,
283  ms_270,
284  ms_272,
285  ms_274,
286  ms_276,
287  ms_278,
288  ms_280,
289  ms_282,
290  ms_284,
291  ms_286,
292  ms_288,
293  ms_290,
294  ms_292,
295  ms_294,
296  ms_296,
297  ms_298,
298  ms_300,
299  ms_302,
300  ms_304,
301  ms_306,
302  ms_308,
303  ms_310,
304  ms_312,
305  ms_314,
306  ms_316,
307  ms_318,
308  ms_320,
309  ms_322,
310  ms_324,
311  ms_326,
312  ms_328,
313  ms_330,
314  ms_332,
315  ms_334,
316  ms_336,
317  ms_338,
318  ms_340,
319  ms_342,
320  ms_344,
321  ms_346,
322  ms_348,
323  ms_350,
324  ms_352,
325  ms_354,
326  ms_356,
327  ms_358,
328  ms_360,
329  ms_362,
330  ms_364,
331  ms_366,
332  ms_368,
333  ms_370,
334  ms_372,
335  ms_374,
336  ms_376,
337  ms_378,
338  ms_380,
339  ms_382,
340  ms_384,
341  ms_386,
342  ms_388,
343  ms_390,
344  ms_392,
345  ms_394,
346  ms_396,
347  ms_398,
348  ms_400,
349  ms_402,
350  ms_404,
351  ms_406,
352  ms_408,
353  ms_410,
354  ms_412,
355  ms_414,
356  ms_416,
357  ms_418,
358  ms_420,
359  ms_422,
360  ms_424,
361  ms_426,
362  ms_428,
363  ms_430,
364  ms_432,
365  ms_434,
366  ms_436,
367  ms_438,
368  ms_440,
369  ms_442,
370  ms_444,
371  ms_446,
372  ms_448,
373  ms_450,
374  ms_452,
375  ms_454,
376  ms_456,
377  ms_458,
378  ms_460,
379  ms_462,
380  ms_464,
381  ms_466,
382  ms_468,
383  ms_470,
384  ms_472,
385  ms_474,
386  ms_476,
387  ms_478,
388  ms_480,
389  ms_482,
390  ms_484,
391  ms_486,
392  ms_488,
393  ms_490,
394  ms_492,
395  ms_494,
396  ms_496,
397  ms_498,
398  ms_500,
399  ms_502,
400  ms_504,
401  ms_506,
402  ms_508,
403  ms_510,
404 } DS2485_full_command_sequence_delays_msecs_T;
405 
406 /* Device Function Commands */
407 int DS2485_WriteMemory(DS2485_memory_page_T pgNumber, const uint8_t *pgData);
408 int DS2485_ReadMemory(DS2485_memory_page_T pgNumber, uint8_t *pgData);
409 int DS2485_ReadStatus(DS2485_status_outputs_T output, uint8_t *status);
410 int DS2485_SetI2cAddress(uint8_t newAddress);
411 int DS2485_SetPageProtection(DS2485_memory_page_T pgNumber, DS2485_page_protection_T protection);
412 int DS2485_ReadOneWirePortConfig(DS2485_configuration_register_address_T reg, uint8_t *regData);
413 int DS2485_WriteOneWirePortConfig(DS2485_configuration_register_address_T reg, const uint8_t *regData);
414 int DS2485_MasterReset(void);
415 int DS2485_OneWireScript(const uint8_t *script, uint8_t script_length, double accumulativeOneWireTime, uint8_t commandsCount, uint8_t *scriptResponse, uint8_t scriptResponse_length);
416 int DS2485_OneWireBlock(const uint8_t *blockData, int blockData_Length, uint8_t *ow_data, bool ow_reset, bool ignore, bool spu, bool pe); // blockData should include read bytes as FFh
417 int DS2485_OneWireWriteBlock(const uint8_t *writeData, int writeData_Length, bool ow_reset, bool ignore, bool spu);
418 int DS2485_OneWireReadBlock(uint8_t *readData, uint8_t bytes);
419 int DS2485_OneWireSearch(uint8_t *romId, uint8_t code, bool ow_reset, bool ignore, bool search_rst, bool *flag);
420 int DS2485_FullCommandSequence(const uint8_t *owData, int owData_Length, uint8_t *rom_id, DS2485_full_command_sequence_delays_msecs_T ow_delay_msecs, uint8_t *ow_rslt_data, uint8_t ow_rslt_len);
421 int DS2485_ComputeCrc16(const uint8_t *crcData, int crcData_Length, uint8_t *crc16);
422 
423 /* Platform-specific I2C command interface implemented in DS2485_port_xxxx.c */
424 int DS2485_ExecuteCommand(const uint8_t *packet, int packetSize, int delay_uSec, uint8_t *response, int responseSize);
425 
426 #ifdef __cplusplus
427 }
428 #endif
429 
430 #endif /* DS2485_H_INCLUDED */
int DS2485_ExecuteCommand(const uint8_t *packet, int packetSize, int delay_uSec, uint8_t *response, int responseSize)
Definition: DS2485_port_maxim.c:63