host_template_task.c

Go to the documentation of this file.
00001 /*This file is prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *
00019  * 1. Redistributions of source code must retain the above copyright notice,
00020  * this list of conditions and the following disclaimer.
00021  *
00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
00023  * this list of conditions and the following disclaimer in the documentation
00024  * and/or other materials provided with the distribution.
00025  *
00026  * 3. The name of Atmel may not be used to endorse or promote products derived
00027  * from this software without specific prior written permission.
00028  *
00029  * 4. This software may only be redistributed and used in connection with an Atmel
00030  * AVR product.
00031  *
00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00042  */
00043 
00044 //_____  I N C L U D E S ___________________________________________________
00045 
00046 #include "config.h"
00047 #include "conf_usb.h"
00048 #include "host_template_task.h"
00049 #include "modules/usb/host_chap9/usb_host_task.h"
00050 #include "modules/usb/host_chap9/usb_host_enum.h"
00051 #include "lib_mcu/usb/usb_drv.h"
00052 #include "lib_mcu/uart/uart_lib.h"
00053 
00054 
00055 //_____ M A C R O S ________________________________________________________
00056 
00057 //_____ D E F I N I T I O N S ______________________________________________
00058 
00059 //_____ D E C L A R A T I O N S ____________________________________________
00060 
00061    U8 tab[64];
00062    U8 busy=FALSE;
00063    U8 pipe_out;
00064    U8 pipe_in;
00065    U8 pipe_interrupt_in;
00066 
00067 
00077 void host_template_task_init(void)
00078 {
00079    U8 i;
00080    for(i=0;i<sizeof(tab);i++)
00081    {
00082       tab[i]=i;
00083    }
00084    Leds_init();
00085    Joy_init();
00086    Hwb_button_init();
00087 }
00088 
00099 void host_template_task(void)
00100 {
00101    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == DISABLE)
00102    U8 sta;
00103    #endif
00104    U16 *ptr_nb;
00105    U16 nb;
00106    U8 i;
00107 
00108    ptr_nb=&nb;
00109 
00110    // First check the host controller is in full operating mode with the B device attached
00111    // and enumerated
00112    if(Is_host_ready())
00113    {
00114       //Put here the code to do in host mode
00115 
00116       // New device connection (executed  only one time after device connection)
00117       if(Is_new_device_connection_event())
00118       {
00119          for(i=0;i<Get_nb_supported_interface();i++)
00120          {
00121             // First interface with two bulk IN/OUT pipes
00122             if(Get_class(i)==0x00 && Get_subclass(i)==0x00 && Get_protocol(i)==0x00)
00123             {
00124                //Get correct physical pipes associated to IN/OUT Endpoints
00125                if(Is_ep_addr_in(Get_ep_addr(i,0)))
00126                {  //Yes associate it to the IN pipe
00127                   pipe_in=host_get_hwd_pipe_nb(Get_ep_addr(i,0));
00128                   pipe_out=host_get_hwd_pipe_nb(Get_ep_addr(i,1));
00129                }
00130                else
00131                {  //No, invert...
00132                   pipe_in=host_get_hwd_pipe_nb(Get_ep_addr(i,1));
00133                   pipe_out=host_get_hwd_pipe_nb(Get_ep_addr(i,0));
00134                }
00135             }
00136             // Seconf interface with interrupt IN pipe
00137             if(Get_class(i)==0x00 && Get_subclass(i)==0x55 && Get_protocol(i)==0xAA)
00138             {
00139                pipe_interrupt_in=host_get_hwd_pipe_nb(Get_ep_addr(i,0));
00140                Host_select_pipe(pipe_interrupt_in);
00141                Host_continuous_in_mode();
00142                Host_unfreeze_pipe();
00143             }
00144          }
00145       }
00146 
00147 
00148      // Firt interface (bulk IN/OUT) management
00149      // In polling mode
00150 #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == DISABLE)
00151       // The sample task sends 64 byte through pipe nb2...
00152       sta=host_send_data(pipe_out,64,tab);
00153 
00154       // And receives 64bytes from pipe nb 1...
00155       *ptr_nb=64;
00156       sta=host_get_data(pipe_in,ptr_nb,tab);
00157 #else
00158       // similar applicative task under interrupt mode...
00159       if (busy==FALSE)
00160       {
00161          busy=TRUE;
00162          host_send_data_interrupt(pipe_out,64,tab,call_back_template_1);
00163       }
00164 
00165 #endif
00166 
00167       // Second interface management (USB interrupt IN pipe)
00168       Host_select_pipe(pipe_interrupt_in);
00169       if(Is_host_in_received())
00170       {
00171          if(Is_host_stall()==FALSE)
00172          {
00173             i=Host_read_byte();
00174             Host_read_byte();
00175          }
00176          Host_ack_in_received(); Host_send_in();
00177       }
00178 
00179       // Here an example of an applicative request to go to USB suspend ...
00180       if(Is_hwb())
00181       {
00182          Host_request_suspend();
00183       }
00184    }
00185    // Here an applicative example of resume request...
00186    if(Is_host_suspended() && Is_btn_middle())
00187    {
00188       Host_request_resume();
00189    }
00190 
00191    //Device disconnection...
00192    if(Is_device_disconnection_event())
00193    {
00194       //Put here code to be executed upon device disconnection...
00195    }
00196 }
00197 
00198 void call_back_template_1(U8 status, U16 nb_byte)
00199 {
00200    if (status==PIPE_GOOD)
00201    {
00202       host_get_data_interrupt(pipe_in,64,tab,call_back_template_2);
00203    }
00204 
00205 }
00206 
00207 void call_back_template_2(U8 status, U16 nb_byte)
00208 {
00209    if (status==PIPE_GOOD)
00210    {
00211       busy=FALSE;
00212    }
00213 }

Generated on Mon Sep 14 13:24:11 2009 for ATMEL by  doxygen 1.5.3