main.c 1.65 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
	if (cnt < 0) {
		printf("pycardium: Error fetching args: %d\n", cnt);
	} else if (cnt > 0) {
36
		epic_uart_write_str(header, sizeof(header));
37
38
39
		printf("  Loading %s ...\n", script_name);
	}

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

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

		mp_init();
47

48
49
50
51
52
53
54
55
56
57
		/* request by badge.team */
		mp_obj_list_init(mp_sys_path, 0);
		mp_obj_list_append(mp_sys_path, MP_ROM_QSTR(MP_QSTR_));
		mp_obj_list_append(
			mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)
		);
		mp_obj_list_append(
			mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_apps)
		);

58
59
60
61
		if (cnt > 0) {
			pyexec_file_if_exists(script_name);
		}

62
		epic_uart_write_str(header, sizeof(header));
Rahix's avatar
Rahix committed
63
		pyexec_friendly_repl();
64

Rahix's avatar
Rahix committed
65
		mp_deinit();
Rahix's avatar
Rahix committed
66
67
68
	}
}

Rahix's avatar
Rahix committed
69
70
71
72
73
void HardFault_Handler(void)
{
	epic_exit(255);
}

Rahix's avatar
Rahix committed
74
75
void gc_collect(void)
{
76
	void *sp = (void *)__get_MSP();
Rahix's avatar
Rahix committed
77
78

	gc_collect_start();
Rahix's avatar
Rahix committed
79
	gc_collect_root(
80
81
		sp,
		((mp_uint_t)&__StackTop - (mp_uint_t)sp) / sizeof(mp_uint_t)
Rahix's avatar
Rahix committed
82
	);
Rahix's avatar
Rahix committed
83
84
	gc_collect_end();
}