blob: 75588f73b72b77b25c539520b86edb8afe13ca24 [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George075d5972014-11-27 20:30:33 +00003 *
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 Gajdusek800d5cd2015-05-10 17:54:09 +020031#include "user_interface.h"
Paul Sokolovskya099bfe2016-03-11 09:38:20 +070032#include "ets_alt_task.h"
Angus Gratton66be82d2023-12-07 14:07:21 +110033#include "py/mphal.h"
Damien George1b7d6722017-02-15 17:45:36 +110034#include "py/runtime.h"
Damien Georgec80614d2019-06-19 14:02:38 +100035#include "py/stream.h"
Paul Sokolovskyc9618892016-03-29 11:13:32 +030036#include "extmod/misc.h"
Damien George136369d2021-07-09 14:19:15 +100037#include "shared/runtime/pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000038
Angus Grattondecf8e62024-02-27 15:32:29 +110039static byte stdin_ringbuf_array[256];
Damien Georgeafd07012018-05-15 15:13:58 +100040ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
Paul Sokolovsky402a7432016-03-29 11:48:43 +030041void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
42const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
43
Damien Georgeafd07012018-05-15 15:13:58 +100044int uart_attached_to_dupterm;
45
Damien George075d5972014-11-27 20:30:33 +000046void mp_hal_init(void) {
stijn84fa3312020-04-16 09:13:57 +020047 // ets_wdt_disable(); // it's a pain while developing
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020048 mp_hal_rtc_init();
Damien George075d5972014-11-27 20:30:33 +000049 uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
Damien Georgeafd07012018-05-15 15:13:58 +100050 uart_attached_to_dupterm = 0;
Damien George075d5972014-11-27 20:30:33 +000051}
52
Damien Georgef2218c22020-05-02 15:04:25 +100053void MP_FASTCODE(mp_hal_delay_us)(uint32_t us) {
Damien Georgee6737142016-03-23 13:01:21 +020054 uint32_t start = system_get_time();
55 while (system_get_time() - start < us) {
Angus Gratton73879732023-12-07 10:05:39 +110056 mp_event_handle_nowait();
Damien Georgee6737142016-03-23 13:01:21 +020057 }
Damien George075d5972014-11-27 20:30:33 +000058}
59
Damien Georgec80614d2019-06-19 14:02:38 +100060uintptr_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 George38e7b842023-03-22 00:14:46 +110065 if (poll_flags & MP_STREAM_POLL_WR) {
Jim Mussared1bf2dcb2022-08-18 14:47:56 +100066 ret |= mp_os_dupterm_poll(poll_flags);
Damien George38e7b842023-03-22 00:14:46 +110067 }
Damien Georgec80614d2019-06-19 14:02:38 +100068 return ret;
69}
70
Damien George0b32e502015-02-13 15:04:53 +000071int mp_hal_stdin_rx_chr(void) {
72 for (;;) {
Damien Georgeafd07012018-05-15 15:13:58 +100073 int c = ringbuf_get(&stdin_ringbuf);
Damien George0b32e502015-02-13 15:04:53 +000074 if (c != -1) {
75 return c;
76 }
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030077 #if 0
78 // Idles CPU but need more testing before enabling
79 if (!ets_loop_iter()) {
Damien George69661f32020-02-27 15:36:53 +110080 asm ("waiti 0");
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030081 }
82 #else
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030083 mp_hal_delay_us(1);
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030084 #endif
Damien George0b32e502015-02-13 15:04:53 +000085 }
Damien George075d5972014-11-27 20:30:33 +000086}
87
Paul Sokolovsky402a7432016-03-29 11:48:43 +030088#if 0
89void 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 Schrieck3bca93b2023-06-18 11:46:25 +020097mp_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 George075d5972014-11-27 20:30:33 +0000105}
106
Paul Sokolovsky402a7432016-03-29 11:48:43 +0300107void 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 Georgef2218c22020-05-02 15:04:25 +1000117uint32_t MP_FASTCODE(mp_hal_ticks_ms)(void) {
Damien George5357dad2019-05-22 15:05:03 +1000118 // 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 George075d5972014-11-27 20:30:33 +0000121}
122
Damien Georgef2218c22020-05-02 15:04:25 +1000123void MP_FASTCODE(mp_hal_delay_ms)(uint32_t delay) {
Paul Sokolovskyebd9f552015-10-29 13:03:44 +0300124 mp_hal_delay_us(delay * 1000);
Damien George075d5972014-11-27 20:30:33 +0000125}
126
Damien Georgeee50a6e2020-08-01 23:50:23 +1000127uint64_t mp_hal_time_ns(void) {
Damien George8f20cdc2020-09-14 12:15:03 +1000128 return pyb_rtc_get_us_since_epoch() * 1000ULL;
Damien Georgeee50a6e2020-08-01 23:50:23 +1000129}
130
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200131void __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 Mussareddef76fe2020-03-02 22:35:22 +1100133 mp_raise_msg(&mp_type_AssertionError, MP_ERROR_TEXT("C-level assert"));
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200134}
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300135
Damien Georgef2218c22020-05-02 15:04:25 +1000136// May be called by uart0_rx_intr_handler.
137void MP_FASTCODE(mp_hal_signal_input)(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300138 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300139 system_os_post(UART_TASK_ID, 0, 0);
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300140 #endif
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300141}
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300142
Damien George39d41532023-11-27 12:00:16 +1100143// this bit is unused in the Xtensa PS register
144#define ETS_LOOP_ITER_BIT (12)
145
146uint32_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
153void 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 Sokolovsky3d830412016-05-03 00:18:14 +0300158// Get pointer to esf_buf bookkeeping structure
159void *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 George69661f32020-02-27 15:36:53 +1100162 return ((void **)esf_rx_buf_alloc)[-1];
Paul Sokolovsky3d830412016-05-03 00:18:14 +0300163}
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
169int ets_esf_free_bufs(int idx) {
170 uint32_t *p = ets_get_esf_buf_ctlblk();
Damien George69661f32020-02-27 15:36:53 +1100171 uint32_t *b = (uint32_t *)p[idx];
Paul Sokolovsky3d830412016-05-03 00:18:14 +0300172 int cnt = 0;
173 while (b) {
Damien George69661f32020-02-27 15:36:53 +1100174 b = (uint32_t *)b[0x20 / 4];
Paul Sokolovsky3d830412016-05-03 00:18:14 +0300175 cnt++;
176 }
177 return cnt;
178}