|
| 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_ESP8266__ |
| 14 | +#define __OWNG_ARDUINO_ESP8266__ |
| 15 | + |
| 16 | +#include "Arduino.h" |
| 17 | +#include "OneWireNg_BitBang.h" |
| 18 | + |
| 19 | +#define __WRITE_GPIO(pin, st) \ |
| 20 | + if (pin < 16) { \ |
| 21 | + if (st) GPOS = (1 << pin); else GPOC = (1 << pin); \ |
| 22 | + } else \ |
| 23 | + if (pin == 16) { \ |
| 24 | + if (st) GP16O |= 1; else GP16O &= ~1; \ |
| 25 | + } |
| 26 | + |
| 27 | +#define __GPIO_AS_INPUT(pin) \ |
| 28 | + if (pin < 16) { \ |
| 29 | + GPEC = (1 << pin); \ |
| 30 | + } else \ |
| 31 | + if (pin == 16) { \ |
| 32 | + GP16E &= ~1; \ |
| 33 | + } |
| 34 | + |
| 35 | +#define __GPIO_AS_OUTPUT(pin) \ |
| 36 | + if (pin < 16) { \ |
| 37 | + GPES = (1 << pin); \ |
| 38 | + } else \ |
| 39 | + if (pin == 16) { \ |
| 40 | + GP16E |= 1; \ |
| 41 | + } |
| 42 | + |
| 43 | +/** |
| 44 | + * Arduino ESP8266 platform GPIO specific implementation. |
| 45 | + */ |
| 46 | +class OneWireNg_ArduinoESP8266: public OneWireNg_BitBang |
| 47 | +{ |
| 48 | +public: |
| 49 | + /** |
| 50 | + * OneWireNg 1-wire service for Arduino ESP8266 platform. |
| 51 | + * |
| 52 | + * Bus powering is supported via switching its GPIO to the high state. |
| 53 | + * In this case the GPIO servers as a voltage source for connected slaves |
| 54 | + * working in parasite powering configuration. |
| 55 | + * |
| 56 | + * @param pin Arduino GPIO pin number used for bit-banging 1-wire bus. |
| 57 | + * @param pullUp If @c true configure internal pull-up resistor for the bus. |
| 58 | + */ |
| 59 | + OneWireNg_ArduinoESP8266(unsigned pin, bool pullUp): |
| 60 | + OneWireNg_BitBang(false) |
| 61 | + { |
| 62 | + _dtaPin = pin; |
| 63 | + initDtaGpio(pullUp); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * OneWireNg 1-wire service for Arduino ESP8266 platform. |
| 68 | + * |
| 69 | + * Bus powering is supported via a switching transistor providing |
| 70 | + * the power to the bus and controlled by a dedicated GPIO (see @sa |
| 71 | + * OneWireNg_BitBang::setupPwrCtrlGpio()). In this configuration the |
| 72 | + * service mimics the open-drain type of output. The approach may be |
| 73 | + * feasible if the GPIO is unable to provide sufficient power for |
| 74 | + * connected slaves working in parasite powering configuration. |
| 75 | + * |
| 76 | + * @param pin Arduino GPIO pin number used for bit-banging 1-wire bus. |
| 77 | + * @param pwrCtrlPin Arduino GPIO pin number controlling the switching |
| 78 | + * transistor. |
| 79 | + * @param pullUp If @c true configure internal pull-up resistor for the bus. |
| 80 | + */ |
| 81 | + OneWireNg_ArduinoESP8266(unsigned pin, unsigned pwrCtrlPin, bool pullUp): |
| 82 | + OneWireNg_BitBang(true) |
| 83 | + { |
| 84 | + _dtaPin = pin; |
| 85 | + _pwrCtrlPin = pwrCtrlPin; |
| 86 | + |
| 87 | + initDtaGpio(pullUp); |
| 88 | + initPwrCtrlGpio(); |
| 89 | + } |
| 90 | + |
| 91 | +protected: |
| 92 | + virtual int readGpioIn(GpioType gpio) |
| 93 | + { |
| 94 | + UNUSED(gpio); |
| 95 | + return (_dtaPin < 16 ? GPIP(_dtaPin) : |
| 96 | + _dtaPin == 16 ? (GP16I & 0x01) : 1); |
| 97 | + } |
| 98 | + |
| 99 | + virtual void writeGpioOut(GpioType gpio, int state) |
| 100 | + { |
| 101 | + if (gpio == GPIO_DTA) { |
| 102 | + __WRITE_GPIO(_dtaPin, state); |
| 103 | + } else { |
| 104 | + __WRITE_GPIO(_pwrCtrlPin, state); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + virtual void setGpioAsInput(GpioType gpio) |
| 109 | + { |
| 110 | + UNUSED(gpio); |
| 111 | + __GPIO_AS_INPUT(_dtaPin); |
| 112 | + } |
| 113 | + |
| 114 | + virtual void setGpioAsOutput(GpioType gpio, int state) |
| 115 | + { |
| 116 | + if (gpio == GPIO_DTA) { |
| 117 | + __GPIO_AS_OUTPUT(_dtaPin); |
| 118 | + __WRITE_GPIO(_dtaPin, state); |
| 119 | + } else { |
| 120 | + __GPIO_AS_OUTPUT(_pwrCtrlPin); |
| 121 | + __WRITE_GPIO(_pwrCtrlPin, state); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void initDtaGpio(bool pullUp) |
| 126 | + { |
| 127 | + pinMode(_dtaPin, (pullUp ? INPUT_PULLUP : INPUT)); |
| 128 | + setupDtaGpio(); |
| 129 | + } |
| 130 | + |
| 131 | + void initPwrCtrlGpio(void) |
| 132 | + { |
| 133 | + pinMode(_pwrCtrlPin, OUTPUT_OPEN_DRAIN); |
| 134 | + setupPwrCtrlGpio(true); |
| 135 | + } |
| 136 | + |
| 137 | +private: |
| 138 | + unsigned _dtaPin; |
| 139 | + unsigned _pwrCtrlPin; |
| 140 | +}; |
| 141 | + |
| 142 | +#undef __GPIO_AS_OUTPUT |
| 143 | +#undef __GPIO_AS_INPUT |
| 144 | +#undef __WRITE_GPIO |
| 145 | + |
| 146 | +#endif /* __OWNG_ARDUINO_ESP8266__ */ |
0 commit comments