mphalport.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2014 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #ifndef INCLUDED_MPHALPORT_H
  29. #define INCLUDED_MPHALPORT_H
  30. #include "py/ringbuf.h"
  31. #include "lib/utils/interrupt_char.h"
  32. #include "freertos/FreeRTOS.h"
  33. #include "freertos/task.h"
  34. extern TaskHandle_t mp_main_task_handle;
  35. extern ringbuf_t stdin_ringbuf;
  36. uint32_t mp_hal_ticks_us(void);
  37. __attribute__((always_inline)) static inline uint32_t mp_hal_ticks_cpu(void) {
  38. uint32_t ccount;
  39. __asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
  40. return ccount;
  41. }
  42. void mp_hal_delay_us(uint32_t);
  43. void mp_hal_delay_us_fast(uint32_t);
  44. void mp_hal_set_interrupt_char(int c);
  45. uint32_t mp_hal_get_cpu_freq(void);
  46. #define mp_hal_quiet_timing_enter() MICROPY_BEGIN_ATOMIC_SECTION()
  47. #define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state)
  48. // Wake up the main task if it is sleeping
  49. void mp_hal_wake_main_task_from_isr(void);
  50. // C-level pin HAL
  51. #include "py/obj.h"
  52. #include "driver/gpio.h"
  53. #define MP_HAL_PIN_FMT "%u"
  54. #define mp_hal_pin_obj_t gpio_num_t
  55. mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in);
  56. #define mp_hal_get_pin_obj(o) machine_pin_get_id(o)
  57. #define mp_obj_get_pin(o) machine_pin_get_id(o) // legacy name; only to support esp8266/modonewire
  58. #define mp_hal_pin_name(p) (p)
  59. static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
  60. gpio_pad_select_gpio(pin);
  61. gpio_set_direction(pin, GPIO_MODE_INPUT);
  62. }
  63. static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
  64. gpio_pad_select_gpio(pin);
  65. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT);
  66. }
  67. static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
  68. gpio_pad_select_gpio(pin);
  69. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
  70. }
  71. static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) {
  72. gpio_set_level(pin, 0);
  73. }
  74. static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
  75. gpio_set_level(pin, 1);
  76. }
  77. static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) {
  78. return gpio_get_level(pin);
  79. }
  80. static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) {
  81. gpio_set_level(pin, v);
  82. }
  83. #endif // INCLUDED_MPHALPORT_H