|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 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 <stdio.h> |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "py/mperrno.h" |
| 31 | +#include "py/mphal.h" |
| 32 | +#include "drivers/dht/dht.h" |
| 33 | + |
| 34 | +STATIC mp_uint_t time_pulse_us(mp_hal_pin_obj_t pin, int pulse_value, mp_uint_t timeout) { |
| 35 | + mp_uint_t start = mp_hal_ticks_us(); |
| 36 | + while (mp_hal_pin_read(pin) != pulse_value) { |
| 37 | + if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) { |
| 38 | + return (mp_uint_t)-1; |
| 39 | + } |
| 40 | + } |
| 41 | + start = mp_hal_ticks_us(); |
| 42 | + while (mp_hal_pin_read(pin) == pulse_value) { |
| 43 | + if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) { |
| 44 | + return (mp_uint_t)-1; |
| 45 | + } |
| 46 | + } |
| 47 | + return mp_hal_ticks_us() - start; |
| 48 | +} |
| 49 | + |
| 50 | +STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { |
| 51 | + mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in); |
| 52 | + mp_hal_pin_open_drain(pin); |
| 53 | + |
| 54 | + mp_buffer_info_t bufinfo; |
| 55 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); |
| 56 | + |
| 57 | + if (bufinfo.len < 5) { |
| 58 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small")); |
| 59 | + } |
| 60 | + |
| 61 | + // issue start command |
| 62 | + mp_hal_pin_od_high(pin); |
| 63 | + mp_hal_delay_ms(250); |
| 64 | + mp_hal_pin_od_low(pin); |
| 65 | + mp_hal_delay_ms(18); |
| 66 | + |
| 67 | + mp_uint_t irq_state = mp_hal_quiet_timing_enter(); |
| 68 | + |
| 69 | + // release the line so the device can respond |
| 70 | + mp_hal_pin_od_high(pin); |
| 71 | + mp_hal_delay_us_fast(10); |
| 72 | + |
| 73 | + // wait for device to respond |
| 74 | + mp_uint_t ticks = mp_hal_ticks_us(); |
| 75 | + while (mp_hal_pin_read(pin) != 0) { |
| 76 | + if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) { |
| 77 | + goto timeout; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // time pulse, should be 80us |
| 82 | + ticks = time_pulse_us(pin, 1, 150); |
| 83 | + if (ticks == (mp_uint_t)-1) { |
| 84 | + goto timeout; |
| 85 | + } |
| 86 | + |
| 87 | + // time 40 pulses for data (either 26us or 70us) |
| 88 | + uint8_t *buf = bufinfo.buf; |
| 89 | + for (int i = 0; i < 40; ++i) { |
| 90 | + ticks = time_pulse_us(pin, 1, 100); |
| 91 | + if (ticks == (mp_uint_t)-1) { |
| 92 | + goto timeout; |
| 93 | + } |
| 94 | + buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48); |
| 95 | + } |
| 96 | + |
| 97 | + mp_hal_quiet_timing_exit(irq_state); |
| 98 | + return mp_const_none; |
| 99 | + |
| 100 | +timeout: |
| 101 | + mp_hal_quiet_timing_exit(irq_state); |
| 102 | + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT))); |
| 103 | +} |
| 104 | +MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto); |
0 commit comments