#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 "lib_mcu/timer/timer8_drv.h"#include "usb_descriptors.h"#include "modules/usb/device_chap9/usb_standard_request.h"#include "sound.h"
Go to the source code of this file.
Functions | |
| void | audio_task_init (void) |
| This function initializes the USB the associated variables. | |
| void | audio_task (void) |
| __interrupt void | timer2_comp_interrupt () |
| U16 | Get_adc_ext_val (void) |
Variables | |
| bit | b_micro_mute |
| Mute control of micro TRUE when ACTIVE, FALSE otherwise. | |
| S16 | s16_micro_volume |
| Represents the current volume of audio stream micro No effet in this demo. | |
| U16 | g_u16_micro_sample_pos = 0 |
| Play position in sample sound storage in flash raw. | |
Definition in file audio_task.c.
| void audio_task_init | ( | void | ) |
This function initializes the USB the associated variables.
Definition at line 79 of file audio_task.c.
References b_micro_mute, FALSE, Hwb_button_init, init_adc(), Joy_init, Leds_init, TIMER8_0_CLKIO_BY_8, and TIMER8_WGM_CTC_OCR.
00080 { 00081 Leds_init(); 00082 Joy_init(); 00083 Hwb_button_init(); 00084 init_adc(); 00085 00086 // Sample rate settings 00087 // At 8 kHz, signal is be sampled every 125µs. When endpoint is full (8 sample words), it is validated. 00088 // But periodically a real-time problem may occur, since the Host token is also sent approx. every 1ms : it can happen 00089 // that the token arrives while the Device endpoint has not been validated yet (the difference between these 00090 // two moments can be some µsecs), and the packet is not sent : 1 frame (1ms) is missed. So the sample rate has been 00091 // fixed every 120µs, to have a 40µs endpoint "free" delay to decrease error cases. 00092 Timer8_set_waveform_mode(TIMER8_WGM_CTC_OCR); // Timer 2 mode CTC 00093 Timer8_set_compare(120); // (8MHz/8) * 120µs = 120ctn 00094 Timer8_set_clock(TIMER8_0_CLKIO_BY_8); // Prescaler div8 00095 Timer8_compare_it_enable(); // Enable compare interrupt 00096 00097 b_micro_mute = FALSE; // default state : enabled 00098 }
| void audio_task | ( | void | ) |
Entry point of the audio management task
Definition at line 103 of file audio_task.c.
References Leds_set_val, MSB, and s16_micro_volume.
00104 { 00105 Leds_set_val(MSB(s16_micro_volume)); 00106 }
| __interrupt void timer2_comp_interrupt | ( | ) |
Timer2 comparator interrupt routine service Get ADC sample (microphone or alternate external source) and load it to the Isochronous endpoint.
Definition at line 120 of file audio_task.c.
References b_micro_mute, EP_AUDIO_IN, EP_SIZE_1, FALSE, g_u16_micro_sample_pos, Get_adc_ext_val(), Is_btn_middle, Is_device_enumerated, Is_joy_down, Is_joy_up, Is_usb_write_enabled, LSB, MSB, sample_sound, SAMPLE_SOUND_LEN, Usb_byte_counter, Usb_get_selected_endpoint, Usb_select_endpoint, Usb_send_in, and Usb_write_byte.
00122 { 00123 S16 sample; 00124 U8 sav_ep; 00125 00126 if(!Is_device_enumerated()) 00127 return; // Device not ready 00128 00129 sav_ep=Usb_get_selected_endpoint(); // save actually pipe 00130 Usb_select_endpoint(EP_AUDIO_IN); 00131 if(!Is_usb_write_enabled()) 00132 { 00133 Usb_select_endpoint(sav_ep); // restore pipe 00134 return; // Endpoint not free 00135 } 00136 00137 if (b_micro_mute == FALSE) 00138 { 00139 //** Acquires new sample (from on-board micro, from external source, or from sample in flash raw ) 00140 if( Is_btn_middle() || Is_joy_up() ) 00141 { 00142 // Play flash raw sample 00143 #ifdef __GNUC__ 00144 LSB(sample) = pgm_read_byte_near(&sample_sound[g_u16_micro_sample_pos++]); 00145 MSB(sample) = pgm_read_byte_near(&sample_sound[g_u16_micro_sample_pos++]); 00146 #else 00147 LSB(sample) = sample_sound[g_u16_micro_sample_pos++]; 00148 MSB(sample) = sample_sound[g_u16_micro_sample_pos++]; 00149 #endif 00150 if (g_u16_micro_sample_pos > SAMPLE_SOUND_LEN) 00151 g_u16_micro_sample_pos=0; // end of sample sound, then run in a loop 00152 } 00153 else if (Is_joy_down()) 00154 { 00155 // Get external input 00156 sample = (S32) (64*Get_adc_ext_val()-0x8000); // EXTERNAL : joystick pushed DOWN 00157 } 00158 else 00159 { 00160 // All joystick directions released 00161 // Get micro input 00162 sample = (S32) (64*Get_adc_mic_val()-0x8000); 00163 } 00164 // Send new sample 00165 Usb_write_byte(LSB(sample)); 00166 Usb_write_byte(MSB(sample)); 00167 } 00168 else 00169 { 00170 //** Mute enable then send new sample null 00171 Usb_write_byte(0); 00172 Usb_write_byte(0); 00173 } 00174 00175 // If pipe full then send it 00176 if (Usb_byte_counter()==EP_SIZE_1) 00177 { 00178 Usb_send_in(); 00179 } 00180 Usb_select_endpoint(sav_ep); // restore pipe 00181 }
| U16 Get_adc_ext_val | ( | void | ) |
Analog Channel 3 Acquisition routine (ADC)
| none |
Definition at line 192 of file audio_task.c.
Referenced by timer2_comp_interrupt().
00193 { 00194 Start_conv_channel(3); 00195 while (!Is_adc_conv_finished()); 00196 return (U16)(ADCL+((U16)(ADCH<<8))); 00197 }
| bit b_micro_mute |
Mute control of micro TRUE when ACTIVE, FALSE otherwise.
Definition at line 65 of file audio_task.c.
Referenced by audio_micro_get_mute(), audio_micro_set_mute(), audio_task_init(), and timer2_comp_interrupt().
Represents the current volume of audio stream micro No effet in this demo.
Definition at line 70 of file audio_task.c.
Referenced by audio_micro_get_vol_cur(), audio_micro_set_volume(), and audio_task().
Play position in sample sound storage in flash raw.
Definition at line 74 of file audio_task.c.
Referenced by timer2_comp_interrupt().
1.5.3