Branch data Line data Source code
1 : : /* DS18B20 measurement module (include-only part of src/ds18b20.c).
2 : : * Not a translation unit on its own: compile src/ds18b20.c, which
3 : : * resolves the DS18B20_DRIVER_BUILD gate. This part owns the DS18B20_ST_*
4 : : * measurement state machine (ds18b20_poll); its CONVERT/REQUEST command
5 : : * builds share the phase_pulses.cmd workspace (see the union in ds18b20.c). */
6 : : #ifndef DS18B20_DRIVER_BUILD
7 : : #error "ds18b20_measure.c is an include-only driver part; compile src/ds18b20.c"
8 : : #endif
9 : :
10 : : #include "ow_profile.h"
11 : :
12 : : /**
13 : : * @defgroup DS18B20_Measurement_Internal DS18B20 Measurement State Machine
14 : : * @{
15 : : */
16 : :
17 : : /**
18 : : * @brief Finish the current scan-mode device read
19 : : * @note Called after every per-device report in scan mode. Advances to the
20 : : * next device (CONTINUE, skipping a fresh conversion) or, after the last
21 : : * device, returns to IDLE. In single-device mode it only returns to IDLE.
22 : : * @note The driver never starts a round on its own: at IDLE it waits for
23 : : * ds18b20_start_measure(), so the measurement cadence is the
24 : : * application's decision.
25 : : */
26 : 825 : static void scan_finish_or_next(void) {
27 [ + + ]: 825 : if ((ctx.flags & DS18B20_FLAG_SCAN_MODE) == 0u) {
28 : : // Parasite power: hold the strong pull-up while the driver is parked
29 : : // so the device capacitors stay charged for the next measurement
30 : : // cycle. ds18b20_start_measure() releases it again in START.
31 [ + + ]: 625 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
32 : 50 : onewire_strong_pullup(1);
33 : : }
34 : 625 : return;
35 : : }
36 : 200 : ctx.scan_index++;
37 [ + + ]: 200 : if (ctx.scan_index < dev_count) {
38 : 100 : ctx.current_state = DS18B20_ST_CONTINUE;
39 : : /* DECODE armed nothing, so without a running timer no UIF would ever
40 : : * drive the CONTINUE state again (single-device rounds get their UIF
41 : : * from ds18b20_start_measure()). Arm a short scheduling delay: its UIF
42 : : * is the bridge to CONTINUE, which then arms the real bus reset. */
43 : 100 : ow_start_wait(SCAN_DEVICE_GAP_US);
44 : : } else {
45 : 100 : ctx.current_state = DS18B20_ST_IDLE;
46 : : // Parasite power: keep the strong pull-up engaged while parked.
47 [ + + ]: 100 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
48 : 25 : onewire_strong_pullup(1);
49 : : }
50 : : }
51 : : }
52 : :
53 : : /**
54 : : * @brief Start one measurement cycle (non-blocking)
55 : : * @see ds18b20_start_measure() in ds18b20.h
56 : : * @note Schedules the bus reset the state machine needs to leave IDLE; the
57 : : * cycle itself is then advanced by ds18b20_poll(). Ignored while a
58 : : * measurement cycle, a device search, a resolution change or a command
59 : : * transaction owns the timer.
60 : : */
61 : 825 : void ds18b20_start_measure(void) {
62 [ + + ]: 825 : if (ctx.current_state != DS18B20_ST_IDLE) {
63 : 25 : return; // a measurement cycle is in progress
64 : : }
65 [ + + + - ]: 800 : if (onewire_search_active() || (res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u ||
66 [ - + ]: 775 : (txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
67 : 25 : return; // the search, a resolution change or a command owns the timer
68 : : }
69 : 775 : ctx.current_state = DS18B20_ST_START;
70 : 775 : ow_timer_kick();
71 : : }
72 : :
73 : : /**
74 : : * @brief Arm simultaneous conversion of every discovered device
75 : : * @see ds18b20_scan_start() in ds18b20.h
76 : : * @note Selects scan mode only. Call ds18b20_start_measure() to start a round.
77 : : */
78 : 375 : void ds18b20_scan_start(void) {
79 [ + + ]: 375 : if (ctx.current_state != DS18B20_ST_IDLE) {
80 : 25 : return; // a measurement cycle is in progress
81 : : }
82 [ + + + + ]: 350 : if (onewire_search_active() || (res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u ||
83 [ + + ]: 300 : (txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
84 : 75 : return; // the search, a resolution change or a command owns the timer
85 : : }
86 [ + + ]: 275 : if (dev_count == 0) {
87 : 25 : return; // nothing discovered: there is no device to convert
88 : : }
89 : 250 : ctx.flags |= DS18B20_FLAG_SCAN_MODE;
90 : 250 : ctx.scan_index = 0;
91 : : }
92 : :
93 : : /**
94 : : * @brief Index of the device whose result ds18b20_complete() just reported
95 : : * @see ds18b20_scan_index() in ds18b20.h
96 : : */
97 : 975 : uint8_t ds18b20_scan_index(void) { return ctx.scan_index; }
98 : :
99 : : /* Lifetime note: the CONVERT/REQUEST builds share phase_pulses.cmd, and the
100 : : * addressed (Match ROM) builds share phase_pulses.addr (see the union in
101 : : * ds18b20.c). This is safe only because issue_command() rebuilds the used
102 : : * member in full on every call, is invoked exclusively from the
103 : : * CONVERT/REQUEST states after onewire_bus_done() has confirmed that the
104 : : * timer/DMA of the previous 1-Wire operation is idle, and the ownership
105 : : * guards (ds18b20_select/search/resolution reject while busy) prevent any
106 : : * concurrent re-entry that could interleave a second build while the
107 : : * CCR3-feed DMA is still reading the table. In other words the rewrite
108 : : * happens strictly between DMA bursts, never during one — the invariant is
109 : : * implicit in the call site, hence documented here at the same level of
110 : : * detail as the B1 guards for the other pulse buffers. */
111 : 825 : static void build_skip_cmd(ow_pulse_t* dst, uint8_t cmd_byte) {
112 : : ow_encode_skip_rom(&dst);
113 : 825 : onewire_encode_byte(dst, cmd_byte);
114 : 825 : dst[DS18B20_BITS_PER_BYTE] = ONEWIRE_RELEASE_PULSE;
115 : 825 : }
116 : :
117 : : /**
118 : : * @brief Check presence and issue a DS18B20 command (shared by CONVERT and
119 : : * REQUEST states)
120 : : * @param[in] cmd_byte Command byte to send (Match ROM table rebuilt per call
121 : : * in address mode, or via the Skip-ROM table in
122 : : * broadcast mode)
123 : : * @param[in] next_state State to transition to on success
124 : : */
125 : 1400 : static void issue_command(uint8_t cmd_byte, ds18b20_state_t next_state) {
126 : 1400 : uint8_t present_ok = ow_has_presence(ctx.capture);
127 [ + + ]: 1400 : if (!present_ok) {
128 : : // Return to IDLE before the callback so a re-selection from inside
129 : : // ds18b20_complete() is accepted (ds18b20_select() only acts at IDLE).
130 : 100 : ctx.current_state = DS18B20_ST_IDLE;
131 : : // Turn the busy indicator off: busy(1) was set in START and this early
132 : : // exit skips the DECODE state where busy(0) is normally cleared.
133 : 100 : ds18b20_busy(0);
134 : 100 : ds18b20_complete(DS18B20_TEMP_ERROR_NO_SENSOR);
135 : : // Parasite power: hold the strong pull-up while parked.
136 [ + + ]: 100 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
137 : 25 : onewire_strong_pullup(1);
138 : : }
139 : 100 : return;
140 : : }
141 [ + + ]: 1300 : if ((ctx.flags & DS18B20_FLAG_ADDRESS_MODE) != 0u) {
142 : 475 : build_addr_cmd(cmd_byte);
143 : 475 : onewire_write_slots(phase_pulses.addr, DS18B20_MATCH_SLOTS);
144 : : } else {
145 : 825 : ow_pulse_t* skip_tbl = phase_pulses.cmd;
146 : 825 : build_skip_cmd(skip_tbl, cmd_byte);
147 : 825 : onewire_write_slots(skip_tbl, DS18B20_DMA_TRANSFERS);
148 : : }
149 : 1300 : ctx.current_state = next_state;
150 : : }
151 : :
152 : : /**
153 : : * @brief Main state machine function - must be called periodically from main loop
154 : : * @note Non-blocking state machine that advances 1-Wire communication state
155 : : * @note Uses timer update interrupt flag to determine when operations complete
156 : : */
157 : 5000 : void ds18b20_poll(void) {
158 : : // Ownership guard: while the device search, a resolution change or a
159 : : // command transaction owns the timer, the measurement state machine must
160 : : // stay out of the way and not react to their UIFs.
161 [ + + + + ]: 5000 : if (onewire_search_active() || (res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u ||
162 [ - + ]: 4950 : (txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
163 : 50 : return;
164 : : }
165 : :
166 : : // Check if timer update interrupt occurred (indicates operation completion)
167 : : // This is the non-blocking way to detect when timed operations finish.
168 : : // onewire_bus_done() *consumes* the update flag, so it must be called
169 : : // exactly once per poll and its result reused below.
170 : 4950 : uint8_t done = ow_op_done();
171 : :
172 [ + + ]: 4950 : if (!done) return;
173 : :
174 : : OW_PROFILE_WORK_BEGIN();
175 : : // State machine to manage 1-Wire communication sequence
176 [ + + + + : 4875 : switch (ctx.current_state) {
+ + + +
+ ]
177 : 50 : case DS18B20_ST_IDLE:
178 : : // A parked driver stays idle until ds18b20_start_measure() requests
179 : : // a round. scan_start() only selects the addressing mode; it does not
180 : : // schedule a conversion or a timer event.
181 : 50 : break;
182 : :
183 : 700 : case DS18B20_ST_START:
184 : : // Turn on LED to indicate measurement in progress
185 : 700 : ds18b20_busy(1);
186 : : // Parasite power: release the strong pull-up so the reset pulse can
187 : : // drive the line LOW; it is re-engaged for the conversion window.
188 [ + + ]: 700 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
189 : 100 : onewire_strong_pullup(0);
190 : : }
191 : : // Initiate 1-Wire bus reset sequence
192 : 700 : onewire_reset(ctx.capture);
193 : : // Transition to CONVERT state
194 : 700 : ctx.current_state = DS18B20_ST_CONVERT;
195 : 700 : break;
196 : :
197 : 750 : case DS18B20_ST_CONVERT:
198 [ + + ]: 750 : if ((ctx.flags & DS18B20_FLAG_SCAN_MODE) != 0u) {
199 : : // Scan mode: broadcast Convert T (Skip ROM) so every sensor starts
200 : : // converting in parallel; a single conversion wait covers them all.
201 : 150 : ctx.scan_index = 0; // new round: read back starting from device 0
202 : 150 : ctx.flags &= (uint8_t)~DS18B20_FLAG_ADDRESS_MODE;
203 : : }
204 : : // Parasite power: the Convert T command is master-only (the slave does
205 : : // not pull the line LOW during it), so keep the strong pull-up engaged
206 : : // while the command is transmitted. This feeds the slave through the
207 : : // command phase; the conversion window below re-asserts it anyway.
208 [ + + ]: 750 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
209 : 125 : onewire_strong_pullup(1);
210 : : }
211 : 750 : issue_command(DS18B20_CONVERT_T, DS18B20_ST_WAIT);
212 : 750 : break;
213 : :
214 : 600 : case DS18B20_ST_WAIT:
215 : : // Parasite power: the sensors draw their supply from the bus line
216 : : // during the whole conversion, so drive the line HIGH actively before
217 : : // the wait starts (engaging here and starting the timer in the same
218 : : // transition keeps wait and supply aligned regardless of poll latency).
219 [ + + ]: 600 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
220 : 100 : onewire_strong_pullup(1);
221 : : }
222 : : // Start timer for the conversion wait (93.75ms @ 9-bit .. 750ms @ 12-bit)
223 : : wait_conversion();
224 : 600 : ctx.current_state = DS18B20_ST_CONTINUE;
225 : 600 : break;
226 : :
227 : 675 : case DS18B20_ST_CONTINUE:
228 : : // Release the strong pull-up BEFORE the reset pulse pulls the line
229 : : // low: the conversion is complete, the devices no longer need the
230 : : // parasite supply and the bus must be free again.
231 : 675 : onewire_strong_pullup(0);
232 : : // Initiate second 1-Wire bus reset sequence
233 : 675 : onewire_reset(ctx.capture);
234 : 675 : ctx.current_state = DS18B20_ST_REQUEST;
235 : 675 : break;
236 : :
237 : 650 : case DS18B20_ST_REQUEST:
238 [ + + ]: 650 : if ((ctx.flags & DS18B20_FLAG_SCAN_MODE) != 0u) {
239 : : // Scan mode: read the current device back via Match ROM. The
240 : : // table is rebuilt from selected_rom on every addressed
241 : : // operation (issue_command), so only the ROM is staged here.
242 [ + + ]: 2025 : for (uint8_t i = 0; i < DS18B20_ROM_BYTES; i++) {
243 : 1800 : ctx.selected_rom[i] = dev_roms[ctx.scan_index][i];
244 : : }
245 : 225 : ctx.flags |= DS18B20_FLAG_ADDRESS_MODE;
246 : : }
247 [ + + ]: 650 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
248 : 75 : onewire_strong_pullup(1);
249 : : }
250 : 650 : issue_command(DS18B20_READ_SCRATCHPAD, DS18B20_ST_READ);
251 : 650 : break;
252 : :
253 : 600 : case DS18B20_ST_READ:
254 [ + + ]: 600 : if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
255 : 75 : onewire_strong_pullup(0);
256 : : }
257 : 600 : onewire_read_data(ctx.pulse, DS18B20_SCRATCHPAD_LEN);
258 : 600 : ctx.current_state = DS18B20_ST_DECODE;
259 : 600 : break;
260 : :
261 : 825 : case DS18B20_ST_DECODE: // Process received data and report temperature
262 : : /* Snapshot pulse widths before decode_scratchpad() overwrites them
263 : : * via the union alias (scratchpad[n] == pulse[n]). */
264 : 825 : ow_stats_capture_pulse(ctx.pulse, DS18B20_SCRATCHPAD_BITS,
265 [ + + ]: 825 : ((ctx.flags & DS18B20_FLAG_ADDRESS_MODE) != 0u) ? ctx.selected_rom
266 : : : (const uint8_t*)0);
267 : : // Decode captured pulse durations into scratchpad bytes
268 : : decode_scratchpad();
269 : : // Turn off LED to indicate measurement complete
270 : 825 : ds18b20_busy(0);
271 : :
272 : : // In single-device mode the callback runs at IDLE, so a re-selection
273 : : // from inside ds18b20_complete() is accepted there. Scan mode keeps its
274 : : // own per-device addressing and stays in DECODE: a select() from the
275 : : // scan callback is rejected, and it reports every device before
276 : : // returning to IDLE at the round end.
277 [ + + ]: 825 : if ((ctx.flags & DS18B20_FLAG_SCAN_MODE) == 0u) {
278 : 625 : ctx.current_state = DS18B20_ST_IDLE;
279 : : }
280 : :
281 : : // Match ROM mode: if the addressed device is absent, nobody drives
282 : : // the bus after the address, so the whole scratchpad reads back as
283 : : // 0xFF. Report it as a missing sensor instead of a bogus CRC error.
284 [ + + ]: 825 : if ((ctx.flags & DS18B20_FLAG_ADDRESS_MODE) != 0u) {
285 : 350 : uint8_t all_ones = 1;
286 [ + + ]: 800 : for (uint8_t i = 0; i < DS18B20_SCRATCHPAD_LEN; i++) {
287 [ + + ]: 750 : if (ctx.scratchpad[i] != 0xFF) {
288 : 300 : all_ones = 0;
289 : 300 : break;
290 : : }
291 : : }
292 [ + + ]: 350 : if (all_ones) {
293 : : OW_PROFILE_WORK_END();
294 : 50 : ds18b20_complete(DS18B20_TEMP_ERROR_NO_SENSOR);
295 : : OW_PROFILE_WORK_BEGIN();
296 : 50 : ow_stats_count_error(DS18B20_TEMP_ERROR_NO_SENSOR,
297 : : ctx.selected_rom);
298 : 50 : scan_finish_or_next();
299 : 50 : break;
300 : : }
301 : : }
302 : :
303 : : // Validate reserved bytes per DS18B20 specification:
304 : : // Byte 5 must be 0xFF, Byte 7 must be 0x10.
305 : : // This catches all-zero, all-0xFF, and bus fault conditions.
306 [ + + - + ]: 775 : if (ctx.scratchpad[5] != 0xFF || ctx.scratchpad[7] != 0x10) {
307 : : OW_PROFILE_WORK_END();
308 : 50 : ds18b20_complete(DS18B20_TEMP_ERROR_CRC_FAIL);
309 : : OW_PROFILE_WORK_BEGIN();
310 : 50 : ow_stats_count_error(DS18B20_TEMP_ERROR_CRC_FAIL,
311 : : ctx.selected_rom);
312 : 50 : scan_finish_or_next();
313 : 50 : break;
314 : : }
315 : :
316 : : // Validate CRC and report temperature or error
317 [ + + ]: 1450 : if (ctx.scratchpad[DS18B20_SCRATCHPAD_LEN - 1] == check_scratchpad_crc()) {
318 : : // CRC valid - decode and report temperature. The scratchpad is
319 : : // trustworthy, so also trust the config byte (byte 4, R1/R0 bits
320 : : // 6:5) and adapt the conversion wait for the next cycle: this keeps
321 : : // the wait in sync with a resolution changed via
322 : : // ds18b20_set_resolution() or externally. (R1/R0 are 0..3, so the
323 : : // derived value is always within DS18B20_RES_MIN..DS18B20_RES_MAX.)
324 : : // It is derived only on a valid CRC so a corrupted config byte can
325 : : // never shorten the next conversion wait prematurely.
326 [ - + ]: 550 : ctx.resolution = DS18B20_RES_MIN + ((ctx.scratchpad[4] >> 5) & 0x3);
327 : : OW_PROFILE_WORK_END();
328 : 550 : ds18b20_complete(decode_temperature());
329 : : OW_PROFILE_WORK_BEGIN();
330 : : } else {
331 : : // CRC invalid - report error (resolution kept unchanged)
332 : : OW_PROFILE_WORK_END();
333 : 175 : ds18b20_complete(DS18B20_TEMP_ERROR_CRC_FAIL);
334 : : OW_PROFILE_WORK_BEGIN();
335 : 175 : ow_stats_count_error(DS18B20_TEMP_ERROR_CRC_FAIL,
336 : : ctx.selected_rom);
337 : : }
338 : :
339 : : // Next scan-mode device (CONTINUE, no fresh conversion) or, after the
340 : : // last device, back to IDLE. In single-device mode this only returns to
341 : : // IDLE, where the driver waits for ds18b20_start_measure().
342 : 725 : scan_finish_or_next();
343 : 725 : break;
344 : :
345 : 25 : default:
346 : : // Unexpected state - report generic error
347 : 25 : ctx.current_state = DS18B20_ST_IDLE;
348 : : OW_PROFILE_WORK_END();
349 : 25 : ds18b20_complete(DS18B20_TEMP_ERROR_GENERIC);
350 : : OW_PROFILE_WORK_BEGIN();
351 : 25 : ow_stats_count_error(DS18B20_TEMP_ERROR_GENERIC,
352 : : (const uint8_t*)0);
353 : 25 : break;
354 : : }
355 : : OW_PROFILE_WORK_END();
356 : : }
357 : :
358 : : /**
359 : : * @}
360 : : */
|