Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 1 | /* |
| 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 Gajdusek | 800d5cd | 2015-05-10 17:54:09 +0200 | [diff] [blame] | 32 | #include "user_interface.h" |
Paul Sokolovsky | a099bfe | 2016-03-11 09:38:20 +0700 | [diff] [blame] | 33 | #include "ets_alt_task.h" |
Paul Sokolovsky | 8ab16b6 | 2015-12-29 20:44:55 +0200 | [diff] [blame] | 34 | #include "py/obj.h" |
| 35 | #include "py/mpstate.h" |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 36 | #include "extmod/misc.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 37 | |
| 38 | extern void ets_wdt_disable(void); |
| 39 | extern void wdt_feed(void); |
| 40 | extern void ets_delay_us(); |
| 41 | |
| 42 | void mp_hal_init(void) { |
| 43 | ets_wdt_disable(); // it's a pain while developing |
Paul Sokolovsky | a4c8ef9 | 2016-02-08 21:43:37 +0200 | [diff] [blame] | 44 | mp_hal_rtc_init(); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 45 | uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200); |
| 46 | } |
| 47 | |
| 48 | void mp_hal_feed_watchdog(void) { |
| 49 | //ets_wdt_disable(); // it's a pain while developing |
| 50 | //WRITE_PERI_REG(0x60000914, 0x73); |
| 51 | //wdt_feed(); // might also work |
| 52 | } |
| 53 | |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 54 | void mp_hal_delay_us(uint32_t us) { |
Damien George | e673714 | 2016-03-23 13:01:21 +0200 | [diff] [blame] | 55 | uint32_t start = system_get_time(); |
| 56 | while (system_get_time() - start < us) { |
| 57 | ets_event_poll(); |
| 58 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 61 | int mp_hal_stdin_rx_chr(void) { |
| 62 | for (;;) { |
| 63 | int c = uart0_rx(); |
| 64 | if (c != -1) { |
| 65 | return c; |
| 66 | } |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 67 | mp_hal_delay_us(1); |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 68 | mp_hal_feed_watchdog(); |
| 69 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 72 | void mp_hal_stdout_tx_char(char c) { |
| 73 | uart_tx_one_char(UART0, c); |
| 74 | mp_uos_dupterm_tx_strn(&c, 1); |
| 75 | } |
| 76 | |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 77 | void mp_hal_stdout_tx_str(const char *str) { |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 78 | while (*str) { |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 79 | mp_hal_stdout_tx_char(*str++); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 83 | void mp_hal_stdout_tx_strn(const char *str, uint32_t len) { |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 84 | while (len--) { |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 85 | mp_hal_stdout_tx_char(*str++); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Damien George | 0b32e50 | 2015-02-13 15:04:53 +0000 | [diff] [blame] | 89 | void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) { |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 90 | while (len--) { |
| 91 | if (*str == '\n') { |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 92 | mp_hal_stdout_tx_char('\r'); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 93 | } |
Paul Sokolovsky | c961889 | 2016-03-29 11:13:32 +0300 | [diff] [blame^] | 94 | mp_hal_stdout_tx_char(*str++); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Paul Sokolovsky | 6a09e7d | 2015-10-29 19:35:04 +0300 | [diff] [blame] | 98 | uint32_t mp_hal_ticks_ms(void) { |
Josef Gajdusek | 800d5cd | 2015-05-10 17:54:09 +0200 | [diff] [blame] | 99 | return system_get_time() / 1000; |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Damien George | b41a14a | 2015-12-28 17:27:44 +0000 | [diff] [blame] | 102 | uint32_t mp_hal_ticks_us(void) { |
| 103 | return system_get_time(); |
| 104 | } |
| 105 | |
Paul Sokolovsky | ebd9f55 | 2015-10-29 13:03:44 +0300 | [diff] [blame] | 106 | void mp_hal_delay_ms(uint32_t delay) { |
| 107 | mp_hal_delay_us(delay * 1000); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void mp_hal_set_interrupt_char(int c) { |
Paul Sokolovsky | d3a4d39 | 2015-12-20 13:58:58 +0200 | [diff] [blame] | 111 | if (c != -1) { |
| 112 | mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception)); |
| 113 | } |
| 114 | extern int interrupt_char; |
| 115 | interrupt_char = c; |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 116 | } |
Paul Sokolovsky | 8ab16b6 | 2015-12-29 20:44:55 +0200 | [diff] [blame] | 117 | |
Paul Sokolovsky | a099bfe | 2016-03-11 09:38:20 +0700 | [diff] [blame] | 118 | void ets_event_poll(void) { |
| 119 | ets_loop_iter(); |
| 120 | if (MP_STATE_VM(mp_pending_exception) != NULL) { |
| 121 | mp_obj_t obj = MP_STATE_VM(mp_pending_exception); |
| 122 | MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; |
| 123 | nlr_raise(obj); |
| 124 | } |
| 125 | } |
| 126 | |
Paul Sokolovsky | 8ab16b6 | 2015-12-29 20:44:55 +0200 | [diff] [blame] | 127 | void __assert_func(const char *file, int line, const char *func, const char *expr) { |
| 128 | printf("assert:%s:%d:%s: %s\n", file, line, func, expr); |
| 129 | nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError, |
| 130 | "C-level assert")); |
| 131 | } |