Branch data Line data Source code
1 : : /**
2 : : * @file ow_glue.h
3 : : * @brief Library-internal bridge between the driver sources and the engine.
4 : : *
5 : : * A few operations have always belonged to the DS18B20/DS2431 drivers, not
6 : : * to a consumer: scheduling a hardware-timed wait (a DS18B20 conversion
7 : : * wait, an EEPROM hold-off window) and kicking the owner's state machine so
8 : : * it advances; polling a scheduled operation to completion (with the
9 : : * low-power sleep handled once, in one place); proving a device presence
10 : : * from the last reset capture; and encoding command-write pulses (the ROM
11 : : * addressing prefix, then raw payload bytes). They are engine calls, but
12 : : * they are made by the driver, and the two are separate translation
13 : : * units - so the contract between them needs a header.
14 : : *
15 : : * It is not a public header. The engine headers are not installed, and a
16 : : * consumer has no business scheduling a bus wait with a value in timer
17 : : * registers: the wait belongs to the driver that knows the device's timing.
18 : : * That header used to expose the two oldest calls in inc/onewire.h, where
19 : : * they read as part of the bus API; they were never meant to be called from
20 : : * outside the library.
21 : : *
22 : : * Library-internal (src/): reached only by the driver sources and by
23 : : * src/onewire.c, which defines the calls.
24 : : */
25 : :
26 : : #ifndef OW_GLUE_H
27 : : #define OW_GLUE_H
28 : :
29 : : #include <stdint.h>
30 : :
31 : : #include "onewire.h"
32 : : #include "ow_compiler.h"
33 : :
34 : : /**
35 : : * @brief Start a hardware-timed wait of a given duration
36 : : * @param[in] us Requested wait in microseconds
37 : : * @note The engine picks the timer periods; the caller states a duration.
38 : : * See ow_port_plan_wait() for the rule.
39 : : */
40 : : void ow_start_wait(uint32_t us);
41 : :
42 : : /**
43 : : * @brief Raise the timer update flag so the caller's state machine advances
44 : : * @note No hardware timing is involved: the operation this call unblocks is
45 : : * scheduled by the owner itself. Used by the DS18B20 driver to start one
46 : : * measurement cycle (ds18b20_start_measure()). Call it only while no
47 : : * operation is in flight.
48 : : */
49 : : void ow_timer_kick(void);
50 : :
51 : : /**
52 : : * @brief Poll the scheduled hardware operation to completion
53 : : * @return 1 when the operation finished, 0 while still running
54 : : * @note Checks onewire_bus_done() once; on low-power builds a still-running
55 : : * long stage is slept through instead of spun on (WFE, woken by the
56 : : * timer's own update event via SEVONPEND - no ISR so this is invisible
57 : : * to the application: the poll just returns when the stage is done),
58 : : * and the wake event is consumed so the caller sees completion on the
59 : : * same poll. Non-low-power builds are a single bus_done() check.
60 : : */
61 : : uint8_t ow_op_done(void);
62 : :
63 : : /**
64 : : * @brief Prove a device presence from the last reset capture
65 : : * @param[in] cap Reset + presence capture (2 entries)
66 : : * @return 1 when at least one device answered, 0 otherwise
67 : : * @note Presence is proved by the captured edge count, not by the buffer
68 : : * contents: the transfer engine reports how many edges arrived, so a
69 : : * stale second slot can never fake a device. Single place for that rule.
70 : : */
71 : : __STATIC_FORCEINLINE uint8_t ow_has_presence(const volatile uint16_t* cap) {
72 : 4675 : return onewire_present(cap, onewire_reset_captured());
73 : : }
74 : :
75 : : /**
76 : : * @brief Encode raw bytes into pulse-table entries, advancing past them
77 : : * @param[in,out] dst Cursor into the pulse table; advanced by n entries
78 : : * @param[in] src Bytes to encode
79 : : * @param[in] n Byte count
80 : : * @return Bytes encoded (n)
81 : : * @note Pure mechanical transform shared by every command build; what the
82 : : * bytes mean (prefix, command, payload) stays in the drivers.
83 : : */
84 : : __STATIC_FORCEINLINE uint8_t ow_encode_bytes(ow_pulse_t** dst, const uint8_t* src, uint8_t n) {
85 [ + + + + ]: 13122 : for (uint8_t i = 0; i < n; i++) {
[ + + + +
+ + + + +
+ ][ + + ]
86 : 10331 : onewire_encode_byte(*dst, src[i]);
87 : 10331 : *dst += ONEWIRE_BITS_PER_BYTE;
88 : : }
89 : 2791 : return n;
90 : : }
91 : :
92 : : /**
93 : : * @brief Encode a Skip ROM prefix, advancing the cursor past it
94 : : * @param[in,out] dst Cursor into the pulse table; advanced by one byte
95 : : * @return Bytes encoded (1)
96 : : * @note Split from the Match ROM form on purpose: each call site knows its
97 : : * addressing mode statically, so there is no dead mode branch to
98 : : * carry (and no branch to leave uncovered). Skip ROM 0xCC is a 1-Wire
99 : : * protocol constant, identical for every slave.
100 : : */
101 : : __STATIC_FORCEINLINE uint8_t ow_encode_skip_rom(ow_pulse_t** dst) {
102 : 2752 : onewire_encode_byte(*dst, ONEWIRE_SKIP_ROM);
103 : 2752 : *dst += ONEWIRE_BITS_PER_BYTE;
104 : 2752 : return 1u;
105 : : }
106 : :
107 : : /**
108 : : * @brief Encode a Match ROM prefix and the 8-byte ROM, advancing the cursor
109 : : * @param[in,out] dst Cursor into the pulse table; advanced past the prefix
110 : : * @param[in] rom 8-byte ROM address (LSB first) of the addressed device
111 : : * @return Bytes encoded (1 + ONEWIRE_ROM_BYTES)
112 : : * @note Match ROM 0x55 is a 1-Wire protocol constant; the addressing policy
113 : : * (when to use Match rather than Skip) stays in the driver.
114 : : */
115 : : __STATIC_FORCEINLINE uint8_t ow_encode_match_rom(ow_pulse_t** dst, const uint8_t* rom) {
116 : 988 : onewire_encode_byte(*dst, ONEWIRE_MATCH_ROM);
117 : 988 : *dst += ONEWIRE_BITS_PER_BYTE;
118 : 988 : return (uint8_t)(1u + ow_encode_bytes(dst, rom, ONEWIRE_ROM_BYTES));
119 : : }
120 : :
121 : : #endif /* OW_GLUE_H */
|