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

#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

23
24
25
26
27
28
29
30
31
32
33
34
35
/* Initialize everything for MicroPython */
void pycardium_hal_init(void)
{
	/* TMR5 is used for interrupts from Epicardium */
	NVIC_EnableIRQ(TMR5_IRQn);

	/*
	 * Enable UART RX Interrupt so Pycardium can sleep until
	 * a character becomes available.
	 */
	epic_interrupt_enable(EPIC_INT_UART_RX);
}

Rahix's avatar
Rahix committed
36
37
38
39
40
41
42
/******************************************************************************
 * Serial Communication
 */

/* Receive single character */
int mp_hal_stdin_rx_chr(void)
{
43
44
45
46
47
	int chr;
	while ((chr = epic_uart_read_char()) < 0) {
		__WFI();
	}
	return chr;
Rahix's avatar
Rahix committed
48
49
}

50
/* Send a string */
Rahix's avatar
Rahix committed
51
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
Rahix's avatar
Rahix committed
52
{
Rahix's avatar
Rahix committed
53
	epic_uart_write_str(str, len);
Rahix's avatar
Rahix committed
54
55
}

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* 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
80
81
82
83
84
/* Used by MicroPython for debug output */
int DEBUG_printf(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
85
	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, args);
Rahix's avatar
Rahix committed
86
87
88
89
	va_end(args);
	return ret;
}

90
91
void __attribute__((noreturn)) sbrk_is_not_implemented___see_issue_44(void);
intptr_t _sbrk(int incr)
92
{
93
	sbrk_is_not_implemented___see_issue_44();
94
95
}

96
void epic_isr_ctrl_c(void)
97
{
98
99
100
	/* 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
101
#if MICROPY_ENABLE_SCHEDULER
102
103
	if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
		MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
104
	}
105
#endif
106
107
108
109
}

void mp_hal_set_interrupt_char(char c)
{
Rahix's avatar
Rahix committed
110
111
112
113
114
	if (c != -1) {
		mp_obj_exception_clear_traceback(
			MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))
		);
	}
115
116

	if (c == 0x03) {
117
		epic_interrupt_enable(EPIC_INT_CTRL_C);
118
	} else {
119
		epic_interrupt_disable(EPIC_INT_CTRL_C);
120
	}
121
122
}

Rahix's avatar
Rahix committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/******************************************************************************
 * 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);
}

137
138
139
140
141
mp_uint_t mp_hal_ticks_ms(void)
{
	return 0;
}

Rahix's avatar
Rahix committed
142
143
144
145
/******************************************************************************
 * Fatal Errors
 */

Rahix's avatar
Rahix committed
146
extern NORETURN void *Reset_Handler(void);
147

Rahix's avatar
Rahix committed
148
void NORETURN nlr_jump_fail(void *val)
Rahix's avatar
Rahix committed
149
{
Rahix's avatar
Rahix committed
150
151
152
	char msg[] = " >>> nlr_jump_fail <<<\r\n";
	epic_uart_write_str(msg, sizeof(msg));

153
	Reset_Handler();
Rahix's avatar
Rahix committed
154
}