diff --git a/bootloader/main.c b/bootloader/main.c index d3577542ede90d0840f6606be5df36764903c333..0132402dadaa63d97a0fa5f4b80afdd723c46f7b 100644 --- a/bootloader/main.c +++ b/bootloader/main.c @@ -198,6 +198,19 @@ static void pmic_button(bool falling) int main(void) { printf("\n\nBootloader\n"); + uint32_t* boot_args = (uint32_t*)0x20080001; + enum card10_reboot_type boot_type = Reboot_Normal; + if ((boot_args[0] ^ 0xbabababa) == boot_args[1]) { + boot_type = (enum card10_reboot_type)boot_args[0]; + boot_args[0] = 0; + boot_args[1] = 0; + } + + if (boot_type == Reboot_Shortcut) { + printf("Trying to shortcut to main epicardium\n"); + + boot((uintptr_t *)PARTITION_START); + } card10_init(); /* @@ -207,8 +220,9 @@ int main(void) bootloader_display_init(); + // If the button is pressed, we go into MSC mode. - if (PB_Get(3)) { + if (boot_type == Reboot_USBStorage || (boot_type == Reboot_Normal && PB_Get(3))) { bootloader_display_header(); bootloader_display_line(2, "USB activated.", 0xffff); bootloader_display_line(3, "Ready.", 0xffff); @@ -217,6 +231,7 @@ int main(void) // If we return, don't try to boot. Maybe rather trigger a software reset. // Reason: Not sure in which state the USB peripheral is and what kind // of interrupts are active. + card10_reset(); while (1) ; } diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 8b0b20deee2754510a6b1dd51b62f351b83d8d5a..169b6cf0b2e8a87f533587256ec63923936bb37b 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -61,6 +61,8 @@ typedef unsigned int size_t; #define API_RTC_GET_SECONDS 0x40 #define API_RTC_SCHEDULE_ALARM 0x41 + +#define API_CORE_REBOOT 0x50 /* clang-format on */ typedef uint32_t api_int_id_t; @@ -633,4 +635,7 @@ API(API_RTC_SCHEDULE_ALARM, int epic_rtc_schedule_alarm(uint32_t timestamp)); */ API_ISR(EPIC_INT_RTC_ALARM, epic_isr_rtc_alarm); + +API(API_CORE_REBOOT, void epic_core_reboot(const char* type)); + #endif /* _EPICARDIUM_H */ diff --git a/epicardium/modules/core.c b/epicardium/modules/core.c new file mode 100644 index 0000000000000000000000000000000000000000..cb81f4a19cc4a604b575dd7c1fd4a675155ebff7 --- /dev/null +++ b/epicardium/modules/core.c @@ -0,0 +1,14 @@ +#include <string.h> + +#include "epicardium.h" +#include "card10.h" + +void epic_core_reboot(const char* type) { + if (strcmp("usbstorage", type) == 0) { + card10_reboot(Reboot_USBStorage); + } else if (strcmp("shortcut", type) == 0) { + card10_reboot(Reboot_Shortcut); + } else { + card10_reboot(Reboot_Normal); + } +} \ No newline at end of file diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build index 113e3cb8ecaebc932a65ce9e6124cc2ff3be60ce..ff58efba1330b382bd9ad78b5cab0bd8747fd6dd 100644 --- a/epicardium/modules/meson.build +++ b/epicardium/modules/meson.build @@ -9,5 +9,6 @@ module_sources = files( 'stream.c', 'vibra.c', 'light_sensor.c', - 'rtc.c' + 'rtc.c', + 'core.c' ) diff --git a/lib/card10/card10.c b/lib/card10/card10.c index b423704f3c969103157a3064fc63c9830ac70506..6b7b9ddf1abbe4447ed182745911321819ec15c2 100644 --- a/lib/card10/card10.c +++ b/lib/card10/card10.c @@ -1,3 +1,4 @@ +#include "card10.h" #include "pmic.h" #include "bosch.h" #include "display.h" @@ -230,6 +231,12 @@ void card10_poll(void) void card10_reset(void) { + card10_reboot(Reboot_Normal); +} + +void card10_reboot(enum card10_reboot_type type) { + uint32_t* boot_args = (uint32_t*)0x20080001; + printf("Resetting ...\n"); /* * Give the UART fifo time to clear. @@ -238,9 +245,14 @@ void card10_reset(void) for (int i = 0; i < 0x1000000; i++) { __asm volatile("nop"); } + + boot_args[0] = type; + boot_args[1] = 0xbabababa ^ type; + MXC_GCR->rstr0 = MXC_F_GCR_RSTR0_SYSTEM; } + void GPIO0_IRQHandler(void) { GPIO_Handler(PORT_0); diff --git a/lib/card10/card10.h b/lib/card10/card10.h index 853a27be90980deda9df88d1c3156a704ada648e..f42dc58fd2ab5e19f46a7e62c7d98740db2c4e26 100644 --- a/lib/card10/card10.h +++ b/lib/card10/card10.h @@ -14,4 +14,12 @@ void core1_stop(void); void card10_poll(void); void card10_reset(void); + +enum card10_reboot_type { + Reboot_Normal, + Reboot_USBStorage, + Reboot_Shortcut +}; + +void card10_reboot(enum card10_reboot_type type); #endif diff --git a/pycardium/meson.build b/pycardium/meson.build index cf5f5ba57ecdbdb4d321cd5d6bc08dfc151024ff..011598b8a168ced30cc0a8a759a217183308f7b8 100644 --- a/pycardium/meson.build +++ b/pycardium/meson.build @@ -9,6 +9,7 @@ modsrc = files( 'modules/light_sensor.c', 'modules/fat_file.c', 'modules/fat_reader_import.c', + 'modules/core.c', ) ################################# diff --git a/pycardium/modules/core.c b/pycardium/modules/core.c new file mode 100644 index 0000000000000000000000000000000000000000..6057ffbee585b51595551467c675c85b209c8e28 --- /dev/null +++ b/pycardium/modules/core.c @@ -0,0 +1,37 @@ +#include "py/obj.h" +#include "py/runtime.h" +#include "py/builtin.h" + +#include "epicardium.h" + +static mp_obj_t mp_core_reboot(mp_obj_t state_obj) +{ + size_t len = 0; + const char* type = mp_obj_str_get_data(state_obj, &len); + epic_core_reboot(type); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(core_reboot_obj, mp_core_reboot); + +static mp_obj_t mp_core_reset(mp_obj_t state_obj) { + epic_core_reboot("normal"); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(core_reset_obj, mp_core_reset); + +static const mp_rom_map_elem_t core_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_core) }, + { MP_ROM_QSTR(MP_QSTR_reboot), MP_ROM_PTR(&core_reboot_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&core_reset_obj) } +}; +static MP_DEFINE_CONST_DICT(core_module_globals, core_module_globals_table); + +// Define module object. +const mp_obj_module_t core_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&core_module_globals, +}; + +/* Register the module to make it available in Python */ +/* clang-format off */ +MP_REGISTER_MODULE(MP_QSTR_core, core_module, 1); diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index dedc57c05513debe59a7441493dcf7d86ec0060d..3c72e10a16df1e1103366594405e4b463e6cdf97 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -72,3 +72,6 @@ Q(tell) Q(TextIOWrapper) Q(write) +Q(core) +Q(reboot) +Q(reset)