[SOLVED]Audio codec shield with Arduino Due

Re: Audio codec shield with Arduino Due

Postby youthreewire » Thu Jul 09, 2015 10:49 pm

I looked at the SS pin wave on the atmega in which case the shield was working. The SS pin was high for exactly 1 us and its freuency was 44 Khz. But in case of the Due that I am experimenting with the SS pin is high for 5us with a frequency of 66.67Khz.I think the SS pin timing is faulty and should be rectified. I do not know what to do.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby guest » Thu Jul 09, 2015 11:39 pm

the first thing to do is to get the right frequency. this is done by setting your clock divider to the right number. it should either be 127 or 128. then, you can set the pulse width by using direct port manipulation. i am assuming you are currently using digitalwrite(), and that you are writing 1, directly followed by 0. if this gives you 5us, you need to write to the pin faster, and direct manipulation using C and the port address is the fastest way of doing this. you will hae to look up online what the port variables are for the DUE. you will also have to place nop() between them to slow it down.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 12:59 am

digitalWrite() without using nop or using nop is at 2us but the SS pin was toggling at 1us in the mega for which the library was working. Some how by changing the divider rc in the code here:
Code: Select all
uint32_t rc = (5714285/128); //128 because we selected TIMER_CLOCK4 above
        //TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        //TC_SetRC(tc, channel, rc);
        //TC_Start(tc, channel);
       
   TC_SetRA(TC2,TC2_CHANNEL_FOR_TCLK8,rc/2);     
   TC_SetRC(TC2, TC2_CHANNEL_FOR_TCLK8,rc);
   /* start the timer */
   TC_Start(TC2, TC2_CHANNEL_FOR_TCLK8);
   /* enable the timer-specific interrupts */
   REG_TC2_IER2 = TC_IER_CPCS | TC_IER_ETRGS;
   TC2->TC_CHANNEL[TC2_CHANNEL_FOR_TCLK8].TC_IER=TC_IER_CPCS;
   TC2->TC_CHANNEL[TC2_CHANNEL_FOR_TCLK8].TC_IDR=~TC_IER_CPCS;
Is still not changing the frequency of the SS pin.It stands high for 2us and if I change the lines in the ISR I get variable delay.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 2:20 am

I managed to turn a pin at 45.454 Khz and I am able to complete the SPI.transfers at such frequency but still my SPI data reads zero.i need to give it a time period of 22.5 microseconds but I am able to delay only by 22 us. And that is why the pin is not toggling at exactly 44100 Khz. I might have to add _asm_(nop) but how many at 96 Mhz of the Due is not know to me.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 3:18 am

Ok, I used _asm_(nops) and now I am very close to 44Khz. This is being generated on pin12 and not the pin 10 which is assigned for SPI transfers in the code.

So what should I set this now to:
Code: Select all
SPI.begin(10);
        SPI.setBitOrder(MSBFIRST);
        SPI.setClockDivider(10,21); // select to ss pin 10, 4 MHz// favourable values 11,21,84
        SPI.setDataMode(10,SPI_MODE0);

Does it need to be changed?

What shall I do now.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 3:27 am

This is the ISR handler code:
Code: Select all
void TC8_Handler()
{
  ledon1= !(ledon1);
    digitalWrite(13,ledon1);
 
    delayMicroseconds(1);
  digitalWriteDirect(12,LOW);   
    //digitalWrite(12,LOW);
   
   
        //temp = left_out;
        //temp1 = right_out;
        temp_spi[0] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[1] = SPI.transfer(10,lowByte(temp),SPI_CONTINUE);
        temp_spi[2] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[3] = SPI.transfer(10,lowByte(temp),SPI_LAST);
        //left_in - (temp_spi[0]<<8)|temp_spi[1];
        //right_in = (temp_spi[2]<<8)|temp_spi[3];
        delayMicroseconds(7.5);
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
       
}
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 6:15 am

Now this is my final code which gives a good 44.1Khz signal for the SS pin based on timer interrupt. Also has a good SCK clock wave at 9mhz. And has the clock out signal coming from the codec configures at 5.7Mhz.
Code: Select all
#include<SPI.h>
#include<Wire.h>
#define TC2_CHANNEL_FOR_TCLK8 2  /* presumed to correspond to XC2 */
#define LINVOL 23
#define RINVOL 23
#define LHPVOL 100
#define RHPVOL 100
#define ADCHPD 0
#define SIDEATT 0
#define SIDETONE 0
#define DACSEL 1
#define BYPASS 0
#define INSEL 0
#define MUTEMIC 1
#define MICBOOST 0
#define SAMPLE_RATE 44

volatile boolean l;
int cnt=0,startflag=0;
unsigned int temp = 0,temp1=0;
volatile int temp_spi[4];
int m1[301],m2[301];
//TC1 ch 0
int left_out,right_out;
enum Phase
   {
      TC2_INTERNAL,
      PIOD_EXTERNAL,
      TC2_EXTERNAL,
      DONE
   };
   
  /* enum Phase
   {
      TC2_EXTERNAL,
      TC2_EXTERNAL,
      TC2_EXTERNAL,
      DONE
   };*/

Phase phase;
int pioIRQsServiced;
int tc2IRQsServiced[3];
unsigned int status;
int upflag = 0;
bool ledon,ledon1=0;
int oldmillis=0;
volatile int left_in=30000;
volatile int right_in=30000;
volatile int myi=0;
/****************************************************************/

void PIOD_Handler(void)
{
   /* clear interrupt */
   status = REG_PIOD_ISR;
   pioIRQsServiced++;
}


/****************************************************************/
inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

inline int digitalReadDirect(int pin){
  return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);
}

void TC8_Handler()
{
  ledon1= !(ledon1);
    digitalWrite(13,ledon1);
  digitalWriteDirect(12,HIGH);
    delayMicroseconds(1);
  digitalWriteDirect(12,LOW);   
    //digitalWrite(12,LOW);
    status = TC_GetStatus(TC2, TC2_CHANNEL_FOR_TCLK8);
   if (status & TC_SR_CPCS)
   {
      if (phase == TC2_INTERNAL)
      {
         tc2IRQsServiced[0]++;
      }
      else
      {
         tc2IRQsServiced[1]++;
      }
   }
   if (status & TC_SR_ETRGS)
   {
      tc2IRQsServiced[2]++;
   }
   
        //temp = left_out;
        //temp1 = right_out;
        temp_spi[0] = SPI.transfer(highByte(temp),SPI_CONTINUE);
        temp_spi[1] = SPI.transfer(lowByte(temp),SPI_CONTINUE);
        temp_spi[2] = SPI.transfer(highByte(temp),SPI_CONTINUE);
        temp_spi[3] = SPI.transfer(lowByte(temp),SPI_LAST);
        left_in - (temp_spi[0]<<8)|temp_spi[1];
        right_in = (temp_spi[2]<<8)|temp_spi[3];
        delayMicroseconds(7.5);
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        /*__asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
       
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");*/
       
       
}


/****************************************************************/

void SetupPIOD(void)
{
   int result;

   /* power on PIO ("Parallel Input/Output Controller") controller D */
   pmc_enable_periph_clk(ID_PIOD);
   /* enable interrupts for PIOD */
   NVIC_EnableIRQ(PIOD_IRQn);
   /* configure PD9 as general-purpose IO pin as opposed to the TCLK8 peripheral */
   result = PIO_Configure(PIOD, PIO_INPUT, PIO_PD9, PIO_IT_RISE_EDGE);
   printf("%d <- PIO_Configure\n", result);
   /* ensure we're interrupting on the rising edge of the external clock */
   REG_PIOD_AIMER = PIO_PD9;
   REG_PIOD_ESR = PIO_PD9;
   REG_PIOD_REHLSR = PIO_PD9;
   /* enable interrupts */
   REG_PIOD_IER = PIO_PD9;
}


/****************************************************************/

void TakeDownPIOD(void)
{
   TC_Stop(TC2, TC2_CHANNEL_FOR_TCLK8);
   TC_SetRC(TC2, TC2_CHANNEL_FOR_TCLK8, 0);
   REG_PIOD_IDR = PIO_PD9;
   NVIC_DisableIRQ(TC8_IRQn);
   pmc_disable_periph_clk(ID_PIOD);
}


/****************************************************************/

void SetupTC2(bool useInternalClock)
{
   int result;

   /* "The user must configure the Power Management Controller before any access to the input line information." per 34.4.2 */
   pmc_enable_periph_clk(ID_TC8);
   /* "Using the PIO Controller requires the NVIC to be programmed first." per 34.4.3 */
   NVIC_EnableIRQ(TC8_IRQn);
   result = PIO_Configure(PIOD, PIO_PERIPH_B, PIO_ABSR_P9, PIO_DEFAULT);
   //printf("%d <- PIO_Configure\n", result);
   if (useInternalClock)
   {
      TC_Configure(TC2,
                TC2_CHANNEL_FOR_TCLK8,
                TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVSEL_UP_RC | TC_CMR_WAVE);
   }
   else
   {
      TC_Configure(TC2,
                TC2_CHANNEL_FOR_TCLK8,
                (TC_CMR_TCCLKS_XC2 |
                 TC_CMR_EEVTEDG_RISING |
                 TC_CMR_EEVT_XC2 |
                 TC_CMR_ENETRG |
                 TC_CMR_WAVSEL_UP_RC |
                 TC_CMR_WAVE));
   }
   //5714285
  uint32_t rc = (5714285/128); //128 because we selected TIMER_CLOCK4 above
        //TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        //TC_SetRC(tc, channel, rc);
        //TC_Start(tc, channel);
       
   TC_SetRA(TC2,TC2_CHANNEL_FOR_TCLK8,rc/2);     
   TC_SetRC(TC2, TC2_CHANNEL_FOR_TCLK8,rc);
   /* start the timer */
   TC_Start(TC2, TC2_CHANNEL_FOR_TCLK8);
   /* enable the timer-specific interrupts */
   REG_TC2_IER2 = TC_IER_CPCS | TC_IER_ETRGS;
   TC2->TC_CHANNEL[TC2_CHANNEL_FOR_TCLK8].TC_IER=TC_IER_CPCS;
   TC2->TC_CHANNEL[TC2_CHANNEL_FOR_TCLK8].TC_IDR=~TC_IER_CPCS;
  // NVIC_EnableIRQ(TC8_IRQn);
}


/****************************************************************/

void TakeDownTC2(void)
{
   TC_Stop(TC2, TC2_CHANNEL_FOR_TCLK8);
   TC_SetRC(TC2, TC2_CHANNEL_FOR_TCLK8, 0);
   REG_TC2_IDR2 = TC_IER_CPCS | TC_IER_ETRGS;
   NVIC_DisableIRQ(TC8_IRQn);
   pmc_disable_periph_clk(ID_TC8);
}

/* Clock settings (96MHz) */
 #define SYS_BOARD_OSCOUNT   (CKGR_MOR_MOSCXTST(0x8UL))
 #define SYS_BOARD_PLLAR     (CKGR_PLLAR_ONE \
                             | CKGR_PLLAR_MULA(0xfUL) \
                             | CKGR_PLLAR_PLLACOUNT(0x3fUL) \
                             | CKGR_PLLAR_DIVA(0x1UL))
 #define SYS_BOARD_MCKR      (PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK)
 
 /* Clock Definitions */
 #define SYS_UTMIPLL             (480000000UL)   /* UTMI PLL frequency */
 
 #define SYS_CKGR_MOR_KEY_VALUE  CKGR_MOR_KEY(0x37UL) /* Key to unlock MOR register */
 
 /* FIXME: should be generated by sock */
 uint32_t SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
 
 void SystemInit(void)
 {
     /* Set FWS according to SYS_BOARD_MCKR configuration */
     EFC0->EEFC_FMR = EEFC_FMR_FWS(4);
     EFC1->EEFC_FMR = EEFC_FMR_FWS(4);
 
     /* Initialize main oscillator */
     if (!(PMC->CKGR_MOR & CKGR_MOR_MOSCSEL)) {
         PMC->CKGR_MOR = SYS_CKGR_MOR_KEY_VALUE | SYS_BOARD_OSCOUNT |
                                 CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
         while (!(PMC->PMC_SR & PMC_SR_MOSCXTS)) {
         }
     }
 
     /* Switch to 3-20MHz Xtal oscillator */
     PMC->CKGR_MOR = SYS_CKGR_MOR_KEY_VALUE | SYS_BOARD_OSCOUNT |
                                CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN | CKGR_MOR_MOSCSEL;
 
     while (!(PMC->PMC_SR & PMC_SR_MOSCSELS)) {
     }
     PMC->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_Msk) |
                               PMC_MCKR_CSS_MAIN_CLK;
     while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {
     }
 
     /* Initialize PLLA */
     PMC->CKGR_PLLAR = SYS_BOARD_PLLAR;
     while (!(PMC->PMC_SR & PMC_SR_LOCKA)) {
     }
 
     /* Switch to main clock */
     PMC->PMC_MCKR = (SYS_BOARD_MCKR & ~PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
     while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {
     }
 
     /* Switch to PLLA */
     PMC->PMC_MCKR = SYS_BOARD_MCKR;
     while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {
     }
 
     SystemCoreClock = CHIP_FREQ_CPU_MAX;
 }
 
 void SystemCoreClockUpdate(void)
 {
     /* Determine clock frequency according to clock register values */
     switch (PMC->PMC_MCKR & PMC_MCKR_CSS_Msk) {
     case PMC_MCKR_CSS_SLOW_CLK: /* Slow clock */
         if (SUPC->SUPC_SR & SUPC_SR_OSCSEL) {
             SystemCoreClock = CHIP_FREQ_XTAL_32K;
         } else {
             SystemCoreClock = CHIP_FREQ_SLCK_RC;
         }
         break;
     case PMC_MCKR_CSS_MAIN_CLK: /* Main clock */
         if (PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) {
             SystemCoreClock = CHIP_FREQ_XTAL_12M;
         } else {
             SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
 
             switch (PMC->CKGR_MOR & CKGR_MOR_MOSCRCF_Msk) {
             case CKGR_MOR_MOSCRCF_4_MHz:
                 break;
             case CKGR_MOR_MOSCRCF_8_MHz:
                 SystemCoreClock *= 2U;
                 break;
             case CKGR_MOR_MOSCRCF_12_MHz:
                 SystemCoreClock *= 3U;
                 break;
             default:
                 break;
             }
         }
         break;
     case PMC_MCKR_CSS_PLLA_CLK: /* PLLA clock */
     case PMC_MCKR_CSS_UPLL_CLK: /* UPLL clock */
         if (PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) {
             SystemCoreClock = CHIP_FREQ_XTAL_12M;
         } else {
             SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
 
             switch (PMC->CKGR_MOR & CKGR_MOR_MOSCRCF_Msk) {
             case CKGR_MOR_MOSCRCF_4_MHz:
                 break;
             case CKGR_MOR_MOSCRCF_8_MHz:
                 SystemCoreClock *= 2U;
                 break;
             case CKGR_MOR_MOSCRCF_12_MHz:
                 SystemCoreClock *= 3U;
                 break;
             default:
                 break;
             }
         }
         if ((PMC->PMC_MCKR & PMC_MCKR_CSS_Msk) == PMC_MCKR_CSS_PLLA_CLK) {
             SystemCoreClock *= ((((PMC->CKGR_PLLAR) & CKGR_PLLAR_MULA_Msk) >>
                                           CKGR_PLLAR_MULA_Pos) + 1U);
             SystemCoreClock /= ((((PMC->CKGR_PLLAR) & CKGR_PLLAR_DIVA_Msk) >>
                                            CKGR_PLLAR_DIVA_Pos));
         } else {
             SystemCoreClock = SYS_UTMIPLL / 2U;
         }
         break;
     }
 
     if ((PMC->PMC_MCKR & PMC_MCKR_PRES_Msk) == PMC_MCKR_PRES_CLK_3) {
         SystemCoreClock /= 3U;
     } else {
         SystemCoreClock >>= ((PMC->PMC_MCKR & PMC_MCKR_PRES_Msk) >>
                                        PMC_MCKR_PRES_Pos);
     }
 }
 
 void TC3_Handler()
{
        TC_GetStatus(TC1, 0);

        /*temp = random(65535);
        //temp = left_out;
        //temp1 = right_out;
        temp_spi[0] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[1] = SPI.transfer(10,lowByte(temp),SPI_CONTINUE);
        temp_spi[2] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[3] = SPI.transfer(10,lowByte(temp),SPI_LAST);
        //left_in - (temp_spi[0]<<8)|temp_spi[1];
        //right_in = (temp_spi[2]<<8)|temp_spi[3];
        /*Serial.println("Hello");
        Serial.println(temp_spi[0]);
        Serial.println(temp_spi[1]);
        Serial.println(temp_spi[2]);
        Serial.println(temp_spi[3]);*/
}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
        pmc_set_writeprotect(false);
        pmc_enable_periph_clk((uint32_t)irq);
        TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
        uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        TC_SetRC(tc, channel, rc);
        TC_Start(tc, channel);
        tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
        tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
        NVIC_EnableIRQ(irq);
}

 void AudioCodec_init() {
        randomSeed(analogRead(0));
        SPI.begin();
        SPI.setBitOrder(MSBFIRST);
        SPI.setClockDivider(21); // select to ss pin 10, 4 MHz// favourable values 11,21,84
        SPI.setDataMode(SPI_MODE0);
       
        // setup i2c pins and configure codec
        // the new Wire library has trouble with 0x00, so (uint8_t) is added
        // To change the Wire interface speed, go to:
        // <path_from_arduino>\hardware\arduino\sam\libraries\Wire\Wire.h
        // and change the parameters TWI_CLOCK, RECV_TIMEOUT and XMIT_TIMEOUT
        // to the desire frequency.
        int temp_wire1;
        int temp_wire2;
       
        Wire.begin();
        Wire.beginTransmission(0x1a);
        Wire.write(0x0c); // power reduction register
        Wire.write((uint8_t)0x00); // turn everything on
        temp_wire1 = Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x0e); // digital data format
        Wire.write(0x03); // 16b SPI mode
        temp_wire2 = Wire.endTransmission();
       
        Serial.println(temp_wire1);
        Serial.println(temp_wire2);
       
        Wire.beginTransmission(0x1a);
        Wire.write((uint8_t)0x00); // left in setup register
        Wire.write((uint8_t)LINVOL);
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x02); // right in setup register
        Wire.write((uint8_t)RINVOL);
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x04); // left headphone out register
        Wire.write((uint8_t)LHPVOL);
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x06); // right headphone out register
        Wire.write((uint8_t)RHPVOL);
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x0a); // digital audio path configuration
        Wire.write((uint8_t)ADCHPD);
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x08); // analog audio pathway configuration
        Wire.write((uint8_t)((SIDEATT << 6)|(SIDETONE << 5)|(DACSEL << 4)|(BYPASS << 3)|(INSEL << 2)|(MUTEMIC << 1)|(MICBOOST << 0)));
        Wire.endTransmission();
       
        Wire.beginTransmission(0x1a);
        Wire.write(0x10); // clock configuration
        #if SAMPLE_RATE == 88
          Wire.write(0xbc);
        #elif SAMPLE_RATE == 44
          Wire.write(0xa0);
        #elif SAMPLE_RATE == 22
          Wire.write(0xe0);
        #elif SAMPLE_RATE == 8
          Wire.write(0xac);
        #elif SAMPLE_RATE == 2
          Wire.write(0xce);
        #endif
        Wire.endTransmission();
     
        Wire.beginTransmission(0x1a);
        Wire.write(0x12); // codec enable
        Wire.write(0x01);
        Wire.endTransmission();
       
}
/****************************************************************/

void setup(void)
{
   pinMode(12,OUTPUT);
   pinMode(13,OUTPUT);
   //SPI.begin();
   SystemInit();
   SystemCoreClockUpdate();
   AudioCodec_init();
   //startTimer(TC1, 0, TC3_IRQn, 44100);
   Serial3.begin(6000000);
   Serial3.println("Ranjan test");
   Serial3.println("start with TC2 internal\n");
   //SetupTC2(false);
}


/****************************************************************/

void loop(void)
{
 
   //Serial2.println("Rnnjan in loop");
    // temp_spi[0] = SPI.transfer(10,highByte(temp),SPI_CONTINUE); 
  phase=TC2_EXTERNAL;
  /*if(startflag==0)
  //if(1)
  {
  //SetupPIOD();
  SetupTC2(false);
  startflag=1;
  }*/
 
  //Serial.println(phase);
   switch (phase)
   {
      case TC2_INTERNAL:
         if (tc2IRQsServiced[0] > 250000)
         {
            phase = PIOD_EXTERNAL;
            TakeDownTC2();
            printf("PASSED\nswitch to PIOD\n");
            SetupPIOD();
         }
         break;
      case PIOD_EXTERNAL:
         if (pioIRQsServiced > 20)
         {
            phase = TC2_EXTERNAL;
            TakeDownTC2();
            printf("PASSED\nswitch to TC2 external\n");
            SetupTC2(false);
         }
         break;
      case TC2_EXTERNAL:
      SetupTC2(false);
         if (tc2IRQsServiced[1] > 20)
         {
            printf("PASSED!!!\n");
            for ( ; ; );
         }
         break;
      default:
         break;
   }
   //delay(1000);
   Serial3.println(temp_spi[1]);
   //Serial3.println(right_in);
  // printf("PIO IRQs: %d, CPCS internal IRQs: %d, CPCS external IRQs: %d, ETRGS external IRQs: %d, status: %08x, CV2: %d\n", pioIRQsServiced, tc2IRQsServiced[0], tc2IRQsServiced[1], tc2IRQsServiced[2], status, REG_TC2_CV2);
 
}


If you can try this code and let me know what is still wrong I will be thankful. It some times gets some values from the mic in temp-spi[0] variables but that is like very sparse. A 100 values might have 2 or 3 useful values. Some times we get a bunch of values.For some reason this code stops spitting the serial port values after some time. Also this uses an FT232H configured at 6000000 bps (6mbps) on Serial port 3.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby guest » Fri Jul 10, 2015 10:59 am

what happens if you get rid of the delayMicroseconds() here? does the pulse width decrease? also, how does the pulse align with the SPI clock? does it match the datasheet?

Code: Select all
digitalWriteDirect(12,HIGH);
delayMicroseconds(1);
digitalWriteDirect(12,LOW);


and what happens when you get rid of this stuff? it shouldnt matter either way, as timing should be determined by the counter, not the interrupt length.

Code: Select all
delayMicroseconds(7.5);
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 10:06 pm

guest wrote:what happens if you get rid of the delayMicroseconds() here? does the pulse width decrease? also, how does the pulse align with the SPI clock? does it match the datasheet?

Code: Select all
digitalWriteDirect(12,HIGH);
delayMicroseconds(1);
digitalWriteDirect(12,LOW);



Yes the pulse width gets reduced if I get rid of it. Actually I checked again on the mega and found that the pin SS is high for 200ns and I made changes to my code accordingly.
and what happens when you get rid of this stuff? it shouldnt matter either way, as timing should be determined by the counter, not the interrupt length.

Code: Select all
delayMicroseconds(7.5);
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");

Yes the phase of signal low get effected if I change the delayMicoseconds and nops here.

also, how does the pulse align with the SPI clock? does it match the datasheet?
I dont understand this.

Here is the code for my updated ISR handler.
Code: Select all
void TC8_Handler()
{
  ledon1= !(ledon1);
    digitalWrite(13,ledon1);
  digitalWriteDirect(12,HIGH);
    //delayMicroseconds(1);
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   __asm__("nop");
   
  digitalWriteDirect(12,LOW);   
    //digitalWrite(10,LOW);
    status = TC_GetStatus(TC2, TC2_CHANNEL_FOR_TCLK8);
   if (status & TC_SR_CPCS)
   {
      if (phase == TC2_INTERNAL)
      {
         tc2IRQsServiced[0]++;
      }
      else
      {
         tc2IRQsServiced[1]++;
      }
   }
   if (status & TC_SR_ETRGS)
   {
      tc2IRQsServiced[2]++;
   }
   
        //temp = left_out;
        //temp1 = right_out;
        temp_spi[0] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[1] = SPI.transfer(10,lowByte(temp),SPI_CONTINUE);
        temp_spi[2] = SPI.transfer(10,highByte(temp),SPI_CONTINUE);
        temp_spi[3] = SPI.transfer(10,lowByte(temp),SPI_LAST);
        left_in - (temp_spi[0]<<8)|temp_spi[1];
        right_in = (temp_spi[2]<<8)|temp_spi[3];
        delayMicroseconds(9);
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
        __asm__("nop");
}
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Arduino Due

Postby youthreewire » Fri Jul 10, 2015 11:08 pm

The due is acting crazy. Some times the delayMicroseconds has an effect and some times it doesnt.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

PreviousNext

Return to Audio Codec Shield

Who is online

Users browsing this forum: No registered users and 1 guest