Skip to content
Snippets Groups Projects
Select Git revision
  • 2f7eca470778c8b3ba3da698206f69e27c67e54b
  • max32xxx default protected
  • dw-cmsisdap-path
3 results

openocd.c

Blame
  • 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();
    }