00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <stdio.h>
00051 #include "compiler.h"
00052 #include "host_cdc_task.h"
00053 #if (defined CDC_USE_UART)&& (CDC_USE_UART== ENABLED)
00054 # include "usart.h"
00055
00056 #endif
00057 #include "conf_usb.h"
00058
00059
00060 #if USB_HOST_FEATURE == ENABLED
00061
00062 #include "board.h"
00063 #ifdef FREERTOS_USED
00064 #include "FreeRTOS.h"
00065 #include "task.h"
00066 #endif
00067 #include "conf_usb.h"
00068 #include "cdc.h"
00069 #include "usb_drv.h"
00070 #include "usb_host_enum.h"
00071 #include "usb_host_task.h"
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 U8 cdc_interface_comm;
00084
00085 U8 pipe_cdc_comm_int;
00086 U8 pipe_cdc_data_bulkin;
00087 U8 pipe_cdc_data_bulkout;
00088
00089 U8 host_tx_cnt;
00090 U8 host_rx_cnt;
00091 U8 cdc_stream_out_array[CDC_STREAM_OUT_SIZE];
00092 U8 cdc_stream_in_array[CDC_STREAM_IN_SIZE];
00093
00094 volatile Bool cdc_new_device_connected;
00095 volatile Bool cdc_connected;
00096 static volatile U16 sof_cnt;
00097
00098
00102 void host_cdc_task_init(void)
00103 {
00104 sof_cnt = 0;
00105 host_tx_cnt = 0;
00106 host_rx_cnt = 0;
00107 cdc_interface_comm = 0;
00108 cdc_new_device_connected = FALSE;
00109 cdc_connected = FALSE;
00110
00111 #ifdef FREERTOS_USED
00112 xTaskCreate(host_cdc_task,
00113 configTSK_USB_HCDC_NAME,
00114 configTSK_USB_HCDC_STACK_SIZE,
00115 NULL,
00116 configTSK_USB_HCDC_PRIORITY,
00117 NULL);
00118 #endif // FREERTOS_USED
00119 }
00120
00121
00125 #ifdef FREERTOS_USED
00126 void host_cdc_task(void *pvParameters)
00127 #else
00128 void host_cdc_task(void)
00129 #endif
00130 {
00131 U8 i;
00132 int c;
00133 static Bool startup=TRUE;
00134
00135 #ifdef FREERTOS_USED
00136 portTickType xLastWakeTime;
00137
00138 xLastWakeTime = xTaskGetTickCount();
00139 while (TRUE)
00140 {
00141 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_HCDC_PERIOD);
00142
00143 #endif // FREERTOS_USED
00144
00145
00146 if( Is_host_ready() )
00147 {
00148
00149 if( cdc_new_device_connected )
00150 {
00151 cdc_new_device_connected = FALSE;
00152
00153
00154 for( i=0 ; i<Get_nb_supported_interface() ; i++ )
00155 {
00156
00157 if( Get_class(i)==CDC_DATA_CLASS && Get_protocol(i)==NO_PROTOCOL )
00158 {
00159 cdc_connected=TRUE;
00160
00161
00162 if (Is_ep_in(i, 0))
00163 {
00164 pipe_cdc_data_bulkin = Get_ep_pipe(i, 0);
00165 pipe_cdc_data_bulkout = Get_ep_pipe(i, 1);
00166 }
00167 else
00168 {
00169 pipe_cdc_data_bulkin = Get_ep_pipe(i, 1);
00170 pipe_cdc_data_bulkout = Get_ep_pipe(i, 0);
00171 }
00172 Host_enable_continuous_in_mode(pipe_cdc_data_bulkin);
00173 Host_unfreeze_pipe(pipe_cdc_data_bulkin);
00174 continue;
00175 }
00176
00177
00178 #if (CDC_USE_MANAGEMENT_INTERFACE==ENABLED)
00179 if( Get_class(i)==CDC_COMM_CLASS && Get_protocol(i)==CDC_COMM_V25ter_PROTOCOL)
00180 {
00181 cdc_interface_comm = i;
00182 pipe_cdc_comm_int = Get_ep_pipe(i, 0);
00183 Host_enable_continuous_in_mode(pipe_cdc_comm_int);
00184 Host_unfreeze_pipe(pipe_cdc_comm_int);
00185 }
00186 #endif
00187 }
00188 }
00189
00190 if( Is_host_cdc_configured() )
00191 {
00192 if( startup )
00193 {
00194 printf("\r\nUSB HOST Communications Device Class demo.\r\n");
00195 startup=FALSE;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 if( (Is_host_in_received(pipe_cdc_data_bulkin)) && (Is_host_stall(pipe_cdc_data_bulkin)==FALSE) )
00207 {
00208 Host_reset_pipe_fifo_access(pipe_cdc_data_bulkin);
00209 #if (CDC_USE_UART==ENABLED)
00210 while( Host_byte_count(pipe_cdc_data_bulkin) != 0 )
00211 {
00212 usart_putchar(DBG_USART, Host_read_pipe_data(pipe_cdc_data_bulkin, 8) );
00213 LED_Toggle(LED_HOST_CDC_B0);
00214 }
00215 Host_ack_in_received(pipe_cdc_data_bulkin);
00216 Host_free_in(pipe_cdc_data_bulkin);
00217 #else
00218 while( (host_rx_cnt != CDC_STREAM_IN_SIZE) && (Host_byte_count(pipe_cdc_data_bulkin) != 0) )
00219 {
00220 cdc_stream_in_array[host_rx_cnt] = Host_read_pipe_data(pipe_cdc_data_bulkin, 8);
00221 host_rx_cnt++;
00222 LED_Toggle(LED_HOST_CDC_B0);
00223 }
00224 if( Host_byte_count(pipe_cdc_data_bulkin) == 0 )
00225 {
00226 Host_ack_in_received(pipe_cdc_data_bulkin);
00227 Host_free_in(pipe_cdc_data_bulkin);
00228 }
00229 #endif
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 #if (CDC_USE_UART==ENABLED)
00242
00243 if( usart_test_hit(DBG_USART) && (host_tx_cnt != CDC_STREAM_OUT_SIZE) )
00244 {
00245 if( USART_SUCCESS==usart_read_char(DBG_USART, &c) )
00246 {
00247 cdc_stream_out_array[host_tx_cnt] = c;
00248 LED_Toggle(LED_HOST_CDC_B1);
00249 host_tx_cnt++;
00250 }
00251 else
00252 usart_reset_status( DBG_USART );
00253 }
00254 #endif
00255
00256
00257 if(((sof_cnt>=CDC_NB_MS_BEFORE_FLUSH) && (host_tx_cnt!=0)) || (host_tx_cnt == CDC_STREAM_OUT_SIZE))
00258 {
00259 sof_cnt=0;
00260 cdc_pipe_out_usb_flush();
00261 }
00262
00263
00264
00265
00266 if (Is_host_in_received(pipe_cdc_comm_int))
00267 {
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 Host_ack_in_received(pipe_cdc_comm_int);
00279 Host_free_in(pipe_cdc_comm_int);
00280 }
00281 }
00282 }
00283 #ifdef FREERTOS_USED
00284 }
00285 #endif
00286 }
00287
00288
00296 void host_sof_action(void)
00297 {
00298 sof_cnt++;
00299 }
00300
00301
00307 void cdc_pipe_out_usb_flush(void)
00308 {
00309 Host_freeze_pipe(pipe_cdc_data_bulkin);
00310 if (PIPE_GOOD == host_send_data(pipe_cdc_data_bulkout, host_tx_cnt, cdc_stream_out_array))
00311 host_tx_cnt = 0;
00312
00313 Host_unfreeze_pipe(pipe_cdc_data_bulkin);
00314 }
00315
00316
00319 void host_cdc_send_encapsulated_command(void)
00320 {
00321 usb_request.bmRequestType = 0x21;
00322 usb_request.bRequest = SEND_ENCAPSULATED_COMMAND;
00323 usb_request.wValue = 0;
00324 usb_request.wIndex = cdc_interface_comm;
00325 usb_request.wLength = ENCAPSULATED_PACKET_LENGTH;
00326 usb_request.incomplete_read = FALSE;
00327 host_transfer_control(data_stage);
00328 }
00329
00330
00335 void host_cdc_get_encapsulated_response(void)
00336 {
00337 usb_request.bmRequestType = 0xA1;
00338 usb_request.bRequest = GET_ENCAPSULATED_COMMAND;
00339 usb_request.wValue = 0;
00340 usb_request.wIndex = cdc_interface_comm;
00341 usb_request.wLength = ENCAPSULATED_PACKET_LENGTH;
00342 usb_request.incomplete_read = FALSE;
00343 host_transfer_control(data_stage);
00344 }
00345
00346
00349 void host_cdc_set_line_coding(void)
00350 {
00351 usb_request.bmRequestType = 0x21;
00352 usb_request.bRequest = SET_LINE_CODING;
00353 usb_request.wValue = 0;
00354 usb_request.wIndex = cdc_interface_comm;
00355 usb_request.wLength = 7;
00356 usb_request.incomplete_read = FALSE;
00357 host_transfer_control(data_stage);
00358 }
00359
00360
00365 void host_cdc_get_line_coding(void)
00366 {
00367 usb_request.bmRequestType = 0xA1;
00368 usb_request.bRequest = GET_LINE_CODING;
00369 usb_request.wValue = 0;
00370 usb_request.wIndex = cdc_interface_comm;
00371 usb_request.wLength = 7;
00372 usb_request.incomplete_read = FALSE;
00373 host_transfer_control(data_stage);
00374 }
00375
00376
00377 #endif // USB_HOST_FEATURE == ENABLED