blob: c78b3600f04f1f45e18c686675e06fb04464fad8 [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 Sokolovsky8ab16b62015-12-29 20:44:55 +020033#include "py/obj.h"
34#include "py/mpstate.h"
Damien George075d5972014-11-27 20:30:33 +000035
36extern void ets_wdt_disable(void);
37extern void wdt_feed(void);
38extern void ets_delay_us();
39
40void mp_hal_init(void) {
41 ets_wdt_disable(); // it's a pain while developing
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020042 mp_hal_rtc_init();
Damien George075d5972014-11-27 20:30:33 +000043 uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
44}
45
46void mp_hal_feed_watchdog(void) {
47 //ets_wdt_disable(); // it's a pain while developing
48 //WRITE_PERI_REG(0x60000914, 0x73);
49 //wdt_feed(); // might also work
50}
51
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030052void mp_hal_delay_us(uint32_t us) {
Damien George075d5972014-11-27 20:30:33 +000053 ets_delay_us(us);
54}
55
Damien George0b32e502015-02-13 15:04:53 +000056int mp_hal_stdin_rx_chr(void) {
57 for (;;) {
58 int c = uart0_rx();
59 if (c != -1) {
60 return c;
61 }
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030062 mp_hal_delay_us(1);
Damien George0b32e502015-02-13 15:04:53 +000063 mp_hal_feed_watchdog();
64 }
Damien George075d5972014-11-27 20:30:33 +000065}
66
Damien George0b32e502015-02-13 15:04:53 +000067void mp_hal_stdout_tx_str(const char *str) {
Damien George075d5972014-11-27 20:30:33 +000068 while (*str) {
69 uart_tx_one_char(UART0, *str++);
70 }
71}
72
Damien George0b32e502015-02-13 15:04:53 +000073void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +000074 while (len--) {
75 uart_tx_one_char(UART0, *str++);
76 }
77}
78
Damien George0b32e502015-02-13 15:04:53 +000079void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
Damien George075d5972014-11-27 20:30:33 +000080 while (len--) {
81 if (*str == '\n') {
82 uart_tx_one_char(UART0, '\r');
83 }
84 uart_tx_one_char(UART0, *str++);
85 }
86}
87
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +030088uint32_t mp_hal_ticks_ms(void) {
Josef Gajdusek800d5cd2015-05-10 17:54:09 +020089 return system_get_time() / 1000;
Damien George075d5972014-11-27 20:30:33 +000090}
91
Damien Georgeb41a14a2015-12-28 17:27:44 +000092uint32_t mp_hal_ticks_us(void) {
93 return system_get_time();
94}
95
Paul Sokolovskyebd9f552015-10-29 13:03:44 +030096void mp_hal_delay_ms(uint32_t delay) {
97 mp_hal_delay_us(delay * 1000);
Damien George075d5972014-11-27 20:30:33 +000098}
99
100void mp_hal_set_interrupt_char(int c) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200101 if (c != -1) {
102 mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
103 }
104 extern int interrupt_char;
105 interrupt_char = c;
Damien George075d5972014-11-27 20:30:33 +0000106}
Paul Sokolovsky8ab16b62015-12-29 20:44:55 +0200107
108void __assert_func(const char *file, int line, const char *func, const char *expr) {
109 printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
110 nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError,
111 "C-level assert"));
112}