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_start PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function starts a reception with the ACM modem. This mechanism */
46 /* allows for non blocking calls based on a packet orientated round */
47 /* robbin buffer. When a packet is fully or partially received, an */
48 /* application callback function is invoked and a new transfer request */
49 /* is rescheduled. */
50 /* */
51 /* INPUT */
52 /* */
53 /* cdc_acm Pointer to cdc_acm class */
54 /* cdc_acm_reception Pointer to reception struct */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* Completion Status */
59 /* */
60 /* CALLS */
61 /* */
62 /* _ux_host_stack_transfer_request Process transfer request */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* Application */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
73 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
74 /* resulting in version 6.1 */
75 /* */
76 /**************************************************************************/
_ux_host_class_cdc_acm_reception_start(UX_HOST_CLASS_CDC_ACM * cdc_acm,UX_HOST_CLASS_CDC_ACM_RECEPTION * cdc_acm_reception)77 UINT _ux_host_class_cdc_acm_reception_start (UX_HOST_CLASS_CDC_ACM *cdc_acm,
78 UX_HOST_CLASS_CDC_ACM_RECEPTION *cdc_acm_reception)
79 {
80
81 UX_TRANSFER *transfer_request;
82 UINT status;
83
84 /* If trace is enabled, insert this event into the trace buffer. */
85 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_RECEPTION_START, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
86
87 /* Ensure the instance is valid. */
88 if (cdc_acm -> ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
89 {
90
91 /* Error trap. */
92 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
93
94 /* If trace is enabled, insert this event into the trace buffer. */
95 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
96
97 return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
98 }
99
100 /* As further protection, we must ensure this instance of the interface is the data interface and not
101 the control interface ! */
102 if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass != UX_HOST_CLASS_CDC_DATA_CLASS)
103 {
104
105 /* Error trap. */
106 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
107
108 /* If trace is enabled, insert this event into the trace buffer. */
109 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
110
111 return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
112 }
113
114 /* Start by aligning the head and tail of buffers to the same address supplied by the application. */
115 cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head = cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_buffer;
116 cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_tail = cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_buffer;
117
118 /* Get the pointer to the bulk in endpoint in the transfer_request. */
119 transfer_request = &cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint -> ux_endpoint_transfer_request;
120
121 /* Initialize the transfer request. */
122 transfer_request -> ux_transfer_request_class_instance = (VOID *) cdc_acm;
123 transfer_request -> ux_transfer_request_data_pointer = cdc_acm_reception -> ux_host_class_cdc_acm_reception_data_head;
124 transfer_request -> ux_transfer_request_requested_length = cdc_acm_reception -> ux_host_class_cdc_acm_reception_block_size;
125 transfer_request -> ux_transfer_request_completion_function = _ux_host_class_cdc_acm_reception_callback;
126
127 /* Save the acm reception structure in the acm structure. */
128 cdc_acm -> ux_host_class_cdc_acm_reception = cdc_acm_reception;
129
130 /* And declare we have a transfer in progress. */
131 cdc_acm_reception -> ux_host_class_cdc_acm_reception_state = UX_HOST_CLASS_CDC_ACM_RECEPTION_STATE_STARTED;
132
133 /* Arm a first transfer on the bulk in endpoint. There is a callback to this function so we return to the caller
134 right away. */
135 status = _ux_host_stack_transfer_request(transfer_request);
136
137 /* We do not know if the first transfer was successful yet. If the status is not OK, we need to stop the transfer
138 in progress flag. */
139 if (status != UX_SUCCESS)
140 cdc_acm_reception -> ux_host_class_cdc_acm_reception_state = UX_HOST_CLASS_CDC_ACM_RECEPTION_STATE_STOPPED;
141
142 return(status);
143 }
144
145