uart.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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) 2016 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. #include <stdio.h>
  29. #include "driver/uart.h"
  30. #include "py/mpstate.h"
  31. #include "py/mphal.h"
  32. STATIC void uart_irq_handler(void *arg);
  33. void uart_init(void) {
  34. uart_isr_handle_t handle;
  35. uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
  36. uart_enable_rx_intr(UART_NUM_0);
  37. }
  38. // all code executed in ISR must be in IRAM, and any const data must be in DRAM
  39. STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
  40. volatile uart_dev_t *uart = &UART0;
  41. uart->int_clr.rxfifo_full = 1;
  42. uart->int_clr.frm_err = 1;
  43. uart->int_clr.rxfifo_tout = 1;
  44. while (uart->status.rxfifo_cnt) {
  45. uint8_t c = uart->fifo.rw_byte;
  46. if (c == mp_interrupt_char) {
  47. // inline version of mp_keyboard_interrupt();
  48. MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
  49. #if MICROPY_ENABLE_SCHEDULER
  50. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
  51. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  52. }
  53. #endif
  54. } else {
  55. // this is an inline function so will be in IRAM
  56. ringbuf_put(&stdin_ringbuf, c);
  57. }
  58. }
  59. }