Board index › Products › Audio Codec Shield

Have the shield working with Mega - library attached

18 posts · 62649 views

Have the shield working with Mega - library attached

by scott2 » Wed Dec 05, 2012 00:36 UTC
I finally got it using a little DAQ/digital signal analyzer

It's a bit of timing voodoo - basically came down to making the SPI calls after the I2C calls for the chip config. An updated library+examples for the Mega is attached below.

The init function now looks like:
void AudioCodecMega_init(){
  // setup i2c pins and configure codec
  // the new Wire library has trouble with 0x00, so (uint8_t) is added
  Wire.begin();
  Wire.beginTransmission(0x1a);
  Wire.write(0x0c); // power reduction register
  Wire.write((uint8_t)0x00); // turn everything on
  Wire.endTransmission();
  
...

  Wire.beginTransmission(0x1a);
  Wire.write(0x12); // codec enable
  Wire.write(0x01);
  Wire.endTransmission();
  
 ...

  // setup spi peripheral
  pinMode (53, OUTPUT);
  digitalWrite(53, LOW); // set ss pin to output low
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  SPI.setDataMode(SPI_MODE0); 

...
}
Attachments
AudioCodecMega.zip (26.5 KiB)
Codec Library updated for Mega

Re: Have the shield working with Mega - library attached

by guest » Thu Dec 06, 2012 00:19 UTC
this is awesome, thanks for digging into it. ill give the library a read over.

Re: Have the shield working with Mega - library attached

by scott2 » Thu Dec 06, 2012 00:25 UTC
np!

It's exactly the same code, except:
- just moved the location of the SPI init calls
- Updated to timer 5 being driven by the shield instead of 1 I think it was before. Updated the example code to look @ the T5 interrupt vector instead.


You may want to update the docs with the new pin mappings as discussed in other forum threads. Didn't write anything about that in there.

Re: Have the shield working with Mega - library attached

by guest » Thu Dec 06, 2012 01:12 UTC
cool, will do. i will also add in the clobber list at the end of the asm calls. its something i forgot to do on the first library i wrote, which is probably the one you ended up grabbing. basically, it tells the compiler which registers the assembly calls are using, so that they can be handled correctly in interrupts and such.

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Sat Aug 09, 2014 05:07 UTC
Thanks for making the Mega library!

I am a newbie to this platform and just trying to get it working at this stage.

Unfortunately, there's no sound at present. Can I confirm that the pin changes are these below?

UNO MEGA FUNCTION
--- ---- --------
A4  D20  I2C (SDA)
A5  D21  I2C (SCL)
D10 D53  SPI (SS)
D11 D51  SPI (MOSI)
D12 D50  SPI (MISO)
D13 D52  SPI (SCK)
D5  D47  T1 -> T5


The UNO pins A4 and A5 weren't listed in the other Mega thread - instead they were noted as A6 and A7.

Re: Have the shield working with Mega - library attached

by guest » Sat Aug 09, 2014 13:42 UTC
those are all the correct UNO pins. how are you testing it?

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Sun Aug 10, 2014 07:54 UTC
My testing procedure was to load up the mega library version of the sine generator example and listen to the output through headphones. There is currently no output.

I will try with another non-mega arduino tonight to test the codec board.

EDIT: Tested with a Diecimila board. The non-mega sine sketch works perfectly, so it's not my codec shield.

Re: Have the shield working with Mega - library attached

by guest » Sun Aug 10, 2014 14:42 UTC
so, there are 2 communication formats involved, i2c and spi. im guessing the i2c might not be working. you can test this as follows:

// 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
#define BYPASS 1 // turn on bypass

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodecMega.h>

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

void setup() {
  AudioCodecMega_init(); // setup codec registers
  // call this last if setting up other parts
}

void loop() {
  while (1); // reduces clock jitter
}

// timer1 interrupt routine - all data processed here
ISR(TIMER5_COMPA_vect, ISR_NAKED) { // dont store any registers
  reti(); // do nothing
}


this turns on the bypass, so whatever goes into the input, comes out the output. run this code, and put a signal at the input, and listen for it at the output. if you can hear the signal at the output, then the I2C is working.

also, it doesnt look like the clobber list ever got put into the OPs library. this may be causing the problem, although not likely.

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Mon Aug 11, 2014 12:31 UTC
That works!

So I2C is working?

Re: Have the shield working with Mega - library attached

by guest » Mon Aug 11, 2014 12:52 UTC
yes, I2C is working, which is good news. so lets try a simple sketch to test the SPI:

    // 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 <AudioCodecMega.h>

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

    void setup() {
      AudioCodecMega_init(); // setup codec registers
      // call this last if setting up other parts
    }

    void loop() {
      while (1); // reduces clock jitter
    }

    // timer1 interrupt routine - all data processed here
    ISR(TIMER5_COMPA_vect) { // NAKED turned off, so registers are stored
      AudioCodecMega_data(&left_in, &right_in, left_out, right_out);
      left_out = left_in;
      right_out = right_in;
      //reti(); // dont need this without NAKED
    }


this should send the input to the output, but without the bypass. if the SPI does not work, there are 2 likely causes: 1 something wrong with the code that is messing up the interrupts. 2 something is wrong with the wiring. the SPI moves at a very fast rate, so long wires could cause problems. also, double check that you have the right pins going to the right holes.

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Mon Aug 11, 2014 14:09 UTC
No sound is echoed from the input to output on this one.

I'll double check the wiring, but since I use the same jumpers on the Diecimila it's probably not that.

What could be wrong with the interrupts?

Re: Have the shield working with Mega - library attached

by guest » Mon Aug 11, 2014 14:10 UTC
i made a slight change to the code above, commenting out reti();

Re: Have the shield working with Mega - library attached

by guest » Mon Aug 11, 2014 16:18 UTC
i will see if i can borrow a mega from someone and give it a try. im pretty sure i tested it when the library first got uploaded, and it worked. but maybe something about arduino has changed since then.

what version of arduino are you using?
can you post a picture of your setup?

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Mon Aug 11, 2014 23:00 UTC
It has the same null result with reti() commented out, unfortunately.

I'm running Arduino 1.0.5 under OSX10.6 (relevant? I know the Linux version has differences)

The board is an Arduino Mega2560.

Photos of my board (the pins where each jumper cable soldered to the board have been clipped):

photo 1l.jpg
Images of mega2560 with audio codec shield

photo 2l.jpg
2

photo 3l.jpg
3


P.S. Thank you for the very very very prompt support you are giving!

Re: Have the shield working with Mega - library attached

by guest » Tue Aug 12, 2014 03:08 UTC
from the second picture, it looks like your spi wires are going to the wrong holes. they all seem to be shifted down by one. the last holes are all ground on that header, so it should start with the second to last. there should only be a one hole gap between the spi lines and the clock line on 47.

if that is all correct, and it still doesnt work, then try this experiment:

1. leave the shield plugged into the mega, with the mega powered on.
2. get a regular arduino, and connect the grounds between the mega and the regular arduino
3. run the wires to the regular arduino
4. see if it works with code loaded onto the regular arduino

Re: Have the shield working with Mega - library attached

by Brownian Lotion » Tue Aug 12, 2014 09:55 UTC
Wow... oops.

You are absolutely right, the pins were in the wrong spot. Moved them one slot to the left and now it works perfectly!

Thank you! And sorry for asking a stupid question, I should know better.

Re: Have the shield working with Mega - library attached

by guest » Tue Aug 12, 2014 13:33 UTC
glad to hear its working!

Re: Have the shield working with Mega - library attached

by youthreewire » Wed Jun 10, 2015 04:10 UTC
Could you please post a wiring diagram between the shield and the Atmega2560. Have you been able to use Serial port functions with the audio library running in parallel? Please let me know.