NEORV32 - Software Framework Documentation
Loading...
Searching...
No Matches
onewire_aux.h
1// APPLICATION NOTE 187 "1-Wire Search Algorithm" by Maxim Integrated
2// https://www.maximintegrated.com/en/design/technical-documents/app-notes/1/187.html
3// modified for the NEORV32 Processor
4
5#ifndef onewire_aux_h
6#define onewire_aux_h
7
8#include <neorv32.h>
9
10// definitions
11#define FALSE 0
12#define TRUE 1
13
14static unsigned char dscrc_table[] = {
15 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
16 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
17 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
18 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
19 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
20 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
21 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
22 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
23 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
24 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
25 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
26 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
27 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
28 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
29 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
30 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
31
32// method declarations
33int OWFirst();
34int OWNext();
35int OWVerify();
36void OWTargetSetup(unsigned char family_code);
37void OWFamilySkipSetup();
38int OWSearch();
39unsigned char docrc8(unsigned char value);
40
41// global search state
42unsigned char ROM_NO[8];
43int LastDiscrepancy;
44int LastFamilyDiscrepancy;
45int LastDeviceFlag;
46unsigned char crc8;
47
48//--------------------------------------------------------------------------
49// Find the 'first' devices on the 1-Wire bus
50// Return TRUE : device found, ROM number in ROM_NO buffer
51// FALSE : no device present
52//
53int OWFirst()
54{
55 // reset the search state
56 LastDiscrepancy = 0;
57 LastDeviceFlag = FALSE;
58 LastFamilyDiscrepancy = 0;
59
60 return OWSearch();
61}
62
63//--------------------------------------------------------------------------
64// Find the 'next' devices on the 1-Wire bus
65// Return TRUE : device found, ROM number in ROM_NO buffer
66// FALSE : device not found, end of search
67//
68int OWNext()
69{
70 // leave the search state alone
71 return OWSearch();
72}
73
74//--------------------------------------------------------------------------
75// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
76// search state.
77// Return TRUE : device found, ROM number in ROM_NO buffer
78// FALSE : device not found, end of search
79//
80int OWSearch()
81{
82 int id_bit_number;
83 int last_zero, rom_byte_number, search_result;
84 int id_bit, cmp_id_bit;
85 unsigned char rom_byte_mask, search_direction;
86
87 // initialize for search
88 id_bit_number = 1;
89 last_zero = 0;
90 rom_byte_number = 0;
91 rom_byte_mask = 1;
92 search_result = 0;
93 crc8 = 0;
94
95 // if the last call was not the last one
96 if (!LastDeviceFlag)
97 {
98 // 1-Wire reset
100 {
101 // reset the search
102 LastDiscrepancy = 0;
103 LastDeviceFlag = FALSE;
104 LastFamilyDiscrepancy = 0;
105 return FALSE;
106 }
107
108 // issue the search command
110
111 // loop to do the search
112 do
113 {
114 // read a bit and its complement
117
118 // check for no devices on 1-wire
119 if ((id_bit == 1) && (cmp_id_bit == 1))
120 break;
121 else
122 {
123 // all devices coupled have 0 or 1
124 if (id_bit != cmp_id_bit)
125 search_direction = id_bit; // bit write value for search
126 else
127 {
128 // if this discrepancy if before the Last Discrepancy
129 // on a previous next then pick the same as last time
130 if (id_bit_number < LastDiscrepancy)
131 search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
132 else
133 // if equal to last pick 1, if not then pick 0
134 search_direction = (id_bit_number == LastDiscrepancy);
135
136 // if 0 was picked then record its position in LastZero
137 if (search_direction == 0)
138 {
139 last_zero = id_bit_number;
140
141 // check for Last discrepancy in family
142 if (last_zero < 9)
143 LastFamilyDiscrepancy = last_zero;
144 }
145 }
146
147 // set or clear the bit in the ROM byte rom_byte_number
148 // with mask rom_byte_mask
149 if (search_direction == 1)
150 ROM_NO[rom_byte_number] |= rom_byte_mask;
151 else
152 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
153
154 // serial number search direction write bit
155 neorv32_onewire_write_bit_blocking(search_direction);
156
157 // increment the byte counter id_bit_number
158 // and shift the mask rom_byte_mask
159 id_bit_number++;
160 rom_byte_mask <<= 1;
161
162 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
163 if (rom_byte_mask == 0)
164 {
165 docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
166 rom_byte_number++;
167 rom_byte_mask = 1;
168 }
169 }
170 }
171 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
172
173 // if the search was successful then
174 if (!((id_bit_number < 65) || (crc8 != 0)))
175 {
176 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
177 LastDiscrepancy = last_zero;
178
179 // check for last device
180 if (LastDiscrepancy == 0)
181 LastDeviceFlag = TRUE;
182
183 search_result = TRUE;
184 }
185 }
186
187 // if no device found then reset counters so next 'search' will be like a first
188 if (!search_result || !ROM_NO[0])
189 {
190 LastDiscrepancy = 0;
191 LastDeviceFlag = FALSE;
192 LastFamilyDiscrepancy = 0;
193 search_result = FALSE;
194 }
195
196 return search_result;
197}
198
199//--------------------------------------------------------------------------
200// Verify the device with the ROM number in ROM_NO buffer is present.
201// Return TRUE : device verified present
202// FALSE : device not present
203//
204int OWVerify()
205{
206 unsigned char rom_backup[8];
207 int i,rslt,ld_backup,ldf_backup,lfd_backup;
208
209 // keep a backup copy of the current state
210 for (i = 0; i < 8; i++)
211 rom_backup[i] = ROM_NO[i];
212 ld_backup = LastDiscrepancy;
213 ldf_backup = LastDeviceFlag;
214 lfd_backup = LastFamilyDiscrepancy;
215
216 // set search to find the same device
217 LastDiscrepancy = 64;
218 LastDeviceFlag = FALSE;
219
220 if (OWSearch())
221 {
222 // check if same device found
223 rslt = TRUE;
224 for (i = 0; i < 8; i++)
225 {
226 if (rom_backup[i] != ROM_NO[i])
227 {
228 rslt = FALSE;
229 break;
230 }
231 }
232 }
233 else
234 rslt = FALSE;
235
236 // restore the search state
237 for (i = 0; i < 8; i++)
238 ROM_NO[i] = rom_backup[i];
239 LastDiscrepancy = ld_backup;
240 LastDeviceFlag = ldf_backup;
241 LastFamilyDiscrepancy = lfd_backup;
242
243 // return the result of the verify
244 return rslt;
245}
246
247//--------------------------------------------------------------------------
248// Setup the search to find the device type 'family_code' on the next call
249// to OWNext() if it is present.
250//
251void OWTargetSetup(unsigned char family_code)
252{
253 int i;
254
255 // set the search state to find SearchFamily type devices
256 ROM_NO[0] = family_code;
257 for (i = 1; i < 8; i++)
258 ROM_NO[i] = 0;
259 LastDiscrepancy = 64;
260 LastFamilyDiscrepancy = 0;
261 LastDeviceFlag = FALSE;
262}
263
264//--------------------------------------------------------------------------
265// Setup the search to skip the current device type on the next call
266// to OWNext().
267//
268void OWFamilySkipSetup()
269{
270 // set the Last discrepancy to last family discrepancy
271 LastDiscrepancy = LastFamilyDiscrepancy;
272 LastFamilyDiscrepancy = 0;
273
274 // check for end of list
275 if (LastDiscrepancy == 0)
276 LastDeviceFlag = TRUE;
277}
278
279//--------------------------------------------------------------------------
280// Calculate the CRC8 of the byte value provided with the current
281// global 'crc8' value.
282// Returns current global crc8 value
283//
284unsigned char docrc8(unsigned char value)
285{
286 // See Application Note 27
287
288 // TEST BUILD
289 crc8 = dscrc_table[crc8 ^ value];
290 return crc8;
291}
292
293#endif // onewire_aux_h
Main NEORV32 core library / HAL include file.
void neorv32_onewire_write_bit_blocking(uint8_t bit)
Definition neorv32_onewire.c:353
void neorv32_onewire_write_byte_blocking(uint8_t byte)
Definition neorv32_onewire.c:390
uint8_t neorv32_onewire_read_bit_blocking(void)
Definition neorv32_onewire.c:333
int neorv32_onewire_reset_blocking(void)
Definition neorv32_onewire.c:313