echo.c 10.1 KB
/*HEADER******************************************************************
**************************************************************************
*** 
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved                                          
***                                                              
*** This software embodies materials and concepts which are      
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license   
*** agreement with ARC International             
***
*** $Workfile:otg_main.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:00 $
***
*** Description:      
***  This file contains the VUSB32 OTG application.
***                                                               
**************************************************************************
*END*********************************************************************/
#ifndef __USB_OS_MQX__
   #include "types.h"
   #include "otgapi.h"
   #include "usb.h"
   #include "devapi.h"
   #include "hostapi.h"
   #include "echo.h"
   #include "test_h.h"
   
   #include "stdio.h"
   #include "stdlib.h"
   #include "string.h"
#else
   #include "mqx.h"
   #include "bsp.h"
   #include "devapi.h"
   #include "hostapi.h"
   #include "echo.h"
#endif

/* global variables */
_usb_device_handle   dev_handle;
_usb_host_handle     host_handle;
_usb_otg_handle      app_otg_handle;

extern volatile APP_GLOBAL_STRUCT app_test_struct;
extern void device_main(_usb_device_handle dev_handle);
extern void host_main(_usb_host_handle host_handle);

uint_32 EVENT[20] = {0};
uint_32 event_count = 0;
volatile boolean HOST_REGISTER_SERVICES = TRUE;

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : otg_device_startup
* Returned Value : None
* Comments       :
*     Start OTG in device mode.
* 
*END*--------------------------------------------------------------------*/
uint_8  otg_device_startup
   (
      void   
   )
{ /* Body */
   uint_8               error;
   uint_32              state=0;
   
   /* Initialize the USB interface */
   error = _usb_device_init(0, &dev_handle, 4);
   
   if (error != USB_OK) {
      printf("\nUSB Device Initialization failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_device_register_service(dev_handle, USB_SERVICE_EP0, 
            service_ep0);
   
   if (error != USB_OK) {
      printf("\nUSB Device Service Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_device_register_service(dev_handle, USB_SERVICE_BUS_RESET, 
      reset_ep0);
   
   if (error != USB_OK) {
      printf("\nUSB Device Service Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
   
   if (state < B_IDLE) {
      error = _usb_device_register_service(dev_handle, USB_SERVICE_SOF, 
               bus_sof);
      if (error != USB_OK) {
         printf("\nUSB Device Service Registration failed. Error: %x", error);
         fflush(stdout);
      } /* Endif */
   } /* Endif */
   return error;
} /* Endbody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : otg_device_shutdown
* Returned Value : None
* Comments       :
*     Start OTG in device shutdown
* 
*END*--------------------------------------------------------------------*/
uint_8  otg_device_shutdown
   (
      void      
   )
{ /* Body */
   
   uint_8               error;

   error = _usb_device_unregister_service(dev_handle, USB_SERVICE_BUS_RESET);
   
   if (error != USB_OK) {
      printf("\nUSB Service Un Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_device_unregister_service(dev_handle, USB_SERVICE_EP0);
   
   if (error != USB_OK) {
      printf("\nUSB Service un Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   /* shutdown  the USB interface */
   _usb_device_shutdown(dev_handle);
   
   return error;  
} /* Endbody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : otg_host_startup
* Returned Value : None
* Comments       :
*     Start OTG in host mode.
* 
*END*--------------------------------------------------------------------*/
uint_8  otg_host_startup
   (
      void   
   )
{ /* Body */
   
   uint_8               error;

   error = _usb_host_init(0, 10, &host_handle);
   if (error != USB_OK) {
      printf("\nUSB Host Initialization failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_host_register_service(host_handle, USB_SERVICE_ATTACH, 
            app_process_attach);
   
   if (error != USB_OK) {
      printf("\nUSB Host Service Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_host_register_service(host_handle, USB_SERVICE_DETACH, 
            app_process_detach);
   
   if (error != USB_OK) {
      printf("\nUSB Host Service Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME, 
            app_process_host_resume);
   
   if (error != USB_OK) {
      printf("\nUSB Host Service Registration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   HOST_REGISTER_SERVICES = TRUE;
   
   return error;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : otg_host_shutdown
* Returned Value : None
* Comments       :
*     Shutdown the  OTG in host mode.
* 
*END*--------------------------------------------------------------------*/
uint_8  otg_host_shutdown
   (
      void   
   )
{ /* Body */
   
   uint_8               error;

   error = _usb_host_unregister_service(host_handle, USB_SERVICE_DETACH);
   
   if (error != USB_OK) {
      printf("\nUSB Service Unregistration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_host_unregister_service(host_handle, USB_SERVICE_ATTACH);
   
   if (error != USB_OK) {
      printf("\nUSB Service Unregistration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   
   error = _usb_host_unregister_service(host_handle, USB_SERVICE_HOST_RESUME);
   
   if (error != USB_OK) {
      printf("\nUSB Service Unregistration failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */

   _usb_host_shutdown(host_handle);
   
   return error;
} /* Endbody */



/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : otg_service
* Returned Value : None
* Comments       :
*     Called upon  OTG service state change.
* 
*END*--------------------------------------------------------------------*/
void otg_service
   (
      /* [IN] Handle of the USB device */
      _usb_otg_handle   handle,
      
      /* [IN] service type */
      uint_32              type
      
   )
{ /* Body */
   uint_8   error;
  
   EVENT[event_count++] = type;
   if (event_count>19) {
      event_count = 0;
   } /* Endif */
   
   switch (type) {
      case USB_OTG_HOST_UP:
         error = otg_host_startup();
         if (error == USB_OK) {
            _usb_otg_set_status(handle, USB_OTG_HOST_ACTIVE, TRUE);
            _usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_DROP, FALSE);
            _usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_REQ, TRUE);
         } /* Endif */
         break;
      
      case USB_OTG_DEVICE_UP:  
         error = otg_device_startup();
         if (error== USB_OK) {
            _usb_otg_set_status(handle,USB_OTG_DEVICE_ACTIVE, TRUE);
         } /* Endif */
         break;
      
      case USB_OTG_HOST_DOWN:  
         error = otg_host_shutdown();
         if (error == USB_OK) {
            _usb_otg_set_status(handle,USB_OTG_HOST_ACTIVE, FALSE);
         } /* Endif */
         app_process_detach(host_handle, 0);
         break;
      
      case USB_OTG_DEVICE_DOWN:
         error = otg_device_shutdown();
         if (error == USB_OK) {
            _usb_otg_set_status(handle,USB_OTG_DEVICE_ACTIVE, FALSE);
         } /* Endif */
         reset_ep0(dev_handle, FALSE, 0, NULL, 0);
         break;
      
      case USB_OTG_SRP_ACTIVE: 
         break;
      
      case USB_OTG_SRP_FAIL:
         break;
      
      case USB_OTG_OVER_CURRENT:
         break;
   
      default:
         break;
   } /* Endswitch */
   return;
} /* Endbody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : main
* Returned Value : None
* Comments       :
*     First function called.  Initialises the USB and registers Chapter 9
*     callback functions.
* 
*END*--------------------------------------------------------------------*/
void main
   (
      void
   )
{ /* Body */
   
   uint_8               error;
   USB_OTG_INIT_STRUCT  init;
   volatile uint_32     i = 0;
   uint_32              state=0;
   
   /* default configuration: A will be host B will be peripheral */
   init.A_BUS_DROP = FALSE;
   init.A_BUS_REQ = TRUE;
   init.B_BUS_REQ = FALSE;
   init.SERVICE = otg_service;
   app_test_struct.DO_HNP = FALSE;

   _disable_interrupts();
   /* Initialize the USB interface */
   error = _usb_otg_init(0, &init, &app_otg_handle);
   if (error != USB_OK) {
      printf("\nUSB OTG Initialization failed. Error: %x", error);
      fflush(stdout);
   } /* Endif */
   _enable_interrupts();
   
   while (TRUE) {
      _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
      if ((state == A_HOST) || (state == B_HOST)) {
         host_main(host_handle);
      }  /* Endif */
      if ((state == A_PERIPHERAL) || (state == B_PERIPHERAL)) {
         device_main(dev_handle);
      } /* Endif */
   } /* Endwhile */
} /* Endbody */
/* EOF */