Branch data Line data Source code
1 : : /* DS18B20 device-search 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 dev_roms,
4 : : * dev_count and search_user_sink, and implements the non-blocking device
5 : : * search plus the scanned-device table accessors. */
6 : : #ifndef DS18B20_DRIVER_BUILD
7 : : #error "ds18b20_search.c is an include-only driver part; compile src/ds18b20.c"
8 : : #endif
9 : :
10 : : /** @brief ROM table of the discovered devices (filled by the device search). */
11 : : static uint8_t dev_roms[DS18B20_MAX_DEVICES][DS18B20_ROM_BYTES];
12 : : /** @brief Number of devices currently stored in dev_roms. */
13 : : static uint8_t dev_count;
14 : :
15 : : /**
16 : : * @defgroup DS18B20_Search_Internal DS18B20 Device Search (via the 1-Wire layer)
17 : : * @brief Wraps the generic Search ROM (0xF0) / Alarm Search (0xEC) engine of
18 : : * the shared 1-Wire layer. The device search additionally stores every
19 : : * found ROM in the scan-mode device table; the alarm search leaves the
20 : : * table untouched so a previous scan keeps its addresses.
21 : : * @{
22 : : */
23 : :
24 : : /** @brief User sink stored for the duration of a search */
25 : : static ds18b20_search_sink_t search_user_sink;
26 : :
27 : : /**
28 : : * @brief Device-search sink: store the ROM in the scan-mode device table
29 : : * (capped at DS18B20_MAX_DEVICES), then forward to the user sink.
30 : : */
31 : 625 : static uint8_t search_store_sink(const uint8_t* rom) {
32 [ + + ]: 625 : if (dev_count < DS18B20_MAX_DEVICES) {
33 [ + + ]: 5400 : for (uint8_t i = 0; i < DS18B20_ROM_BYTES; i++) {
34 : 4800 : dev_roms[dev_count][i] = rom[i];
35 : : }
36 : 600 : dev_count++;
37 : : }
38 [ + + ]: 625 : return search_user_sink ? search_user_sink(rom) : 0;
39 : : }
40 : :
41 : : /**
42 : : * @brief Alarm-search sink: forward to the user sink without touching the
43 : : * scan-mode device table.
44 : : */
45 : 200 : static uint8_t search_alarm_sink(const uint8_t* rom) {
46 [ + + ]: 200 : return search_user_sink ? search_user_sink(rom) : 0;
47 : : }
48 : :
49 : : /**
50 : : * @brief Start a non-blocking device search
51 : : * @param[in] sink Callback invoked per found DS18B20 device (may be NULL)
52 : : * @param[in] max_devices Maximum number of devices to report (0 aborts)
53 : : * @note The device search (re)populates the scan-mode device table.
54 : : * @note Ownership guards: the search, the measurement state machine, command
55 : : * transactions and resolution changes all share TIM1/DMA, so a new search
56 : : * may only be started while all of them are idle; a running search
57 : : * rejects a new start.
58 : : */
59 : 675 : void ds18b20_search_start(ds18b20_search_sink_t sink, uint8_t max_devices) {
60 [ + + ]: 675 : if (ctx.current_state != DS18B20_ST_IDLE) {
61 : 25 : return; // a measurement cycle is in progress
62 : : }
63 [ + + ]: 650 : if (onewire_search_active()) {
64 : 25 : return; // a search is already running - keep its sink and table
65 : : }
66 [ + + ]: 625 : if ((txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
67 : 25 : return; // a command transaction is running
68 : : }
69 [ + + ]: 600 : if ((res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u) {
70 : 25 : return; // a resolution change owns the timer
71 : : }
72 : 575 : dev_count = 0;
73 : 575 : search_user_sink = sink;
74 : 575 : onewire_search_start(search_store_sink, max_devices, DS18B20_SEARCH_ROM, DS18B20_FAMILY_CODE);
75 : : }
76 : :
77 : : /**
78 : : * @brief Start a non-blocking alarm search
79 : : * @param[in] sink Callback invoked per DS18B20 currently in alarm (may be NULL)
80 : : * @param[in] max_devices Maximum number of alarmed devices to report (0 aborts)
81 : : * @note Only devices in alarm state respond to Alarm Search (0xEC). The
82 : : * scan-mode device table is left untouched.
83 : : */
84 : 400 : void ds18b20_alarm_search_start(ds18b20_search_sink_t sink, uint8_t max_devices) {
85 [ + + ]: 400 : if (ctx.current_state != DS18B20_ST_IDLE) {
86 : 25 : return; // a measurement cycle is in progress
87 : : }
88 [ + + ]: 375 : if (onewire_search_active()) {
89 : 25 : return; // a search is already running - keep its sink
90 : : }
91 [ + + ]: 350 : if ((txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
92 : 25 : return; // a command transaction is running
93 : : }
94 [ + + ]: 325 : if ((res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u) {
95 : 25 : return; // a resolution change owns the timer
96 : : }
97 : 300 : search_user_sink = sink;
98 : 300 : onewire_search_start(search_alarm_sink, max_devices, DS18B20_ALARM_SEARCH, DS18B20_FAMILY_CODE);
99 : : }
100 : :
101 : : /**
102 : : * @brief Advance the non-blocking device search by one hardware operation
103 : : * @return 1 when the search is finished, 0 while still running
104 : : */
105 : 48025 : uint8_t ds18b20_search_poll(void) { return onewire_search_poll(); }
106 : :
107 : : /**
108 : : * @brief Number of DS18B20 devices found (valid once the search finished)
109 : : * @return Count of found devices
110 : : */
111 : 550 : uint8_t ds18b20_search_count(void) { return onewire_search_count(); }
112 : :
113 : : /**
114 : : * @brief Advance the non-blocking alarm search by one hardware operation
115 : : * @return 1 when the search is finished, 0 while still running
116 : : */
117 : 19225 : uint8_t ds18b20_alarm_search_poll(void) { return onewire_search_poll(); }
118 : :
119 : : /**
120 : : * @brief Number of DS18B20 devices found in alarm (valid once finished)
121 : : * @return Count of alarmed devices
122 : : */
123 : 325 : uint8_t ds18b20_alarm_search_count(void) { return onewire_search_count(); }
124 : :
125 : : /**
126 : : * @}
127 : : */
128 : :
129 : : /**
130 : : * @brief Number of DS18B20 devices stored by the driver
131 : : * @see ds18b20_device_count() in ds18b20.h
132 : : */
133 : 125 : uint8_t ds18b20_device_count(void) { return dev_count; }
134 : :
135 : : /**
136 : : * @brief ROM address of a discovered device
137 : : * @see ds18b20_device_rom() in ds18b20.h
138 : : */
139 : 950 : const uint8_t* ds18b20_device_rom(uint8_t index) {
140 [ + + ]: 950 : if (index >= dev_count) {
141 : 100 : return 0;
142 : : }
143 : 850 : return dev_roms[index];
144 : : }
|