host_cdc_task.h File Reference


Detailed Description

Management of the USB host CDC task.

This file manages the USB host CDC task.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file host_cdc_task.h.

#include "conf_usb.h"
#include "usb_host_task.h"

Go to the source code of this file.

Defines

#define CDC_NB_MS_BEFORE_FLUSH   5
 Data Pipe flush timout in milli-second.
#define CDC_STREAM_IN_SIZE   0x10
 Size of receive buffer (must be less or equal than pipe size !).
#define CDC_STREAM_OUT_SIZE   0x10
 Size of transmit buffer (must be less or equal than pipe size !).
#define CDC_USE_MANAGEMENT_INTERFACE   ENABLED
 Enable or not the access to Management Element (Interrupt Pipe).
#define CDC_USE_UART   ENABLED
 Enable or not the USART to USB gateway features & functions.
#define ENCAPSULATED_PACKET_LENGTH   0x0A
 Set here the length of your encapsulated command (user defined).
#define Is_host_cdc_configured()   (cdc_connected && !Is_host_suspended())
#define LED_HOST_CDC_B0   LED_MONO0_GREEN
#define LED_HOST_CDC_B1   LED_MONO1_GREEN
#define LED_HOST_CDC_B2   LED_MONO2_GREEN
#define LED_HOST_CDC_B3   LED_MONO3_GREEN

Functions

void cdc_pipe_out_usb_flush (void)
 cdc_pipe_out_usb_flush
void host_cdc_get_encapsulated_response (void)
 This function requests a format-specific packet.
void host_cdc_get_line_coding (void)
 This function requests the communication settings from the device.
void host_cdc_send_encapsulated_command (void)
 This function sends a format-specific packet.
void host_cdc_set_line_coding (void)
 This function set communication settings on the device.
void host_cdc_task (void *pvParameters)
 This function manages the host CDC task.
void host_cdc_task_init (void)
 This function initializes the host CDC task.
void host_sof_action (void)
 host_sof_action

Variables

U8 cdc_stream_in_array [CDC_STREAM_IN_SIZE]
U8 cdc_stream_out_array [CDC_STREAM_OUT_SIZE]
volatile Bool ms_connected


Define Documentation

#define CDC_NB_MS_BEFORE_FLUSH   5

Data Pipe flush timout in milli-second.

Definition at line 94 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define CDC_STREAM_IN_SIZE   0x10

Size of receive buffer (must be less or equal than pipe size !).

Note:
: Irrelevent when CDC_USE_UART is DISABLED.

Definition at line 91 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define CDC_STREAM_OUT_SIZE   0x10

Size of transmit buffer (must be less or equal than pipe size !).

Note:
: Irrelevent when CDC_USE_UART is DISABLED.

Definition at line 86 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define CDC_USE_MANAGEMENT_INTERFACE   ENABLED

Enable or not the access to Management Element (Interrupt Pipe).

Possible values ENABLED or DISABLED

Definition at line 76 of file host_cdc_task.h.

#define CDC_USE_UART   ENABLED

Enable or not the USART to USB gateway features & functions.

Possible values ENABLED or DISABLED

Definition at line 81 of file host_cdc_task.h.

#define ENCAPSULATED_PACKET_LENGTH   0x0A

Set here the length of your encapsulated command (user defined).

Definition at line 97 of file host_cdc_task.h.

Referenced by host_cdc_get_encapsulated_response(), and host_cdc_send_encapsulated_command().

 
#define Is_host_cdc_configured (  )     (cdc_connected && !Is_host_suspended())

Definition at line 66 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define LED_HOST_CDC_B0   LED_MONO0_GREEN

Definition at line 100 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define LED_HOST_CDC_B1   LED_MONO1_GREEN

Definition at line 101 of file host_cdc_task.h.

Referenced by host_cdc_task().

#define LED_HOST_CDC_B2   LED_MONO2_GREEN

Definition at line 102 of file host_cdc_task.h.

#define LED_HOST_CDC_B3   LED_MONO3_GREEN

Definition at line 103 of file host_cdc_task.h.


Function Documentation

void cdc_pipe_out_usb_flush ( void   ) 

cdc_pipe_out_usb_flush

This function sends the data stored in the temporary transmit buffer. This function does nothing if there is no data in the buffer.

Definition at line 307 of file host_cdc_task.c.

References cdc_stream_out_array, host_tx_cnt, pipe_cdc_data_bulkin, and pipe_cdc_data_bulkout.

Referenced by host_cdc_task().

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;    // if frame not sent, will try again next time (no data loss)
00312 
00313    Host_unfreeze_pipe(pipe_cdc_data_bulkin);
00314 }

void host_cdc_get_encapsulated_response ( void   ) 

This function requests a format-specific packet.

Note:
The response is stored into data_stage.

Definition at line 335 of file host_cdc_task.c.

References cdc_interface_comm, ENCAPSULATED_PACKET_LENGTH, and GET_ENCAPSULATED_COMMAND.

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 }

void host_cdc_get_line_coding ( void   ) 

This function requests the communication settings from the device.

Note:
The response is stored into data_stage.

Definition at line 365 of file host_cdc_task.c.

References cdc_interface_comm, and GET_LINE_CODING.

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 }

void host_cdc_send_encapsulated_command ( void   ) 

This function sends a format-specific packet.

Definition at line 319 of file host_cdc_task.c.

References cdc_interface_comm, ENCAPSULATED_PACKET_LENGTH, and SEND_ENCAPSULATED_COMMAND.

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 }

void host_cdc_set_line_coding ( void   ) 

This function set communication settings on the device.

Definition at line 349 of file host_cdc_task.c.

References cdc_interface_comm, and SET_LINE_CODING.

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 }

void host_cdc_task ( void *  pvParameters  ) 

This function manages the host CDC task.

Definition at line 126 of file host_cdc_task.c.

References cdc_connected, cdc_interface_comm, CDC_NB_MS_BEFORE_FLUSH, cdc_new_device_connected, cdc_pipe_out_usb_flush(), cdc_stream_in_array, CDC_STREAM_IN_SIZE, cdc_stream_out_array, CDC_STREAM_OUT_SIZE, configTSK_USB_HCDC_PERIOD, host_rx_cnt, host_tx_cnt, Is_host_cdc_configured, LED_HOST_CDC_B0, LED_HOST_CDC_B1, pipe_cdc_comm_int, pipe_cdc_data_bulkin, pipe_cdc_data_bulkout, and sof_cnt.

Referenced by host_cdc_task_init(), and main().

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     // First, check the host controller is in full operating mode with the
00145     // B-device attached and enumerated
00146     if( Is_host_ready() )
00147     {
00148       // New device connection (executed only once after device connection)
00149       if( cdc_new_device_connected )
00150       {
00151         cdc_new_device_connected = FALSE;
00152 
00153         // For all supported interfaces
00154         for( i=0 ; i<Get_nb_supported_interface() ; i++ )
00155         {
00156           // Data Interface
00157           if( Get_class(i)==CDC_DATA_CLASS && Get_protocol(i)==NO_PROTOCOL )
00158           {
00159             cdc_connected=TRUE;
00160 
00161             // Get correct physical pipes associated with IN/OUT endpoints
00162             if (Is_ep_in(i, 0))
00163             { // Yes, associate it with the IN pipe
00164               pipe_cdc_data_bulkin = Get_ep_pipe(i, 0);
00165               pipe_cdc_data_bulkout = Get_ep_pipe(i, 1);
00166             }
00167             else
00168             { // No, invert...
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           // Management Interface
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;      // store interface number
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         // Check DATA_PIPE_IN for incoming data
00199         // ************************************
00200         // 1 -> UART-USB Mode
00201         //   Data received is automatically sent on the UART
00202         // 2 -> Normal Mode
00203         //   Data received is stored in the "cdc_stream_in_array[CDC_STREAM_IN_SIZE]" array and may be read by firmware
00204         //   When firmware reads the array it must do it entirely and then set "host_rx_cnt" variable to 0
00205         //   It must not partially read the array and decrement host_rx_cnt (will lead to array overflow)
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);    // pipe is empty
00216           Host_free_in(pipe_cdc_data_bulkin);            // ready to receive more data
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);    // pipe is empty
00227             Host_free_in(pipe_cdc_data_bulkin);            // ready to receive more data
00228           }
00229 #endif
00230         }
00231 
00232 
00233         // Check if data to send on DATA_PIPE_OUT state
00234         // ********************************************
00235         // Data to be sent is stored in the "cdc_stream_out_array[CDC_STREAM_OUT_SIZE]" array
00236         // Data may come indifferently from neither UART or RAM (firmware)
00237         // Data is sent to CDC Device when :
00238         //    - user requires it, by calling "cdc_pipe_out_usb_flush()" function
00239         //    - time-out period is elapsed (CDC_NB_MS_BEFORE_FLUSH = number of SOF seen)
00240         //    - buffer is full (host_tx_cnt = CDC_STREAM_OUT_SIZE)
00241 #if (CDC_USE_UART==ENABLED)
00242         // Check if new byte in USART, to be stored for USB
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         // Check if pipe flush is needed (buffer full or time-out period elapsed)
00257         if(((sof_cnt>=CDC_NB_MS_BEFORE_FLUSH) && (host_tx_cnt!=0)) || (host_tx_cnt == CDC_STREAM_OUT_SIZE))  //Flush buffer by Timeout
00258         {
00259            sof_cnt=0;
00260            cdc_pipe_out_usb_flush();
00261         }
00262 
00263 
00264         // Check in COMM_PIPE_IN for incoming notifications
00265         // ************************************************
00266         if (Is_host_in_received(pipe_cdc_comm_int))
00267         {
00268            // Handle here notification messages sent by device
00269            // Notifications messages have the following structure :
00270            //  bmRequestType - bNotification - wValue - wIndex - wLength - Data     (wLength is the number of bytes of the Data field)
00271 
00272            //   - NETWORK_CONNECTION : indicates that device has connected to network
00273            //   - RESPONSE_AVAILABLE : indicates that device has a ready encapsulated response (wait for host request)
00274            //   - SERIAL_STATE : indicates state of device' UART (errors, carriers and misc. signals)
00275            //   - etc...
00276 
00277            // ...and now...just coding...
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 }

void host_cdc_task_init ( void   ) 

This function initializes the host CDC task.

Definition at line 102 of file host_cdc_task.c.

References cdc_connected, cdc_interface_comm, cdc_new_device_connected, configTSK_USB_HCDC_NAME, configTSK_USB_HCDC_PRIORITY, configTSK_USB_HCDC_STACK_SIZE, host_cdc_task(), host_rx_cnt, host_tx_cnt, and sof_cnt.

Referenced by main().

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 }


Variable Documentation

U8 cdc_stream_in_array[CDC_STREAM_IN_SIZE]

Definition at line 92 of file host_cdc_task.c.

Referenced by host_cdc_task().

U8 cdc_stream_out_array[CDC_STREAM_OUT_SIZE]

Definition at line 91 of file host_cdc_task.c.

Referenced by cdc_pipe_out_usb_flush(), and host_cdc_task().

volatile Bool ms_connected


Generated on Fri Feb 19 02:32:25 2010 for AVR32 - USB CDC Example by  doxygen 1.5.5