blob: 7c9590ab678d818d02aa868b33e2acd8a5391236 [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
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +030039STATIC byte input_buf_array[256];
40ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
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 George075d5972014-11-27 20:30:33 +000044void mp_hal_init(void) {
Paul Sokolovskyf2a21a22016-08-20 16:33:04 +030045 //ets_wdt_disable(); // it's a pain while developing
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020046 mp_hal_rtc_init();
Damien George075d5972014-11-27 20:30:33 +000047 uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
48}
49
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030050void mp_hal_delay_us(uint32_t us) {
Damien Georgee6737142016-03-23 13:01:21 +020051 uint32_t start = system_get_time();
52 while (system_get_time() - start < us) {
53 ets_event_poll();
54 }
Damien George075d5972014-11-27 20:30:33 +000055}
56
Damien George0b32e502015-02-13 15:04:53 +000057int mp_hal_stdin_rx_chr(void) {
58 for (;;) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030059 int c = ringbuf_get(&input_buf);
Damien George0b32e502015-02-13 15:04:53 +000060 if (c != -1) {
61 return c;
62 }
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030063 #if 0
64 // Idles CPU but need more testing before enabling
65 if (!ets_loop_iter()) {
66 asm("waiti 0");
67 }
68 #else
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030069 mp_hal_delay_us(1);
Paul Sokolovsky4b3f1d72016-09-19 00:23:38 +030070 #endif
Damien George0b32e502015-02-13 15:04:53 +000071 }
Damien George075d5972014-11-27 20:30:33 +000072}
73
Paul Sokolovskyc9618892016-03-29 11:13:32 +030074void mp_hal_stdout_tx_char(char c) {
75 uart_tx_one_char(UART0, c);
76 mp_uos_dupterm_tx_strn(&c, 1);
77}
78
Paul Sokolovsky402a7432016-03-29 11:48:43 +030079#if 0
80void mp_hal_debug_str(const char *str) {
81 while (*str) {
82 uart_tx_one_char(UART0, *str++);
83 }
84 uart_flush(UART0);
85}
86#endif
87
Damien George0b32e502015-02-13 15:04:53 +000088void mp_hal_stdout_tx_str(const char *str) {
Damien George075d5972014-11-27 20:30:33 +000089 while (*str) {
Paul Sokolovskyc9618892016-03-29 11:13:32 +030090 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +000091 }
92}
93
Damien George0b32e502015-02-13 15:04:53 +000094void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +000095 while (len--) {
Paul Sokolovskyc9618892016-03-29 11:13:32 +030096 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +000097 }
98}
99
Damien George0b32e502015-02-13 15:04:53 +0000100void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +0000101 while (len--) {
102 if (*str == '\n') {
Paul Sokolovskyc9618892016-03-29 11:13:32 +0300103 mp_hal_stdout_tx_char('\r');
Damien George075d5972014-11-27 20:30:33 +0000104 }
Paul Sokolovskyc9618892016-03-29 11:13:32 +0300105 mp_hal_stdout_tx_char(*str++);
Damien George075d5972014-11-27 20:30:33 +0000106 }
107}
108
Paul Sokolovsky402a7432016-03-29 11:48:43 +0300109void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
110 (void)env;
111 while (len--) {
112 if (*str == '\n') {
113 uart_tx_one_char(UART0, '\r');
114 }
115 uart_tx_one_char(UART0, *str++);
116 }
117}
118
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +0300119uint32_t mp_hal_ticks_ms(void) {
Josef Gajdusek800d5cd2015-05-10 17:54:09 +0200120 return system_get_time() / 1000;
Damien George075d5972014-11-27 20:30:33 +0000121}
122
Damien Georgeb41a14a2015-12-28 17:27:44 +0000123uint32_t mp_hal_ticks_us(void) {
124 return system_get_time();
125}
126
Paul Sokolovskyebd9f552015-10-29 13:03:44 +0300127void mp_hal_delay_ms(uint32_t delay) {
128 mp_hal_delay_us(delay * 1000);
Damien George075d5972014-11-27 20:30:33 +0000129}
130
131void mp_hal_set_interrupt_char(int c) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200132 if (c != -1) {
133 mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
134 }
135 extern int interrupt_char;
136 interrupt_char = c;
Damien George075d5972014-11-27 20:30:33 +0000137}
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200138
Paul Sokolovskya099bfe2016-03-11 09:38:20 +0700139void ets_event_poll(void) {
140 ets_loop_iter();
141 if (MP_STATE_VM(mp_pending_exception) != NULL) {
142 mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
143 MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
144 nlr_raise(obj);
145 }
146}
147
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200148void __assert_func(const char *file, int line, const char *func, const char *expr) {
149 printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
150 nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError,
151 "C-level assert"));
152}
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300153
154void mp_hal_signal_input(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300155 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300156 system_os_post(UART_TASK_ID, 0, 0);
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300157 #endif
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300158}
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300159
160static int call_dupterm_read(void) {
161 if (MP_STATE_PORT(term_obj) == NULL) {
162 return -1;
163 }
164
165 nlr_buf_t nlr;
166 if (nlr_push(&nlr) == 0) {
Paul Sokolovskya22b6eb2016-07-04 21:34:40 +0300167 mp_obj_t readinto_m[3];
168 mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_readinto, readinto_m);
169 readinto_m[2] = MP_STATE_PORT(dupterm_arr_obj);
170 mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m);
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300171 if (res == mp_const_none) {
Paul Sokolovsky116eeee2016-05-24 01:30:28 +0300172 nlr_pop();
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300173 return -2;
174 }
Paul Sokolovsky38056182016-07-05 14:18:12 +0300175 if (res == MP_OBJ_NEW_SMALL_INT(0)) {
Paul Sokolovsky0ab37252016-05-20 22:20:37 +0300176 mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
Paul Sokolovsky2b05b602016-05-24 15:04:59 +0300177 nlr_pop();
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300178 return -1;
179 }
Paul Sokolovsky38056182016-07-05 14:18:12 +0300180 mp_buffer_info_t bufinfo;
181 mp_get_buffer_raise(MP_STATE_PORT(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ);
Paul Sokolovsky2b05b602016-05-24 15:04:59 +0300182 nlr_pop();
Paul Sokolovsky4681b862016-05-24 01:37:56 +0300183 if (*(byte*)bufinfo.buf == interrupt_char) {
184 mp_keyboard_interrupt();
185 return -2;
186 }
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300187 return *(byte*)bufinfo.buf;
188 } else {
Paul Sokolovsky0ab37252016-05-20 22:20:37 +0300189 mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val);
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300190 }
191
192 return -1;
193}
194
195STATIC void dupterm_task_handler(os_event_t *evt) {
Paul Sokolovsky6f3b9932016-04-17 18:11:04 +0300196 static byte lock;
197 if (lock) {
198 return;
199 }
200 lock = 1;
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300201 while (1) {
202 int c = call_dupterm_read();
203 if (c < 0) {
204 break;
205 }
206 ringbuf_put(&input_buf, c);
207 }
208 mp_hal_signal_input();
Paul Sokolovsky6f3b9932016-04-17 18:11:04 +0300209 lock = 0;
Paul Sokolovsky98af8912016-03-31 19:49:55 +0300210}
211
212STATIC os_event_t dupterm_evt_queue[4];
213
214void dupterm_task_init() {
215 system_os_task(dupterm_task_handler, DUPTERM_TASK_ID, dupterm_evt_queue, MP_ARRAY_SIZE(dupterm_evt_queue));
216}
217
218void mp_hal_signal_dupterm_input(void) {
219 system_os_post(DUPTERM_TASK_ID, 0, 0);
220}
Damien Georgea2d5d842016-04-22 10:04:12 +0100221
Damien George4b37e772016-05-26 17:06:40 +0100222void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin_id) {
Damien Georgea2d5d842016-04-22 10:04:12 +0100223 const pyb_pin_obj_t *pin = &pyb_pin_obj[pin_id];
Damien George67a6d312016-04-22 10:35:26 +0100224
225 if (pin->phys_port == 16) {
226 // configure GPIO16 as input with output register holding 0
227 WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
228 WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
229 WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input
230 WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1)); // out=0
231 return;
232 }
233
Damien Georgea2d5d842016-04-22 10:04:12 +0100234 ETS_GPIO_INTR_DISABLE();
235 PIN_FUNC_SELECT(pin->periph, pin->func);
236 GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin->phys_port)),
237 GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin->phys_port)))
238 | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); // open drain
239 GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS,
240 GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << pin->phys_port));
241 ETS_GPIO_INTR_ENABLE();
242}
Paul Sokolovsky3d830412016-05-03 00:18:14 +0300243
244// Get pointer to esf_buf bookkeeping structure
245void *ets_get_esf_buf_ctlblk(void) {
246 // Get literal ptr before start of esf_rx_buf_alloc func
247 extern void *esf_rx_buf_alloc();
248 return ((void**)esf_rx_buf_alloc)[-1];
249}
250
251// Get number of esf_buf free buffers of given type, as encoded by index
252// idx 0 corresponds to buf types 1, 2; 1 - 4; 2 - 5; 3 - 7; 4 - 8
253// Only following buf types appear to be used:
254// 1 - tx buffer, 5 - management frame tx buffer; 8 - rx buffer
255int ets_esf_free_bufs(int idx) {
256 uint32_t *p = ets_get_esf_buf_ctlblk();
257 uint32_t *b = (uint32_t*)p[idx];
258 int cnt = 0;
259 while (b) {
260 b = (uint32_t*)b[0x20 / 4];
261 cnt++;
262 }
263 return cnt;
264}
Paul Sokolovsky64ad8382016-07-31 02:30:05 +0300265
266extern int mp_stream_errno;
267int *__errno() {
268 return &mp_stream_errno;
269}