1919#include "asf/sam0/drivers/port/port.h"
2020#include "asf/sam0/drivers/sercom/usart/usart.h"
2121#include "asf/sam0/drivers/system/system.h"
22+ #include <board.h>
2223
2324#include "mpconfigboard.h"
2425#include "modmachine_pin.h"
@@ -158,16 +159,7 @@ void init_flash_fs() {
158159static char * stack_top ;
159160static char heap [8192 ];
160161
161- int main (int argc , char * * argv ) {
162- // initialise the cpu and peripherals
163- #if MICROPY_MIN_USE_SAMD21_MCU
164- void samd21_init (void );
165- samd21_init ();
166- #endif
167-
168- int stack_dummy ;
169- stack_top = (char * )& stack_dummy ;
170-
162+ void reset_mp () {
171163 #if MICROPY_ENABLE_GC
172164 gc_init (heap , heap + sizeof (heap ));
173165 #endif
@@ -176,11 +168,22 @@ int main(int argc, char **argv) {
176168 MP_STATE_PORT (mp_kbd_exception ) = mp_obj_new_exception (& mp_type_KeyboardInterrupt );
177169
178170 pin_init0 ();
171+ }
172+
173+ int main (int argc , char * * argv ) {
174+ // initialise the cpu and peripherals
175+ #if MICROPY_MIN_USE_SAMD21_MCU
176+ void samd21_init (void );
177+ samd21_init ();
178+ #endif
179179
180180 // Initialise the local flash filesystem.
181181 // Create it if needed, mount in on /flash, and set it as current dir.
182182 init_flash_fs ();
183183
184+ int stack_dummy ;
185+ reset_mp ();
186+
184187 #if MICROPY_REPL_EVENT_DRIVEN
185188 pyexec_event_repl_init ();
186189 for (;;) {
@@ -190,10 +193,24 @@ int main(int argc, char **argv) {
190193 }
191194 }
192195 #else
193- pyexec_friendly_repl ();
196+ // Main script is finished, so now go into REPL mode.
197+ // The REPL mode can change, or it can request a soft reset.
198+ int exit_code = 0 ;
199+ for (;;) {
200+ if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL ) {
201+ exit_code = pyexec_raw_repl ();
202+ } else {
203+ exit_code = pyexec_friendly_repl ();
204+ }
205+ if (exit_code == PYEXEC_FORCED_EXIT ) {
206+ mp_hal_stdout_tx_str ("soft reboot\r\n" );
207+ stack_top = (char * )& stack_dummy ;
208+ reset_mp ();
209+ } else if (exit_code != 0 ) {
210+ break ;
211+ }
212+ }
194213 #endif
195- //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
196- //do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
197214 mp_deinit ();
198215 return 0 ;
199216}
@@ -239,33 +256,34 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c
239256struct usart_module usart_instance ;
240257
241258void samd21_init (void ) {
259+ irq_initialize_vectors ();
260+ cpu_irq_enable ();
242261
243- irq_initialize_vectors ();
244- cpu_irq_enable ();
262+ // Initialize the sleep manager
263+ sleepmgr_init ();
245264
246- // Initialize the sleep manager
247- sleepmgr_init ();
265+ system_init ();
248266
249- system_init ();
267+ delay_init ();
250268
251- delay_init ();
269+ board_init ();
252270
253- // Uncomment to init PIN_PA17 for debugging.
254- // struct port_config pin_conf;
255- // port_get_config_defaults(&pin_conf);
256- //
257- // pin_conf.direction = PORT_PIN_DIR_OUTPUT;
258- // port_pin_set_config(MICROPY_HW_LED1, &pin_conf);
259- // port_pin_set_output_level(MICROPY_HW_LED1, false);
271+ // Uncomment to init PIN_PA17 for debugging.
272+ // struct port_config pin_conf;
273+ // port_get_config_defaults(&pin_conf);
274+ //
275+ // pin_conf.direction = PORT_PIN_DIR_OUTPUT;
276+ // port_pin_set_config(MICROPY_HW_LED1, &pin_conf);
277+ // port_pin_set_output_level(MICROPY_HW_LED1, false);
260278
261- #ifdef USB_REPL
262- udc_start ();
263- #endif
279+ #ifdef USB_REPL
280+ udc_start ();
281+ #endif
264282
265- // TODO(tannewt): Switch to proper pyb based UARTs.
266- #ifdef UART_REPL
267- configure_usart ();
268- #endif
283+ // TODO(tannewt): Switch to proper pyb based UARTs.
284+ #ifdef UART_REPL
285+ configure_usart ();
286+ #endif
269287}
270288
271289#endif
0 commit comments