blob: 351bf522c82954074d111ee619b76c476aab6750 [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"
31#include "esp_mphal.h"
Josef Gajdusek800d5cd2015-05-10 17:54:09 +020032#include "user_interface.h"
Paul Sokolovskya099bfe2016-03-11 09:38:20 +070033#include "ets_alt_task.h"
Damien George1b7d6722017-02-15 17:45:36 +110034#include "py/runtime.h"
Paul Sokolovskyc9618892016-03-29 11:13:32 +030035#include "extmod/misc.h"
Paul Sokolovsky98af8912016-03-31 19:49:55 +030036#include "lib/utils/pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000037
Damien Georgeafd07012018-05-15 15:13:58 +100038STATIC byte stdin_ringbuf_array[256];
39ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
Paul Sokolovsky402a7432016-03-29 11:48:43 +030040void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
41const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
42
Damien Georgeafd07012018-05-15 15:13:58 +100043int uart_attached_to_dupterm;
44
Damien George075d5972014-11-27 20:30:33 +000045void mp_hal_init(void) {
Paul Sokolovskyf2a21a22016-08-20 16:33:04 +030046 //ets_wdt_disable(); // it's a pain while developing
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020047 mp_hal_rtc_init();
Damien George075d5972014-11-27 20:30:33 +000048 uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
Damien Georgeafd07012018-05-15 15:13:58 +100049 uart_attached_to_dupterm = 0;
Damien George075d5972014-11-27 20:30:33 +000050}
51
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030052void mp_hal_delay_us(uint32_t us) {
Damien Georgee6737142016-03-23 13:01:21 +020053 uint32_t start = system_get_time();
54 while (system_get_time() - start < us) {
55 ets_event_poll();
56 }
Damien George075d5972014-11-27 20:30:33 +000057}
58
Damien George0b32e502015-02-13 15:04:53 +000059int mp_hal_stdin_rx_chr(void) {
60 for (;;) {
Damien Georgeafd07012018-05-15 15:13:58 +100061 int c = ringbuf_get(&stdin_ringbuf);
Damien George0b32e502015-02-13 15:04:53 +000062 if (c != -1) {
63 return c;
64 }
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030065 #if 0
66 // Idles CPU but need more testing before enabling
67 if (!ets_loop_iter()) {
68 asm("waiti 0");
69 }
70 #else
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030071 mp_hal_delay_us(1);
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030072 #endif
Damien George0b32e502015-02-13 15:04:53 +000073 }
Damien George075d5972014-11-27 20:30:33 +000074}
75
Paul Sokolovsky402a7432016-03-29 11:48:43 +030076#if 0
77void mp_hal_debug_str(const char *str) {
78 while (*str) {
79 uart_tx_one_char(UART0, *str++);
80 }
81 uart_flush(UART0);
82}
83#endif
84
Damien George0b32e502015-02-13 15:04:53 +000085void mp_hal_stdout_tx_str(const char *str) {
Damien Georgeafd07012018-05-15 15:13:58 +100086 mp_uos_dupterm_tx_strn(str, strlen(str));
Damien George075d5972014-11-27 20:30:33 +000087}
88
Damien George0b32e502015-02-13 15:04:53 +000089void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
Damien Georgeafd07012018-05-15 15:13:58 +100090 mp_uos_dupterm_tx_strn(str, len);
Damien George075d5972014-11-27 20:30:33 +000091}
92
Damien George0b32e502015-02-13 15:04:53 +000093void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
Damien Georgeb00040c2017-10-03 19:36:49 +110094 const char *last = str;
Damien George075d5972014-11-27 20:30:33 +000095 while (len--) {
96 if (*str == '\n') {
Damien Georgeb00040c2017-10-03 19:36:49 +110097 if (str > last) {
98 mp_uos_dupterm_tx_strn(last, str - last);
99 }
Damien Georgeb00040c2017-10-03 19:36:49 +1100100 mp_uos_dupterm_tx_strn("\r\n", 2);
101 ++str;
102 last = str;
103 } else {
Damien Georgeafd07012018-05-15 15:13:58 +1000104 ++str;
Damien George075d5972014-11-27 20:30:33 +0000105 }
Damien Georgeb00040c2017-10-03 19:36:49 +1100106 }
107 if (str > last) {
108 mp_uos_dupterm_tx_strn(last, str - last);
Damien George075d5972014-11-27 20:30:33 +0000109 }
110}
111
Paul Sokolovsky402a7432016-03-29 11:48:43 +0300112void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
113 (void)env;
114 while (len--) {
115 if (*str == '\n') {
116 uart_tx_one_char(UART0, '\r');
117 }
118 uart_tx_one_char(UART0, *str++);
119 }
120}
121
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +0300122uint32_t mp_hal_ticks_ms(void) {
Damien George5357dad2019-05-22 15:05:03 +1000123 // Compute milliseconds from 64-bit microsecond counter
124 system_time_update();
125 return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_time_low_word) / 1000;
Damien George075d5972014-11-27 20:30:33 +0000126}
127
Damien Georgeb41a14a2015-12-28 17:27:44 +0000128uint32_t mp_hal_ticks_us(void) {
129 return system_get_time();
130}
131
Paul Sokolovskyebd9f552015-10-29 13:03:44 +0300132void mp_hal_delay_ms(uint32_t delay) {
133 mp_hal_delay_us(delay * 1000);
Damien George075d5972014-11-27 20:30:33 +0000134}
135
Paul Sokolovskya099bfe2016-03-11 09:38:20 +0700136void ets_event_poll(void) {
137 ets_loop_iter();
Damien George1b7d6722017-02-15 17:45:36 +1100138 mp_handle_pending();
Paul Sokolovskya099bfe2016-03-11 09:38:20 +0700139}
140
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200141void __assert_func(const char *file, int line, const char *func, const char *expr) {
142 printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
143 nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError,
144 "C-level assert"));
145}
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300146
147void mp_hal_signal_input(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300148 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300149 system_os_post(UART_TASK_ID, 0, 0);
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300150 #endif
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300151}
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300152
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300153STATIC void dupterm_task_handler(os_event_t *evt) {
Paul Sokolovsky6f3b9932016-04-17 18:11:04 +0300154 static byte lock;
155 if (lock) {
156 return;
157 }
158 lock = 1;
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300159 while (1) {
Damien George37282f82017-10-13 20:01:57 +1100160 int c = mp_uos_dupterm_rx_chr();
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300161 if (c < 0) {
162 break;
163 }
Damien Georgeafd07012018-05-15 15:13:58 +1000164 ringbuf_put(&stdin_ringbuf, c);
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300165 }
166 mp_hal_signal_input();
Paul Sokolovsky6f3b9932016-04-17 18:11:04 +0300167 lock = 0;
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300168}
169
170STATIC os_event_t dupterm_evt_queue[4];
171
172void dupterm_task_init() {
173 system_os_task(dupterm_task_handler, DUPTERM_TASK_ID, dupterm_evt_queue, MP_ARRAY_SIZE(dupterm_evt_queue));
174}
175
176void mp_hal_signal_dupterm_input(void) {
177 system_os_post(DUPTERM_TASK_ID, 0, 0);
178}
Damien Georgea2d5d842016-04-22 10:04:12 +0100179
Paul Sokolovsky3d830412016-05-03 00:18:14 +0300180// Get pointer to esf_buf bookkeeping structure
181void *ets_get_esf_buf_ctlblk(void) {
182 // Get literal ptr before start of esf_rx_buf_alloc func
183 extern void *esf_rx_buf_alloc();
184 return ((void**)esf_rx_buf_alloc)[-1];
185}
186
187// Get number of esf_buf free buffers of given type, as encoded by index
188// idx 0 corresponds to buf types 1, 2; 1 - 4; 2 - 5; 3 - 7; 4 - 8
189// Only following buf types appear to be used:
190// 1 - tx buffer, 5 - management frame tx buffer; 8 - rx buffer
191int ets_esf_free_bufs(int idx) {
192 uint32_t *p = ets_get_esf_buf_ctlblk();
193 uint32_t *b = (uint32_t*)p[idx];
194 int cnt = 0;
195 while (b) {
196 b = (uint32_t*)b[0x20 / 4];
197 cnt++;
198 }
199 return cnt;
200}
Paul Sokolovsky64ad8382016-07-31 02:30:05 +0300201
202extern int mp_stream_errno;
203int *__errno() {
204 return &mp_stream_errno;
205}