This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SD card writing speed issue

Hi,


I am new to nrf9160 and zephyr. I was using the SD card and a breakout board on SPI bus to write the raw data coming from the PDM interface on nrf9160 Feather. Before that I want to do a SD write test to see how the speed is, so I wrote a simple test as attached.
The problem I am facing is:

  • I am able to mount, write and read the file on the SD card, but only on a limit speed of ~ 29KBytes/s, which according to this thread , is using a SPI speed of 4MHz. On my overlay file, I set spi-max-frequency = <8000000> . So I expect it should be at least faster than 29KBytes/s on SD write but not. Then I try changing the spi-max-frequency as different value, like 24MHz, 4MHz, even 800KHz. None of them change the speed and still keep the SD write speed as 29KBytes/s. I wonder if I have done in the wrong way of setting the SPI frequency?
  • Second thing I’ve found from the ‘disk_access_spi_sdhc.c’ under ‘zephyr/subsys/disk/’ path that it #define SDHC_SPI_SPEED 4000000, it kind of make sense that why it is running on 4MHz SPI speed, but again, it should be able to change the SPI frequency in some way, which back to my first question if I have done in the wrong way of setting the SPI frequency?
  • Third thing I want to ask, as I checked the data sheet of nrf9160, it doesn’t said anything on SPI frequency but from the register, it only list from 125kbps to 8Mbps. I wonder if it is correct to say SPI only run at a maximum speed of 8MHz on nrf9160?

Can anyone help with the issue please, thanks in advanced!!

Zirun

Attachment:
my .overlay:

my prj.conf:

My code:

Hers is the result from the terminal:


In this way, speed is 32256 Bytes / (11.652130 - 10.573150) sec ~= 29.19KB/s

Parents
  • Hi,

     

    The driver sets the frequency configuration to 4MHz:

    https://github.com/nrfconnect/sdk-zephyr/blob/v2.4.0-ncs2/drivers/spi/spi_nrfx_spim.c#L431

     

    The DMA length is 12 bit, meaning that you have 2047 bytes max per transaction: https://infocenter.nordicsemi.com/topic/ps_nrf9160/spim.html?cp=2_0_0_5_12_5_27#register.RXD.MAXCNT

    The configuration "CONFIG_SPI_NRFX_RAM_BUFFER_SIZE=8" gives a chunk size of 8 bytes, as the DMA can only transfer from a RAM buffer. You can try to change this to a higher value, lets say 64, and see if this affects the transfer speed. That being said; you're using a filesystem on your SDHC card, which requires handling as well. Changing the SPI speed can reduce the physical delay between the nRF and SD-card, but you still have to conform to the fatfs filesystem.

     

    Could you also try to store a larger file to see if this speed is the same then as well?

     

    Kind regards,

    Håkon

  • Hi,

    Thanks for your reply.

    The driver sets the frequency configuration to 4MHz:

    - So can I set it a different value by setting spi-max-frequency in the overlay file? 

     Or maybe, in another approach, setting 

     

    You can try to change this to a higher value, lets say 64, and see if this affects the transfer speed & Could you also try to store a larger file to see if this speed is the same then as well?

    - I try in both approach, the result is still the same, even worse when I store a larger file with a speed ~ 24KB/sec.

       Also I probe the CLK line and find that it is at 250KHz.

    Zirun

  • Hi,

     

    The speeds you're getting (just below 30kB/s) seems to correspond with the numbers provided in this zephyr PR:

    https://github.com/zephyrproject-rtos/zephyr/pull/26319

     

    It looks like you are writing at 4MHz SPI speed, given the speed results you're seeing. As you can see from the comparison with 250k/4M, its not a linear speed difference, which indicates that it takes quite some processing to conform to the specific filesystem.

     

    Kind regards,

    Håkon

  • Hi,

    I understand what you mentioned in the answer. However, I just wonder why changing the SPI speed doesn't work, at least different frequency(or speed) will give you a different frequency in the scope, not matter what the value is. Or maybe I defined it in the wrong way. 

    From my observation, the only way that the speed changed is changing the SDHC_SPI_INITIAL_SPEED to a different value, which is a hard coding define value. Additionally, this is the initial value, the speed should change to another value (SDHC_SPI_SPEED) after initialization here, but it doesn't.  

    So my question is:

    - What is the correct way to change/define the SPI frequency in my case?

    - Why the hard coding value SDHC_SPI_SPEED doesn't affect the actual read/write speed?

    My apology if my expression makes you confused.

    Thanks,

    Zirun

  • Hi,

    Z.Hong said:
    From my observation, the only way that the speed changed is changing the SDHC_SPI_INITIAL_SPEED to a different value, which is a hard coding define value. Additionally, this is the initial value, the speed should change to another value (SDHC_SPI_SPEED) after initialization here, but it doesn't.  
    Z.Hong said:
    - What is the correct way to change/define the SPI frequency in my case?
    Z.Hong said:
    - Why the hard coding value SDHC_SPI_SPEED doesn't affect the actual read/write speed?

    As the frequency in the SDHC library is hard-coded, a change to the zephyr project is needed to allow other frequencies at initial speed and "normal speed".

    By looking at the driver port, spi_nrfx_spim.c, it looks like a weakness there (or a intentional implementation), where you can only configure the device once:

    https://github.com/nrfconnect/sdk-zephyr/blob/master/drivers/spi/spi_nrfx_spim.c#L112-L114

    I would recommend that you either remove this check, or set the SDHC_SPI_INITIAL_SPEED equal to SDHC_SPI_SPEED, ie. up to 8 MHz.

     

    Kind regards,

    Håkon

      

  • Thank you so much for the help!

  • I hit this obstacle recently too. SD Card reads capped at about 27kB/s. My my, what a mission to get to the bottom of it. After a long battle, the issue is now resolved for me. I ended up using a few fixes that have been applied to the Zephyr upstream project, and back-porting them to the now very out of date nRF Connect SDK downstream project. I've summarised the changes in these three diffs:

    sdhc-speed-fix.patch

    spi-devicetree-fix-part1.patch

    spi-devicetree-fix-part2.patch

    My next issue is that SD Card reading disrupts I2S comms... and so the saga continues.

Reply
  • I hit this obstacle recently too. SD Card reads capped at about 27kB/s. My my, what a mission to get to the bottom of it. After a long battle, the issue is now resolved for me. I ended up using a few fixes that have been applied to the Zephyr upstream project, and back-porting them to the now very out of date nRF Connect SDK downstream project. I've summarised the changes in these three diffs:

    sdhc-speed-fix.patch

    spi-devicetree-fix-part1.patch

    spi-devicetree-fix-part2.patch

    My next issue is that SD Card reading disrupts I2S comms... and so the saga continues.

Children
No Data