main.c 1.33 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include "epicardium.h"
2
#include "api/caller.h"
3
#include "mphalport.h"
Rahix's avatar
Rahix committed
4
#include "card10-version.h"
5

Rahix's avatar
Rahix committed
6
7
#include "max32665.h"

Rahix's avatar
Rahix committed
8
#include "lib/utils/pyexec.h"
Rahix's avatar
Rahix committed
9
10
#include "py/gc.h"
#include "py/runtime.h"
11
#include "py/stackctrl.h"
Rahix's avatar
Rahix committed
12

Rahix's avatar
Rahix committed
13
#include <stdio.h>
14

Rahix's avatar
Rahix committed
15
16
17
/* Defined in linker script */
extern void *__StackTop, *__StackLimit;
extern void *__HeapBase, *__HeapLimit;
18

Rahix's avatar
Rahix committed
19
20
21
22
23
24
25
static const char header[] =
	"--------------------------------\r\n"
	"          Pycardium\r\n"
	" Version: " CARD10_VERSION
	"\r\n"
	"--------------------------------\r\n";

Rahix's avatar
Rahix committed
26
27
int main(void)
{
28
29
	char script_name[128] = { 0 };
	int cnt = api_fetch_args(script_name, sizeof(script_name));
Rahix's avatar
Rahix committed
30

31
32
	pycardium_hal_init();

33
34
35
36
37
38
39
40
	epic_uart_write_str(header, sizeof(header));

	if (cnt < 0) {
		printf("pycardium: Error fetching args: %d\n", cnt);
	} else if (cnt > 0) {
		printf("  Loading %s ...\n", script_name);
	}

41
42
	mp_stack_set_top(&__StackTop);
	mp_stack_set_limit((mp_int_t)&__StackLimit);
Rahix's avatar
Rahix committed
43
44

	while (1) {
45
		gc_init(&__HeapBase + 1024 * 10, &__HeapLimit);
Rahix's avatar
Rahix committed
46
47

		mp_init();
48
49
50
51
52

		if (cnt > 0) {
			pyexec_file_if_exists(script_name);
		}

Rahix's avatar
Rahix committed
53
		pyexec_friendly_repl();
54

Rahix's avatar
Rahix committed
55
		mp_deinit();
Rahix's avatar
Rahix committed
56
57
58
	}
}

Rahix's avatar
Rahix committed
59
60
61
62
63
void HardFault_Handler(void)
{
	epic_exit(255);
}

Rahix's avatar
Rahix committed
64
65
void gc_collect(void)
{
66
	void *sp = (void *)__get_MSP();
Rahix's avatar
Rahix committed
67
68

	gc_collect_start();
Rahix's avatar
Rahix committed
69
	gc_collect_root(
70
71
		sp,
		((mp_uint_t)&__StackTop - (mp_uint_t)sp) / sizeof(mp_uint_t)
Rahix's avatar
Rahix committed
72
	);
Rahix's avatar
Rahix committed
73
74
	gc_collect_end();
}