Skip to content

Commit ccacdf4

Browse files
committed
stmhal: Clean up reset/soft-reset code; fix bug init'ing VCP exc.
Make a clearer distinction between init functions that must be done before any scripts can run (xxx_init0) and those that can be safely deferred (xxx_init). Fix bug initialising USB VCP exception. Addresses issue adafruit#788. Re-order some init function to improve reliability of reset/soft-reset.
1 parent 8dbbbbc commit ccacdf4

File tree

9 files changed

+57
-41
lines changed

9 files changed

+57
-41
lines changed

stmhal/extint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ const mp_obj_type_t extint_type = {
346346
.locals_dict = (mp_obj_t)&extint_locals_dict,
347347
};
348348

349-
void extint_init(void) {
349+
void extint_init0(void) {
350350
for (extint_vector_t *v = extint_vector; v < &extint_vector[EXTI_NUM_VECTORS]; v++) {
351351
v->callback_obj = mp_const_none;
352352
v->param = NULL;

stmhal/extint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#define EXTI_TRIGGER_FALLING (offsetof(EXTI_TypeDef, FTSR))
4747
#define EXTI_TRIGGER_RISING_FALLING (EXTI_TRIGGER_RISING + EXTI_TRIGGER_FALLING) // just different from RISING or FALLING
4848

49-
void extint_init(void);
49+
void extint_init0(void);
5050

5151
uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t callback_obj, bool override_callback_obj, void *param);
5252

stmhal/main.c

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,26 @@ int main(void) {
333333
pyb_stdio_uart = NULL;
334334
#endif
335335

336-
readline_init();
337-
pin_init();
338-
extint_init();
336+
// Initialise low-level sub-systems. Here we need to very basic things like
337+
// zeroing out memory and resetting any of the sub-systems. Following this
338+
// we can run Python scripts (eg boot.py), but anything that is configurable
339+
// by boot.py must be set after boot.py is run.
340+
341+
readline_init0();
342+
pin_init0();
343+
extint_init0();
344+
timer_init0();
345+
346+
#if MICROPY_HW_ENABLE_RNG
347+
rng_init0();
348+
#endif
339349

340-
// local filesystem init
350+
i2c_init0();
351+
spi_init0();
352+
pyb_usb_init0();
353+
354+
// Initialise the local flash filesystem.
355+
// Create it if needed, and mount in on /flash.
341356
{
342357
// try to mount the flash
343358
FRESULT res = f_mount(&fatfs0, "/flash", 1);
@@ -383,7 +398,11 @@ int main(void) {
383398
}
384399
}
385400

386-
// make sure we have a /flash/boot.py
401+
// The current directory is used as the boot up directory.
402+
// It is set to the internal flash filesystem by default.
403+
f_chdrive("/flash");
404+
405+
// Make sure we have a /flash/boot.py. Create it if needed.
387406
{
388407
FILINFO fno;
389408
#if _USE_LFN
@@ -419,9 +438,6 @@ int main(void) {
419438
}
420439
}
421440

422-
// root device defaults to internal flash filesystem
423-
f_chdrive("/flash");
424-
425441
#if defined(USE_DEVICE_MODE)
426442
usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH;
427443
#endif
@@ -433,7 +449,7 @@ int main(void) {
433449
if (res != FR_OK) {
434450
printf("[SD] could not mount SD card\n");
435451
} else {
436-
// use SD card as root device
452+
// use SD card as current directory
437453
f_chdrive("/sd");
438454

439455
// TODO these should go before the /flash entries in the path
@@ -448,9 +464,6 @@ int main(void) {
448464
}
449465
}
450466
}
451-
#else
452-
// Get rid of compiler warning if no SDCARD is configured.
453-
(void)first_soft_reset;
454467
#endif
455468

456469
// run boot.py, if it exists
@@ -469,6 +482,10 @@ int main(void) {
469482
led_state(3, 0);
470483
led_state(4, 0);
471484

485+
// Now we initialise sub-systems that need configuration from boot.py,
486+
// or whose initialisation can be safely deferred until after running
487+
// boot.py.
488+
472489
#if defined(USE_HOST_MODE)
473490
// USB host
474491
pyb_usb_host_init();
@@ -487,15 +504,6 @@ int main(void) {
487504
}
488505
#endif
489506

490-
timer_init0();
491-
492-
#if MICROPY_HW_ENABLE_RNG
493-
rng_init0();
494-
#endif
495-
496-
i2c_init0();
497-
spi_init0();
498-
499507
#if MICROPY_HW_HAS_MMA7660
500508
// MMA accel: init and reset
501509
accel_init();
@@ -511,7 +519,15 @@ int main(void) {
511519
dac_init();
512520
#endif
513521

514-
// now that everything is initialised, run main script
522+
#if MICROPY_HW_ENABLE_CC3K
523+
// wifi using the CC3000 driver
524+
pyb_wlan_init();
525+
pyb_wlan_start();
526+
#endif
527+
528+
// At this point everything is fully configured and initialised.
529+
530+
// Run the main script from the current directory.
515531
if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
516532
const char *main_py;
517533
if (pyb_config_main == MP_OBJ_NULL) {
@@ -527,14 +543,8 @@ int main(void) {
527543
}
528544
}
529545

530-
#if MICROPY_HW_ENABLE_CC3K
531-
// wifi using the CC3000 driver
532-
pyb_wlan_init();
533-
pyb_wlan_start();
534-
#endif
535-
536-
// enter REPL
537-
// REPL mode can change, or it can request a soft reset
546+
// Main script is finished, so now go into REPL mode.
547+
// The REPL mode can change, or it can request a soft reset.
538548
for (;;) {
539549
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
540550
if (pyexec_raw_repl() != 0) {
@@ -547,6 +557,8 @@ int main(void) {
547557
}
548558
}
549559

560+
// soft reset
561+
550562
printf("PYB: sync filesystems\n");
551563
storage_flush();
552564

stmhal/pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ STATIC mp_obj_t pin_class_mapper;
9898
STATIC mp_obj_t pin_class_map_dict;
9999
STATIC bool pin_class_debug;
100100

101-
void pin_init(void) {
101+
void pin_init0(void) {
102102
pin_class_mapper = mp_const_none;
103103
pin_class_map_dict = mp_const_none;
104104
pin_class_debug = false;

stmhal/pin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ typedef struct {
7878
extern const pin_named_pins_obj_t pin_board_pins_obj;
7979
extern const pin_named_pins_obj_t pin_cpu_pins_obj;
8080

81-
void pin_init(void);
81+
void pin_init0(void);
8282
const pin_obj_t *pin_find(mp_obj_t user_obj);
8383
const pin_obj_t *pin_find_named_pin(const pin_named_pin_t *pins, const char *name);
8484
const pin_af_obj_t *pin_find_af(const pin_obj_t *pin, uint8_t fn, uint8_t unit, uint8_t pin_type);

stmhal/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL,
5252

5353
enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O };
5454

55-
void readline_init(void) {
55+
void readline_init0(void) {
5656
memset(readline_hist, 0, READLINE_HIST_SIZE * sizeof(const char*));
5757
}
5858

stmhal/readline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
void readline_init(void);
27+
void readline_init0(void);
2828
int readline(vstr_t *line, const char *prompt);

stmhal/usb.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@
4545
USBD_HandleTypeDef hUSBDDevice;
4646
#endif
4747

48-
static int dev_is_enabled = 0;
49-
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
48+
STATIC int dev_is_enabled = 0;
49+
STATIC mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
50+
51+
void pyb_usb_init0(void) {
52+
// create an exception object for interrupting by VCP
53+
mp_const_vcp_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "VCPInterrupt");
54+
USBD_CDC_SetInterrupt(VCP_CHAR_NONE, mp_const_vcp_interrupt);
55+
}
5056

5157
void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
5258
#ifdef USE_DEVICE_MODE
@@ -72,9 +78,6 @@ void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
7278
USBD_Start(&hUSBDDevice);
7379
}
7480
dev_is_enabled = 1;
75-
76-
// create an exception object for interrupting by VCP
77-
mp_const_vcp_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "VCPInterrupt");
7881
#endif
7982
}
8083

stmhal/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef enum {
4343

4444
const mp_obj_type_t pyb_usb_vcp_type;
4545

46+
void pyb_usb_init0(void);
4647
void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium);
4748
void pyb_usb_dev_stop(void);
4849
bool usb_vcp_is_enabled(void);

0 commit comments

Comments
 (0)