Mon, 24 Jun 2024 17:05:20 +0200
Start migration to IDF 5.1
0 | 1 | /* |
2 | * MIT License | |
3 | * | |
4 | * Copyright (c) 2017 David Antliff | |
5 | * Copyright (c) 2017 Chris Morgan <chmorgan@gmail.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in all | |
15 | * copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
26 | /** | |
27 | * @file | |
28 | */ | |
29 | ||
30 | #include <stddef.h> | |
31 | #include <stdbool.h> | |
32 | #include <inttypes.h> | |
33 | #include <string.h> | |
34 | #include <stdlib.h> | |
35 | ||
36 | #include "freertos/FreeRTOS.h" | |
37 | #include "freertos/task.h" | |
38 | #include "esp_log.h" | |
39 | #include "sdkconfig.h" | |
40 | #include "driver/gpio.h" | |
129 | 41 | #include "rom/ets_sys.h" // for ets_delay_us() |
42 | #include "rom/gpio.h" // for gpio_pad_select_gpio() | |
0 | 43 | |
44 | #include "owb.h" | |
45 | #include "owb_gpio.h" | |
46 | ||
47 | static const char * TAG = "owb_gpio"; | |
48 | ||
49 | // Define PHY_DEBUG to enable GPIO output around when the bus is sampled | |
50 | // by the master (this library). This GPIO output makes it possible to | |
51 | // validate the master's sampling using an oscilloscope. | |
52 | // | |
53 | // For the debug GPIO the idle state is low and made high before the 1-wire sample | |
54 | // point and low again after the sample point | |
55 | #undef PHY_DEBUG | |
56 | ||
57 | #ifdef PHY_DEBUG | |
58 | // Update these defines to a pin that you can access | |
59 | #define PHY_DEBUG_GPIO GPIO_NUM_27 | |
60 | #define PHY_DEBUG_GPIO_MASK GPIO_SEL_27 | |
61 | #endif | |
62 | ||
63 | /// @cond ignore | |
64 | struct _OneWireBus_Timing | |
65 | { | |
66 | uint32_t A, B, C, D, E, F, G, H, I, J; | |
67 | }; | |
129 | 68 | /// @endcond |
0 | 69 | |
70 | // 1-Wire timing delays (standard) in microseconds. | |
71 | // Labels and values are from https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 | |
72 | static const struct _OneWireBus_Timing _StandardTiming = { | |
73 | 6, // A - read/write "1" master pull DQ low duration | |
74 | 64, // B - write "0" master pull DQ low duration | |
75 | 60, // C - write "1" master pull DQ high duration | |
76 | 10, // D - write "0" master pull DQ high duration | |
77 | 9, // E - read master pull DQ high duration | |
78 | 55, // F - complete read timeslot + 10ms recovery | |
79 | 0, // G - wait before reset | |
80 | 480, // H - master pull DQ low duration | |
81 | 70, // I - master pull DQ high duration | |
82 | 410, // J - complete presence timeslot + recovery | |
83 | }; | |
84 | ||
85 | static void _us_delay(uint32_t time_us) | |
86 | { | |
87 | ets_delay_us(time_us); | |
88 | } | |
89 | ||
129 | 90 | /// @cond ignore |
0 | 91 | #define info_from_bus(owb) container_of(owb, owb_gpio_driver_info, bus) |
129 | 92 | /// @endcond |
0 | 93 | |
94 | /** | |
95 | * @brief Generate a 1-Wire reset (initialization). | |
96 | * @param[in] bus Initialised bus instance. | |
97 | * @param[out] is_present true if device is present, otherwise false. | |
98 | * @return status | |
99 | */ | |
100 | static owb_status _reset(const OneWireBus * bus, bool * is_present) | |
101 | { | |
102 | bool present = false; | |
103 | portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; | |
104 | portENTER_CRITICAL(&timeCriticalMutex); | |
105 | ||
106 | owb_gpio_driver_info *i = info_from_bus(bus); | |
107 | ||
108 | gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); | |
109 | _us_delay(bus->timing->G); | |
110 | gpio_set_level(i->gpio, 0); // Drive DQ low | |
111 | _us_delay(bus->timing->H); | |
112 | gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus | |
113 | gpio_set_level(i->gpio, 1); // Reset the output level for the next output | |
114 | _us_delay(bus->timing->I); | |
115 | ||
116 | #ifdef PHY_DEBUG | |
117 | gpio_set_level(PHY_DEBUG_GPIO, 1); | |
118 | #endif | |
119 | ||
120 | int level1 = gpio_get_level(i->gpio); | |
121 | ||
122 | #ifdef PHY_DEBUG | |
123 | gpio_set_level(PHY_DEBUG_GPIO, 0); | |
124 | #endif | |
125 | ||
126 | _us_delay(bus->timing->J); // Complete the reset sequence recovery | |
127 | ||
128 | #ifdef PHY_DEBUG | |
129 | gpio_set_level(PHY_DEBUG_GPIO, 1); | |
130 | #endif | |
131 | ||
132 | int level2 = gpio_get_level(i->gpio); | |
133 | ||
134 | #ifdef PHY_DEBUG | |
135 | gpio_set_level(PHY_DEBUG_GPIO, 0); | |
136 | #endif | |
137 | ||
138 | portEXIT_CRITICAL(&timeCriticalMutex); | |
139 | ||
140 | present = (level1 == 0) && (level2 == 1); // Sample for presence pulse from slave | |
141 | ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present); | |
142 | ||
143 | *is_present = present; | |
144 | ||
145 | return OWB_STATUS_OK; | |
146 | } | |
147 | ||
148 | /** | |
149 | * @brief Send a 1-Wire write bit, with recovery time. | |
150 | * @param[in] bus Initialised bus instance. | |
151 | * @param[in] bit The value to send. | |
152 | */ | |
153 | static void _write_bit(const OneWireBus * bus, int bit) | |
154 | { | |
155 | int delay1 = bit ? bus->timing->A : bus->timing->C; | |
156 | int delay2 = bit ? bus->timing->B : bus->timing->D; | |
157 | owb_gpio_driver_info *i = info_from_bus(bus); | |
158 | ||
159 | portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; | |
160 | portENTER_CRITICAL(&timeCriticalMutex); | |
161 | ||
162 | gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); | |
163 | gpio_set_level(i->gpio, 0); // Drive DQ low | |
164 | _us_delay(delay1); | |
165 | gpio_set_level(i->gpio, 1); // Release the bus | |
166 | _us_delay(delay2); | |
167 | ||
168 | portEXIT_CRITICAL(&timeCriticalMutex); | |
169 | } | |
170 | ||
171 | /** | |
172 | * @brief Read a bit from the 1-Wire bus and return the value, with recovery time. | |
173 | * @param[in] bus Initialised bus instance. | |
174 | */ | |
175 | static int _read_bit(const OneWireBus * bus) | |
176 | { | |
177 | int result = 0; | |
178 | owb_gpio_driver_info *i = info_from_bus(bus); | |
179 | ||
180 | portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; | |
181 | portENTER_CRITICAL(&timeCriticalMutex); | |
182 | ||
183 | gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); | |
184 | gpio_set_level(i->gpio, 0); // Drive DQ low | |
185 | _us_delay(bus->timing->A); | |
186 | gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus | |
187 | gpio_set_level(i->gpio, 1); // Reset the output level for the next output | |
188 | _us_delay(bus->timing->E); | |
189 | ||
190 | #ifdef PHY_DEBUG | |
191 | gpio_set_level(PHY_DEBUG_GPIO, 1); | |
192 | #endif | |
193 | ||
194 | int level = gpio_get_level(i->gpio); | |
195 | ||
196 | #ifdef PHY_DEBUG | |
197 | gpio_set_level(PHY_DEBUG_GPIO, 0); | |
198 | #endif | |
199 | ||
200 | _us_delay(bus->timing->F); // Complete the timeslot and 10us recovery | |
201 | ||
202 | portEXIT_CRITICAL(&timeCriticalMutex); | |
203 | ||
204 | result = level & 0x01; | |
205 | ||
206 | return result; | |
207 | } | |
208 | ||
209 | /** | |
210 | * @brief Write 1-Wire data byte. | |
211 | * NOTE: The data is shifted out of the low bits, eg. it is written in the order of lsb to msb | |
212 | * @param[in] bus Initialised bus instance. | |
213 | * @param[in] data Value to write. | |
214 | * @param[in] number_of_bits_to_read bits to write | |
215 | */ | |
216 | static owb_status _write_bits(const OneWireBus * bus, uint8_t data, int number_of_bits_to_write) | |
217 | { | |
218 | ESP_LOGD(TAG, "write 0x%02x", data); | |
219 | for (int i = 0; i < number_of_bits_to_write; ++i) | |
220 | { | |
221 | _write_bit(bus, data & 0x01); | |
222 | data >>= 1; | |
223 | } | |
224 | ||
225 | return OWB_STATUS_OK; | |
226 | } | |
227 | ||
228 | /** | |
229 | * @brief Read 1-Wire data byte from bus. | |
230 | * NOTE: Data is read into the high bits, eg. each bit read is shifted down before the next bit is read | |
231 | * @param[in] bus Initialised bus instance. | |
232 | * @return Byte value read from bus. | |
233 | */ | |
234 | static owb_status _read_bits(const OneWireBus * bus, uint8_t *out, int number_of_bits_to_read) | |
235 | { | |
236 | uint8_t result = 0; | |
237 | for (int i = 0; i < number_of_bits_to_read; ++i) | |
238 | { | |
239 | result >>= 1; | |
240 | if (_read_bit(bus)) | |
241 | { | |
242 | result |= 0x80; | |
243 | } | |
244 | } | |
245 | ESP_LOGD(TAG, "read 0x%02x", result); | |
246 | *out = result; | |
247 | ||
248 | return OWB_STATUS_OK; | |
249 | } | |
250 | ||
251 | static owb_status _uninitialize(const OneWireBus * bus) | |
252 | { | |
253 | // Nothing to do here for this driver_info | |
254 | return OWB_STATUS_OK; | |
255 | } | |
256 | ||
257 | static const struct owb_driver gpio_function_table = | |
258 | { | |
259 | .name = "owb_gpio", | |
260 | .uninitialize = _uninitialize, | |
261 | .reset = _reset, | |
262 | .write_bits = _write_bits, | |
263 | .read_bits = _read_bits | |
264 | }; | |
265 | ||
129 | 266 | OneWireBus* owb_gpio_initialize(owb_gpio_driver_info * driver_info, int gpio) |
0 | 267 | { |
129 | 268 | ESP_LOGD(TAG, "%s(): gpio %d\n", __func__, gpio); |
0 | 269 | |
270 | driver_info->gpio = gpio; | |
271 | driver_info->bus.driver = &gpio_function_table; | |
272 | driver_info->bus.timing = &_StandardTiming; | |
129 | 273 | driver_info->bus.strong_pullup_gpio = GPIO_NUM_NC; |
0 | 274 | |
275 | // platform specific: | |
276 | gpio_pad_select_gpio(driver_info->gpio); | |
277 | ||
278 | #ifdef PHY_DEBUG | |
279 | gpio_config_t io_conf; | |
280 | io_conf.intr_type = GPIO_INTR_DISABLE; | |
281 | io_conf.mode = GPIO_MODE_OUTPUT; | |
282 | io_conf.pin_bit_mask = PHY_DEBUG_GPIO_MASK; | |
283 | io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE; | |
284 | io_conf.pull_up_en = GPIO_PULLUP_DISABLE; | |
285 | ESP_ERROR_CHECK(gpio_config(&io_conf)); | |
286 | #endif | |
287 | ||
288 | return &(driver_info->bus); | |
289 | } |