Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Audio Library modifies pins?

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    6

    Audio Library modifies pins?

    Hi!

    I was reading the SPI library information from the Teensy documentation and it seems that the audio library changes the SPI pins.

    You should be aware that libraries sometimes have to move SPI pins. (The Audio Library is an example). If you add an SPI device yto your project and it does not work, check whether the library has moved the pins and if so, use the same pins the library does.
    Is there anywhere I can find which audio objects change these pins, and a more detailed information why this happens?

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,717
    The reason is the I2S (used for audio input and output) pins conflict with the default SPI pins. Unfortunately, Paul doesn't list the I2S pins in the pinout cards.

    I put together this little spreadsheet to try and identify all of the pin uses in the Teensy 3.0/3.1/3.2/3.5/3.6/LC:


    The main I2S pins are:
    • Pin 9 BCLK (this is one of the hardware CS pins)
    • Pin 11 MCLK (this is the default SPI MOSI0 pin)
    • Pin 13 RXD0 (this is the default SPI SCLK pin)
    • Pin 15 TXD1 (this is one of the hardware CS pins)
    • Pin 22 RXD0 (this is an alternate hardware CS pin)
    • Pin 23 LRCLK (this is an alternate hardware CS pin)
    • Pin 30 RXD1


    On the Teensy 3.5/3.6 there are alternate pins for the I2S pins, but not as many alternates on the Teensy 3.2. So, instead, the alternate SPI pins needed to be used.

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks, Michael, that's extremely helpful!

    I'm currently using a Teensy 3.6, an audio board and a nRF24L01+, and it seems that all three things together are not working quite right. I wired the RF24 to Teensy as follows:

    CE -pin 28
    CSN -pin 39
    MOSI - pin 11
    MISO - pin 12
    SCK - pin 13

    So, I have these questions:

    1. Is it possible to configure it that way? I'm not so sure my CE and CSN pins are correct, but the pinout cards marked those as SPI0.

    2. I was checking your spreadsheet and pin 11 (and pin 39) is also MCLK and pin 13 (and 28) is RX0. So, which pins can I actually use to connect my RF24? (The audio board is soldered to the Teensy)

    3. Is there a way to wire the 3 things together without using SPI1?

    4. If I were to use SPI1, are there any software modifications to be made in some libraries?

    Thanks!

  4. #4
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,717

    Cool

    Use the alternate SPI pins to mirror what the audio board uses:

    • Pin 7 for MOSI (instead of pin 11)
    • Pin 14 for SCK (instead of pin 13)


    As it says in the SPI page (https://www.pjrc.com/teensy/td_libs_SPI.html):

    Sometimes, the SPI pins are already in use for other tasks when an SPI device is added to a project. If that task is simply a digital pin, or an analog input, it is usually better to move that to another pin so that the hardware SPI can be used. Sometimes though, the conflicting pin cannot be moved. The Audio Adapter, for example, uses some of the SPI pins to talk to the Audio DAC over I2S. For this case, Teensy 3.x provides an alternate set of SPI pins.

    The main SPI pins are enabled by default. SPI pins can be moved to their alternate position with SPI.setMOSI(pin), SPI.setMISO(pin), and SPI.setSCK(pin). You can move all of them, or just the ones that conflict, as you prefer. The pin must be the actual alternate pin supported by the hardware, see the table above; you can't just assign any random pin.

    You should be aware that libraries sometimes have to move SPI pins. (The Audio Library is an example). If you add an SPI device yto your project and it does not work, check whether the library has moved the pins and if so, use the same pins the library does.

  5. #5
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    I tried setting up those pins in the most basic Hello World, but it is not working neither this:

    Code:
    SPI.setMOSI(7);
    SPI.setSCK(14);
    pinMode(13,OUTPUT);
    SPI.begin();
    nor this:

    Code:
    SPI.begin();
    SPI.setMOSI(7);
    SPI.setSCK(14);
    pinMode(13,OUTPUT);
    I read in another thread that pin 13 must be redefined then:

    // First reassign pin 13 to Alt1 so that it is not SCK but the LED still works
    CORE_PIN13_CONFIG = PORT_PCR_MUX(1);
    // and then reassign pin 14 to SCK
    CORE_PIN14_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);

    But that is neither working. Any ideas?

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,596
    The audio library comes with many examples. In Arduino, click File > Examples > Audio to access them. WavFilePlayer is a good place to start for an example which uses SPI to access a SD card. You might also check out the 31 page tutorial. It's examples are under File > Examples > Audio > Tutorial.

    https://www.pjrc.com/store/audio_tutorial_kit.html

    Aside from following the known-good examples, I'll quickly comment on 2 things in the code fragments shown.

    1: DO NOT use pinMode() if you want the pin to be usable by SPI. On AVR this can be done, but on Teensy 3.x (and most other modern chips) the pins have a mux for which function control the pin. GPIO is one of the mux options, not something done in parallel as on AVR and other old chips. If you use pinMode() after SPI is using the pin, the mux gets reconfigured to assign the pin to the GPIO peripheral.

    2: Generally you would use SPI.setMOSI() and SPI.setSCK() before any begin() function. Especially with SD.begin() or Ethernet.begin() or others for actual peripherals, you need to assign the pins before trying to initialize the external hardware.

  7. #7
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks, Paul! Seems that they're now sending data I initialized SPI before initializing the radio and the audio board.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •