mphalport.c 2.19 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
4
5
#include <stdint.h>

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

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

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

Rahix's avatar
Rahix committed
16
17
18
19
20
21
22
/******************************************************************************
 * Serial Communication
 */

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

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

32
33
34
35
36
37
38
39
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
40
41
42
43
44
45
46
47
		/* 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
48
49
50
51
52
	}
}

void mp_hal_set_interrupt_char(char c)
{
Rahix's avatar
Rahix committed
53
54
55
56
57
	if (c != -1) {
		mp_obj_exception_clear_traceback(
			MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))
		);
	}
58
59
60
	do_interrupt = (c == 0x03);
}

Rahix's avatar
Rahix committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/******************************************************************************
 * 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
75
76
77
78
/******************************************************************************
 * Fatal Errors
 */

Rahix's avatar
Rahix committed
79
void NORETURN nlr_jump_fail(void* val)
Rahix's avatar
Rahix committed
80
81
{
	/* TODO: Report error and restart */
Rahix's avatar
Rahix committed
82
83
84
	char msg[] = " >>> nlr_jump_fail <<<\r\n";
	epic_uart_write_str(msg, sizeof(msg));

Rahix's avatar
Rahix committed
85
	while (1) {
Rahix's avatar
Rahix committed
86
		__asm("nop");
Rahix's avatar
Rahix committed
87
88
89
90
91
92
93
	}
}

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

Rahix's avatar
Rahix committed
94
mp_lexer_t* mp_lexer_new_from_file(const char* filename)
Rahix's avatar
Rahix committed
95
96
97
98
99
{
	/* TODO: Do we need an implementation for this? */
	mp_raise_OSError(MP_ENOENT);
}

Rahix's avatar
Rahix committed
100
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t* args, mp_map_t* kwargs)
Rahix's avatar
Rahix committed
101
102
103
104
105
{
	/* TODO: Once fs is implemented, get this working as well */
	return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);