From ae1cc22d725d641dbe8317a8722b6f6a39b86a35 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 6 Aug 2018 20:28:20 +0700 Subject: [PATCH] update bootloader main --- src/main.c | 104 +++++++++++++++++++++++++++++++++------------- src/usb/msc_uf2.c | 18 -------- src/usb/msc_uf2.h | 8 ---- 3 files changed, 76 insertions(+), 54 deletions(-) diff --git a/src/main.c b/src/main.c index 7ece482..51c2c37 100644 --- a/src/main.c +++ b/src/main.c @@ -57,11 +57,23 @@ #include "nrf_delay.h" #include "pstorage.h" +#ifdef NRF52840_XXAA #include "nrf_usbd.h" - #include "tusb.h" #include "usb/msc_uf2.h" +/* tinyusb function that handles power event (detected, ready, removed) + * We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. */ +extern void tusb_hal_nrf_power_event(uint32_t event); + +// TODO fully move to nrfx +enum { + NRFX_POWER_USB_EVT_DETECTED, /**< USB power detected on the connector (plugged in). */ + NRFX_POWER_USB_EVT_REMOVED, /**< USB power removed from the connector. */ + NRFX_POWER_USB_EVT_READY /**< USB power regulator ready. */ +}; +#endif + #define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0] @@ -192,6 +204,65 @@ void blinky_ota_disconneted(void) } +void board_init(void) +{ + button_pin_init(BOOTLOADER_BUTTON); + button_pin_init(FRESET_BUTTON); + nrf_delay_us(100); // wait for the pin state is stable + + led_pin_init(LED_RED); + led_pin_init(LED_BLUE); + + // Init scheduler and timer (use scheduler) + APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE); + app_timer_init(); +} + +void boad_usb_init(void) +{ + // USB power may already be ready at this time -> no event generated + // We need to invoke the handler based on the status initially + uint32_t usb_reg; + +#ifdef SOFTDEVICE_PRESENT + uint8_t sd_en = false; + (void) sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) { + sd_power_usbdetected_enable(true); + sd_power_usbpwrrdy_enable(true); + sd_power_usbremoved_enable(true); + + sd_power_usbregstatus_get(&usb_reg); + }else +#else + { + // Power module init + const nrfx_power_config_t pwr_cfg = { 0 }; + nrfx_power_init(&pwr_cfg); + + // Register tusb function as USB power handler + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; + nrfx_power_usbevt_init(&config); + + nrfx_power_usbevt_enable(); + + usb_reg = NRF_POWER->USBREGSTATUS; + } +#endif + + if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); + } + + if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); + } + + // Init tusb stack + tusb_init(); +} + /**@brief Function for initializing the BLE stack. * * @details Initializes the SoftDevice and the BLE event interrupt. @@ -293,18 +364,7 @@ int main(void) APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE); - /* Initialize GPIOs - * For metro52 LED_BLUE is muxed with FRESET */ - button_pin_init(BOOTLOADER_BUTTON); - button_pin_init(FRESET_BUTTON); - nrf_delay_us(100); // wait for the pin state is stable - - led_pin_init(LED_RED); - led_pin_init(LED_BLUE); // on metro52 will override FRESET - - // Init scheduler and timer (use scheduler) - APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE); - app_timer_init(); + board_init(); /* Initialize a blinky timer to show that we're in bootloader */ (void) app_timer_create(&blinky_timer_id, APP_TIMER_MODE_REPEATED, blinky_handler); @@ -312,9 +372,6 @@ int main(void) // Init bootloader (void) bootloader_init(); - // Init msc flash, must be after bootloader_init(), before SD init - msc_uf2_init(); - if (bootloader_dfu_sd_in_progress()) { APP_ERROR_CHECK( bootloader_dfu_sd_update_continue() ); @@ -330,8 +387,7 @@ int main(void) app_timer_start(blinky_timer_id, APP_TIMER_TICKS(LED_BLINK_INTERVAL), NULL); } - // Init usb stack - tusb_init(); + boad_usb_init(); /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/ // DFU button pressed @@ -485,12 +541,12 @@ void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) //--------------------------------------------------------------------+ void tud_mount_cb(void) { - msc_uf2_mount(); + } void tud_umount_cb(void) { - msc_uf2_umount(); + } uint32_t tusb_hal_millis(void) @@ -532,14 +588,6 @@ uint32_t proc_soc(void) pstorage_sys_event_handler(soc_evt); /*------------- usb power event handler -------------*/ - extern void tusb_hal_nrf_power_event(uint32_t evt); - enum // TODO remove when migrating to SDK15 - { - NRFX_POWER_USB_EVT_DETECTED = 0, - NRFX_POWER_USB_EVT_REMOVED, - NRFX_POWER_USB_EVT_READY - }; - int32_t usbevt = (soc_evt == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED: (soc_evt == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY : (soc_evt == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1; diff --git a/src/usb/msc_uf2.c b/src/usb/msc_uf2.c index 36bf506..c8a8f20 100644 --- a/src/usb/msc_uf2.c +++ b/src/usb/msc_uf2.c @@ -50,24 +50,6 @@ void read_block(uint32_t block_no, uint8_t *data); int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *state*/); -/*------------------------------------------------------------------*/ -/* API - *------------------------------------------------------------------*/ -void msc_uf2_init(void) -{ - -} - -void msc_uf2_mount(void) -{ - -} - -void msc_uf2_umount(void) -{ - -} - //--------------------------------------------------------------------+ // tinyusb callbacks //--------------------------------------------------------------------+ diff --git a/src/usb/msc_uf2.h b/src/usb/msc_uf2.h index 80bb010..b3a4294 100644 --- a/src/usb/msc_uf2.h +++ b/src/usb/msc_uf2.h @@ -57,14 +57,6 @@ VERIFY_STATIC( MSC_UF2_FLASH_ADDR_START+MSC_UF2_FLASH_SIZE == BOOTLOADER_REGION_START-DFU_APP_DATA_RESERVED, ); -/*------------------------------------------------------------------*/ -/* Note ATTR_WEAK is used when CFG_TUD_MSC = 0 - *------------------------------------------------------------------*/ - -ATTR_WEAK void msc_uf2_init(void); -ATTR_WEAK void msc_uf2_mount(void); -ATTR_WEAK void msc_uf2_umount(void); - #ifdef __cplusplus }