Audio codec shield with Intel Edison

Re: Audio codec shield with Intel Edison

Postby guest » Wed Dec 02, 2015 11:44 pm

that code may not work, depending upon how youve defined the variables, it might lose the sign bit (which sounds like crackling). a good test is to create a signed 16b integer, and increment its value by 1 each time you write to the codec. the output should look like a saw tooth wave. if it does not, the shape it forms can tell you something about where the error is occuring (as longs as its not just noise). the frequency is really low at a +1 increment, so try +100 if youre not probing the DC coupled output (or just want to listen to it).
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Intel Edison

Postby youthreewire » Fri Dec 04, 2015 7:07 am

Could you please try the software SPI code on one of your arduinos with the shield and guide me?Please.... Here is the link to software SPI. I changed the code of the transfer function further.
my transfer function is like this
Code: Select all
uint8_t spi_transfer(uint8_t b,uint8_t uu_firstbyte)
  {
    uint8_t reply=0;
    char bits[8] = {0, 0, 0, 0, 0, 0, 0, 0};  //reading buffer
     
      for(uint8_t _bit = 0;_bit < 8;_bit++)
      {
        if(uu_firstbyte==1 && _bit==0)
        {
        digitalWriteDirect(11, HIGH);
        __asm__("nop");
         __asm__("nop");
         __asm__("nop");
        digitalWriteDirect(_SCK, HIGH);
         __asm__("nop");
       
         bits[_bit] = digitalReadDirect(_MISO);
         digitalWriteDirect(_MOSI, !!(b & msk[_bit]));
         digitalWriteDirect(11, LOW);
         digitalWriteDirect(_SCK, LOW);
        }

        if((_bit>=1)||(uu_firstbyte==2))
        {
         bits[_bit] = digitalReadDirect(_MISO);
         digitalWriteDirect(_MOSI, !!(b & msk[_bit]));
       digitalWriteDirect(_SCK, HIGH);
        __asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
       digitalWriteDirect(_SCK, LOW);
        }
      }
     
      reply = PCK();
 
    return reply;
     
 
      }
     
  }


The reason I think this is important is that once the software spi gets working we can extend it to any other microcontroller or processor board and the shield will be free from hardware spi dependency.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Intel Edison

Postby guest » Fri Dec 04, 2015 2:41 pm

sure, i can give it a try, but it might be a while. did you have any luck getting it working on your end?

also, how long a "nop" is, is hardware dependent. on some platforms it might make the SPI too fast.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Intel Edison

Postby youthreewire » Sat Dec 05, 2015 12:48 am

Thanks. Please give it a try.

I am running the code on Arduino Due which is overclocked at 96 mhz. So as you rightly said nop will introduce different timing. You will be needing an oscilloscope that can measure in nano seconds (my scale was that one division on the scope was 200 nanoseconds long).I tried changing the values to match with the hardware spi that I got to work on the Due.

I will wait for the results from your end on the software SPI. You know better about timing.

Here is the link to the original software SPI .
http://www.iesensor.com/blog/2013/07/09/software-spi-library-for-arduino-tested-on-mega2560/
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Intel Edison

Postby guest » Mon Dec 07, 2015 4:12 pm

do you have a complete sketch i can test? im not sure what 'b' or msk[] are referring to.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Intel Edison

Postby youthreewire » Tue Dec 08, 2015 11:15 pm

I will give you the sketch and I think as you said I should test with an unsigned 8 bit integer.I will get back to you shortly.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Intel Edison

Postby youthreewire » Fri Dec 11, 2015 12:35 am

Here is the sketch.It compiles but there could be a logical error
Code: Select all
// setup codec parameters
    // must be done before #includes
    // see readme file in libraries folder for explanations
    #define SAMPLE_RATE 44 // 44.1Khz
    #define ADCS 0 // no ADCs are being used

    // include necessary libraries
    #include <Wire.h>
    #include <SPI.h>
    #include <AudioCodec.h>
    #include <digitalWriteFast.h>
   #define _MISO  8
    #define _MOSI  9
    #define _SCK   10
    #define _SS  11
    #define PCK() (bits[0] << 7 | bits[1] << 6 | bits[2] << 5 | bits[3] << 4 | bits[4] << 3 | bits[5] << 2 | bits[6] << 1 | bits[7])
    unsigned char msk[] = {0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1};
   // #define PCK() (bits[0] << 7 | bits[1] << 6 | bits[2] << 5 | bits[3] << 4 | bits[4] << 3 | bits[5] << 2 | bits[6] << 1 | bits[7]);
 
    // create data variables for audio transfer
    int left_in = 0x0000;
    int left_out = 0x0000;
    int right_in = 0x0000;
    int right_out = 0x0000;
uint8_t spi_transfer(uint8_t b,uint8_t uu_firstbyte)
  {
    uint8_t reply=0;
    char bits[8] = {0, 0, 0, 0, 0, 0, 0, 0};  //reading buffer
     
      for(uint8_t _bit = 0;_bit < 8;_bit++)
      {
        if(uu_firstbyte==1 && _bit==0)
        {
        digitalWriteFast(11, HIGH);
        __asm__("nop");
         __asm__("nop");
         __asm__("nop");
        digitalWriteFast(_SCK, HIGH);
         __asm__("nop");
       
         bits[_bit] = digitalReadFast(_MISO);
         digitalWriteFast(_MOSI, !!(b & msk[_bit]));
         digitalWriteFast(11, LOW);
         digitalWriteFast(_SCK, LOW);
        }

        if((_bit>=1)||(uu_firstbyte==2))
        {
         bits[_bit] = digitalReadFast(_MISO);
         digitalWriteFast(_MOSI, !!(b & msk[_bit]));
       digitalWriteFast(_SCK, HIGH);
        __asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
         //__asm__("nop");
       digitalWriteFast(_SCK, LOW);
        }
      }
     
      reply = PCK();
 
    return reply;
     
 
     
     
  }
    void setup() {
      Serial.begin(115200);
      AudioCodec_init(); // setup codec registers
      // call this last if setting up other parts
    }

    void loop() {
      //Serial.println(left_in); // reduces clock jitter
     
    }

static inline void AudioCodec_data2(int _lin, int _rin, int _lout, int _rout)
{ _lout= spi_transfer(_lin,1);
  _rout = spi_transfer(_rin,2);
 
}
// timer1 interrupt routine - all data processed here
    ISR(TIMER1_COMPA_vect) { // NAKED turned off, so registers are stored
      AudioCodec_data2(left_in, right_in, left_out, right_out);
      left_out = left_in;
      right_out = right_in;
      //Serial.println(left_in);
      //reti(); // dont need this without NAKED
    }
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Intel Edison

Postby guest » Fri Dec 11, 2015 3:52 pm

what failure mode do you see with this code?

a couple of things to note:

1. you can not send audio data over the serial comm, the audio data rate is too high
2. hardware spi is still being setup with codec.h, which is probably conflicting with direct port manipulation.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Audio codec shield with Intel Edison

Postby youthreewire » Sat Dec 12, 2015 4:19 am

I know that the audio data cannot be sent over the serial port. Also I just made this sketch for you.I was using an ARDUINO Due and since you had atmega328 I just wrote this code up.Feel free to modify as you want it.

I also created a 6 MBPS transfer rate on the Due but still it had some delay.
youthreewire
 
Posts: 117
Joined: Tue Jun 09, 2015 8:06 pm

Re: Audio codec shield with Intel Edison

Postby guest » Sun Dec 13, 2015 10:14 pm

i gave it a try, and sadly it does not work. here is my code:

Code: Select all
// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 0 // no ADCs are being used

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>
#include <digitalWriteFast.h>
#define _MISO  12
#define _MOSI  11
#define _SCK   13
#define _SS  10
#define PCK() (bits[0] << 7 | bits[1] << 6 | bits[2] << 5 | bits[3] << 4 | bits[4] << 3 | bits[5] << 2 | bits[6] << 1 | bits[7])
unsigned char msk[] = {0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1};

// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;

uint8_t spi_transfer(uint8_t b,uint8_t uu_firstbyte) {
  uint8_t reply=0;
  char bits[8] = {0, 0, 0, 0, 0, 0, 0, 0};  //reading buffer
  for(uint8_t _bit = 0;_bit < 8;_bit++) {
    if(uu_firstbyte==1 && _bit==0) {
      digitalWriteFast(_SS, HIGH);
 //     __asm__("nop");
 //     __asm__("nop");
 //     __asm__("nop");
      digitalWriteFast(_SCK, HIGH);
 //     __asm__("nop");
      bits[_bit] = digitalReadFast(_MISO);
      digitalWriteFast(_MOSI, !!(b & msk[_bit]));
      digitalWriteFast(_SS, LOW);
      digitalWriteFast(_SCK, LOW);
    }
    if((_bit>=1)||(uu_firstbyte==2)) {
      bits[_bit] = digitalReadFast(_MISO);
      digitalWriteFast(_MOSI, !!(b & msk[_bit]));
      digitalWriteFast(_SCK, HIGH);
 //     __asm__("nop");
      digitalWriteFast(_SCK, LOW);
    }
  }
  reply = PCK();
  return reply;   
}

void setup() {
  AudioCodec_init(); // setup codec registers
  SPI.end(); // disable hardware SPI
  pinMode(_SS, OUTPUT);
  pinMode(_SCK, OUTPUT);
  pinMode(_MOSI, OUTPUT);
  pinMode(_MISO, INPUT);
}

void loop() {
}

// timer1 interrupt routine - all data processed here
ISR(TIMER1_COMPA_vect) {
  left_in = spi_transfer(left_out,1);
  right_in = spi_transfer(right_out,2);
  left_out = left_in;
  right_out = right_in;
}


one major error is that its only transferring 16b per frame, rather than the required 32b (L+R), but that isnt the limiting factor right now. the speed of the spi-transfer function is just too slow. i may have loaded the wrong digitalwritefast library, but if even so, there probably isnt one that works any faster. the issue is that most are going to use a function call rather than inlining the code. they will probably also try to replicate the pwm override capability. both of these things slow it down to an unusable state. another issue is that any digitalread will be too slow. it takes at least 2 clocks of setup before an input pin is stabilized to read. multiply that by the 32b required is a minimum of 64 clock cycles of just waiting.

the fastest possible, using inline assembly and knowing ahead of time which pins you were using, and unrolling the loop would take around 6 clock cycles per bit, which is 2/3rds of the total time available at 44ksps. there wouldnt be time left over to do much else.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

PreviousNext

Return to Audio Codec Shield

Who is online

Users browsing this forum: Google [Bot] and 1 guest


cron