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