|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Piotr Stolarz |
| 3 | + * OneWireNg: Ardiono 1-wire service library |
| 4 | + * |
| 5 | + * Distributed under the 2-clause BSD License (the License) |
| 6 | + * see accompanying file LICENSE for details. |
| 7 | + * |
| 8 | + * This software is distributed WITHOUT ANY WARRANTY; without even the |
| 9 | + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | + * See the License for more information. |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef __OWNG_ARDUINO_ESP32__ |
| 14 | +#define __OWNG_ARDUINO_ESP32__ |
| 15 | + |
| 16 | +#include <assert.h> |
| 17 | +#include "Arduino.h" |
| 18 | +#include "OneWireNg_BitBang.h" |
| 19 | + |
| 20 | +#define __READ_GPIO(pin) \ |
| 21 | + (pin < 32 ? ((GPIO.in >> pin) & 0x01) : \ |
| 22 | + pin < 40 ? ((GPIO.in1.val >> (pin - 32)) & 0x01) : 1) |
| 23 | + |
| 24 | +#define __WRITE_GPIO(pin, st) \ |
| 25 | + if (pin < 32) { \ |
| 26 | + if (st) GPIO.out_w1ts = ((uint32_t)1 << pin); \ |
| 27 | + else GPIO.out_w1tc = ((uint32_t)1 << pin); \ |
| 28 | + } else \ |
| 29 | + if (pin < 34) { \ |
| 30 | + if (st) GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32)); \ |
| 31 | + else GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32)); \ |
| 32 | + } |
| 33 | + |
| 34 | +#define __GPIO_AS_INPUT(pin) \ |
| 35 | + if(pin < 32) { \ |
| 36 | + GPIO.enable_w1tc = ((uint32_t)1 << pin); \ |
| 37 | + } else \ |
| 38 | + if (pin < 40) { \ |
| 39 | + GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32)); \ |
| 40 | + } |
| 41 | + |
| 42 | +#define __GPIO_AS_OUTPUT(pin) \ |
| 43 | + if (pin < 32) { \ |
| 44 | + GPIO.enable_w1ts = ((uint32_t)1 << pin); \ |
| 45 | + } else \ |
| 46 | + if (pin < 34) { \ |
| 47 | + GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32)); \ |
| 48 | + } |
| 49 | + |
| 50 | +/** |
| 51 | + * Arduino ESP32 platform GPIO specific implementation. |
| 52 | + */ |
| 53 | +class OneWireNg_ArduinoESP32: public OneWireNg_BitBang |
| 54 | +{ |
| 55 | +public: |
| 56 | + /** |
| 57 | + * OneWireNg 1-wire service for Arduino ESP32 platform. |
| 58 | + * |
| 59 | + * Bus powering is supported via switching its GPIO to the high state. |
| 60 | + * In this case the GPIO servers as a voltage source for connected slaves |
| 61 | + * working in parasite powering configuration. |
| 62 | + * |
| 63 | + * @param pin Arduino GPIO pin number used for bit-banging 1-wire bus. |
| 64 | + * @param pullUp If @c true configure internal pull-up resistor for the bus. |
| 65 | + */ |
| 66 | + OneWireNg_ArduinoESP32(unsigned pin, bool pullUp): |
| 67 | + OneWireNg_BitBang(false) |
| 68 | + { |
| 69 | + /* pins above 33 can only be inputs */ |
| 70 | + assert(pin < 34); |
| 71 | + |
| 72 | + _dtaPin = pin; |
| 73 | + initDtaGpio(pullUp); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * OneWireNg 1-wire service for Arduino ESP32 platform. |
| 78 | + * |
| 79 | + * Bus powering is supported via a switching transistor providing |
| 80 | + * the power to the bus and controlled by a dedicated GPIO (see @sa |
| 81 | + * OneWireNg_BitBang::setupPwrCtrlGpio()). In this configuration the |
| 82 | + * service mimics the open-drain type of output. The approach may be |
| 83 | + * feasible if the GPIO is unable to provide sufficient power for |
| 84 | + * connected slaves working in parasite powering configuration. |
| 85 | + * |
| 86 | + * @param pin Arduino GPIO pin number used for bit-banging 1-wire bus. |
| 87 | + * @param pwrCtrlPin Arduino GPIO pin number controlling the switching |
| 88 | + * transistor. |
| 89 | + * @param pullUp If @c true configure internal pull-up resistor for the bus. |
| 90 | + */ |
| 91 | + OneWireNg_ArduinoESP32(unsigned pin, unsigned pwrCtrlPin, bool pullUp): |
| 92 | + OneWireNg_BitBang(true) |
| 93 | + { |
| 94 | + /* pins above 33 can only be inputs */ |
| 95 | + assert(pin < 40 && pwrCtrlPin < 34); |
| 96 | + |
| 97 | + _dtaPin = pin; |
| 98 | + _pwrCtrlPin = pwrCtrlPin; |
| 99 | + |
| 100 | + initDtaGpio(pullUp); |
| 101 | + initPwrCtrlGpio(); |
| 102 | + } |
| 103 | + |
| 104 | +protected: |
| 105 | + virtual int readGpioIn(GpioType gpio) |
| 106 | + { |
| 107 | + UNUSED(gpio); |
| 108 | + return __READ_GPIO(_dtaPin); |
| 109 | + } |
| 110 | + |
| 111 | + virtual void writeGpioOut(GpioType gpio, int state) |
| 112 | + { |
| 113 | + if (gpio == GPIO_DTA) { |
| 114 | + __WRITE_GPIO(_dtaPin, state); |
| 115 | + } else { |
| 116 | + __WRITE_GPIO(_pwrCtrlPin, state); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + virtual void setGpioAsInput(GpioType gpio) |
| 121 | + { |
| 122 | + UNUSED(gpio); |
| 123 | + __GPIO_AS_INPUT(_dtaPin); |
| 124 | + } |
| 125 | + |
| 126 | + virtual void setGpioAsOutput(GpioType gpio, int state) |
| 127 | + { |
| 128 | + if (gpio == GPIO_DTA) { |
| 129 | + __GPIO_AS_OUTPUT(_dtaPin); |
| 130 | + __WRITE_GPIO(_dtaPin, state); |
| 131 | + } else { |
| 132 | + __GPIO_AS_OUTPUT(_pwrCtrlPin); |
| 133 | + __WRITE_GPIO(_pwrCtrlPin, state); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + void initDtaGpio(bool pullUp) |
| 138 | + { |
| 139 | + pinMode(_dtaPin, INPUT | (pullUp ? PULLUP : 0)); |
| 140 | + setupDtaGpio(); |
| 141 | + } |
| 142 | + |
| 143 | + void initPwrCtrlGpio(void) |
| 144 | + { |
| 145 | + pinMode(_pwrCtrlPin, OUTPUT); |
| 146 | + setupPwrCtrlGpio(true); |
| 147 | + } |
| 148 | + |
| 149 | +private: |
| 150 | + unsigned _dtaPin; |
| 151 | + unsigned _pwrCtrlPin; |
| 152 | +}; |
| 153 | + |
| 154 | +#undef __GPIO_AS_OUTPUT |
| 155 | +#undef __GPIO_AS_INPUT |
| 156 | +#undef __WRITE_GPIO |
| 157 | +#undef __READ_GPIO |
| 158 | + |
| 159 | +#endif /* __OWNG_ARDUINO_ESP32__ */ |
0 commit comments