Skip to content
Snippets Groups Projects
Select Git revision
  • fec2b757f17a3e6b2244d4cd2278cf7f3a8d14ca
  • master default protected
  • schneider/ir
  • rahix/user-space-ctx
  • schneider/iaq-python
  • schneider/ble-mini-demo
  • schneider/ble-ecg-stream-visu
  • schneider/mp-exception-print
  • schneider/sleep-display
  • schneider/deepsleep4
  • schneider/deepsleep2
  • schneider/deepsleep
  • schneider/ble-central
  • rahix/bluetooth-app-favorite
  • schneider/v1.17-changelog
  • schneider/ancs
  • schneider/png
  • schneider/freertos-list-debug
  • schneider/212-reset-hardware-when-entering-repl
  • schneider/bonding-fail-if-full
  • schneider/ble-fixes-2020-3
  • v1.18
  • v1.17
  • v1.16
  • v1.15
  • v1.14
  • v1.13
  • v1.12
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
41 results

main.c

Blame
  • user avatar
    schneider authored
    fec2b757
    History
    main.c 2.38 KiB
    #include "epicardium.h"
    #include "api/caller.h"
    #include "mphalport.h"
    #include "card10-version.h"
    #include "modules/interrupt.h"
    
    #include "max32665.h"
    
    #include "shared/runtime/pyexec.h"
    #include "shared/readline/readline.h"
    #include "py/gc.h"
    #include "py/runtime.h"
    #include "py/stackctrl.h"
    
    #include <stdio.h>
    
    /* Defined in linker script */
    extern void *__StackTop, *__StackLimit;
    extern void *__HeapBase, *__HeapLimit;
    
    static const char header[] =
    	"--------------------------------\r\n"
    	"          Pycardium\r\n"
    	" Version: " CARD10_VERSION
    	"\r\n"
    	"--------------------------------\r\n";
    
    int main(void)
    {
    	char script_name[128] = { 0 };
    	int cnt = api_fetch_args(script_name, sizeof(script_name));
    
    	pycardium_hal_init();
    
    	epic_uart_write_str(header, sizeof(header));
    
    	if (cnt < 0) {
    		printf("pycardium: Error fetching args: %d\n", cnt);
    
    		/* Go to REPL instead. */
    		cnt = 0;
    	}
    
    	if (cnt > 0) {
    		printf("  Loading %s ...\n", script_name);
    	} else {
    		printf("  Entering REPL ...\n\n");
    	}
    
    	mp_stack_set_top(&__StackTop);
    	mp_stack_set_limit((mp_int_t)&__StackLimit);
    
    	gc_init(&__HeapBase + 1024 * 10, &__HeapLimit);
    
    	mp_init();
    
    	readline_init0();
    	interrupt_init0();
    
    	/* 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));
    
    	if (cnt > 0) {
    		int ret = pyexec_file_if_exists(script_name);
    
    		if (ret == 0) {
    			/*
    			 * The script was aborted via uncaught exception; Sadly
    			 * we can't find out what happened so let's hope it was
    			 * a KeyboardInterrupt and drop to REPL.
    			 *
    			 * In the future it might be interesting to rework this
    			 * so any other exception will lead to epic_exit(1).
    			 */
    			epic_exec("");
    		}
    
    		/* The script ended execution normally; return to menu */
    		epic_exit(0);
    	}
    
    	for (;;) {
    		if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
    			if (pyexec_raw_repl() != 0) {
    				break;
    			}
    		} else {
    			if (pyexec_friendly_repl() != 0) {
    				break;
    			}
    		}
    	}
    
    	mp_deinit();
    
    	epic_exit(0);
    }
    
    void HardFault_Handler(void)
    {
    	epic_exit(255);
    }
    
    void _exit(void)
    {
    	epic_exit(254);
    }
    
    void gc_collect(void)
    {
    	void *sp = (void *)__get_MSP();
    
    	gc_collect_start();
    	gc_collect_root(
    		sp,
    		((mp_uint_t)&__StackTop - (mp_uint_t)sp) / sizeof(mp_uint_t)
    	);
    	gc_collect_end();
    }