Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2014 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 | #include "ets_sys.h" |
| 29 | #include "etshal.h" |
| 30 | #include "uart.h" |
Josef Gajdusek | 800d5cd | 2015-05-10 17:54:09 +0200 | [diff] [blame] | 31 | #include "user_interface.h" |
Paul Sokolovsky | a099bfe | 2016-03-11 09:38:20 +0700 | [diff] [blame] | 32 | #include "ets_alt_task.h" |
Angus Gratton | 66be82d | 2023-12-07 14:07:21 +1100 | [diff] [blame] | 33 | #include "py/mphal.h" |
Damien George | 1b7d672 | 2017-02-15 17:45:36 +1100 | [diff] [blame] | 34 | #include "py/runtime.h" |
Damien George | c80614d | 2019-06-19 14:02:38 +1000 | [diff] [blame] | 35 | #include "py/stream.h" |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame] | 36 | #include "extmod/misc.h" |
Damien George | 136369d | 2021-07-09 14:19:15 +1000 | [diff] [blame] | 37 | #include "shared/runtime/pyexec.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 38 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 39 | static byte stdin_ringbuf_array[256]; |
Damien George | afd0701 | 2018-05-15 15:13:58 +1000 | [diff] [blame] | 40 | ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0}; |
Paul Sokolovsky | 402a743 | 2016-03-29 11:48:43 +0300 | [diff] [blame] | 41 | void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len); |
| 42 | const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked}; |
| 43 | |
Damien George | afd0701 | 2018-05-15 15:13:58 +1000 | [diff] [blame] | 44 | int uart_attached_to_dupterm; |
| 45 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 46 | void mp_hal_init(void) { |
stijn | 84fa331 | 2020-04-16 09:13:57 +0200 | [diff] [blame] | 47 | // ets_wdt_disable(); // it's a pain while developing |
Paul Sokolovsky | a4c8ef9 | 2016-02-08 21:43:37 +0200 | [diff] [blame] | 48 | mp_hal_rtc_init(); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 49 | uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200); |
Damien George | afd0701 | 2018-05-15 15:13:58 +1000 | [diff] [blame] | 50 | uart_attached_to_dupterm = 0; |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Damien George | f2218c2 | 2020-05-02 15:04:25 +1000 | [diff] [blame] | 53 | void MP_FASTCODE(mp_hal_delay_us)(uint32_t us) { |
Damien George | e673714 | 2016-03-23 13:01:21 +0200 | [diff] [blame] | 54 | uint32_t start = system_get_time(); |
| 55 | while (system_get_time() - start < us) { |
Angus Gratton | 7387973 | 2023-12-07 10:05:39 +1100 | [diff] [blame] | 56 | mp_event_handle_nowait(); |
Damien George | e673714 | 2016-03-23 13:01:21 +0200 | [diff] [blame] | 57 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Damien George | c80614d | 2019-06-19 14:02:38 +1000 | [diff] [blame] | 60 | uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { |
| 61 | uintptr_t ret = 0; |
| 62 | if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) { |
| 63 | ret |= MP_STREAM_POLL_RD; |
| 64 | } |
Damien George | 38e7b84 | 2023-03-22 00:14:46 +1100 | [diff] [blame] | 65 | if (poll_flags & MP_STREAM_POLL_WR) { |
Jim Mussared | 1bf2dcb | 2022-08-18 14:47:56 +1000 | [diff] [blame] | 66 | ret |= mp_os_dupterm_poll(poll_flags); |
Damien George | 38e7b84 | 2023-03-22 00:14:46 +1100 | [diff] [blame] | 67 | } |
Damien George | c80614d | 2019-06-19 14:02:38 +1000 | [diff] [blame] | 68 | return ret; |
| 69 | } |
| 70 | |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 71 | int mp_hal_stdin_rx_chr(void) { |
| 72 | for (;;) { |
Damien George | afd0701 | 2018-05-15 15:13:58 +1000 | [diff] [blame] | 73 | int c = ringbuf_get(&stdin_ringbuf); |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 74 | if (c != -1) { |
| 75 | return c; |
| 76 | } |
Paul Sokolovsky | 4b3f1d7 | 2016-09-19 00:23:38 +0300 | [diff] [blame] | 77 | #if 0 |
| 78 | // Idles CPU but need more testing before enabling |
| 79 | if (!ets_loop_iter()) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 80 | asm ("waiti 0"); |
Paul Sokolovsky | 4b3f1d7 | 2016-09-19 00:23:38 +0300 | [diff] [blame] | 81 | } |
| 82 | #else |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 83 | mp_hal_delay_us(1); |
Paul Sokolovsky | 4b3f1d7 | 2016-09-19 00:23:38 +0300 | [diff] [blame] | 84 | #endif |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 85 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Paul Sokolovsky | 402a743 | 2016-03-29 11:48:43 +0300 | [diff] [blame] | 88 | #if 0 |
| 89 | void mp_hal_debug_str(const char *str) { |
| 90 | while (*str) { |
| 91 | uart_tx_one_char(UART0, *str++); |
| 92 | } |
| 93 | uart_flush(UART0); |
| 94 | } |
| 95 | #endif |
| 96 | |
Maarten van der Schrieck | 3bca93b | 2023-06-18 11:46:25 +0200 | [diff] [blame] | 97 | mp_uint_t mp_hal_stdout_tx_strn(const char *str, uint32_t len) { |
| 98 | int dupterm_res = mp_os_dupterm_tx_strn(str, len); |
| 99 | if (dupterm_res < 0) { |
| 100 | // no outputs, nothing was written |
| 101 | return 0; |
| 102 | } else { |
| 103 | return dupterm_res; |
| 104 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Paul Sokolovsky | 402a743 | 2016-03-29 11:48:43 +0300 | [diff] [blame] | 107 | void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) { |
| 108 | (void)env; |
| 109 | while (len--) { |
| 110 | if (*str == '\n') { |
| 111 | uart_tx_one_char(UART0, '\r'); |
| 112 | } |
| 113 | uart_tx_one_char(UART0, *str++); |
| 114 | } |
| 115 | } |
| 116 | |
Damien George | f2218c2 | 2020-05-02 15:04:25 +1000 | [diff] [blame] | 117 | uint32_t MP_FASTCODE(mp_hal_ticks_ms)(void) { |
Damien George | 5357dad | 2019-05-22 15:05:03 +1000 | [diff] [blame] | 118 | // Compute milliseconds from 64-bit microsecond counter |
| 119 | system_time_update(); |
| 120 | return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_time_low_word) / 1000; |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Damien George | f2218c2 | 2020-05-02 15:04:25 +1000 | [diff] [blame] | 123 | void MP_FASTCODE(mp_hal_delay_ms)(uint32_t delay) { |
Paul Sokolovsky | ebd9f55 | 2015-10-29 13:03:44 +0300 | [diff] [blame] | 124 | mp_hal_delay_us(delay * 1000); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Damien George | ee50a6e | 2020-08-01 23:50:23 +1000 | [diff] [blame] | 127 | uint64_t mp_hal_time_ns(void) { |
Damien George | 8f20cdc | 2020-09-14 12:15:03 +1000 | [diff] [blame] | 128 | return pyb_rtc_get_us_since_epoch() * 1000ULL; |
Damien George | ee50a6e | 2020-08-01 23:50:23 +1000 | [diff] [blame] | 129 | } |
| 130 | |
Paul Sokolovsky | 8ab16b6 | 2015-12-29 20:44:55 +0200 | [diff] [blame] | 131 | void __assert_func(const char *file, int line, const char *func, const char *expr) { |
| 132 | printf("assert:%s:%d:%s: %s\n", file, line, func, expr); |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 133 | mp_raise_msg(&mp_type_AssertionError, MP_ERROR_TEXT("C-level assert")); |
Paul Sokolovsky | 8ab16b6 | 2015-12-29 20:44:55 +0200 | [diff] [blame] | 134 | } |
Paul Sokolovsky | 61fa7c8 | 2016-03-30 18:50:38 +0300 | [diff] [blame] | 135 | |
Damien George | f2218c2 | 2020-05-02 15:04:25 +1000 | [diff] [blame] | 136 | // May be called by uart0_rx_intr_handler. |
| 137 | void MP_FASTCODE(mp_hal_signal_input)(void) { |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 138 | #if MICROPY_REPL_EVENT_DRIVEN |
Paul Sokolovsky | 61fa7c8 | 2016-03-30 18:50:38 +0300 | [diff] [blame] | 139 | system_os_post(UART_TASK_ID, 0, 0); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 140 | #endif |
Paul Sokolovsky | 61fa7c8 | 2016-03-30 18:50:38 +0300 | [diff] [blame] | 141 | } |
Paul Sokolovsky | 98af891 | 2016-03-31 19:49:55 +0300 | [diff] [blame] | 142 | |
Damien George | 39d4153 | 2023-11-27 12:00:16 +1100 | [diff] [blame] | 143 | // this bit is unused in the Xtensa PS register |
| 144 | #define ETS_LOOP_ITER_BIT (12) |
| 145 | |
| 146 | uint32_t esp_disable_irq(void) { |
| 147 | uint32_t state = disable_irq(); |
| 148 | state = (state & ~(1 << ETS_LOOP_ITER_BIT)) | (ets_loop_iter_disable << ETS_LOOP_ITER_BIT); |
| 149 | ets_loop_iter_disable = 1; |
| 150 | return state; |
| 151 | } |
| 152 | |
| 153 | void esp_enable_irq(uint32_t state) { |
| 154 | ets_loop_iter_disable = (state >> ETS_LOOP_ITER_BIT) & 1; |
| 155 | enable_irq(state & ~(1 << ETS_LOOP_ITER_BIT)); |
| 156 | } |
| 157 | |
Paul Sokolovsky | 3d83041 | 2016-05-03 00:18:14 +0300 | [diff] [blame] | 158 | // Get pointer to esf_buf bookkeeping structure |
| 159 | void *ets_get_esf_buf_ctlblk(void) { |
| 160 | // Get literal ptr before start of esf_rx_buf_alloc func |
| 161 | extern void *esf_rx_buf_alloc(); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 162 | return ((void **)esf_rx_buf_alloc)[-1]; |
Paul Sokolovsky | 3d83041 | 2016-05-03 00:18:14 +0300 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Get number of esf_buf free buffers of given type, as encoded by index |
| 166 | // idx 0 corresponds to buf types 1, 2; 1 - 4; 2 - 5; 3 - 7; 4 - 8 |
| 167 | // Only following buf types appear to be used: |
| 168 | // 1 - tx buffer, 5 - management frame tx buffer; 8 - rx buffer |
| 169 | int ets_esf_free_bufs(int idx) { |
| 170 | uint32_t *p = ets_get_esf_buf_ctlblk(); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 171 | uint32_t *b = (uint32_t *)p[idx]; |
Paul Sokolovsky | 3d83041 | 2016-05-03 00:18:14 +0300 | [diff] [blame] | 172 | int cnt = 0; |
| 173 | while (b) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 174 | b = (uint32_t *)b[0x20 / 4]; |
Paul Sokolovsky | 3d83041 | 2016-05-03 00:18:14 +0300 | [diff] [blame] | 175 | cnt++; |
| 176 | } |
| 177 | return cnt; |
| 178 | } |