Hi all,
I have seen several threads discussing how to use Teensy+Audio board and also use SPI at the same time. And nonetheless, I cannot seem to get this to work in practice. I am hoping that I am making a stupid mistake that a generous forum user can easily correct.
I am using Teensy 3.2 with the audio shield and using MCP4922 DAC chips for analog outputs as part of a larger Teensy based system. An example sketch that replicates the issue is shown below. To make the example sketch I merged together part of an audio tutorial and some of the code I am using to test the DAC outputs (in other words, this is an example just for the purposes of showcasing the problem - it is not the code I am actually trying to use, which is part of a larger library I am writing. But it shows the same problem.).
Here is a statement of the problem in words:
1) when I run this sketch as written, SPI communication with the DACs works fine (I get a nice output ramp), but there is no audio output.
2) if I comment out (a) the SPI.begin() function, and (b) calls to the MCP4922_write() function in my loop, I (obviously) lose the DAC output but now I do have audio output and I can hear the kick sample play repeatedly with headphones connected to the audio shield.
So, clearly, I have not figured out the right way to use the SPI library to enable both of these at once. What am I missing?
I have seen several threads discussing how to use Teensy+Audio board and also use SPI at the same time. And nonetheless, I cannot seem to get this to work in practice. I am hoping that I am making a stupid mistake that a generous forum user can easily correct.
I am using Teensy 3.2 with the audio shield and using MCP4922 DAC chips for analog outputs as part of a larger Teensy based system. An example sketch that replicates the issue is shown below. To make the example sketch I merged together part of an audio tutorial and some of the code I am using to test the DAC outputs (in other words, this is an example just for the purposes of showcasing the problem - it is not the code I am actually trying to use, which is part of a larger library I am writing. But it shows the same problem.).
Here is a statement of the problem in words:
1) when I run this sketch as written, SPI communication with the DACs works fine (I get a nice output ramp), but there is no audio output.
2) if I comment out (a) the SPI.begin() function, and (b) calls to the MCP4922_write() function in my loop, I (obviously) lose the DAC output but now I do have audio output and I can hear the kick sample play repeatedly with headphones connected to the audio shield.
So, clearly, I have not figured out the right way to use the SPI library to enable both of these at once. What am I missing?
Code:
//awkward merge of a DAC hardware test and part of an
//audio library example to illustrate a
//problem when using SPI.begin() with the audio library
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include "AudioSampleKick.h"
int chip_select = 1;
AudioPlayMemory playMem1;
AudioMixer4 mixer1;
AudioOutputI2S i2s1;
AudioConnection patchCord4(playMem1, 0, mixer1, 0);
AudioConnection patchCord5(mixer1, 0, i2s1, 0);
AudioConnection patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
void setup() {
AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
mixer1.gain(0, 0.4);
pinMode(chip_select, OUTPUT);
digitalWrite(chip_select, HIGH); // HIGH);
SPI.begin();
SPI.setMOSI(7);
SPI.setSCK(14);
}
void loop() {
playMem1.play(AudioSampleKick);
for (int i = 0; i < 4096; i ++) {
MCP4922_write(chip_select, 0, i); //OUTPUT 2
MCP4922_write(chip_select, 1, i); //OUTPUT 4
delay(1);
}
}
void MCP4922_write(int cs_pin, byte dac, int value) {
byte low = value & 0xff;
byte high = (value >> 8) & 0x0f;
dac = (dac & 1) << 7;
SPI.beginTransaction(SPISettings(4000000,MSBFIRST,SPI_MODE0));
digitalWrite(cs_pin, LOW);
SPI.transfer(dac | 0x30 | high);
SPI.transfer(low);
digitalWrite(cs_pin, HIGH);
SPI.endTransaction();
}