laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/osmo-ccid-firmware/+/39445?usp=email )

Change subject: fix usb usb reset handling by delayed soft reset
......................................................................

fix usb usb reset handling by delayed soft reset

Requires BL clock init, not compatible with old bootloaders!

Will always reboot to DFU mode with old bootloader/wrong clocks.

Change-Id: I0939930a42f3009abf7e670561a123963bbd3845
---
M sysmoOCTSIM/atmel_start.c
M sysmoOCTSIM/hpl/core/hpl_init.c
M sysmoOCTSIM/hpl/usb/hpl_usb.c
M sysmoOCTSIM/main.c
M sysmoOCTSIM/usb_start.c
5 files changed, 34 insertions(+), 22 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  Jenkins Builder: Verified




diff --git a/sysmoOCTSIM/atmel_start.c b/sysmoOCTSIM/atmel_start.c
index d6304fa..4d52d0d 100644
--- a/sysmoOCTSIM/atmel_start.c
+++ b/sysmoOCTSIM/atmel_start.c
@@ -9,5 +9,4 @@
 #ifdef ENABLE_DBG_UART7
        stdio_redirect_init();
 #endif
-       usb_init();
 }
diff --git a/sysmoOCTSIM/hpl/core/hpl_init.c b/sysmoOCTSIM/hpl/core/hpl_init.c
index be0db93..4121053 100644
--- a/sysmoOCTSIM/hpl/core/hpl_init.c
+++ b/sysmoOCTSIM/hpl/core/hpl_init.c
@@ -53,14 +53,15 @@
 void _init_chip(void)
 {
        hri_nvmctrl_set_CTRLA_RWS_bf(NVMCTRL, CONF_NVM_WAIT_STATE);
-
+#if 0
        _osc32kctrl_init_sources();
        _oscctrl_init_sources();
        _mclk_init();
-#if _GCLK_INIT_1ST
+       #if _GCLK_INIT_1ST
        _gclk_init_generators_by_fref(_GCLK_INIT_1ST);
-#endif
+       #endif
        _oscctrl_init_referenced_generators();
+#endif
        _gclk_init_generators_by_fref(_GCLK_INIT_LAST);

 #if CONF_DMAC_ENABLE
diff --git a/sysmoOCTSIM/hpl/usb/hpl_usb.c b/sysmoOCTSIM/hpl/usb/hpl_usb.c
index 99e4606..c7e2d33 100644
--- a/sysmoOCTSIM/hpl/usb/hpl_usb.c
+++ b/sysmoOCTSIM/hpl/usb/hpl_usb.c
@@ -44,6 +44,7 @@
 /* save previous setup state to allow device reset when receving usb reset 
after the device
  * was previously properly configured, i.e. when powered externally and usb is 
disconnected and reconnected */
 volatile bool address_was_set = false;
+volatile bool delayed_usb_reset = false;

 /**
  * \brief Dummy callback function
@@ -980,14 +981,9 @@

        _usb_d_dev_reset_epts();

-       if(address_was_set == true) {
-               _usb_d_dev_detach();
+       if (address_was_set == true) {
                address_was_set = 0;
-               delay_ms(100);
-               __disable_irq();
-               __DMB();
-               __DSB();
-               NVIC_SystemReset();
+               delayed_usb_reset = true;
        }

        dev_inst.callbacks.event(USB_EV_RESET, 0);
diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index d1524c5..afb70e9 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -65,8 +65,6 @@
        /* increase drive strength of 20Mhz SIM clock output to 8mA
         * (there are 8 inputs + traces to drive!) */
        hri_port_set_PINCFG_DRVSTR_bit(PORT, 0, 11);
-
-       ccid_app_init();
 }

 /***********************************************************************
@@ -467,6 +465,17 @@

 char rstcause_buf[RSTCAUSE_STR_SIZE];

+void do_usb_res(void)
+{
+       hri_usbdevice_set_CTRLA_SWRST_bit(USB);
+       uint32_t *application_start_address = (uint32_t *)(16384);
+       __set_MSP(*application_start_address);
+       __DSB();
+       __ISB();
+       asm("bx %0" ::"r"(*(application_start_address + 1)));
+}
+
+extern volatile bool delayed_usb_reset;

 int main(void)
 {
@@ -494,18 +503,23 @@

 #endif

-       atmel_start_init();
+       // return to dfu on mismatched old BL with improper clock config.
+       if (hri_gclk_read_GENCTRL_SRC_bf(GCLK, 0) == 
GCLK_GENCTRL_SRC_XOSC1_Val) {
+               *(uint32_t *)HSRAM_ADDR = 0x44465521;
+               NVIC_SystemReset();
+       }
+
        get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf));
        str_to_usb_desc(sernr_buf, sizeof(sernr_buf), sernr_buf_descr, 
sizeof(sernr_buf_descr));

        str_to_usb_desc(product_buf, sizeof(product_buf) - 1, 
product_buf_descr, sizeof(product_buf_descr));
        get_rstcause_str(rstcause_buf);

-
-
-       usb_start();
-
+       atmel_start_init();
        board_init();
+       usb_init();
+       usb_start();
+       ccid_app_init();

 #ifdef WITH_DEBUG_CDC
        command_init("sysmoOCTSIM> ");
@@ -562,6 +576,8 @@

 //     command_print_prompt();
        while (true) { // main loop
+               if (delayed_usb_reset)
+                       do_usb_res();
                command_try_recv();
                poll_card_detect();
                submit_next_irq();
diff --git a/sysmoOCTSIM/usb_start.c b/sysmoOCTSIM/usb_start.c
index d4057be..6946126 100644
--- a/sysmoOCTSIM/usb_start.c
+++ b/sysmoOCTSIM/usb_start.c
@@ -10,13 +10,13 @@
 #include "usb_descriptors.h"

 #define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ
-
+#ifdef WITH_DEBUG_CDC
 /** Buffers to receive and echo the communication bytes. */
 static uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4];
-
+#endif
 /** Ctrl endpoint buffer */
 static uint8_t ctrl_buffer[64];
-
+#ifdef WITH_DEBUG_CDC
 /**
  * \brief Callback invoked when bulk OUT data received
  */
@@ -56,7 +56,7 @@
        /* No error. */
        return false;
 }
-
+#endif
 extern const struct usbd_descriptors usb_descs[];

 /* transmit given string descriptor */

--
To view, visit https://gerrit.osmocom.org/c/osmo-ccid-firmware/+/39445?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: osmo-ccid-firmware
Gerrit-Branch: master
Gerrit-Change-Id: I0939930a42f3009abf7e670561a123963bbd3845
Gerrit-Change-Number: 39445
Gerrit-PatchSet: 7
Gerrit-Owner: Hoernchen <ew...@sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <lafo...@osmocom.org>

Reply via email to