|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015-2017 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/mphal.h" |
| 28 | +#include "esp8266/esponewire.h" |
| 29 | + |
| 30 | +#define TIMING_RESET1 (0) |
| 31 | +#define TIMING_RESET2 (1) |
| 32 | +#define TIMING_RESET3 (2) |
| 33 | +#define TIMING_READ1 (3) |
| 34 | +#define TIMING_READ2 (4) |
| 35 | +#define TIMING_READ3 (5) |
| 36 | +#define TIMING_WRITE1 (6) |
| 37 | +#define TIMING_WRITE2 (7) |
| 38 | +#define TIMING_WRITE3 (8) |
| 39 | + |
| 40 | +uint16_t esp_onewire_timings[9] = {480, 40, 420, 5, 5, 40, 10, 50, 10}; |
| 41 | + |
| 42 | +#define DELAY_US mp_hal_delay_us_fast |
| 43 | + |
| 44 | +int esp_onewire_reset(mp_hal_pin_obj_t pin) { |
| 45 | + mp_hal_pin_write(pin, 0); |
| 46 | + DELAY_US(esp_onewire_timings[TIMING_RESET1]); |
| 47 | + uint32_t i = disable_irq(); |
| 48 | + mp_hal_pin_write(pin, 1); |
| 49 | + DELAY_US(esp_onewire_timings[TIMING_RESET2]); |
| 50 | + int status = !mp_hal_pin_read(pin); |
| 51 | + enable_irq(i); |
| 52 | + DELAY_US(esp_onewire_timings[TIMING_RESET3]); |
| 53 | + return status; |
| 54 | +} |
| 55 | + |
| 56 | +int esp_onewire_readbit(mp_hal_pin_obj_t pin) { |
| 57 | + mp_hal_pin_write(pin, 1); |
| 58 | + uint32_t i = disable_irq(); |
| 59 | + mp_hal_pin_write(pin, 0); |
| 60 | + DELAY_US(esp_onewire_timings[TIMING_READ1]); |
| 61 | + mp_hal_pin_write(pin, 1); |
| 62 | + DELAY_US(esp_onewire_timings[TIMING_READ2]); |
| 63 | + int value = mp_hal_pin_read(pin); |
| 64 | + enable_irq(i); |
| 65 | + DELAY_US(esp_onewire_timings[TIMING_READ3]); |
| 66 | + return value; |
| 67 | +} |
| 68 | + |
| 69 | +void esp_onewire_writebit(mp_hal_pin_obj_t pin, int value) { |
| 70 | + uint32_t i = disable_irq(); |
| 71 | + mp_hal_pin_write(pin, 0); |
| 72 | + DELAY_US(esp_onewire_timings[TIMING_WRITE1]); |
| 73 | + if (value) { |
| 74 | + mp_hal_pin_write(pin, 1); |
| 75 | + } |
| 76 | + DELAY_US(esp_onewire_timings[TIMING_WRITE2]); |
| 77 | + mp_hal_pin_write(pin, 1); |
| 78 | + DELAY_US(esp_onewire_timings[TIMING_WRITE3]); |
| 79 | + enable_irq(i); |
| 80 | +} |
0 commit comments