mphalport.c 2.77 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
4
5
6
7
8
#include "api/common.h"
#include "epicardium.h"

#include "max32665.h"
#include "mxc_delay.h"
#include "tmr.h"

/* stdarg.h must be included before mpprint.h */
Rahix's avatar
Rahix committed
9
#include <stdarg.h>
Rahix's avatar
Rahix committed
10
11
12
13

#include "py/lexer.h"
#include "py/mpconfig.h"
#include "py/mperrno.h"
14
#include "py/mpstate.h"
Rahix's avatar
Rahix committed
15
16
#include "py/obj.h"
#include "py/runtime.h"
17
#include "py/mpprint.h"
Rahix's avatar
Rahix committed
18

Rahix's avatar
Rahix committed
19
20
21
#include <stdint.h>
#include <stdio.h>
#include <string.h>
22

Rahix's avatar
Rahix committed
23
24
25
26
27
28
29
/******************************************************************************
 * Serial Communication
 */

/* Receive single character */
int mp_hal_stdin_rx_chr(void)
{
Rahix's avatar
Rahix committed
30
	return (int)epic_uart_read_chr();
Rahix's avatar
Rahix committed
31
32
}

33
/* Send a string */
Rahix's avatar
Rahix committed
34
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
Rahix's avatar
Rahix committed
35
{
Rahix's avatar
Rahix committed
36
	epic_uart_write_str(str, len);
Rahix's avatar
Rahix committed
37
38
}

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Send a string, but replace \n with \n\r */
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len)
{
	/*
	 * Only print one line at a time.  Insert `\r` between lines so
	 * they are properly displayed on the serial console.
	 */
	size_t i, last = 0;
	for (i = 0; i < len; i++) {
		if (str[i] == '\n') {
			epic_uart_write_str(&str[last], i - last);
			epic_uart_write_str("\r", 1);
			last = i;
		}
	}
	epic_uart_write_str(&str[last], len - last);
}

/* Send a zero-terminated string */
void mp_hal_stdout_tx_str(const char *str)
{
	mp_hal_stdout_tx_strn(str, strlen(str));
}

Rahix's avatar
Rahix committed
63
64
65
66
67
/* Used by MicroPython for debug output */
int DEBUG_printf(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
68
	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, args);
Rahix's avatar
Rahix committed
69
70
71
72
	va_end(args);
	return ret;
}

73
74
void __attribute__((noreturn)) sbrk_is_not_implemented___see_issue_44(void);
intptr_t _sbrk(int incr)
75
{
76
	sbrk_is_not_implemented___see_issue_44();
77
78
}

79
void epic_isr_ctrl_c(void)
80
{
81
82
83
	/* Taken from lib/micropython/micropython/lib/utils/interrupt_char.c */
	MP_STATE_VM(mp_pending_exception) =
		MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
Rahix's avatar
Rahix committed
84
#if MICROPY_ENABLE_SCHEDULER
85
86
	if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
		MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
87
	}
88
#endif
89
90
91
92
}

void mp_hal_set_interrupt_char(char c)
{
Rahix's avatar
Rahix committed
93
94
95
96
97
	if (c != -1) {
		mp_obj_exception_clear_traceback(
			MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))
		);
	}
98
99

	if (c == 0x03) {
100
		epic_interrupt_enable(EPIC_INT_CTRL_C);
101
	} else {
102
		epic_interrupt_disable(EPIC_INT_CTRL_C);
103
	}
104
105
}

Rahix's avatar
Rahix committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/******************************************************************************
 * Time & Delay
 */

void mp_hal_delay_ms(mp_uint_t ms)
{
	mxc_delay(ms * 1000);
}

void mp_hal_delay_us(mp_uint_t us)
{
	mxc_delay(us);
}

120
121
122
123
124
mp_uint_t mp_hal_ticks_ms(void)
{
	return 0;
}

Rahix's avatar
Rahix committed
125
126
127
128
/******************************************************************************
 * Fatal Errors
 */

Rahix's avatar
Rahix committed
129
extern NORETURN void *Reset_Handler(void);
130

Rahix's avatar
Rahix committed
131
void NORETURN nlr_jump_fail(void *val)
Rahix's avatar
Rahix committed
132
{
Rahix's avatar
Rahix committed
133
134
135
	char msg[] = " >>> nlr_jump_fail <<<\r\n";
	epic_uart_write_str(msg, sizeof(msg));

136
	Reset_Handler();
Rahix's avatar
Rahix committed
137
}