cdc_task.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2007, 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  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 //_____  I N C L U D E S ___________________________________________________
00042 
00043 #include "config.h"
00044 #include "conf_usb.h"
00045 #include "cdc_task.h"
00046 #include "lib_mcu/usb/usb_drv.h"
00047 #include "usb_descriptors.h"
00048 #include "modules/usb/device_chap9/usb_standard_request.h"
00049 #include "usb_specific_request.h"
00050 #include "lib_mcu/uart/uart_lib.h"
00051 #include "uart_usb_lib.h"
00052 #include <stdio.h>
00053 
00054 
00055 //_____ M A C R O S ________________________________________________________
00056 
00057 
00058 
00059 //_____ D E F I N I T I O N S ______________________________________________
00060 
00061 
00062 
00063 //_____ D E C L A R A T I O N S ____________________________________________
00064 
00065 
00066 volatile        U8 cpt_sof;
00067 extern          U8    rx_counter;
00068 extern          U8    tx_counter;
00069 extern volatile U8 usb_request_break_generation;
00070 
00071 S_line_coding line_coding;
00072 S_line_status line_status;      // for detection of serial state input lines
00073 S_serial_state serial_state;   // for serial state output lines
00074 
00075 volatile U8 rs2usb[10];
00076 
00077 
00086 void cdc_task_init(void)
00087 {
00088    uart_init();
00089    Uart_enable_it_rx();
00090    Leds_init();
00091    Joy_init();
00092    Hwb_button_init();
00093    Usb_enable_sof_interrupt();
00094 #ifdef __GNUC__
00095    fdevopen((int (*)(char, FILE*))(uart_usb_putchar),(int (*)(FILE*))uart_usb_getchar); //for printf redirection
00096 #endif
00097 }
00098 
00099 
00100 
00108 void cdc_task(void)
00109 {
00110    
00111    if(Is_device_enumerated() && line_status.DTR) //Enumeration processs OK and COM port openned ?
00112    {
00113       if (Uart_tx_ready())    //USART free ?
00114       {
00115          if (uart_usb_test_hit())   // Something received from the USB ?
00116          {
00117              while (rx_counter)
00118             {
00119                uart_putchar(uart_usb_getchar());   // loop back USB to USART
00120                Led3_toggle();
00121             }
00122          }
00123       }
00124 
00125       if ( cpt_sof>=REPEAT_KEY_PRESSED)   //Debounce joystick events
00126       {
00127          if (Is_btn_middle()){
00128             printf ("Select Pressed !\r\n");
00129          }
00130          if (Is_joy_right()) {
00131             printf ("Right Pressed !\r\n");
00132             serial_state.bDCD = TRUE;
00133          }
00134          else
00135             serial_state.bDCD = FALSE;
00136 
00137          if (Is_joy_left()) {
00138             printf ("Left Pressed !\r\n");
00139             serial_state.bDSR = TRUE;
00140          }
00141          else
00142             serial_state.bDSR = FALSE;
00143 
00144          if (Is_joy_down())
00145          printf ("Down Pressed !\r\n");
00146 
00147          if (Is_joy_up())
00148          printf ("Up Pressed !\r\n");
00149 
00150          if(Is_btn_left())
00151          printf("Hello from AT90USBXXX !\r\n");
00152          
00153          cdc_update_serial_state();
00154       }
00155 
00156       if(usb_request_break_generation==TRUE)
00157       {
00158          usb_request_break_generation=FALSE;
00159          Led2_toggle();
00160       }
00161    }
00162 }
00163 
00175 void sof_action()
00176 {
00177    cpt_sof++;
00178 }
00179 
00180 
00186 #ifdef __GNUC__
00187  ISR(USART1_RX_vect)
00188 #else
00189 #pragma vector = USART1_RX_vect
00190 __interrupt void usart_receive_interrupt()
00191 #endif
00192 {
00193    U8 i=0;
00194    U8 save_ep;
00195    
00196    if(Is_device_enumerated()) 
00197    {
00198       save_ep=Usb_get_selected_endpoint();   
00199       Usb_select_endpoint(TX_EP);
00200       do 
00201       {
00202          if(Uart_rx_ready())
00203          {
00204             rs2usb[i]=Uart_get_byte();
00205             i++;
00206          }
00207       }while(Is_usb_write_enabled()==FALSE );
00208       uart_usb_send_buffer((U8*)&rs2usb,i);
00209       Usb_select_endpoint(save_ep);
00210    }
00211 }

Generated on Fri Oct 31 15:55:11 2008 for ATMEL by  doxygen 1.5.3