PDA

View Full Version : Teensy 3.2 Master and Teensy 3.2 Slave with Audio Adapter - Slave locks up in Setup()



CodeMonster
03-17-2016, 09:31 PM
I'm using a Teensy 3.2 w/ Audio Adapter as a slave to another Teensy 3.2. (The Teensy w/ audio board works on its own perfectly.) This is part of a much larger project but I've stripped the rest of it down to just the following.

6727

I want to press the button on the Master and Wire.write(1) and have the Slave receive it and start playing a file. I tested the send/receive code and it works. When I add the audio initialization in the Setup() function, the Slave locks up. I narrowed it down to the "sgtl5000_1.enable();" line.

Is there a problem with using 2 Teeny's like this with an audio board? I'm using arbitrary addresses of 5 and 8. I couldn't find anywhere where it matters what the addresses are. I tried other addresses with the same results.

Any help would be appreciated.

// ***************************MASTER CODE
#include <Wire.h>

// I2C addresses
const int masterAddress = 5;
const int slaveAddress = 8;

// Fire
const int fireButton = 11;
int fireState = LOW;

int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
Wire.begin (masterAddress);

pinMode(fireButton, INPUT);

if (DEBUG) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}
}

// Main program
void loop()
{
fireState = digitalRead(fireButton);

if (fireState == HIGH) {
Fire();
}
delay(500);

if (DEBUG) Serial.println(fireState);
}

void Fire() {
if (fireState == HIGH)
{
// Request Fire from Slave
Wire.beginTransmission (slaveAddress);
Wire.write (1);
Wire.endTransmission ();
delay(100);
}
}



// *****************SLAVE CODE
#include <Wire.h>
#include <Audio.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;

const int masterAddress = 5;
const int slaveAddress = 8;

void setup() {
// put your setup code here, to run once:
Wire.begin (slaveAddress);
Wire.onReceive (receiveEvent);

Serial.begin(9600);

AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.0);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
}

void loop() {

}

void receiveEvent (int intCount)
{
Serial.println("Received request");

sgtl5000_1.volume(.3);

if (playSdWav1.isPlaying() == false) {
Serial.println("Start playing");
playSdWav1.play("SDTEST2.WAV");
delay(10); // wait for library to parse WAV info
}

} // end of receiveEvent

Frank B
03-18-2016, 12:07 AM
The Teensy with the Audioboard is already a master - it talks with the SGTL5000 over I2C.

PaulStoffregen
03-18-2016, 12:35 AM
There's at least 1 problem. When you call sgtl5000_1.enable(); the Wire library is restarted in master only mode. Here's the enable() code:

https://github.com/PaulStoffregen/Audio/blob/master/control_sgtl5000.cpp#L511

CodeMonster
03-18-2016, 12:36 AM
I know. I'm using 2 Teensys. One w/ an audio board and one without. I want to use I2C to between the two Teensys.

CodeMonster
03-18-2016, 12:38 AM
Paul, I tried swapping the master/slave relationship and it behaves the same. So how do I do this?

PaulStoffregen
03-18-2016, 12:49 AM
Paul, I tried swapping the master/slave relationship and it behaves the same. So how do I do this?

Well, on the Teensy with the buttons, you'd use Wire.begin(address), and Wire.onRequest(). See File > Examples > Wire > slave_sender. Presumably you'll put code in the requestEvent() to read your pushbuttons and send the info.

On the Teensy with the audio board, you'd use code similar to File > Examples > Wire > master_reader to fetch the data from the Teensy with the buttons.

Or you could connect Serial1 TX1 to RX1 and vise-versa (if you need 2 way communication) and use Serial1.write() to send and Serial1.available() and Serial1.read() to receive. Many people end up using Serial1 because it's simpler and easier to understand and troubleshoot.

CodeMonster
03-18-2016, 01:12 AM
So you're saying make the button teensy the slave and the audio teensy the master and request rather than send?

I'll try that. It doesn't seem like it's going to get past the initialization code though. When the audio teensy is the master it still fails right there. I'll play with it more though. Thanks.

PaulStoffregen
03-18-2016, 01:23 AM
When the audio teensy is the master it still fails right there.

As always, you have to post code which reproduces the problem. Your original message was well done.

CodeMonster
03-20-2016, 05:45 AM
It's working perfectly now. Thanks for mentioning the sample code. It allowed me to start from scratch and build the rest of the code from there. I reorganized my project so the the audio Teensy is the master with all the inputs and the slave Teensy has all the outputs. That way I only have to send switch, button, and pot values to the slave and not have to worry about requests.

I've used forums all over the Net and this is by far the best. Thanks for sharing your time with newbies and wanna-be makers! ;)