hid_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 "hid_task.h"
00049 #include "lib_mcu/usb/usb_drv.h"
00050 #include "usb_descriptors.h"
00051 #include "modules/usb/device_chap9/usb_standard_request.h"
00052 #include "usb_specific_request.h"
00053 #include "lib_mcu/util/start_boot.h"
00054 
00055 
00056 
00057 //_____ M A C R O S ________________________________________________________
00058 
00059 
00060 //_____ D E F I N I T I O N S ______________________________________________
00061 
00062 
00063 //_____ D E C L A R A T I O N S ____________________________________________
00064 
00065          volatile U8    cpt_sof=0;
00066 extern            U8    jump_bootloader;
00067                   U8    g_last_joy       =0;
00068 
00069 
00070 void hid_report_out  (void);
00071 void hid_report_in   (void);
00072 
00075 void hid_task_init(void)
00076 {
00077    Leds_init();
00078    Joy_init();
00079 }
00080 
00084 void hid_task(void)
00085 {
00086    if(!Is_device_enumerated())          // Check USB HID is enumerated
00087       return;
00088 
00089    hid_report_out();
00090    hid_report_in();
00091 }
00092 
00093 
00096 void hid_report_out(void)
00097 {
00098    Usb_select_endpoint(EP_HID_OUT);
00099    if(Is_usb_receive_out())
00100    {
00101       //* Read report from HID Generic Demo
00102       U8 led_state;
00103       U8 led_number;
00104       led_state      = Usb_read_byte()&0x0F; // RepportOUT[0] is LEDS value
00105       led_number     = Usb_read_byte()&0x0F;
00106       switch (led_number)
00107       {
00108          case 1:
00109             if(led_state)
00110             {  Led0_on();   }
00111             else {Led0_off();}
00112             break;
00113          case 2:
00114             if(led_state)
00115             {  Led1_on();   }
00116             else {Led1_off();}
00117             break;
00118          case 3:
00119             if(led_state)
00120             {  Led2_on();   }
00121             else {Led2_off();}
00122             break;
00123          case 4:
00124             if(led_state)
00125             {  Led3_on();   }
00126             else {Led3_off();}
00127             break;
00128       }  
00129       Usb_ack_receive_out();
00130    }
00131 
00132    //** Check if we received DFU mode command from host
00133    if(jump_bootloader)
00134    {
00135       U32 volatile tempo;
00136       Leds_off();
00137       Usb_detach();                          // Detach actual generic HID application
00138       for(tempo=0;tempo<70000;tempo++);      // Wait some time before
00139       start_boot();                          // Jumping to booltoader
00140    }
00141 }
00142 
00143 
00146 void hid_report_in(void)
00147 {
00148    U8 joy   =0;
00149 
00150    Usb_select_endpoint(EP_HID_IN);
00151    if(!Is_usb_write_enabled())
00152       return;                                // Not ready to send report
00153    
00154    // Build the Joytick report
00155    if(Is_joy_up()|| Is_joy_down() || Is_joy_right() || Is_joy_left() )                     
00156    {
00157      joy=0x01;
00158    }
00159    if(joy==g_last_joy)
00160       return;                                // Same report then no send report
00161    g_last_joy=joy;
00162   
00163    // Send report
00164    Usb_write_byte(g_last_joy);               // Joystick
00165    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00166    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00167    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00168    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00169    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00170    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00171    Usb_write_byte(GPIOR1);                   // Dummy (not used)
00172    Usb_ack_in_ready();                       // Send data over the USB
00173 }
00174 
00179 void sof_action()
00180 {
00181    cpt_sof++;
00182 }

Generated on Mon Sep 14 13:54:01 2009 for ATMEL by  doxygen 1.5.3