• AVR Freaks

Hot!Speed of SD Card Read with MCC generated SPI and FATFs - How to change?

Author
PRWiley
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2019/07/04 06:16:51
  • Location: Lewisburg PA USA
  • Status: offline
2019/08/28 08:46:01 (permalink)
0

Speed of SD Card Read with MCC generated SPI and FATFs - How to change?

I am working on code for my PIC18F46K42 to read a WAV file and play it out via PWM.
 
I am using the FATFs library that now available via the MCC. I am using MPLAB X v5.25 and the XC complier v2.05. I am reading a PNY SDHC card via an adafruit Micro-SD breakout board. I set up the FATFs library and SPI via the MCC and can read data from the SD card. The WAV file iI am using is one channel, 8bit, sample rate 22050Hz and features me doing a slow count.
 
The audio plays back through the PWM pin attached to a LM386 preamp (no filtering), but it does so very slowly, about half speed. So, I have an issue with time someplace.
 
Because I am new to this, there are many possible timing issues. The one I want to eliminate first is the possibility that the data is being read from the SD card too slowly. My understanding that the initialization of the SD card has to happen at a relatively slow speed, under 400kHz, and that after initialization the read speed can be increased. 
 
Looking at the code and documentation, it is not clear to me whether I have to (or can) increase the speed of reads from the SD card after initialization or whether that happens automatically somehow in the MCC generated code. 
 
The "Easy Setup" tab for the SPIMASTER (under Libraries > Foundation Services) shows values for SDFAST and SDSLOW, 4000kHz and 400 kHz respectively, so I think the speed-up is supposed to happen automatically. Does anyone know for sure? Is the SDFAST related to the speed of the SD card read? What limits this value?
 
I have HINTOSC set at 16MHz. If I set it any higher, the SD Card is not read.
 
There are a number of MCC generated files that appear to be related: spi_master.c, sd_spi.c, and spil_driver.c but at my current level of understanding, I don’t get all the interrelationships. Happy to post if those would help.

Peter R. Wiley
I feel a lot more like I do now than I did when I came in.
#1

7 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3064
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/08/28 09:37:11 (permalink)
    +1 (1)
    Nothing happens automatically. MCC provides some utility code, but usually you have to call it.
     
    Re your clock frequency issue: I assume that you entered the 16 MHz in MCC prior generating your current files, now trying to just increase the clock.
    If you want to increase the clock, declare the higher frequency in MCC and re-generate all those fancy files.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    PRWiley
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/07/04 06:16:51
    • Location: Lewisburg PA USA
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/08/31 07:23:48 (permalink)
    0
    I have had some time to dig a bit more into all those fancy MCC files (and yes, I understand they have be regenerated). When one sets up FATFs with the MCC one gets a bunch of files in three folders the header and source directories: drivers, fatfs, and sd_spi. 
     
    Comments in sd_spi.h detail the card initiation one would expect:
    -------------------------------------------------------------------------------------------
    SD Card SPI Initialization Sequence (for physical layer v1.x or v2.0 device) is as follows:
    -------------------------------------------------------------------------------------------
    0. Power up tasks
        a. Initialize microcontroller SPI module to no more than 400kbps rate so as to support MMC devices.
        b. Add delay for SD card power up, prior to sending it any commands. It wants the
            longer of: 1ms, the Vdd ramp time (time from 2.7V to Vdd stable), and 74+ clock pulses.
    1. Send CMD0 (GO_IDLE_STATE) with CS = 0. This puts the media in SPI mode and software resets the SD/MMC card.
    2. Send CMD8 (SEND_IF_COND). This requests what voltage the card wants to run at.
        Note: Some cards will not support this command.
        a. If illegal command response is received, this implies either a v1.x physical spec device, or not an SD card (ex: MMC).
        b. If normal response is received, then it must be a v2.0 or later SD memory card.

    If v1.x device:
    -----------------
    3. Send CMD1 repeatedly, until initialization complete (indicated by R1 response uint8_t/idle bit == 0)
    4. Basic initialization is complete. May now switch to higher SPI frequencies.
    5. Send CMD9 to read the CSD structure. This will tell us the total flash size and other info which will be useful later.
    6. Parse CSD structure bits (based on v1.x structure format) and extract useful information about the media.
    7. The card is now ready to perform application data transfers.

    If v2.0+ device:
    -----------------
    3. Verify the voltage range is feasible. If not, unusable card, should notify user that the card is incompatible with this host.
    4. Send CMD58 (Read OCR).
    5. Send CMD55, then ACMD41 (SD_SEND_OP_COND, with HCS = 1).
        a. Loop CMD55/ACMD41 until R1 response uint8_t == 0x00 (indicating the card is no longer busy/no longer in idle state).
    6. Send CMD58 (Get CCS).
        a. If CCS = 1 --> SDHC card.
        b. If CCS = 0 --> Standard capacity SD card (which is v2.0+).
    7. Basic initialization is complete. May now switch to higher SPI frequencies.
    8. Send CMD9 to read the CSD structure. This will tell us the total flash size and other info which will be useful later.
    9. Parse CSD structure bits (based on v2.0 structure format) and extract useful information about the media.
    10. The card is now ready to perform application data transfers.
    --------------------------------------------------------------------------------

     
    Step 7 suggests that one ought to be able to speed up reading from the card but does not explain how that is done. Unfortunately, my knowledge of C is such that I don't understand if the answer is obvious from the code (attached).
     
    I've posted a video to youtube of the .wav playback that seems to be about 40% percent of the actual speed. I can't post the link here for fear of being denied access, but it can be reconstructed from https colon slash slash youtu.be slash r-keP2MOtnQ.
     
    The reason I think the SD Card read speed is the issue is because when I done this same trick with an Arduino the frequency of the LED flashing on the SD Card breakout board is faster.
     

    Peter R. Wiley
    I feel a lot more like I do now than I did when I came in.
    #3
    PRWiley
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/07/04 06:16:51
    • Location: Lewisburg PA USA
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/09/02 08:56:26 (permalink)
    0
    Back at this and still not making much progress understanding the MCC-generated code for the SPI and SD Card and how they are supposed to work together.
     
    This function call in a couple of places sd_spi.c makes me think the generated code does speed up the spi for the SD Card "automatically":
     
    if(SD_SPI_master_open(SDFAST) == false)
        {
            return false;
        }

     
    But I am having a hard time following the big picture due to my level of understand of C.  The value of SDFAST is set, I think, in Foundation Services > SPIMASTER tab, but changing the values there appears to have not effect on playback speed.
    post edited by PRWiley - 2019/09/02 09:03:05

    Peter R. Wiley
    I feel a lot more like I do now than I did when I came in.
    #4
    beans
    Moderator
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2016/06/16 15:32:27
    • Location: 0
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/09/04 17:04:14 (permalink)
    +2 (2)
    The SD Card driver does in fact use the SDSLOW speed only for card enumeration so as you've stated we only need to look at the SDFAST configuration in the Foundation Services -> SPIMASTER to know the max transfer speed.
     
    Probably the first and easiest thing to check is that you are getting the SPI clock the the specified max speed. For the K42 that appears to be 10MHz SPI which indicates your system clock must to 40MHz or more. You should be able to keep an eye on the actual speed of the SPI in Foundation Service -> SPIMASTER SDFAST configuration. If you set the SDFAST Speed(kHz) text box to 10000 (10MHz) and then look at the actual speed box you should see the actual SPI clock speed that can be generated based on your current system clock settings. 40MHz system clock will give a 10MHz SPI clock exactly.
    #5
    PRWiley
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/07/04 06:16:51
    • Location: Lewisburg PA USA
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/09/07 07:29:32 (permalink)
    0
    @beans. Thanks for taking a look. Your suggestion helps me understand better some of the interrelationships.
    On the chip (PIC18F46K42) I'm using the closest I can get to 40MHz is 48MHz.  There are some odd things happening with the MCC that I think are confusing matters.
     
    The first time I switched the system clock to 48MHz and then looked at the numbers in the MCC the SPI actual speed was reported as 7.75 kHz and this setting broke the card reading. The card would not mount.
     
    After restarting MPLAB I got more reasonable numbers in the SPI pane. Setting the SDFAST to 2,500 increased the the actual speed to 2500. When the code was run the card reading was speeded up,  obvious because card read LED was solid rather than blinking and because the playback now sounded like Alvin as in Alvin and the Chickmunks, that is, faster and higher pitched. A definite change in accord with the change in value. Audio playback sounded the much same until I reduced SDFAST to 400, at which the playback sounded slowed down again. Any value above 5000 for SDFAST seems to prevent the card from mounting.
     
    So I put the SDFAST up to 2,500 again and started to experiment Timer Period for TMR1. In my code, a function, loadDuty(), is called to load data to the PWM every 45us (1/22050, 22050 being the sample rate of the audio file) via TMR1. In the course of doing this I some how broke the TMR1 interrupt and now nothing plays back because loadDuty is never called. I'm trying to figure that issue out.

    Peter R. Wiley
    I feel a lot more like I do now than I did when I came in.
    #6
    PRWiley
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/07/04 06:16:51
    • Location: Lewisburg PA USA
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/09/20 09:37:00 (permalink)
    +1 (1)
    Coming back to this project again and it now works relatively well as https: slash slash youtu.be slash ztN4mbMTnLY will demonstrate. Thanks to all who offered comments.
     
    I was confused for awhile because the MCC would periodically set the values set in the Foundation Services SPIMASTER without being asked, changing both the SPI Mode and Speed settings in certain circumstances that I haven't figured out.
     
    The 8bit fidelity is, of course, not the greatest but it's almost reasonable for voice. I am thinking it should be possible to do 16bit. Working on that.
    post edited by PRWiley - 2019/09/21 09:14:44

    Peter R. Wiley
    I feel a lot more like I do now than I did when I came in.
    #7
    katela
    Super Member
    • Total Posts : 1411
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Speed of SD Card Read with MCC generated SPI and FATFs - How to change? 2019/09/20 17:37:18 (permalink)
    0
    Great!

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5