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 - Accessing 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: 26 Dec 2020 9:52 PM Date Created
  • Views 1531 views
  • Likes 4 likes
  • Comments 0 comments
  • oled
  • audio amplifier
  • dac
  • holidayspecial20ch
  • adafruit qt py
Related
Recommended

A QT Py Christmas - Accessing external Flash memory

ralphjy
ralphjy
26 Dec 2020

In the previous post (links at bottom), I had used my Xiao Expansion board to play Christmas music stored on the SD card using the SamdAudio player.

 

I don't have an SD card on my QT Py ornament, but I did add a 2MB GD25Q16 external SPI flash chip to the QT Py.  The music file that I'm using, "we-wish-you-a-merry-christmas.wav", is 1.39MB - so it should fit in the flash memory.

 

I've previously used the SPI flash chip as external file storage for CircuitPython, so the interface has been tested.  I'm programming the QT Py using the Arduino IDE and Adafruit has "fatfs" libraries for the SPI flash.  The libraries allow for erasing and formatting the memory and creating and reading/writing files from within the Arduino program.  The feature that is missing using the Arduino IDE is the ability to do file transfers between the flash memory and the host computer, so copying files to the flash memory is problematic.  The good news is that the file transfer capability is available in CircuitPython.  The process for using it with Arduino, however, is somewhat convoluted and I haven't tried it before.

 

The fatfs library setup that is used with the Arduino IDE is not compatible with CircuitPython, so a different library setup is required to be able access files that are transferred with CircuitPython.  Adafruit has provided a CircuitPython example of the fatfs library to use with the Arduino IDE.   After you include the Adafruit SPIFlash library the example is available - SdFat_circuitpython.

image

 

File transfer process and program usage

Load CircuitPython

The QT Py uses a special "haxpress" version of CircuitPython.  You can get the latest stable uf2 file at this link: https://downloads.circuitpython.org/bin/qtpy_m0_haxpress/en_US/adafruit-circuitpython-qtpy_m0_haxpress-en_US-6.0.0.uf2

The current version of the bootloader creates a drive instance that you can use to upload programs simply by dragging and dropping the uf2 file onto the drive instance (this works for any uf2, not just CircuitPython).

 

If you aren't in bootloader mode, you can enter it by doing a quick double press of the reset button on the QT Py.

Here is what the drive looks like in bootloader mode:

image

If you want to save the current firmware that is programmed you can just copy the CURRENT.UF2 file.  This allows you to reload that firmware easily after using CircuitPython (this is the internal flash, not the external one).

 

Transfer files to external flash

When CircuitPython runs it will present the external flash as a drive that you can drag and drop files to and from the host computer.

Here is what my drive looks like: (I have files there from using CircuitPython previously programming the OLED display)

image

 

     image

     So, I proceeded to drag and drop the WAV file onto the drive and the transfer started.  Not having done this with a "large" file before I wasn't sure how long it would take.  The copy window popped up immediately.

 

     image

 

     After about 5 minutes of the status not changing, I was worried; after 10 minutes really worried.  I guess using this method that this status window stays at 0% until it's done.  After about 15 minutes it finished.

 

  image

 

image

 

     Verify Arduino program can find the file

 

 

     QT_Py_SdFat_circuitpython.ino    derived from SdFat_circuitpython.ino example

 

// Adafruit QT Py CircuitPython Flash Example
// Original Author: Tony DiCola
//
// This is an example of reading and writing data from Arduino
// to the QT Py flash filesystem used by CircuitPython.
// You can create, update, and read files on the CircuitPython
// filesystem in an Arduino sketch and then later load CircuitPython
// to interact with the same files.
//
// Note before you use this sketch you must load CircuitPython
// on your QT Py.  This will create the filesystem and
// initialize it, then you can load this example and read/write
// files on the board.
//
// Usage:
// - Modify the pins and type of fatfs object in the config
//   section below if necessary (usually not necessary).
// - Upload this sketch to your QT Py board.
// - Open the serial monitor at 115200 baud.  You should see the
//   example start to run and messages printed to the monitor.
//   If you don't see anything close the serial monitor, press
//   the board reset buttton, wait a few seconds, then open the
//   serial monitor again.
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.h>

// 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

Adafruit_SPIFlash flash(&flashTransport);

// file system object from SdFat
FatFileSystem fatfs;

void setup() {
  // Initialize serial port and wait for it to open before continuing.
  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }
  Serial.println("Adafruit QT Py CircuitPython Flash Example");

  // Initialize flash library and check its chip ID.
  if (!flash.begin()) {
    Serial.println("Error, failed to initialize flash chip!");
    while(1);
  }
  Serial.print("Flash chip JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);

  // First call begin to mount the filesystem.  Check that it returns true
  // to make sure the filesystem was mounted.
  if (!fatfs.begin(&flash)) {
    Serial.println("Failed to mount filesystem!");
    Serial.println("Was CircuitPython loaded on the board first to create the filesystem?");
    while(1);
  }
  Serial.println("Mounted filesystem!");

  // Check if a music file exists:
  if (fatfs.exists("we-wish-you-a-merry-christmas.wav")) {
    Serial.println("Found we-wish-you-a-merry-christmas.wav");
  }
  else {
    Serial.println("No we-wish-you-a-merry-christmas.wav found...");
  }

  Serial.println("Finished!");
}


void loop() {
  // Nothing to do in the loop.
  delay(100);
}

 

     Program output on Serial Monitor:

image

 

 

Almost done.  I'll do a final post with a demo when everything is integrated and working.

 

 

Links to previous blogs

A QT Py Christmas

A QT Py Christmas - add Cap Sense Buttons

A QT Py Christmas - add Sound

  • Sign in to reply
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