mphalport.c 3.01 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>
Rahix's avatar
Rahix committed
4
5
6
7

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

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

#include "epicardium.h"
Rahix's avatar
Rahix committed
17

Rahix's avatar
Rahix committed
18
19
20
21
22
23
24
/******************************************************************************
 * Serial Communication
 */

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

/* Send string of given length */
Rahix's avatar
Rahix committed
29
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
Rahix's avatar
Rahix committed
30
{
Rahix's avatar
Rahix committed
31
	epic_uart_write_str(str, len);
Rahix's avatar
Rahix committed
32
33
}

Rahix's avatar
Rahix committed
34
35
36
37
38
39
40
41
42
43
/* Used by MicroPython for debug output */
int DEBUG_printf(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	int ret = vprintf(fmt, args);
	va_end(args);
	return ret;
}

44
45
46
/* newlib syscall to allow printf to work */
long _write(int fd, const char *buf, size_t cnt)
{
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	/*
	 * 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 < cnt; i++) {
		if (buf[i] == '\n') {
			epic_uart_write_str(&buf[last], i - last);
			epic_uart_write_str("\r", 1);
			last = i;
		}
	}
	if (last != i) {
		epic_uart_write_str(&buf[last], cnt - last);
	}
62
63
64
	return cnt;
}

65
66
67
68
69
70
71
72
bool do_interrupt = false;

/* Timer Interrupt used for control char notification */
void TMR5_IRQHandler(void)
{
	TMR_IntClear(MXC_TMR5);

	if (do_interrupt) {
Rahix's avatar
Rahix committed
73
74
75
76
77
78
79
80
		/* 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));
#if MICROPY_ENABLE_SCHEDULER
		if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
			MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
		}
#endif
81
82
83
84
85
	}
}

void mp_hal_set_interrupt_char(char c)
{
Rahix's avatar
Rahix committed
86
87
88
89
90
	if (c != -1) {
		mp_obj_exception_clear_traceback(
			MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))
		);
	}
91
92
93
	do_interrupt = (c == 0x03);
}

Rahix's avatar
Rahix committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/******************************************************************************
 * 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);
}

Rahix's avatar
Rahix committed
108
109
110
111
/******************************************************************************
 * Fatal Errors
 */

Rahix's avatar
Rahix committed
112
extern NORETURN void *Reset_Handler(void);
113

Rahix's avatar
Rahix committed
114
void NORETURN nlr_jump_fail(void *val)
Rahix's avatar
Rahix committed
115
{
Rahix's avatar
Rahix committed
116
117
118
	char msg[] = " >>> nlr_jump_fail <<<\r\n";
	epic_uart_write_str(msg, sizeof(msg));

119
	Reset_Handler();
Rahix's avatar
Rahix committed
120
121
122
123
124
125
}

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

Rahix's avatar
Rahix committed
126
mp_lexer_t *mp_lexer_new_from_file(const char *filename)
Rahix's avatar
Rahix committed
127
128
129
130
131
{
	/* TODO: Do we need an implementation for this? */
	mp_raise_OSError(MP_ENOENT);
}

Rahix's avatar
Rahix committed
132
mp_import_stat_t mp_import_stat(const char *path)
133
134
135
136
{
	return MP_IMPORT_STAT_NO_EXIST;
}

Rahix's avatar
Rahix committed
137
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
Rahix's avatar
Rahix committed
138
139
{
	/* TODO: Once fs is implemented, get this working as well */
140
	mp_raise_NotImplementedError ("FS is not yet implemented");
Rahix's avatar
Rahix committed
141
142
143
	return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);