blob: 2f813ca22f0de7755c86e6a8644aa253336543ca [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001/*
2 * This file is part of the Micro Python project, http://micropython.org/
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"
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"
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +020034#include "py/obj.h"
35#include "py/mpstate.h"
Paul Sokolovskyc9618892016-03-29 11:13:32 +030036#include "extmod/misc.h"
Paul Sokolovsky98af8912016-03-31 19:49:55 +030037#include "lib/utils/pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000038
39extern void ets_wdt_disable(void);
40extern void wdt_feed(void);
41extern void ets_delay_us();
42
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +030043STATIC byte input_buf_array[256];
44ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
Paul Sokolovsky402a7432016-03-29 11:48:43 +030045void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
46const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
47
Damien George075d5972014-11-27 20:30:33 +000048void mp_hal_init(void) {
49 ets_wdt_disable(); // it's a pain while developing
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020050 mp_hal_rtc_init();
Damien George075d5972014-11-27 20:30:33 +000051 uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
52}
53
54void mp_hal_feed_watchdog(void) {
55 //ets_wdt_disable(); // it's a pain while developing
56 //WRITE_PERI_REG(0x60000914, 0x73);
57 //wdt_feed(); // might also work
58}
59
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030060void mp_hal_delay_us(uint32_t us) {
Damien Georgee6737142016-03-23 13:01:21 +020061 uint32_t start = system_get_time();
62 while (system_get_time() - start < us) {
63 ets_event_poll();
64 }
Damien George075d5972014-11-27 20:30:33 +000065}
66
Damien George0b32e502015-02-13 15:04:53 +000067int mp_hal_stdin_rx_chr(void) {
68 for (;;) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030069 int c = ringbuf_get(&input_buf);
Damien George0b32e502015-02-13 15:04:53 +000070 if (c != -1) {
71 return c;
72 }
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030073 mp_hal_delay_us(1);
Damien George0b32e502015-02-13 15:04:53 +000074 mp_hal_feed_watchdog();
75 }
Damien George075d5972014-11-27 20:30:33 +000076}
77
Paul Sokolovskyc9618892016-03-29 11:13:32 +030078void mp_hal_stdout_tx_char(char c) {
79 uart_tx_one_char(UART0, c);
80 mp_uos_dupterm_tx_strn(&c, 1);
81}
82
Paul Sokolovsky402a7432016-03-29 11:48:43 +030083#if 0
84void mp_hal_debug_str(const char *str) {
85 while (*str) {
86 uart_tx_one_char(UART0, *str++);
87 }
88 uart_flush(UART0);
89}
90#endif
91
Damien George0b32e502015-02-13 15:04:53 +000092void mp_hal_stdout_tx_str(const char *str) {
Damien George075d5972014-11-27 20:30:33 +000093 while (*str) {
Paul Sokolovskyc9618892016-03-29 11:13:32 +030094 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +000095 }
96}
97
Damien George0b32e502015-02-13 15:04:53 +000098void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +000099 while (len--) {
Paul Sokolovskyc9618892016-03-29 11:13:32 +0300100 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +0000101 }
102}
103
Damien George0b32e502015-02-13 15:04:53 +0000104void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +0000105 while (len--) {
106 if (*str == '\n') {
Paul Sokolovskyc9618892016-03-29 11:13:32 +0300107 mp_hal_stdout_tx_char('\r');
Damien George075d5972014-11-27 20:30:33 +0000108 }
Paul Sokolovskyc9618892016-03-29 11:13:32 +0300109 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +0000110 }
111}
112
Paul Sokolovsky402a7432016-03-29 11:48:43 +0300113void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
114 (void)env;
115 while (len--) {
116 if (*str == '\n') {
117 uart_tx_one_char(UART0, '\r');
118 }
119 uart_tx_one_char(UART0, *str++);
120 }
121}
122
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +0300123uint32_t mp_hal_ticks_ms(void) {
Josef Gajdusek800d5cd2015-05-10 17:54:09 +0200124 return system_get_time() / 1000;
Damien George075d5972014-11-27 20:30:33 +0000125}
126
Damien Georgeb41a14a2015-12-28 17:27:44 +0000127uint32_t mp_hal_ticks_us(void) {
128 return system_get_time();
129}
130
Paul Sokolovskyebd9f552015-10-29 13:03:44 +0300131void mp_hal_delay_ms(uint32_t delay) {
132 mp_hal_delay_us(delay * 1000);
Damien George075d5972014-11-27 20:30:33 +0000133}
134
135void mp_hal_set_interrupt_char(int c) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200136 if (c != -1) {
137 mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
138 }
139 extern int interrupt_char;
140 interrupt_char = c;
Damien George075d5972014-11-27 20:30:33 +0000141}
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200142
Paul Sokolovskya099bfe2016-03-11 09:38:20 +0700143void ets_event_poll(void) {
144 ets_loop_iter();
145 if (MP_STATE_VM(mp_pending_exception) != NULL) {
146 mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
147 MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
148 nlr_raise(obj);
149 }
150}
151
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200152void __assert_func(const char *file, int line, const char *func, const char *expr) {
153 printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
154 nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError,
155 "C-level assert"));
156}
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300157
158void mp_hal_signal_input(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300159 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300160 system_os_post(UART_TASK_ID, 0, 0);
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300161 #endif
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300162}
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300163
164static int call_dupterm_read(void) {
165 if (MP_STATE_PORT(term_obj) == NULL) {
166 return -1;
167 }
168
169 nlr_buf_t nlr;
170 if (nlr_push(&nlr) == 0) {
171 mp_obj_t read_m[3];
172 mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m);
173 read_m[2] = MP_OBJ_NEW_SMALL_INT(1);
174 mp_obj_t res = mp_call_method_n_kw(1, 0, read_m);
175 if (res == mp_const_none) {
176 return -2;
177 }
178 mp_buffer_info_t bufinfo;
179 mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
180 if (bufinfo.len == 0) {
181 mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n");
182 MP_STATE_PORT(term_obj) = NULL;
183 return -1;
184 }
185 nlr_pop();
186 return *(byte*)bufinfo.buf;
187 } else {
188 // Temporarily disable dupterm to avoid infinite recursion
189 mp_obj_t save_term = MP_STATE_PORT(term_obj);
190 MP_STATE_PORT(term_obj) = NULL;
191 mp_printf(&mp_plat_print, "dupterm: ");
192 mp_obj_print_exception(&mp_plat_print, nlr.ret_val);
193 MP_STATE_PORT(term_obj) = save_term;
194 }
195
196 return -1;
197}
198
199STATIC void dupterm_task_handler(os_event_t *evt) {
200 while (1) {
201 int c = call_dupterm_read();
202 if (c < 0) {
203 break;
204 }
205 ringbuf_put(&input_buf, c);
206 }
207 mp_hal_signal_input();
208}
209
210STATIC os_event_t dupterm_evt_queue[4];
211
212void dupterm_task_init() {
213 system_os_task(dupterm_task_handler, DUPTERM_TASK_ID, dupterm_evt_queue, MP_ARRAY_SIZE(dupterm_evt_queue));
214}
215
216void mp_hal_signal_dupterm_input(void) {
217 system_os_post(DUPTERM_TASK_ID, 0, 0);
218}