aboutsummaryrefslogtreecommitdiff
path: root/lib/onewire_master.h
blob: ca218c704c0ecdb108a8519adf6d48cf3bb8ba79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/** library for 1-wire protocol as master
 *  @file
 *  @author King Kévin <kingkevin@cuvoodoo.info>
 *  @date 2017-2018
 *  @note peripherals used: timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio
 *  @note overdrive mode is not provided
 */
#pragma once

/** @defgroup onewire_master_gpio GPIO used for 1-wire signal
 *  @note external pull-up resistor on pin is required (< 5 kOhm)
 *  @{
 */
#define ONEWIRE_MASTER_PORT C /**< GPIO port */
#define ONEWIRE_MASTER_PIN 9  /**< GPIO pin */
/** @} */

/** setup 1-wire peripheral
 */
void onewire_master_setup(void);
/** release 1-wire peripheral
 */
void onewire_master_release(void);
/** send reset pulse
 *  @return if slaves have indicated their presence
 */
bool onewire_master_reset(void);
/** compute CRC for 1-Wire
 *  @note this CRC-8 uses normal polynomial 0x31, reverse polynomial 0x8C, start value 0x00
 *  @param[in] data bytes on which to calculate CRC checksum on
 *  @param[in] length number of bytes in data
 *  @return computed CRC checksum
 */
uint8_t onewire_master_crc(uint8_t* data, uint32_t length);
/** send READ ROM command and read ROM code response
 *  @note user needs to send reset pulse before
 *  @return ROM code read
 */
uint64_t onewire_master_rom_read(void);
/** send SEARCH ROM command
 *  @note user needs to send reset pulse before
 *  @warning undefined behaviour if a ROM code different than the last found is provided
 *  @param[in,out] code use 0 to start search ROM code from scratch, or last know value to search next; writes back next ROM code found, or 0 if error occurred
 *  @param[in] alarm search only for ROM codes for slaves with an alarm flag set
 *  @return if an additional slave has been detected
 *  @warning when the code found is 0 it very probably means that the 1-wire line is not pulled up instead of actually having found a slave with ROM code 0
 */
bool onewire_master_rom_search(uint64_t* code, bool alarm);
/** send SKIP ROM command (all slaves on the bus will be selected)
 *  @note user needs to send reset pulse before
 *  @return if operation succeeded
 */
bool onewire_master_rom_skip(void);
/** send MATCH ROM command to select a specific slave
 *  @note user needs to send reset pulse before
 *  @param[in] code ROM code of slave to select
 *  @return if operation succeeded
 */
bool onewire_master_rom_match(uint64_t code);
/** read data byte
 *  @note it is up to the user to send the reset pulse 
 *  @param[out] data buffer to save data read
 *  @return if operation succeeded
 */
bool onewire_master_read_byte(uint8_t* data);
/** write data byte
 *  @note it is up to the user to send the reset pulse 
 *  @param[in] data byte to write
 *  @return if operation succeeded
 */
bool onewire_master_write_byte(uint8_t data);
/** issue function and read data
 *  @note user needs to send a ROM command before
 *  @param[in] function function command to send
 *  @param[out] data buffer to save read bits (NULL if only the function command should be sent)
 *  @param[in] bits number of bits to read (0 if only the function command should be sent)
 *  @return if operation succeeded
 */
bool onewire_master_function_read(uint8_t function, uint8_t* data, uint32_t bits);
/** issue function and write data
 *  @note user needs to send a ROM command before
 *  @param[in] function function command to send
 *  @param[out] data data to write (NULL if only the function command should be sent)
 *  @param[in] bits number of bits to write (0 if only the function command should be sent)
 *  @return if operation succeeded
 */
bool onewire_master_function_write(uint8_t function, uint8_t* data, uint32_t bits);

/** device corresponding to a family code
 */
struct onewire_family_code_t {
	uint8_t code; /**< ROM ID code */
	const char* device; /**< device name(s) */
};

/** list of possible devices corresponding to the family code
 *  sources:
 *  - http://owfs.org/index.php?page=family-code-list
 *  - http://owfs.sourceforge.net/family.html
 *  - https://www.maximintegrated.com/en/app-notes/index.mvp/id/155
 *  - https://github.com/owfs/owfs-doc/wiki/1Wire-Device-List
 *  - IDs seen or reported
 */
static const struct onewire_family_code_t onewire_family_codes[] = {
	{
		.code = 0x01,
		.device = "DS1990R/DS2401/DS2411/DS2490A",
	},
	{
		.code = 0x02,
		.device = "DS1991/DS1425",
	},
	{
		.code = 0x04,
		.device = "DS1994/DS2404",
	},
	{
		.code = 0x05,
		.device = "DS2405",
	},
	{
		.code = 0x06,
		.device = "DS1993",
	},
	{
		.code = 0x08,
		.device = "DS1992",
	},
	{
		.code = 0x09,
		.device = "DS1982/DS2502/DS2703/DS2704",
	},
	{
		.code = 0x0a,
		.device = "DS1995",
	},
	{
		.code = 0x0b,
		.device = "DS1985/DS2505",
	},
	{
		.code = 0x0c,
		.device = "DS1996",
	},
	{
		.code = 0x0f,
		.device = "DS1986/DS2506",
	},
	{
		.code = 0x10,
		.device = "DS1920/DS18S20",
	},
	{
		.code = 0x12,
		.device = "DS2406/DS2407",
	},
	{
		.code = 0x14,
		.device = "DS1971/DS2430A",
	},
	{
		.code = 0x16,
		.device = "DS1954/DS1957",
	},
	{
		.code = 0x18,
		.device = "DS1963S/DS1962",
	},
	{
		.code = 0x1a,
		.device = "DS1963L",
	},
	{
		.code = 0x1b,
		.device = "DS2436",
	},
	{
		.code = 0x1c,
		.device = "DS28E04-100",
	},
	{
		.code = 0x1d,
		.device = "DS2423",
	},
	{
		.code = 0x1e,
		.device = "DS2437",
	},
	{
		.code = 0x1f,
		.device = "DS2409",
	},
	{
		.code = 0x20,
		.device = "DS2450",
	},
	{
		.code = 0x21,
		.device = "DS1921",
	},
	{
		.code = 0x22,
		.device = "DS1922",
	},
	{
		.code = 0x23,
		.device = "DS1973/DS2433",
	},
	{
		.code = 0x24,
		.device = "DS1904/DS2415",
	},
	{
		.code = 0x26,
		.device = "DS2438",
	},
	{
		.code = 0x27,
		.device = "DS2417",
	},
	{
		.code = 0x28,
		.device = "DS18B20",
	},
	{
		.code = 0x29,
		.device = "DS2408",
	},
	{
		.code = 0x2c,
		.device = "DS2890",
	},
	{
		.code = 0x2d,
		.device = "DS1972/DS2431",
	},
	{
		.code = 0x2e,
		.device = "DS2770",
	},
	{
		.code = 0x2f,
		.device = "DS28E01-100",
	},
	{
		.code = 0x30,
		.device = "DS2760/DS2761/DS2762",
	},
	{
		.code = 0x31,
		.device = "DS2720",
	},
	{
		.code = 0x32,
		.device = "DS2780",
	},
	{
		.code = 0x33,
		.device = "DS1961S/DS2432",
	},
	{
		.code = 0x34,
		.device = "DS2703",
	},
	{
		.code = 0x35,
		.device = "DS2755",
	},
	{
		.code = 0x36,
		.device = "DS2740",
	},
	{
		.code = 0x37,
		.device = "DS1977",
	},
	{
		.code = 0x3a,
		.device = "DS2413",
	},
	{
		.code = 0x3b,
		.device = "DS1825/MAX31826/MAX31850",
	},
	{
		.code = 0x3d,
		.device = "DS2781",
	},
	{
		.code = 0x41,
		.device = "DS1922/DS1923/DS2422",
	},
	{
		.code = 0x42,
		.device = "DS28EA00",
	},
	{
		.code = 0x43,
		.device = "DS28EC20",
	},
	{
		.code = 0x44,
		.device = "DS28E10",
	},
	{
		.code = 0x51,
		.device = "DS2751",
	},
	{
		.code = 0x7e,
		.device = "EDS00xx",
	},
	{
		.code = 0x81,
		.device = "DS1420/DS2490R/DS2490B",
	},
	{
		.code = 0x82,
		.device = "DS1425",
	},
	{
		.code = 0x84,
		.device = "DS2404S",
	},
	{
		.code = 0x89,
		.device = "DS1982U/DS2502",
	},
	{
		.code = 0x8b,
		.device = "DS1985U/DS2505",
	},
	{
		.code = 0x8f,
		.device = "DS1986U/DS2506",
	},
	{
		.code = 0xa0,
		.device = "mRS001",
	},
	{
		.code = 0xa1,
		.device = "mVM001",
	},
	{
		.code = 0xa2,
		.device = "mCM001",
	},
	{
		.code = 0xa6,
		.device = "mTS017",
	},
	{
		.code = 0xb1,
		.device = "mTC001",
	},
	{
		.code = 0xb2,
		.device = "mAM001",
	},
	{
		.code = 0xb3,
		.device = "DS2432/mTC002",
	},
	{
		.code = 0xfc,
		.device = "BAE0910/BAE0911",
	},
	{
		.code = 0xff,
		.device = "Swart LCD",
	}
};