Micro SD Card Slot with Nucleo L476RG

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
Sgt_Pepper
Posts: 6
Joined: Fri Jun 07, 2019 10:05 pm

Micro SD Card Slot with Nucleo L476RG

Post by Sgt_Pepper » Thu Mar 05, 2020 7:30 pm

Hello,

I am trying to make a data logger with Micropython running on a Nucleo L476RG. I want to save data from two ADXL375 accelerometers and write them to a micro SD card. I originally purchased a micro SD card breakout board from Adafruit that allows a connection over SPI and I used the sdcard.py module from the Github to interface with it. After some initial testing it seems that the read/write speeds are not fast enough for the data rates that I am trying to achieve.

After doing some more reading I realized that the PyBoard automatically detects micro SD cards and (correct me if I am wrong) utilizes the faster SDIO protocol to communicate with it. I read some posts on another forum where a couple users were able to read/write to the SD card incredibly quickly on the PyBoard.

As a final effort, I connected my micro SD card slot to the Nucleo following the schematic for the PyBoard. I figured if I wired it exactly the same as the PyBoard it should work. Unfortunately after double checking connections and plugging it in I could never get the /sd directory to appear.

I was wondering if anyone might have some insight on this topic. I read in a forum post on another site that external SD cards are not supported on the Nucleo L476RG micropython firmware like they are on the PyBoard. Is this correct? And if not, how can I successfully connect the micro SD card to the Nucleo?

Thanks for the help!

User avatar
jimmo
Posts: 1381
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Micro SD Card Slot with Nucleo L476RG

Post by jimmo » Thu Mar 05, 2020 11:39 pm

You'll likely have to build custom firmware with the sdio pins configured in mpconfigport.h

See ports/stm32/boards/PYBV11/mpconfigport.h (which you can copy into boards/NUCLEO_L476RG/mpconfigport.h)

Sgt_Pepper
Posts: 6
Joined: Fri Jun 07, 2019 10:05 pm

Re: Micro SD Card Slot with Nucleo L476RG

Post by Sgt_Pepper » Tue Mar 10, 2020 7:09 pm

@jimmo Thanks for the reply. I just decided to buy a PyBoard and continue testing with that.

I'm sure that the results I found from my testing are trivial to most people but for anyone (like myself) who is relatively new to micropython and the different boards out there I figured this might be useful.

I ran some tests to compare write speeds between writing to an SD card over SPI using the sdcard.py module available at https://github.com/micropython/micropyt ... /sdcard.py and the builtin driver that works on the PyBoard.

I was originally using the Nucleo L476RG for a project with an external micro SD card slot and the sdcard.py module. I found that the speeds weren't fast enough for what I needed and then learned that the micropython build for the L476RG chips don't support reading/writing to an SD card with the native SDIO protocol that the PyBoard uses. Case in point, if I wanted faster speeds I had to switch to the PyBoard. After purchasing one I ran some tests to see just how fast writing to an SD card was on the PyBoard. The test code wrote 1000 bytes in binary format to the SD card. The timing included creating the file and closing the file. I ran the test code 100 times and averaged the results to try and get a better idea of the actual performance. Here are the results:

Board: NUCLEO L476RG
Time: 0.1412486
Frequency: 7081.431

Board: PyBoard v1.1
Time: 0.05409655
Frequency: 19156.81

As shown the PyBoard is way faster than the sdcard.py module working over SPI. So, if your application needs to go fast make sure to take advantage of the faster speeds on the PyBoard! There is a lot of good information related to this topic that I found useful at the following forum post: viewtopic.php?f=6&t=296

Here is the test code in case anyone is interested. I used it for both boards, uncommenting the commented section when I loaded it on the Nucleo:

Code: Select all

import utime
import os

# Used to connect to SD card on Nucleo over SPI. Used in conjunction with sdcard.py from https://github.com/micropython/micropython/blob/master/drivers/sdcard/sdcard.py
# SPI1_CS1 = pyb.Pin(pyb.Pin.cpu.A4) # SPI pin
# sd = SD_Driver.SDCard(pyb.SPI(1, pyb.SPI.MASTER, baudrate=5000000), SPI1_CS1)
# pyb.mount(sd, '/sd')


def speedTest():
    startTime = utime.ticks_us()

    file = open('/sd/log.bin', 'wb')

    for i in range(1000):
        file.write(bytes([1]))

    file.close()

    deltaTime = (utime.ticks_diff(utime.ticks_us(), startTime))/1000000

    time = deltaTime
    freq = 1000/deltaTime

    return [time, freq]


@ micropython.native
def speedTest_Native():
    startTime = utime.ticks_us()

    file = open('/sd/log.bin', 'wb')

    for i in range(1000):
        file.write(bytes([1]))

    file.close()

    deltaTime = (utime.ticks_diff(utime.ticks_us(), startTime))/1000000

    time = deltaTime
    freq = 1000/deltaTime

    return [time, freq]


def average():
    time_sum = 0
    freq_sum = 0
    data = []

    for i in range(100):
        data = speedTest()
        time_sum += data[0]
        freq_sum += data[1]

    time_avg = time_sum/100
    freq_avg = freq_sum/100

    print("Average time: " + str(time_avg))
    print("Average frequency: " + str(freq_avg))

def average_native():
    time_sum = 0
    freq_sum = 0
    data = []

    for i in range(100):
        data = speedTest_Native()
        time_sum += data[0]
        freq_sum += data[1]

    time_avg = time_sum/100
    freq_avg = freq_sum/100

    print("Average time: " + str(time_avg))
    print("Average frequency: " + str(freq_avg))

Post Reply