dht.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <stdio.h>
  27. #include "py/runtime.h"
  28. #include "py/mperrno.h"
  29. #include "py/mphal.h"
  30. #include "extmod/machine_pulse.h"
  31. #include "drivers/dht/dht.h"
  32. STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
  33. mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in);
  34. mp_hal_pin_open_drain(pin);
  35. mp_buffer_info_t bufinfo;
  36. mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
  37. if (bufinfo.len < 5) {
  38. mp_raise_ValueError("buffer too small");
  39. }
  40. // issue start command
  41. mp_hal_pin_od_high(pin);
  42. mp_hal_delay_ms(250);
  43. mp_hal_pin_od_low(pin);
  44. mp_hal_delay_ms(18);
  45. mp_uint_t irq_state = mp_hal_quiet_timing_enter();
  46. // release the line so the device can respond
  47. mp_hal_pin_od_high(pin);
  48. mp_hal_delay_us_fast(10);
  49. // wait for device to respond
  50. mp_uint_t ticks = mp_hal_ticks_us();
  51. while (mp_hal_pin_read(pin) != 0) {
  52. if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
  53. goto timeout;
  54. }
  55. }
  56. // time pulse, should be 80us
  57. ticks = machine_time_pulse_us(pin, 1, 150);
  58. if ((mp_int_t)ticks < 0) {
  59. goto timeout;
  60. }
  61. // time 40 pulses for data (either 26us or 70us)
  62. uint8_t *buf = bufinfo.buf;
  63. for (int i = 0; i < 40; ++i) {
  64. ticks = machine_time_pulse_us(pin, 1, 100);
  65. if ((mp_int_t)ticks < 0) {
  66. goto timeout;
  67. }
  68. buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48);
  69. }
  70. mp_hal_quiet_timing_exit(irq_state);
  71. return mp_const_none;
  72. timeout:
  73. mp_hal_quiet_timing_exit(irq_state);
  74. mp_raise_OSError(MP_ETIMEDOUT);
  75. }
  76. MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto);