element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Holiday Special 20
  • Challenges & Projects
  • Project14
  • Holiday Special 20
  • More
  • Cancel
Holiday Special 20
Blog A QT Py Christmas - Failed to play audio from external Flash memory
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Holiday Special 20 to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ralphjy
  • Date Created: 4 Jan 2021 12:15 AM Date Created
  • Views 1540 views
  • Likes 8 likes
  • Comments 4 comments
  • oled
  • dac
  • class d audio amplifier
  • holidayspecial20ch
  • adafruit qt py
Related
Recommended

A QT Py Christmas - Failed to play audio from external Flash memory

ralphjy
ralphjy
4 Jan 2021

This will be my final post for the Holiday Special 20 project.  I was able to accomplish much of what I had planned, but failed to get the Christmas music to play from the external flash memory on the QT Py.  I'll use this post to describe the problems that I had and to summarize the project.  And I'll do a demo video.

 

Here are a couple of pictures of the completed construction (front and top view) which show the addition of the audio amplifier and speaker:

image

image

 

SamdAudio library problems

I had chosen to use the SamdAudio library because it supports playing WAV audio files from both SD card and external flash memory using a SAMD21 MCU.  I demonstrated in a previous post playing WAV audio from SD card on a Xiao Expansion board and in the following post I demonstrated loading and accessing a WAV file in the external flash memory on the QT Py.  So, all that remained was to add the library and play the audio from the flash memory.

 

Unfortunately, the devil is always in the details and I could not get the playback to work.  The first problem that I encountered was that the SamdAudio library was last updated about 2 years ago and it is no longer compatible with the current Adafruit SPIFlash library and it was trying to include files that no longer exist, so the program would not compile.  I thought about trying to find the earlier version of the Adafruit library that it used, but I found that others had encountered this problem and there was a forked version of the SamdAudio library that was updated to work with the new SPIFlash library (the SD part of the old library still works).   The new library is the Audio_FeatherM0 which was used on the Adafruit FeatherM0 (also uses a SAMD21 variant).

    

With the Audio_FeatherM0 library the program compiled and it could find the WAV file in the flash memory, but the playback did not work.  The audio library is non-blocking, which it achieves by using timers and interrupts for reading the flash memory and writing to the DAC.  The good news and bad news is that the SAMD MCUs have highly configurable timer/counters.  The bad news part is that there are a lot of registers that you need to get right to make a timer/counter work correctly (clock source, pre-scaler, output multiplexer, etc.).  Since this library has worked with other SAMD21s, the most likely issue is that there is a resource conflict with the clock that is being used for the timer/counter.  I tried a few different clocks, but I couldn't get it to work.  I'm reasonably confident that the flash memory read is working, but I'll need to defer the debug until I understand the SAMD21 better.

 

Sort of working demo

I decided that I would put together a demo of how it should have worked.  I'm going to use the Xiao Expansion board to play the music, but I'll trigger an interrupt to that board from the second cap sense button on the QT Py.

Everything almost worked image....

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

QT_Py_Christmas.ino

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#include "Adafruit_FreeTouch.h"


//#ifdef __AVR__
//  #include <avr/power.h>
//#endif


#define PIN 8
#define EXT_TRIG 6
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, PIN, NEO_GRB + NEO_KHZ800);


// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.


// Add the Fretouch pins
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(A2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
//Adafruit_FreeTouch qt_3 = Adafruit_FreeTouch(A3, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);


// Capacitive touch variables
int touch = 750;    // Threshold value for on and off
long oldState = 0;
long offState = 0;
int colorPattern =0;


// Display variables
int BRIGHTNESS = 20;


// Delay variables
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0;   // will store last time the LED was updated


void setup() {
  pinMode(EXT_TRIG, OUTPUT);    // trigger output for Xiao to start playing music
  Serial.begin(115200);


  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Clear the buffer
  display.clearDisplay();

  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(32,0);
  display.println("QT Py");
  display.setCursor(6,22);
  display.println("Merry Xmas");
  display.setCursor(12,44);
  display.println("Element14");
  display.display(); // Update screen with each newly-drawn rectangle 


  // Initialize NeoPixel ring
  strip.begin();
  strip.setBrightness(20);
  strip.show(); // Update all pixels


  // Initialize Freetouch pins
  if (! qt_1.begin())  
    Serial.println("Failed to begin qt on pin A1");
  if (! qt_2.begin())  
    Serial.println("Failed to begin qt on pin A2");

}


void loop() {

  currentMillis = millis();   // capture the latest value of millis()
                              //   this is equivalent to noting the time from a clock
                              //   use the same time for functions to keep them synchronized

  checkpress();   //check to see if the button's been pressed

}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}


void rainbow(uint8_t wait) {
  uint16_t i, j;


  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;


  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();


      delay(wait);


      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}


//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}


// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


// Fill the dots one after the other with a color
void tricolorWipe(uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i+=3) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));  // red
    strip.setPixelColor(i+1, strip.Color(0, 255, 0));  // green
    strip.setPixelColor(i+2, strip.Color(0, 0, 255));  // blue
    strip.show();
    delay(wait);
  }
}

  // Check state of Freetouch pins
  void checkpress() {

// Get current button state.

    long newState =  qt_1.measure(); 
    long switchState = qt_2.measure(); 

  if (newState > touch) {  

  //select pattern to run
    switch(colorPattern){
      case 0:
              colorWipe(strip.Color(255, 0, 0), 30); // Red
              break;
      case 1:
              colorWipe(strip.Color(0, 255, 0), 50); // Green
              break;
      case 2:
              colorWipe(strip.Color(0, 0, 255), 50); // Blue
              break;
      case 3:
              tricolorWipe(50);
              break;              
      case 4:
              strip.setBrightness(0);
              strip.show(); // Update all pixels
              delay(300);
    }
  
    colorPattern++;
    if (colorPattern == 5){
      strip.setBrightness(20);
      colorPattern=0;
    }
  }


  if (switchState > touch) {
    // Clear the buffer
    display.clearDisplay();
  
    //display.drawRect(0, 0, 128, 15, SSD1306_WHITE);
    // display.drawRect(0, 17, 128, 47, SSD1306_WHITE);
    display.setTextColor(WHITE);
    display.setTextSize(2);
    //display.setCursor(48,4);
    display.setCursor(25,0);
    display.println("Playing");
    //display.setTextSize(2);
    display.setCursor(10,22);
    display.println("Christmas");
    display.setCursor(30,44);
    display.println("Music");
    display.display(); // Update screen with each newly-drawn rectangle 


    digitalWrite(EXT_TRIG, LOW);    // trigger Xiao interrupt to start playing music
  }
  
  // Set the last button state to the old state.
  oldState = newState;
  offState = switchState;


}

 

Links to previous blogs

A QT Py Christmas

A QT Py Christmas - add Cap Sense Buttons

A QT Py Christmas - add Sound

A QT Py Christmas - Accessing external Flash memory

  • Sign in to reply

Top Comments

  • neilk
    neilk over 4 years ago +1
    Ralph, a really nice build; such a shame that you were defeated by stuff that hasn't been updated correctly. I've almost been caught by that recently, but managed to find a solution - which is still "officially…
  • sobellinni
    sobellinni over 4 years ago +1
    I tried for weeks to get any of the audio libraries for playing flash to work , so I finally wrote my own app this works #include <Arduino.h> #include <SPI.h> #include "SdFat.h" #include "Adafruit_SPIFlash…
  • ralphjy
    ralphjy over 4 years ago in reply to sobellinni

    Thanks, I’ll give it a try.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sobellinni
    sobellinni over 4 years ago

    I tried for weeks to get any of the audio libraries for playing flash to work , so I finally wrote my own app

    this works

    #include <Arduino.h>

    #include <SPI.h>

    #include "SdFat.h"

    #include "Adafruit_SPIFlash.h"

    #include "Adafruit_ZeroTimer.h"

     

     

    // Uncomment to run example with custom SPI and SS e.g with FRAM breakout

    // #define CUSTOM_CS   A5

    // #define CUSTOM_SPI  SPI

     

     

    #if defined(CUSTOM_CS) && defined(CUSTOM_SPI)

      Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);

     

     

    #elif CONFIG_IDF_TARGET_ESP32S2

      // ESP32-S2 use same flash device that store code.

      // Therefore there is no need to specify the SPI and SS

      Adafruit_FlashTransport_ESP32 flashTransport;

     

     

    #else

      // On-board external flash (QSPI or SPI) macros should already

      // defined in your board variant if supported

      // - EXTERNAL_FLASH_USE_QSPI

      // - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI

      #if defined(EXTERNAL_FLASH_USE_QSPI)

        Adafruit_FlashTransport_QSPI flashTransport;

     

     

      #elif defined(EXTERNAL_FLASH_USE_SPI)

        Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, EXTERNAL_FLASH_USE_SPI);

     

     

      #else

        #error No QSPI/SPI flash are defined on your board variant.h !

      #endif

    #endif

    Adafruit_SPIFlash flash(&flashTransport);

     

     

    // file system object from SdFat

    FatFileSystem fatfs;

    File myFile;

     

     

     

     

    // This example can have just about any frequency for the callback

    // automatically calculated!

    float freq = 8000.0; // 8 KHz

    int phase = 0;

    int fnote  = 0;

    int val =0;

    int writebufcounter = 0;

    int writebufpointer = 0;

    int readbufcounter = 0;

    int readbufpointer = 0;

    byte bval=0;

    byte fval=0;

    byte soundbuffer[2][256];

    bool bufferfull[2];

     

     

     

     

    int soundbytes;

     

     

     

     

    int whichbuf =0;

     

     

     

     

    // timer tester

    Adafruit_ZeroTimer zerotimer = Adafruit_ZeroTimer(3);

     

     

    void TC3_Handler() {

      Adafruit_ZeroTimer::timerHandler(3);

    }

     

     

    // the timer callback

     

     

    void TimerCallback0(void){

    digitalWrite(9, LOW);

      //read buffer till all read and make it not full

    if (bufferfull[readbufpointer]==true){

      bval = soundbuffer[readbufpointer][readbufcounter];

      readbufcounter++;

      if (readbufcounter>255){

     

      bufferfull[readbufpointer] = false;  //enable file to write

         if (readbufpointer==0)  digitalWrite(6, LOW);

         if (readbufpointer==1)  digitalWrite(7, LOW);

         

      readbufcounter = 0;

      readbufpointer  = 1 & (readbufpointer ^1);  // point to the other buffer

      }

     

     

      }

      else {

      bval = 0x80;

      }

     

     

     

     

       val = ((int)bval<<2)&0x3ff;

       analogWrite(0, val );

     

     

       digitalWrite(9, HIGH);

       }

     

     

     

     

     

     

     

     

     

     

     

     

    void play(const char *fname) {

    bufferfull[0]=false;

    bufferfull[1]=false; 

    writebufpointer =0;

    readbufpointer =0; 

    digitalWrite(8, HIGH);

    myFile = fatfs.open(fname);

    if (myFile) {

    Serial.println(fname);

    Serial.println(myFile.size());

    Serial.println("b4readin");

    }

     

     

    for(int i =0; i<44; i++)

            myFile.read();   // eliminate header

        Serial.println("counted44");

     

        while (soundbytes = myFile.available()) {  // find out how may bytes are still available

       

        if (bufferfull[writebufpointer]==false){   //buffer available

        fval= myFile.read();

    //Serial.write(fval);

        soundbuffer[writebufpointer][writebufcounter]  = fval;  // fill sound buffer

    writebufcounter++;

    if (writebufcounter>255){

      bufferfull[writebufpointer] = true;  //stop file to write

              if (writebufpointer==0)  digitalWrite(6, HIGH);

              if (writebufpointer==1)  digitalWrite(7, HIGH);

      writebufcounter = 0;

      writebufpointer  = 1 & (writebufpointer ^1);;  // point to the other buffer

      }

    }

        }

      

    // file is done reading, see if there is still a buffer to fill  

     

    while (bufferfull[writebufpointer]=false){

     

    soundbuffer[writebufpointer][writebufcounter]  = 0x80;  // fill sound buffer  with audio 0

    writebufcounter++;

    if (writebufcounter>255){

    bufferfull[writebufpointer] = true;

         if (writebufpointer==0)  digitalWrite(6, HIGH);

         if (writebufpointer==1)  digitalWrite(7, HIGH);

    writebufcounter = 0;

    }

    //DONE

    }

    digitalWrite(8, LOW);

        }

     

     

     

     

    void setup() {

     

     

      // Open serial communications and wait for port to open:

      Serial.begin(115200);

      while (!Serial) {

      delay(1); // wait for serial port to connect. Needed for native USB port only

      }

     

        Serial.print("Initializing Filesystem on external flash...");

       

        // Init external flash

        flash.begin();

     

        // Open file system on the flash

        if ( !fatfs.begin(&flash) ) {

          Serial.println("Error: filesystem is not existed. Please try SdFat_format example to make one.");

          while(1) yield();

        }

     

      Serial.println("initialization done.");

      pinMode(6, OUTPUT);

      pinMode(7, OUTPUT);

      pinMode(8, OUTPUT);

      pinMode(9, OUTPUT);

     

     

      tc_clock_prescaler prescaler = TC_CLOCK_PRESCALER_DIV1;

     

     

      zerotimer.enable(false);

      zerotimer.configure(prescaler,       // prescaler =1

              TC_COUNTER_SIZE_16BIT,       // bit width of timer/counter

              TC_WAVE_GENERATION_MATCH_PWM // frequency or PWM mode

              );

     

     

      zerotimer.setCompare(0, 1000);   // 48000000/8000 = 6000

      zerotimer.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TimerCallback0);

      zerotimer.enable(true);

    }

     

     

     

     

     

     

     

     

     

     

    void loop() {

     

     

    play("0.wav");

    delay(1000);

    play("1.wav");

    delay(1000);

    play("2.wav");

    delay(1000);

    play("3.wav");

    delay(1000);

    play("4.wav");

    delay(1000);

    play("5.wav");

    delay(1000);

     

     

    }










    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 4 years ago

    Ralph, a really nice build; such a shame that you were defeated by stuff that hasn't been updated correctly.

     

    I've almost been caught by that recently, but managed to find a solution - which is still "officially" undocumented!

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • hugohu
    hugohu over 4 years ago

    "Failed"

     

    Ah that sucks.

     

    But, nice project image

     

    had fun reading your blogs.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube