cdc_rf_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_rf_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/spi/spi_lib.h"
00051 #include "lib_board/nrf24l01/nRF_API.h"
00052 
00053 //#include <stdio.h>
00054 
00055 
00056 
00057 //_____ M A C R O S ________________________________________________________
00058 
00059 
00060 
00061 
00062 
00063 //_____ D E F I N I T I O N S ______________________________________________
00064 
00065 
00066 
00067 //_____ D E C L A R A T I O N S ____________________________________________
00068 
00069 
00070 volatile U8 cpt_sof;
00071 volatile U8 led_tx_flash=0;
00072 volatile U8 led_rx_flash=0;
00073 volatile U16 cpt_sof_led;
00074 
00075          S_line_coding line_coding;
00076          U8 rx_buffer[RF_BUFFER_SIZE];
00077          U8 tx_buffer[RF_BUFFER_SIZE];
00078 
00079 
00080 
00081          U8 tx_counter;
00082          U8 rx_counter;
00083 
00084 #define Flash_tx_led()  (led_tx_flash=0,Led1_on())
00085 #define Flash_rx_led()  (led_rx_flash=0,Led0_on())
00086          
00087 
00096 void cdc_rf_task_init(void)
00097 {
00098    volatile U32 tempo;
00099    
00100    Leds_init();
00101    //Init nr24L01 driver
00102    NRF_port_init();
00103    spi_init(SPI_MASTER);   
00104    NRF_enable();            // Enable receiver
00105    NRF_disable();            // Enable receiver
00106    for(tempo=0;tempo<FOSC/10;tempo++);     
00107    NRF_init_rx();           // Set nRf primary receiver
00108 
00109    NRF_set_payload(0,RF_BUFFER_SIZE);   // Pipe 0 receiver is 32 bytes
00110    NRF_enable();            // Enable receiver
00111 
00112    tx_counter = 0;
00113    rx_counter = 0;
00114    Usb_enable_sof_interrupt();
00115 }
00116 
00117 
00118 
00119 
00120 
00128 void cdc_rf_task(void)
00129 {
00130    U8 i,j;
00131 
00132    if(Is_device_enumerated()) //Enumeration processs OK ?
00133    {
00134       if(cpt_sof>=NB_MS_BEFORE_FLUSH && tx_counter!=0 )  //Flush buffer in Timeout
00135       {
00136          cpt_sof=0;
00137          usb_flush();
00138       }
00139 
00140       if(Is_NRF_IRQ())
00141       {
00142          if (Is_NRF_received())              //Something on RF ?
00143          {
00144             NRF_rx_buffer(rx_buffer,RF_BUFFER_SIZE);     //Read data buffer from Rf
00145             i=rx_buffer[0];                  // Get number of data byte
00146             for(j=1;j<=i;j++)
00147             {
00148                usb_putchar(rx_buffer[j]);
00149             }
00150             Flash_rx_led();
00151             NRF_ack_received();
00152          }
00153       }
00154       
00155 
00156       if(usb_test_hit())
00157       {
00158          tx_buffer[0]=rx_counter;
00159          for(j=1;j<=tx_buffer[0];j++)
00160          {
00161             tx_buffer[j]=usb_getchar();
00162          }
00163          NRF_init_tx();                   // RF is Tx mode
00164          NRF_tx_buffer(tx_buffer,RF_BUFFER_SIZE);       // Send data to Rf
00165          while(Is_not_NRF_IRQ());         // Wait event from Rf
00166          if(Is_NRF_transmit())            // Data sent ?
00167          {
00168             NRF_ack_transmit();
00169             Flash_tx_led();
00170          }
00171          else if (Is_NRF_timeout())
00172          {
00173             NRF_ack_timeout();
00174          }
00175          NRF_init_rx();
00176       }
00177    }
00178 }
00179 
00180 
00181 
00189 void sof_action()
00190 {
00191 
00192    cpt_sof++;
00193    if(led_tx_flash<=LED_FLASH_DELAY)
00194    {
00195      led_tx_flash++;
00196    }
00197    if(led_tx_flash==LED_FLASH_DELAY)
00198    {
00199       Led1_off();     
00200    }
00201    
00202    if(led_rx_flash<=LED_FLASH_DELAY)
00203    {
00204      led_rx_flash++;
00205    }
00206    if(led_rx_flash==LED_FLASH_DELAY)
00207    {
00208       Led0_off();     
00209    }
00210    
00211 
00212    if(cpt_sof_led++>=RF_LEDS_OFF_DELAY+RF_LEDS_FLASH_DELAY+1) cpt_sof_led=0;
00213    if(cpt_sof_led==RF_LEDS_OFF_DELAY)
00214    {
00215        Led0_on();
00216        Led1_on();
00217    }
00218    if(cpt_sof_led==RF_LEDS_OFF_DELAY+RF_LEDS_FLASH_DELAY)
00219    {
00220        Led0_off();
00221        Led1_off();
00222    }
00223 
00224 }
00225 
00226 
00232 bit usb_test_hit(void)
00233 {
00234   if (!rx_counter)
00235   {
00236     Usb_select_endpoint(TX_EP);
00237     if (Is_usb_receive_out())
00238     {
00239       rx_counter = Usb_byte_counter();
00240       if (!rx_counter)
00241       {
00242         Usb_ack_receive_out();
00243       }
00244     }
00245   }
00246   return (rx_counter!=0);
00247 }
00248 
00257 char usb_getchar(void)
00258 {
00259   register Uchar data_rx;
00260 
00261   Usb_select_endpoint(TX_EP);
00262   if (!rx_counter) while (!usb_test_hit());
00263   data_rx=Usb_read_byte();
00264   rx_counter--;
00265   if (!rx_counter) Usb_ack_receive_out();
00266   return data_rx;
00267 }
00268 
00269 
00276 bit usb_tx_ready(void)
00277 {
00278   if (!Is_usb_write_enabled())
00279   {
00280     return FALSE;
00281   }
00282   return TRUE;
00283 }
00284 
00294 int usb_putchar(int data_to_send)
00295 {
00296    Usb_select_endpoint(RX_EP);
00297    while(!usb_tx_ready()); // Wait Endpoint ready
00298    Usb_write_byte(data_to_send);
00299    tx_counter++;
00300    if(!Is_usb_write_enabled()) //If Endpoint full -> flush
00301      {
00302       usb_flush();
00303      }
00304    return data_to_send;
00305 }
00306 
00307 
00312 void usb_flush (void)
00313 {
00314    Usb_select_endpoint(RX_EP);
00315    Usb_send_in();
00316    tx_counter = 0;
00317 }
00318 
00319 

Generated on Mon May 5 15:57:54 2008 for ATMEL by  doxygen 1.5.1-p1