Most Esp32 modules have SPIFFS. This is is usually achieved with and EEPROM that is external to the ESP32 chip. However, some of the smaller packages like the 32 pin C3 or C6 have the flash built into the chip. That is why when you look at the chip there is no shield over it, or at least that is my understanding of it.
The problem with the ESP32-C3 supermini was that the SPI memory had dedicated GPIO for it that were actually broken out on the board. The ESP32-C6 has more pins, so even though the SuperMini has 20 pins broken out to the headers the internal SPI pins are not exposed and overlapping.
I tried to go straight for the Micropython sound creation using I2C. I was able to do this with the ESP32-C3 and a previous version of this board. In fact, I was able to get ChatGPT to write code to play Happy Birthday. However, both ChatGPT and Grok failed me on this one. Looks like the ESP32-C6 driver for Micropython is not updated to run I2S. Grok offer to take me through recompiling the driver to include it but I will try some other options first.
Lets first confirm that the LittleFS (Little file system) is working. The ESP32-C6 uses the ESP32-C6H4 chip. It is the 32 pin version that has the memory on board, instead of in a separate flash chip. I asked Grok to write some code to provide that it is working and this is what I go.
import machine
import os
import time
import uos
# Define pins for the onboard switch and red LED
button_pin = machine.Pin(8, machine.Pin.IN, machine.Pin.PULL_UP) # Onboard switch on GPIO8, with pull-up
led_pin = machine.Pin(7, machine.Pin.OUT) # Red LED on GPIO7
# Function to initialize and mount LittleFS
def init_littlefs():
try:
# Check if LittleFS is already formatted
vfs = uos.VfsLfs2
block_size = 4096 # Typical block size for ESP32-C6 flash
block_count = 32 # Number of blocks (adjust based on flash size, e.g., 128KB)
# Format and mount LittleFS
print("Formatting LittleFS...")
uos.VfsLfs2.mkfs(block_size, block_count)
uos.mount(uos.VfsLfs2(block_size, block_count), "/")
print("LittleFS mounted successfully")
except Exception as e:
print("Error initializing LittleFS:", e)
try:
# If already formatted, just mount
uos.mount(uos.VfsLfs2(block_size, block_count), "/")
print("LittleFS mounted successfully (already formatted)")
except Exception as e:
print("Failed to mount LittleFS:", e)
return False
return True
# Function to write data to a file
def write_file(filename, data):
try:
with open(filename, "w") as f:
f.write(data)
print(f"Wrote data to {filename}")
except Exception as e:
print(f"Error writing to {filename}:", e)
# Function to read data from a file
def read_file(filename):
try:
with open(filename, "r") as f:
data = f.read()
print(f"Read from {filename}: {data}")
return data
except Exception as e:
print(f"Error reading from {filename}:", e)
return None
# Main demo function
def littlefs_demo():
# Initialize LittleFS
if not init_littlefs():
print("Cannot proceed with demo due to LittleFS failure.")
return
# Sample data to write
sample_data = "Hello, ESP32-C6 LittleFS Demo!\n"
filename = "/demo.txt"
# Write to file
write_file(filename, sample_data)
# Read from file
read_file(filename)
# List directory contents
print("Directory contents:", os.listdir("/"))
# Main loop to trigger the demo using the onboard switch
print("Press the onboard switch (GPIO8) to run the LittleFS demo...")
while True:
if button_pin.value() == 0: # Button pressed (low when using pull-up)
print("Button pressed - Running LittleFS demo")
led_pin.value(1) # Turn red LED ON (HIGH)
littlefs_demo()
led_pin.value(0) # Turn red LED OFF (LOW)
time.sleep(1) # Debounce delay to avoid multiple triggers
time.sleep(0.01) # Small delay to prevent excessive CPU usage
We got an error this time.
MPY: soft reboot
Press the onboard switch (GPIO8) to run the LittleFS demo...
Button pressed - Running LittleFS demo
Formatting LittleFS...
Error initializing LittleFS: extra positional arguments given
Failed to mount LittleFS: extra positional arguments given
Cannot proceed with demo due to LittleFS failure.
Well dropped that into Grok and told it to fix it.
Here was the output.
import machine
import os
import uos
import time
# Define pins for the onboard switch and red LED
button_pin = machine.Pin(8, machine.Pin.IN, machine.Pin.PULL_UP) # Onboard switch on GPIO8, with pull-up
led_pin = machine.Pin(7, machine.Pin.OUT) # Red LED on GPIO7
# Function to initialize and mount LittleFS
def init_littlefs():
try:
# Check if LittleFS is already mounted
try:
uos.listdir('/')
print("LittleFS is already mounted")
return True
except OSError:
print("LittleFS not mounted, attempting to mount...")
# Mount the default LittleFS partition (ESP32-C6 uses a pre-defined partition)
try:
uos.mount(uos.VfsLfs2(), "/")
print("LittleFS mounted successfully")
return True
except OSError as e:
print("Mount failed, attempting to format and mount...")
# If no partition exists, format a new LittleFS (use with caution, erases data)
# Note: On ESP32, this requires a pre-defined partition in the firmware
# For now, we'll assume the partition is set up during firmware build
uos.VfsLfs2.mkfs(uos.VfsLfs2())
uos.mount(uos.VfsLfs2(), "/")
print("LittleFS formatted and mounted successfully")
return True
except Exception as e:
print("Error initializing LittleFS:", e)
return False
# Function to write data to a file
def write_file(filename, data):
try:
with open(filename, "w") as f:
f.write(data)
print(f"Wrote data to {filename}")
except Exception as e:
print(f"Error writing to {filename}:", e)
# Function to read data from a file
def read_file(filename):
try:
with open(filename, "r") as f:
data = f.read()
print(f"Read from {filename}: {data}")
return data
except Exception as e:
print(f"Error reading from {filename}:", e)
return None
# Main demo function
def littlefs_demo():
# Initialize LittleFS
if not init_littlefs():
print("Cannot proceed with demo due to LittleFS failure.")
return
# Sample data to write
sample_data = "Hello, ESP32-C6 LittleFS Demo!\n"
filename = "/demo.txt"
# Write to file
write_file(filename, sample_data)
# Read from file
read_file(filename)
# List directory contents
print("Directory contents:", os.listdir("/"))
# Main loop to trigger the demo using the onboard switch
print("Press the onboard switch (GPIO8) to run the LittleFS demo...")
while True:
if button_pin.value() == 0: # Button pressed (low when using pull-up)
print("Button pressed - Running LittleFS demo")
led_pin.value(1) # Turn red LED ON (HIGH)
littlefs_demo()
led_pin.value(0) # Turn red LED OFF (LOW)
time.sleep(1) # Debounce delay to avoid multiple triggers
time.sleep(0.01) # Small delay to prevent excessive CPU usage
And yes, success!
Here is the output from the terminal.
Press the onboard switch (GPIO8) to run the LittleFS demo...
Button pressed - Running LittleFS demo
LittleFS is already mounted
Wrote data to /demo.txt
Read from /demo.txt: Hello, ESP32-C6 LittleFS Demo!
Directory contents: ['boot.py', 'demo.txt', 'main.py']
Next step, playing some music.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.