1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device dfu Class                                                    */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_dfu.h"
28 #include "ux_device_stack.h"
29 
30 
31 #if defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_dfu_tasks_run                      PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the background task of the dfu class. It waits for */
45 /*    the dfu command to signal a DFU_DETACH stage and either force a     */
46 /*    disconnect from the device or wait for the host to detach.          */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    class_instance                              DFU class instance      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_utility_delay_ms                  Delay for some time           */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    USBX                                                                */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
69 /*                                                                        */
70 /**************************************************************************/
_ux_device_class_dfu_tasks_run(VOID * class_instance)71 UINT  _ux_device_class_dfu_tasks_run(VOID *class_instance)
72 {
73 
74 UX_SLAVE_CLASS_DFU              *dfu;
75 UX_SLAVE_DCD                    *dcd;
76 ULONG                           actual_flags;
77 
78     /* Get the dfu instance from this class container.  */
79     dfu =  (UX_SLAVE_CLASS_DFU *) class_instance;
80 
81     /* Process when there is flags.  */
82     if (dfu -> ux_device_class_dfu_flags == 0)
83         return(UX_STATE_IDLE);
84     actual_flags = dfu -> ux_device_class_dfu_flags;
85 
86     /* Check the source of event.  */
87     if (actual_flags & UX_DEVICE_CLASS_DFU_THREAD_EVENT_DISCONNECT)
88     {
89         dfu -> ux_device_class_dfu_flags &= ~UX_DEVICE_CLASS_DFU_THREAD_EVENT_DISCONNECT;
90 
91         /* We need to disconnect.  The control command for DETACH is still being processed, wait 2-3 ms. */
92         _ux_utility_delay_ms(2);
93 
94         /* Get the pointer to the DCD.  */
95         dcd =  &_ux_system_slave -> ux_system_slave_dcd;
96 
97         /* Issue a Soft Disconnect.  */
98         dcd -> ux_slave_dcd_function(dcd, UX_DCD_CHANGE_STATE, (VOID *) UX_DEVICE_FORCE_DISCONNECT);
99 
100     }
101 
102     /* Check the source of event.  */
103     if (actual_flags & UX_DEVICE_CLASS_DFU_THREAD_EVENT_WAIT_RESET)
104     {
105         dfu -> ux_device_class_dfu_flags &= ~UX_DEVICE_CLASS_DFU_THREAD_EVENT_WAIT_RESET;
106 
107         /* We need to wait for reset.  Arm a timer.  The timeout value is indicated in ms from
108             the device framework.  */
109         _ux_utility_delay_ms(_ux_system_slave -> ux_system_slave_device_dfu_detach_timeout);
110 
111         /* Check the mode.  */
112         if (_ux_system_slave -> ux_system_slave_device_dfu_mode ==  UX_DEVICE_CLASS_DFU_MODE_RUNTIME)
113 
114             /* We are still in RunTime mode. The host never reset. Revert to AppIdle state.  */
115             _ux_system_slave -> ux_system_slave_device_dfu_state_machine = UX_SYSTEM_DFU_STATE_APP_IDLE;
116 
117     }
118     return(UX_STATE_WAIT);
119 }
120 #endif
121