<p>Kévin Redon has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/12508">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">implement flashing-side of the DFU state machine<br><br>the USB-side state machine currently sets the length to 0,<br>preventing the actual flashing to be tested<br><br>Change-Id: I5ed9cc2a22ed5e41bb59a3ce3f21ab098cec48e7<br>---<br>M usb_start.c<br>1 file changed, 69 insertions(+), 17 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-asf4-dfu refs/changes/08/12508/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/usb_start.c b/usb_start.c</span><br><span>index d911db4..0ce8aba 100644</span><br><span>--- a/usb_start.c</span><br><span>+++ b/usb_start.c</span><br><span>@@ -45,15 +45,20 @@</span><br><span> /** Ctrl endpoint buffer */</span><br><span> static uint8_t ctrl_buffer[64];</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/** Application address in the flash</span><br><span style="color: hsl(120, 100%, 40%);">+ (</span><br><span style="color: hsl(120, 100%, 40%);">+ *  It comes after the bootloader.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  The NVMCTRL allows to reserve space at the beginning of the flash for the bootloader, but only in multiples of 8192 bytes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  The binary just with the USB stack already uses 8 kB, we reserve 16 kB for the bootloader (giving use a bit of margin for future fixes).</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define APPLICATION_ADDR (8192*2)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /**</span><br><span>  * \brief USB DFU Init</span><br><span>  */</span><br><span> void usb_dfu_init(void)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-   /* usb stack init */</span><br><span>         usbdc_init(ctrl_buffer);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-        /* usbdc_register_funcion inside */</span><br><span>  dfudf_init();</span><br><span> </span><br><span>    usbdc_start(single_desc);</span><br><span>@@ -61,27 +66,74 @@</span><br><span> }</span><br><span> </span><br><span> /**</span><br><span style="color: hsl(0, 100%, 40%);">- * Example of using CDC ACM Function.</span><br><span style="color: hsl(0, 100%, 40%);">- * \note</span><br><span style="color: hsl(0, 100%, 40%);">- * In this example, we will use a PC as a USB host:</span><br><span style="color: hsl(0, 100%, 40%);">- * - Connect the DEBUG USB on XPLAINED board to PC for program download.</span><br><span style="color: hsl(0, 100%, 40%);">- * - Connect the TARGET USB on XPLAINED board to PC for running program.</span><br><span style="color: hsl(0, 100%, 40%);">- * The application will behave as a virtual COM.</span><br><span style="color: hsl(0, 100%, 40%);">- * - Open a HyperTerminal or other COM tools in PC side.</span><br><span style="color: hsl(0, 100%, 40%);">- * - Send out a character or string and it will echo the content received.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief reset device</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_dfu_reset(const enum usb_event ev, const uint32_t param)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   (void)param; // not used</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (ev) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case USB_EV_RESET:</span><br><span style="color: hsl(120, 100%, 40%);">+            usbdc_detach(); // make sure we are detached</span><br><span style="color: hsl(120, 100%, 40%);">+          NVIC_SystemReset(); // initiate a system reset</span><br><span style="color: hsl(120, 100%, 40%);">+                break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     </span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Enter USB DFU runtime</span><br><span>  */</span><br><span> void usb_dfu(void)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-   while (!dfudf_is_enabled()) {</span><br><span style="color: hsl(0, 100%, 40%);">-           // wait DFU to be installed</span><br><span style="color: hsl(0, 100%, 40%);">-     };</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-      while (1) {</span><br><span style="color: hsl(120, 100%, 40%);">+   while (!dfudf_is_enabled()); // wait for DFU to be installed</span><br><span style="color: hsl(120, 100%, 40%);">+  gpio_set_pin_level(LED_SYSTEM, false); // switch LED on to indicate USB DFU stack is ready</span><br><span style="color: hsl(120, 100%, 40%);">+    while (true) { // main DFU infinite loop</span><br><span style="color: hsl(120, 100%, 40%);">+              // run the second part of the USB DFU state machine handling non-USB aspects</span><br><span style="color: hsl(120, 100%, 40%);">+          if (USB_DFU_STATE_DFU_DNLOAD_SYNC == dfu_state || USB_DFU_STATE_DFU_DNBUSY == dfu_state) { // there is some data to be flashed</span><br><span style="color: hsl(120, 100%, 40%);">+                        gpio_set_pin_level(LED_SYSTEM, true); // switch LED off to indicate we are flashing</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (dfu_download_length > 0) { // there is some data to be flashed</span><br><span style="color: hsl(120, 100%, 40%);">+                         int32_t rc = flash_write(&FLASH_0, APPLICATION_ADDR + dfu_download_progress, dfu_download_data, dfu_download_length); // write downloaded data chunk to flash</span><br><span style="color: hsl(120, 100%, 40%);">+                             if (ERR_NONE == rc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                 dfu_state = USB_DFU_STATE_DFU_DNLOAD_IDLE; // indicate flashing this block has been completed</span><br><span style="color: hsl(120, 100%, 40%);">+                         } else { // there has been a programming error</span><br><span style="color: hsl(120, 100%, 40%);">+                                        dfu_state = USB_DFU_STATE_DFU_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+                                  if (ERR_BAD_ADDRESS == rc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                          dfu_status = USB_DFU_STATUS_ERR_ADDRESS;</span><br><span style="color: hsl(120, 100%, 40%);">+                                      } else if (ERR_DENIED == rc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                                dfu_status = USB_DFU_STATUS_ERR_WRITE;</span><br><span style="color: hsl(120, 100%, 40%);">+                                        } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                                              dfu_status = USB_DFU_STATUS_ERR_PROG;</span><br><span style="color: hsl(120, 100%, 40%);">+                                 }</span><br><span style="color: hsl(120, 100%, 40%);">+                             }</span><br><span style="color: hsl(120, 100%, 40%);">+                     } else { // there was no data to flash</span><br><span style="color: hsl(120, 100%, 40%);">+                                // this case should not happen, but it's not a critical error</span><br><span style="color: hsl(120, 100%, 40%);">+                             dfu_state = USB_DFU_STATE_DFU_DNLOAD_IDLE; // indicate flashing can continue</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+                     gpio_set_pin_level(LED_SYSTEM, false); // switch LED on to indicate USB DFU can resume</span><br><span style="color: hsl(120, 100%, 40%);">+                }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (USB_DFU_STATE_DFU_MANIFEST == dfu_state) { // we can start manifestation (finish flashing)</span><br><span style="color: hsl(120, 100%, 40%);">+                        // in theory every DFU files should have a suffix to with a CRC to check the data</span><br><span style="color: hsl(120, 100%, 40%);">+                     // in practice most downloaded files are just the raw binary with DFU suffix</span><br><span style="color: hsl(120, 100%, 40%);">+                  dfu_manifestation_complete = true; // we completed flashing and all checks</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (usb_dfu_func_desc->bmAttributes & USB_DFU_ATTRIBUTES_MANIFEST_TOLERANT) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          dfu_state = USB_DFU_STATE_DFU_MANIFEST_SYNC;</span><br><span style="color: hsl(120, 100%, 40%);">+                  } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              dfu_state = USB_DFU_STATE_DFU_MANIFEST_WAIT_RESET;</span><br><span style="color: hsl(120, 100%, 40%);">+                    }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (USB_DFU_STATE_DFU_MANIFEST_WAIT_RESET == dfu_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (usb_dfu_func_desc->bmAttributes & USB_DFU_ATTRIBUTES_WILL_DETACH) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                usb_dfu_reset(USB_EV_RESET, 0); // immediately reset</span><br><span style="color: hsl(120, 100%, 40%);">+                  } else { // wait for USB reset</span><br><span style="color: hsl(120, 100%, 40%);">+                                usb_d_register_callback(USB_D_CB_EVENT, (FUNC_PTR)usb_dfu_reset); // register new USB reset event handler</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span>    }</span><br><span> }</span><br><span> </span><br><span> void usb_init(void)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span>       usb_dfu_init();</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/12508">change 12508</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/12508"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-asf4-dfu </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I5ed9cc2a22ed5e41bb59a3ce3f21ab098cec48e7 </div>
<div style="display:none"> Gerrit-Change-Number: 12508 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kévin Redon <kredon@sysmocom.de> </div>