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