⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio_task.c

📁 ATMEL at90usb128 usb audio driver for win-avr
💻 C
字号:
//!
//! @file audio_task.c,v
//!
//! Copyright (c) 2006 Atmel.
//!
//! Please read file license.txt for copyright notice.
//!
//! @brief This file manages the audio task.
//!
//!
//! @version 1.5 at90usb128-demo-audio-1_0_2 $Id: audio_task.c,v 1.5 2006/08/02 14:03:31 rletendu Exp $
//!
//! @todo
//! @bug
//!/

//_____  I N C L U D E S ___________________________________________________

#include "config.h"
#include "conf_usb.h"
#include "audio_task.h"
#include "lib_board/stk_525/stk_525.h"
#include "lib_mcu/usb/usb_drv.h"
#include "lib_mcu/adc/adc_drv.h"
#include "usb_descriptors.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "sound.h"

//_____ M A C R O S ________________________________________________________


//_____ D E F I N I T I O N S ______________________________________________



//_____ D E C L A R A T I O N S ____________________________________________


static U16 index;
volatile U8 index_conv=0;
volatile U16 offset;
volatile U16 adc;

//!
//! Public : (bit) mute
//! mute is set to TRUE when ACTIVE
//! mute is set to FALSE otherwise
//!/
bit   mute;

//!
//! Public : (S16) volume
//! volume represents the current volume of audio stream
//!/
S16 volume;

//!
//! @brief This function initializes the USB the associated variables.
//!
//! @param none
//!
//! @return none
//!
//!/
void audio_task_init(void)
{
   Leds_init();
   Joy_init();
   Hwb_button_init();
   init_adc();
   TCCR2A=0x02;         // Prescaler div8
   OCR2A=125;           // Reload value to count 8x125=1000 for FOSC 8MHz =>125礢
   TCCR2B=0x02;         // Timer 2 mode CTC
   TIMSK2|=(1<<OCIE2A); // Enable compare interrupt
}

//! @brief Entry point of the audio management
//!
//! @param none
//!
//! @return none
void audio_task(void)
{
  U8 i;

   // Joystick select activation -> Play build in flash raw sample
   if(Is_joy_select() && Is_device_enumerated() )
   {
      TIMSK2&=~(1<<OCIE2A); //Disable compare interrupt
      Usb_select_endpoint(EP_1);
      if (Is_usb_write_enabled())   // Endpoint buffer free ?
      {
         for(i=0;i< EP_IN_LENGTH;i++)   // Fill endpoint with sample raw
         {
            if(mute==FALSE)
            {
   #ifdef AVRGCC
               Usb_write_byte(pgm_read_byte_near(&sample_sound[index++]));
   #else
               Usb_write_byte(sample_sound[index++]);
   #endif
               if (index > SAMPLE_SOUND_LEN)
               { index=0; } //end of sample sound: run in a loop
            }
            else
            {
              Usb_write_byte(0x00);
            }
         }
         Usb_send_in();
      }
   }
   else   // otherwize play microphone sample with timer 2 sampling interrupt
   { TIMSK2|=(1<<OCIE2A);} //! Enable compare interrupt
   Leds_set_val(MSB(volume));
}

//! @brief Timer2 comparator interrupt routine service
//!
//! When in microphone mode, get adc microphone sample and load it to
//! the Isochronous endpoint.
//!
//! @note The dual bank endpoint usage allows to get ride of an extra synchonisation buffer
//!
//! @param none
//!
//! @return none
#ifdef AVRGCC
 ISR(TIMER2_COMPA_vect)
#else
#pragma vector = TIMER2_COMPA_vect
__interrupt void timer2_comp_interrupt()
#endif
{
   S16 sample;
   U8 sav_ep=UENUM;      // Caution save and restore UENUM register

   sample= (S32)(64*Get_adc_mic_val()-32768);
   Usb_select_endpoint(EP_1);
   if (Is_device_enumerated() && Is_usb_write_enabled())
   {
      Usb_write_byte(LSB(sample));
      Usb_write_byte(MSB(sample));
      index_conv+=2;
      if (index_conv>=EP_IN_LENGTH)
      {
          Usb_send_in();
          index_conv=0;
      }
   }
   UENUM=sav_ep;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -