esponewire.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "py/mphal.h"
  27. #include "esp8266/esponewire.h"
  28. #define TIMING_RESET1 (0)
  29. #define TIMING_RESET2 (1)
  30. #define TIMING_RESET3 (2)
  31. #define TIMING_READ1 (3)
  32. #define TIMING_READ2 (4)
  33. #define TIMING_READ3 (5)
  34. #define TIMING_WRITE1 (6)
  35. #define TIMING_WRITE2 (7)
  36. #define TIMING_WRITE3 (8)
  37. uint16_t esp_onewire_timings[9] = {480, 40, 420, 5, 5, 40, 10, 50, 10};
  38. #define DELAY_US mp_hal_delay_us_fast
  39. int esp_onewire_reset(mp_hal_pin_obj_t pin) {
  40. mp_hal_pin_write(pin, 0);
  41. DELAY_US(esp_onewire_timings[TIMING_RESET1]);
  42. uint32_t i = MICROPY_BEGIN_ATOMIC_SECTION();
  43. mp_hal_pin_write(pin, 1);
  44. DELAY_US(esp_onewire_timings[TIMING_RESET2]);
  45. int status = !mp_hal_pin_read(pin);
  46. MICROPY_END_ATOMIC_SECTION(i);
  47. DELAY_US(esp_onewire_timings[TIMING_RESET3]);
  48. return status;
  49. }
  50. int esp_onewire_readbit(mp_hal_pin_obj_t pin) {
  51. mp_hal_pin_write(pin, 1);
  52. uint32_t i = MICROPY_BEGIN_ATOMIC_SECTION();
  53. mp_hal_pin_write(pin, 0);
  54. DELAY_US(esp_onewire_timings[TIMING_READ1]);
  55. mp_hal_pin_write(pin, 1);
  56. DELAY_US(esp_onewire_timings[TIMING_READ2]);
  57. int value = mp_hal_pin_read(pin);
  58. MICROPY_END_ATOMIC_SECTION(i);
  59. DELAY_US(esp_onewire_timings[TIMING_READ3]);
  60. return value;
  61. }
  62. void esp_onewire_writebit(mp_hal_pin_obj_t pin, int value) {
  63. uint32_t i = MICROPY_BEGIN_ATOMIC_SECTION();
  64. mp_hal_pin_write(pin, 0);
  65. DELAY_US(esp_onewire_timings[TIMING_WRITE1]);
  66. if (value) {
  67. mp_hal_pin_write(pin, 1);
  68. }
  69. DELAY_US(esp_onewire_timings[TIMING_WRITE2]);
  70. mp_hal_pin_write(pin, 1);
  71. DELAY_US(esp_onewire_timings[TIMING_WRITE3]);
  72. MICROPY_END_ATOMIC_SECTION(i);
  73. }