mphalport.c 3.33 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include <stdint.h>
Rahix's avatar
Rahix committed
2
3
#include <stdarg.h>
#include <stdio.h>
4
#include <string.h>
Rahix's avatar
Rahix committed
5
6
7
8

#include "py/lexer.h"
#include "py/mpconfig.h"
#include "py/mperrno.h"
9
#include "py/mpstate.h"
Rahix's avatar
Rahix committed
10
11
#include "py/obj.h"
#include "py/runtime.h"
12
#include "py/mpprint.h"
Rahix's avatar
Rahix committed
13

Rahix's avatar
Rahix committed
14
#include "mxc_delay.h"
15
16
17
18
#include "max32665.h"
#include "tmr.h"

#include "epicardium.h"
19
#include "api/common.h"
20

Rahix's avatar
Rahix committed
21
22
23
24
25
26
27
/******************************************************************************
 * Serial Communication
 */

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

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

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 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
61
62
63
64
65
/* Used by MicroPython for debug output */
int DEBUG_printf(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
66
	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, args);
Rahix's avatar
Rahix committed
67
68
69
70
	va_end(args);
	return ret;
}

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

77
void epic_isr_ctrl_c(void)
78
{
79
80
81
	/* 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
82
#if MICROPY_ENABLE_SCHEDULER
83
84
	if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
		MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
85
	}
86
#endif
87
88
89
90
}

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

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

Rahix's avatar
Rahix committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/******************************************************************************
 * 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);
}

118
119
120
121
122
mp_uint_t mp_hal_ticks_ms(void)
{
	return 0;
}

Rahix's avatar
Rahix committed
123
124
125
126
/******************************************************************************
 * Fatal Errors
 */

Rahix's avatar
Rahix committed
127
extern NORETURN void *Reset_Handler(void);
128

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

134
	Reset_Handler();
Rahix's avatar
Rahix committed
135
136
137
138
139
140
}

/******************************************************************************
 * Stubs
 */

Rahix's avatar
Rahix committed
141
mp_lexer_t *mp_lexer_new_from_file(const char *filename)
Rahix's avatar
Rahix committed
142
143
144
145
146
{
	/* TODO: Do we need an implementation for this? */
	mp_raise_OSError(MP_ENOENT);
}

Rahix's avatar
Rahix committed
147
mp_import_stat_t mp_import_stat(const char *path)
148
149
150
151
{
	return MP_IMPORT_STAT_NO_EXIST;
}

Rahix's avatar
Rahix committed
152
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
Rahix's avatar
Rahix committed
153
154
{
	/* TODO: Once fs is implemented, get this working as well */
155
	mp_raise_NotImplementedError("FS is not yet implemented");
Rahix's avatar
Rahix committed
156
157
158
	return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);