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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   ACM CDC Class                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_cdc_acm.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_cdc_acm_reception_callback           PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the callback from the USBX transfer functions,     */
46 /*    it is called when a full or partial transfer has been done for a    */
47 /*    bulk in transfer. It calls back the application.                    */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    transfer_request                      Pointer to transfer request   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_cdc_acm_reception_callback(UX_TRANSFER * transfer_request)74 VOID  _ux_host_class_cdc_acm_reception_callback (UX_TRANSFER *transfer_request)
75 {
76 
77 UX_HOST_CLASS_CDC_ACM               *cdc_acm;
78 UX_HOST_CLASS_CDC_ACM_RECEPTION     *cdc_acm_reception;
79 
80     /* Get the class instance for this transfer request.  */
81     cdc_acm =  (UX_HOST_CLASS_CDC_ACM *) transfer_request -> ux_transfer_request_class_instance;
82 
83     /* Get the pointer to the acm reception structure.  */
84     cdc_acm_reception =  cdc_acm -> ux_host_class_cdc_acm_reception;
85 
86     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
87     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
88     {
89 
90         /* The reception is stopped.  */
91         cdc_acm_reception -> ux_host_class_cdc_acm_reception_state =  UX_HOST_CLASS_CDC_ACM_RECEPTION_STATE_STOPPED;
92 
93         /* We do not proceed.  */
94         return;
95 
96     }
97 
98     /* Move to the next reception buffer.  Check if we are at the end of the application buffer.  */
99     if (cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head + cdc_acm_reception -> ux_host_class_cdc_acm_reception_block_size >=
100         cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_buffer + cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_buffer_size)
101 
102             /* Program the head to be at the beginning of the application buffer.  */
103             cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head =  cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_buffer;
104 
105     else
106 
107             /* Program the head to be after the current buffer.  */
108             cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head +=  cdc_acm_reception -> ux_host_class_cdc_acm_reception_block_size;
109 
110     /* OVERFLOW check: if the head reaches the tail buffer that contains reception data */
111     if (cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_tail ==  cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head)
112     {
113 
114         /* Error trap. */
115         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
116 
117         /* If trace is enabled, insert this event into the trace buffer.  */
118         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_BUFFER_OVERFLOW, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
119 
120         /* We have an overflow. We cannot continue.  Report to the application.  */
121         cdc_acm_reception -> ux_host_class_cdc_acm_reception_callback(cdc_acm, UX_BUFFER_OVERFLOW, UX_NULL, 0);
122 
123         /* And stop the transfer in progress flag.  */
124         cdc_acm_reception -> ux_host_class_cdc_acm_reception_state =  UX_HOST_CLASS_CDC_ACM_RECEPTION_STATE_STOPPED;
125 
126         return;
127     }
128 
129     /* We need to report this transfer to the application.  */
130     cdc_acm_reception -> ux_host_class_cdc_acm_reception_callback(cdc_acm,
131                                                                     transfer_request -> ux_transfer_request_completion_code,
132                                                                     transfer_request -> ux_transfer_request_data_pointer,
133                                                                     transfer_request -> ux_transfer_request_actual_length);
134 
135     /* Set data pointer to the next reception buffer.  */
136     transfer_request -> ux_transfer_request_data_pointer = cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head;
137 
138     /* Arm another transfer.  */
139     _ux_host_stack_transfer_request(transfer_request);
140 
141     /* There is no status to be reported back to the stack.  */
142     return;
143 }
144 
145