esp32-warm-water
esp32 based project for the control of a heating element based on temperature
owb_gpio.c
Go to the documentation of this file.
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 
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 = CONFIG_OWB_GPIO_TAG;
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 
62 struct _OneWireBus_Timing
63 {
64  uint32_t A, B, C, D, E, F, G, H, I, J;
65 };
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 
89 #define info_from_bus(owb) container_of(owb, owb_gpio_driver_info, bus)
91 
98 static owb_status _reset(const OneWireBus *bus, bool *is_present)
99 {
100  bool present = false;
101  portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
102  portENTER_CRITICAL(&timeCriticalMutex);
103 
104  owb_gpio_driver_info *i = info_from_bus(bus);
105 
106  gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT);
107  _us_delay(bus->timing->G);
108  gpio_set_level(i->gpio, 0); // Drive DQ low
109  _us_delay(bus->timing->H);
110  gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus
111  gpio_set_level(i->gpio, 1); // Reset the output level for the next output
112  _us_delay(bus->timing->I);
113 
114 #ifdef PHY_DEBUG
115  gpio_set_level(PHY_DEBUG_GPIO, 1);
116 #endif
117 
118  int level1 = gpio_get_level(i->gpio);
119 
120 #ifdef PHY_DEBUG
121  gpio_set_level(PHY_DEBUG_GPIO, 0);
122 #endif
123 
124  _us_delay(bus->timing->J); // Complete the reset sequence recovery
125 
126 #ifdef PHY_DEBUG
127  gpio_set_level(PHY_DEBUG_GPIO, 1);
128 #endif
129 
130  int level2 = gpio_get_level(i->gpio);
131 
132 #ifdef PHY_DEBUG
133  gpio_set_level(PHY_DEBUG_GPIO, 0);
134 #endif
135 
136  portEXIT_CRITICAL(&timeCriticalMutex);
137 
138  present = (level1 == 0) && (level2 == 1); // Sample for presence pulse from slave
139  ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present);
140 
141  *is_present = present;
142 
143  return OWB_STATUS_OK;
144 }
145 
151 static void _write_bit(const OneWireBus *bus, int bit)
152 {
153  int delay1 = bit ? bus->timing->A : bus->timing->C;
154  int delay2 = bit ? bus->timing->B : bus->timing->D;
155  owb_gpio_driver_info *i = info_from_bus(bus);
156 
157  portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
158  portENTER_CRITICAL(&timeCriticalMutex);
159 
160  gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT);
161  gpio_set_level(i->gpio, 0); // Drive DQ low
162  _us_delay(delay1);
163  gpio_set_level(i->gpio, 1); // Release the bus
164  _us_delay(delay2);
165 
166  portEXIT_CRITICAL(&timeCriticalMutex);
167 }
168 
173 static int _read_bit(const OneWireBus *bus)
174 {
175  int result = 0;
176  owb_gpio_driver_info *i = info_from_bus(bus);
177 
178  portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
179  portENTER_CRITICAL(&timeCriticalMutex);
180 
181  gpio_set_direction(i->gpio, GPIO_MODE_OUTPUT);
182  gpio_set_level(i->gpio, 0); // Drive DQ low
183  _us_delay(bus->timing->A);
184  gpio_set_direction(i->gpio, GPIO_MODE_INPUT); // Release the bus
185  gpio_set_level(i->gpio, 1); // Reset the output level for the next output
186  _us_delay(bus->timing->E);
187 
188 #ifdef PHY_DEBUG
189  gpio_set_level(PHY_DEBUG_GPIO, 1);
190 #endif
191 
192  int level = gpio_get_level(i->gpio);
193 
194 #ifdef PHY_DEBUG
195  gpio_set_level(PHY_DEBUG_GPIO, 0);
196 #endif
197 
198  _us_delay(bus->timing->F); // Complete the timeslot and 10us recovery
199 
200  portEXIT_CRITICAL(&timeCriticalMutex);
201 
202  result = level & 0x01;
203 
204  return result;
205 }
206 
214 static owb_status _write_bits(const OneWireBus *bus, uint8_t data, int number_of_bits_to_write)
215 {
216  ESP_LOGD(TAG, "write 0x%02x", data);
217  for (int i = 0; i < number_of_bits_to_write; ++i)
218  {
219  _write_bit(bus, data & 0x01);
220  data >>= 1;
221  }
222 
223  return OWB_STATUS_OK;
224 }
225 
232 static owb_status _read_bits(const OneWireBus *bus, uint8_t *out, int number_of_bits_to_read)
233 {
234  uint8_t result = 0;
235  for (int i = 0; i < number_of_bits_to_read; ++i)
236  {
237  result >>= 1;
238  if (_read_bit(bus))
239  {
240  result |= 0x80;
241  }
242  }
243  ESP_LOGD(TAG, "read 0x%02x", result);
244  *out = result;
245 
246  return OWB_STATUS_OK;
247 }
248 
249 static owb_status _uninitialize(const OneWireBus *bus)
250 {
251  // Nothing to do here for this driver_info
252  return OWB_STATUS_OK;
253 }
254 
255 static const struct owb_driver gpio_function_table =
256  {
257  .name = "owb_gpio",
258  .uninitialize = _uninitialize,
259  .reset = _reset,
260  .write_bits = _write_bits,
261  .read_bits = _read_bits};
262 
264 {
265  ESP_LOGD(TAG, "%s(): gpio %d", __func__, gpio);
266 
267  driver_info->gpio = gpio;
268  driver_info->bus.driver = &gpio_function_table;
269  driver_info->bus.timing = &_StandardTiming;
270  driver_info->bus.strong_pullup_gpio = GPIO_NUM_NC;
271 
272  // platform specific:
273  gpio_pad_select_gpio(driver_info->gpio);
274 
275 #ifdef PHY_DEBUG
276  gpio_config_t io_conf;
277  io_conf.intr_type = GPIO_INTR_DISABLE;
278  io_conf.mode = GPIO_MODE_OUTPUT;
279  io_conf.pin_bit_mask = PHY_DEBUG_GPIO_MASK;
280  io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
281  io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
282  ESP_ERROR_CHECK(gpio_config(&io_conf));
283 #endif
284 
285  return &(driver_info->bus);
286 }
Interface definitions for the 1-Wire bus component.
owb_status
Represents the result of OWB API functions.
Definition: owb.h:114
@ OWB_STATUS_OK
Operation succeeded.
Definition: owb.h:116
#define GPIO_NUM_NC
ESP-IDF prior to v4.x does not define GPIO_NUM_NC.
Definition: owb.h:60
OneWireBus * owb_gpio_initialize(owb_gpio_driver_info *driver_info, int gpio)
Initialise the GPIO driver.
Definition: owb_gpio.c:263
Interface definitions for the ESP32 GPIO driver used to communicate with devices on the One Wire Bus.
Structure containing 1-Wire bus information relevant to a single instance.
Definition: owb.h:69
const struct _OneWireBus_Timing * timing
Pointer to timing information.
Definition: owb.h:70
gpio_num_t strong_pullup_gpio
Set if an external strong pull-up circuit is required.
Definition: owb.h:73
const struct owb_driver * driver
Pointer to hardware driver instance.
Definition: owb.h:74
const char * name
Definition: owb.h:129
GPIO driver information.
Definition: owb_gpio.h:50
OneWireBus bus
OneWireBus instance.
Definition: owb_gpio.h:52
int gpio
Value of the GPIO connected to the 1-Wire bus.
Definition: owb_gpio.h:51
const char * TAG
Definition: wifi_sta.c:42