|
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" |
|
41 |
|
42 #include "owb.h" |
|
43 #include "owb_gpio.h" |
|
44 |
|
45 static const char * TAG = "owb_gpio"; |
|
46 |
|
47 // Define PHY_DEBUG to enable GPIO output around when the bus is sampled |
|
48 // by the master (this library). This GPIO output makes it possible to |
|
49 // validate the master's sampling using an oscilloscope. |
|
50 // |
|
51 // For the debug GPIO the idle state is low and made high before the 1-wire sample |
|
52 // point and low again after the sample point |
|
53 #undef PHY_DEBUG |
|
54 |
|
55 #ifdef PHY_DEBUG |
|
56 // Update these defines to a pin that you can access |
|
57 #define PHY_DEBUG_GPIO GPIO_NUM_27 |
|
58 #define PHY_DEBUG_GPIO_MASK GPIO_SEL_27 |
|
59 #endif |
|
60 |
|
61 /// @cond ignore |
|
62 struct _OneWireBus_Timing |
|
63 { |
|
64 uint32_t A, B, C, D, E, F, G, H, I, J; |
|
65 }; |
|
66 //// @endcond |
|
67 |
|
68 // 1-Wire timing delays (standard) in microseconds. |
|
69 // Labels and values are from https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 |
|
70 static const struct _OneWireBus_Timing _StandardTiming = { |
|
71 6, // A - read/write "1" master pull DQ low duration |
|
72 64, // B - write "0" master pull DQ low duration |
|
73 60, // C - write "1" master pull DQ high duration |
|
74 10, // D - write "0" master pull DQ high duration |
|
75 9, // E - read master pull DQ high duration |
|
76 55, // F - complete read timeslot + 10ms recovery |
|
77 0, // G - wait before reset |
|
78 480, // H - master pull DQ low duration |
|
79 70, // I - master pull DQ high duration |
|
80 410, // J - complete presence timeslot + recovery |
|
81 }; |
|
82 |
|
83 static void _us_delay(uint32_t time_us) |
|
84 { |
|
85 ets_delay_us(time_us); |
|
86 } |
|
87 |
|
88 #define info_from_bus(owb) container_of(owb, owb_gpio_driver_info, bus) |
|
89 |
|
90 /** |
|
91 * @brief Generate a 1-Wire reset (initialization). |
|
92 * @param[in] bus Initialised bus instance. |
|
93 * @param[out] is_present true if device is present, otherwise false. |
|
94 * @return status |
|
95 */ |
|
96 static owb_status _reset(const OneWireBus * bus, bool * is_present) |
|
97 { |
|
98 bool present = false; |
|
99 portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; |
|
100 portENTER_CRITICAL(&timeCriticalMutex); |
|
101 |
|
102 owb_gpio_driver_info *i = info_from_bus(bus); |
|
103 |
|
104 gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); |
|
105 _us_delay(bus->timing->G); |
|
106 gpio_set_level(i->gpio, 0); // Drive DQ low |
|
107 _us_delay(bus->timing->H); |
|
108 gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus |
|
109 gpio_set_level(i->gpio, 1); // Reset the output level for the next output |
|
110 _us_delay(bus->timing->I); |
|
111 |
|
112 #ifdef PHY_DEBUG |
|
113 gpio_set_level(PHY_DEBUG_GPIO, 1); |
|
114 #endif |
|
115 |
|
116 int level1 = gpio_get_level(i->gpio); |
|
117 |
|
118 #ifdef PHY_DEBUG |
|
119 gpio_set_level(PHY_DEBUG_GPIO, 0); |
|
120 #endif |
|
121 |
|
122 _us_delay(bus->timing->J); // Complete the reset sequence recovery |
|
123 |
|
124 #ifdef PHY_DEBUG |
|
125 gpio_set_level(PHY_DEBUG_GPIO, 1); |
|
126 #endif |
|
127 |
|
128 int level2 = gpio_get_level(i->gpio); |
|
129 |
|
130 #ifdef PHY_DEBUG |
|
131 gpio_set_level(PHY_DEBUG_GPIO, 0); |
|
132 #endif |
|
133 |
|
134 portEXIT_CRITICAL(&timeCriticalMutex); |
|
135 |
|
136 present = (level1 == 0) && (level2 == 1); // Sample for presence pulse from slave |
|
137 ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present); |
|
138 |
|
139 *is_present = present; |
|
140 |
|
141 return OWB_STATUS_OK; |
|
142 } |
|
143 |
|
144 /** |
|
145 * @brief Send a 1-Wire write bit, with recovery time. |
|
146 * @param[in] bus Initialised bus instance. |
|
147 * @param[in] bit The value to send. |
|
148 */ |
|
149 static void _write_bit(const OneWireBus * bus, int bit) |
|
150 { |
|
151 int delay1 = bit ? bus->timing->A : bus->timing->C; |
|
152 int delay2 = bit ? bus->timing->B : bus->timing->D; |
|
153 owb_gpio_driver_info *i = info_from_bus(bus); |
|
154 |
|
155 portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; |
|
156 portENTER_CRITICAL(&timeCriticalMutex); |
|
157 |
|
158 gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); |
|
159 gpio_set_level(i->gpio, 0); // Drive DQ low |
|
160 _us_delay(delay1); |
|
161 gpio_set_level(i->gpio, 1); // Release the bus |
|
162 _us_delay(delay2); |
|
163 |
|
164 portEXIT_CRITICAL(&timeCriticalMutex); |
|
165 } |
|
166 |
|
167 /** |
|
168 * @brief Read a bit from the 1-Wire bus and return the value, with recovery time. |
|
169 * @param[in] bus Initialised bus instance. |
|
170 */ |
|
171 static int _read_bit(const OneWireBus * bus) |
|
172 { |
|
173 int result = 0; |
|
174 owb_gpio_driver_info *i = info_from_bus(bus); |
|
175 |
|
176 portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED; |
|
177 portENTER_CRITICAL(&timeCriticalMutex); |
|
178 |
|
179 gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT); |
|
180 gpio_set_level(i->gpio, 0); // Drive DQ low |
|
181 _us_delay(bus->timing->A); |
|
182 gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus |
|
183 gpio_set_level(i->gpio, 1); // Reset the output level for the next output |
|
184 _us_delay(bus->timing->E); |
|
185 |
|
186 #ifdef PHY_DEBUG |
|
187 gpio_set_level(PHY_DEBUG_GPIO, 1); |
|
188 #endif |
|
189 |
|
190 int level = gpio_get_level(i->gpio); |
|
191 |
|
192 #ifdef PHY_DEBUG |
|
193 gpio_set_level(PHY_DEBUG_GPIO, 0); |
|
194 #endif |
|
195 |
|
196 _us_delay(bus->timing->F); // Complete the timeslot and 10us recovery |
|
197 |
|
198 portEXIT_CRITICAL(&timeCriticalMutex); |
|
199 |
|
200 result = level & 0x01; |
|
201 |
|
202 return result; |
|
203 } |
|
204 |
|
205 /** |
|
206 * @brief Write 1-Wire data byte. |
|
207 * NOTE: The data is shifted out of the low bits, eg. it is written in the order of lsb to msb |
|
208 * @param[in] bus Initialised bus instance. |
|
209 * @param[in] data Value to write. |
|
210 * @param[in] number_of_bits_to_read bits to write |
|
211 */ |
|
212 static owb_status _write_bits(const OneWireBus * bus, uint8_t data, int number_of_bits_to_write) |
|
213 { |
|
214 ESP_LOGD(TAG, "write 0x%02x", data); |
|
215 for (int i = 0; i < number_of_bits_to_write; ++i) |
|
216 { |
|
217 _write_bit(bus, data & 0x01); |
|
218 data >>= 1; |
|
219 } |
|
220 |
|
221 return OWB_STATUS_OK; |
|
222 } |
|
223 |
|
224 /** |
|
225 * @brief Read 1-Wire data byte from bus. |
|
226 * NOTE: Data is read into the high bits, eg. each bit read is shifted down before the next bit is read |
|
227 * @param[in] bus Initialised bus instance. |
|
228 * @return Byte value read from bus. |
|
229 */ |
|
230 static owb_status _read_bits(const OneWireBus * bus, uint8_t *out, int number_of_bits_to_read) |
|
231 { |
|
232 uint8_t result = 0; |
|
233 for (int i = 0; i < number_of_bits_to_read; ++i) |
|
234 { |
|
235 result >>= 1; |
|
236 if (_read_bit(bus)) |
|
237 { |
|
238 result |= 0x80; |
|
239 } |
|
240 } |
|
241 ESP_LOGD(TAG, "read 0x%02x", result); |
|
242 *out = result; |
|
243 |
|
244 return OWB_STATUS_OK; |
|
245 } |
|
246 |
|
247 static owb_status _uninitialize(const OneWireBus * bus) |
|
248 { |
|
249 // Nothing to do here for this driver_info |
|
250 return OWB_STATUS_OK; |
|
251 } |
|
252 |
|
253 static const struct owb_driver gpio_function_table = |
|
254 { |
|
255 .name = "owb_gpio", |
|
256 .uninitialize = _uninitialize, |
|
257 .reset = _reset, |
|
258 .write_bits = _write_bits, |
|
259 .read_bits = _read_bits |
|
260 }; |
|
261 |
|
262 OneWireBus* owb_gpio_initialize(owb_gpio_driver_info *driver_info, int gpio) |
|
263 { |
|
264 ESP_LOGI(TAG, "%s(): gpio %d\n", __func__, gpio); |
|
265 |
|
266 driver_info->gpio = gpio; |
|
267 driver_info->bus.driver = &gpio_function_table; |
|
268 driver_info->bus.timing = &_StandardTiming; |
|
269 |
|
270 // platform specific: |
|
271 gpio_pad_select_gpio(driver_info->gpio); |
|
272 |
|
273 #ifdef PHY_DEBUG |
|
274 gpio_config_t io_conf; |
|
275 io_conf.intr_type = GPIO_INTR_DISABLE; |
|
276 io_conf.mode = GPIO_MODE_OUTPUT; |
|
277 io_conf.pin_bit_mask = PHY_DEBUG_GPIO_MASK; |
|
278 io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE; |
|
279 io_conf.pull_up_en = GPIO_PULLUP_DISABLE; |
|
280 ESP_ERROR_CHECK(gpio_config(&io_conf)); |
|
281 #endif |
|
282 |
|
283 return &(driver_info->bus); |
|
284 } |