void codec_init(void){ manejador = DSK6713_AIC23_openCodec(0,&configuracion); DSK6713_AIC23_setFreq(manejador,fs); DSK6713_DIP_init(); DSK6713_LED_init(); }
int main(void) { /* Initialize the CSL and the CPLD */ CSL_init(); DSK6713_init(); DSK6713_LED_init(); /* Turn on one LED so we can see it executed at least the main function */ DSK6713_LED_on(0); /* Initialize the DIP switches to be able to read them */ DSK6713_DIP_init(); /* Configure the codec according to the definitions in config_AIC23.c * via the McBSP0 interface */ conf_AIC23(); /* Configure the McBSP to transfer the data from and to the codec */ conf_MCBSP(); /* Start the MCBSP */ start_MCBSP(); /* Configure EDMA */ conf_EDMA(); /* Time to initialize the buffer and zerofill it */ for(i = 0; i < 10; i++) FIFO_I[i] = 0; for(i = 0; i < 10; i++) FIFO_Q[i] = 0; /* Config Interrupts */ IRQ_enable(IRQ_EVT_EDMAINT); IRQ_map(IRQ_EVT_EDMAINT, 8); IRQ_globalEnable(); /* Enable the EDMA channels */ EDMA_enableChannel(hEDMATrx); /******************************************************/ /* We should be done here by now. The McBSP generates an * Interrupt (called "event" in this case) each time * there's a new word ready to be written or ready to * be transferred from the serial port to the * input buffer. We use it for the golden wire config * and will disable the input when we throw in the * QPSK modulation algorithm as it is not needed by then. */ /******************************************************/ /* End main - RTOS takes over */ }
/* * main() - The main user task. Performs application initialization and * starts the data transfer. */ void main() { /* Initialize Board Support Library */ DSK6713_init(); /* Initialize LEDs and DIP switches */ DSK6713_LED_init(); DSK6713_DIP_init(); /* Clear buffers */ memset((void *)gBufferXmtPing, 0, BUFFSIZE * 4 * 2); AIC23_setParams(&config); // Configure the codec initMcbsp(); // Initialize McBSP1 for audio transfers IRQ_globalDisable(); // Disable global interrupts during setup initEdma(); // Initialize the EDMA controller initIrq(); // Initialize interrupts IRQ_globalEnable(); // Re-enable global interrupts }
void main() { // initialize the save buffers for(j = 0; j < 2; j++){ for (i=0;i<SAVES;i++) { cde_save[i] = 0; pcf_save[i] = 0; clockoffset_save[i] = 0.0; fde_save[i] = 0.0; imax_save[i] = 0; recbuf_start_clock_save[i] = 0; ppm_estimate_save[i] = 0.0; ytilde_save[i] = 0.0; state_prediction[j][i] = 0.0; state_estimate[j][i] = 0.0; } } // set up the fractionally shifted buffers for(k = 0; k < FER; k++){ fractionalShift = ((double) k )/((double) FER); // number of samples to shift for (i=-N;i<=N;i++){ x = ((double) i - fractionalShift)*BW; if (x==0.0) { y = 32767.0; } else { t = ((double) i - fractionalShift)*CBW; y = 32767.0*cos(2*PI*t)*sin(PI*x)/(PI*x); // double } j = i; if (j<0) { j += L; // wrap } allMyDelayedWaveforms[k][j] = (short) y; } } // set up the cosine and sin matched filters for searching // also initialize searching buffer for (i=0;i<M;i++){ t = i*CBW; // time y = cos(2*PI*t); // cosine matched filter (double) mfc[i] = (float) y; // cast and store y = sin(2*PI*t); // sine matched filter (double) mfs[i] = (float) y; // cast and store buf[i] = 0; // clear searching buffer } // initialize clock buffers for (i=0;i<L;i++) { clockbuf[i] = 0; clockbuf_shifted[0][i] = 0; clockbuf_shifted[1][i] = 0; } // initialize sinc pulse buffer for (i=0;i<LL;i++) sincpulsebuf[i] = 0; // set up clock buffer and sinc pulse buffer // to play modulated sinc centered at zero for (i=-N;i<=N;i++){ x = i*BW; if (i!=0) { t = i*CBW; y = 32767.0*cos(2*PI*t)*sin(PI*x)/(PI*x); // double } else { y = 32767.0; } j = i; if (j<0) { j += L; // wrap } clockbuf[j] = (short) y; j = i; if (j<0) { j += LL; // wrap } sincpulsebuf[j] = (short) y; } // set up inphase and quadrature sinc pulses for coarse and fine delay estimators j = 0; for (i=-N;i<=N;i++){ x = i*BW; if (i!=0) { t = i*CBW; si[j] = (float) (cos(2*PI*t)*sin(PI*x)/(PI*x)); sq[j] = (float) (sin(2*PI*t)*sin(PI*x)/(PI*x)); } else { si[j] = 1.0; sq[j] = 0.0; } j++; } DSK6713_init(); // Initialize the board support library, must be called first DSK6713_LED_init(); // initialize LEDs hCodec = DSK6713_AIC23_openCodec(0, &config); // open codec and get handle // Configure buffered serial ports for 32 bit operation // This allows transfer of both right and left channels in one read/write MCBSP_FSETS(SPCR1, RINTM, FRM); MCBSP_FSETS(SPCR1, XINTM, FRM); MCBSP_FSETS(RCR1, RWDLEN1, 32BIT); MCBSP_FSETS(XCR1, XWDLEN1, 32BIT); // set codec sampling frequency DSK6713_AIC23_setFreq(hCodec, DSK6713_AIC23_FREQ_8KHZ); // interrupt setup IRQ_globalDisable(); // Globally disables interrupts IRQ_nmiEnable(); // Enables the NMI interrupt IRQ_map(IRQ_EVT_RINT1,15); // Maps an event to a physical interrupt IRQ_enable(IRQ_EVT_RINT1); // Enables the event IRQ_globalEnable(); // Globally enables interrupts DSK6713_LED_toggle(3); while(1) // main loop { if ((state==2)&&(delay_est_done==0)){ // TIME TO COMPUTE DELAY ESTIMATES DSK6713_LED_toggle(1); // toggle LED here for diagnostics /***************** * * * ESTIMATION * * * ****************/ // compute coarse delay estimate zmax = 0.0; // maximum imax = 0; // index of maximum for (i=0;i<(2*M-1);i++){ // lag index z = 0; for (j=0;j<(2*N+1);j++) { z+= si[j]*recbuf[i+j]; // correlation at lag i } if (abs(z)>zmax) { zmax = abs(z); // store maximum imax = i; // store index of maximum } } cde = recbuf_start_clock + imax + N; // coarse delay estimate (DRB: +N here because si is already shifted by N) // cde is the number of samples elapsed since we launched the S->M sinc pulse // compute fine delay estimate zi = 0.0; // in phase zq = 0.0; // quadrature for (j=0;j<(2*N+1);j++) { zi+= si[j]*recbuf[imax+j]; // correlation at lag imax zq+= sq[j]*recbuf[imax+j]; // correlation at lag imax } pcf = atan2(zq,zi)*(2*INVPI); // assumes wc = pi/2 fde = (float) cde + pcf; // compute actual clock offset // the value computed here is always non-negative and // represents the number of samples the master clock is ahead of the slave clock // (or the number of samples the slave clock is behind the master clock) // S->M sinc pulse was launched at time 0 // M->S sinc pulse was received at time fde (should be positive) // to synchronize, we want slave clock ticks to appear at fde/2 + k*L for k=0,1,.... clockoffset = fde*0.5; // testing/debugging imax_save[save_index] = imax; recbuf_start_clock_save[save_index] = recbuf_start_clock; cde_save[save_index] = cde; pcf_save[save_index] = pcf; fde_save[save_index] = fde; clockoffset_save[save_index] = clockoffset; //If first time running if(!hasSaved && save_index == 0){ state_estimate[0][0] = clockoffset* TS; state_estimate[1][0] = 0; state_prediction[0][1] = clockoffset * TS; state_prediction[1][1] = 0; } else { // save_index > 0 ytilde = clockoffset * TS - state_prediction[0][save_index]; // innovation (difference between current observation and prediction) state_estimate[0][save_index] = state_prediction[0][save_index] + ((double) K1)*ytilde; // filtered clock offset estimate state_estimate[1][save_index] = state_prediction[1][save_index] + ((double)K2)*ytilde; // filtered frequency offset estimate //Determine if a glitch has happened if (save_index+1<SAVES) { // generate predictions for next observation (make sure multiplication by LL doesn't cause datatype problems) state_prediction[0][save_index+1] = state_estimate[0][save_index] + state_estimate[1][save_index] * (double)T0;//* LL; state_prediction[1][save_index+1] = state_estimate[1][save_index]; } else if(save_index + 1 == SAVES){ state_prediction[0][0] = state_estimate[0][save_index] + state_estimate[1][save_index]* (double)T0;//* LL; state_prediction[1][0] = state_prediction[1][save_index]; } } //Calculate the rate offset if(!hasSaved && save_index < 1){ if(save_index == 0) rateoffset_estimate = 0; else rateoffset_estimate = TS * (clockoffset_save[save_index] - clockoffset_save[save_index - 1]); } //Otherwise, use kalman filter else{ rateoffset_estimate = state_prediction[0][save_index] - state_estimate[0][save_index]; } // if(clockoffset_save[save_index] > L && clockoffset_save[save_index - 1] < L) adjustedclockoffset = clockoffset_save[save_index] + 5 * L * rateoffset_estimate; else if(clockoffset_save[save_index] > L && clockoffset_save[save_index - 1] < L) adjustedclockoffset = clockoffset_save[save_index] + 3 * L *rateoffset_estimate; else adjustedclockoffset = clockoffset_save[save_index] + 4 * L * rateoffset_estimate; //////// j = (short) adjustedclockoffset; // casting from float to short just truncates, e.g., 3.99 becomes 3 fractionalShift = adjustedclockoffset - (double) j; // fractionalShift >= 0 and < 1 tells us how much of a sample is left k = (short) (fractionalShift * (double) FER + 0.5); // this now rounds and givse results on 0,...,FER if (k==FER) { // we rounded up to the next whole sample k = 0; j++; } for (i=0;i<L;i++) { ell = j+i; while (ell>=L) { ell = ell-L; } if (current_clockbuf==0) { clockbuf_shifted[1][ell] = allMyDelayedWaveforms[k][i]; // write other buffer } else { clockbuf_shifted[0][ell] = allMyDelayedWaveforms[k][i]; // write other buffer } } // when can we swap buffers? buffer_swap_index = j+L/2; // midpoint of the silent part (I think) while (buffer_swap_index>=L) buffer_swap_index = buffer_swap_index-L; // tell the ISR the calculations are done delay_est_done = 1; // update save index save_index++; if (save_index>=SAVES) // wrap { save_index = 0; hasSaved = 1; } DSK6713_LED_toggle(1); // toggle LED here for diagnostics } else if (buffer_just_swapped==1) { // this code computes the next buffer and attempts to corect for the frequency offset buffer_just_swapped = 0; // copy appropriate fractionally shifted clock buffer to shifted clock buffer adjustedclockoffset = adjustedclockoffset + ((double) L)*one_over_beta; // adjust latency of next pulse while (adjustedclockoffset>((double) L)) adjustedclockoffset = adjustedclockoffset - (double) L; j = (short) adjustedclockoffset; // casting from float to short just truncates, e.g., 3.99 becomes 3 fractionalShift = adjustedclockoffset - (double) j; // fractionalShift >= 0 and < 1 tells us how much of a sample is left k = (short) (fractionalShift * (double) FER); // this also truncates and should give result on 0,...,FER-1 for (i=0;i<L;i++) { ell = j+i; if (ell>=L) { ell = ell-L; } if (current_clockbuf==0) { clockbuf_shifted[1][ell] = allMyDelayedWaveforms[k][i]; // write other buffer } else { clockbuf_shifted[0][ell] = allMyDelayedWaveforms[k][i]; // write other buffer } } } // if ((state==2)&&(delay_est_done==0)) } // while(1) } // void main
void main() { // set up the cosine and sin matched filters for searching // also initialize searching buffer for (i=0;i<M;i++){ t = i*0.25; // time y = cos(2*PI*t); // cosine matched filter (double) mfc[i] = (float) y; // cast and store y = sin(2*PI*t); // sine matched filter (double) mfs[i] = (float) y; // cast and store buf[i] = 0.0; // clear searching buffer fbuf[i] = 0.0; } // initialize input buffer for (i=0;i<BL;i++) inbuf[i] = 0.0; // initialize clock buffer for (i=0;i<L;i++) clockbuf[i] = 0; // set up clock buffer to play modulated sinc centered at zero for (i=-N;i<=N;i++){ x = i*BW; if (i!=0) { t = i*0.25; y = ((double) CLOCKAMPLITUDE)*cos(CARRIERKHZ*PI*t)*sin(PI*x)/(PI*x); // double } else { y = ((double) CLOCKAMPLITUDE); } j = i; if (j<0) { j += L; // wrap } clockbuf[j] = (short) y; } DSK6713_init(); // Initialize the board support library, must be called first DSK6713_LED_init(); // initialize LEDs hCodec = DSK6713_AIC23_openCodec(0, &config); // open codec and get handle // Configure buffered serial ports for 32 bit operation // This allows transfer of both right and left channels in one read/write MCBSP_FSETS(SPCR1, RINTM, FRM); MCBSP_FSETS(SPCR1, XINTM, FRM); MCBSP_FSETS(RCR1, RWDLEN1, 32BIT); MCBSP_FSETS(XCR1, XWDLEN1, 32BIT); // set codec sampling frequency DSK6713_AIC23_setFreq(hCodec, DSK6713_AIC23_FREQ_16KHZ); // interrupt setup IRQ_globalDisable(); // Globally disables interrupts IRQ_nmiEnable(); // Enables the NMI interrupt IRQ_map(IRQ_EVT_RINT1,15); // Maps an event to a physical interrupt IRQ_enable(IRQ_EVT_RINT1); // Enables the event IRQ_globalEnable(); // Globally enables interrupts while(1) // main loop { } }