zirunhong I wonder if I have done in the wrong way of setting the SPI frequency?

The frequency is a function of both the nRF9160 and the SD card you’re using. What’s the rated max speed of the card?

Seems like you’re setting the frequency correctly in your overlay

zirunhong I wonder if it is correct to say SPI only run at a maximum speed of 8MHz on nrf9160?

From a brief glance, the test code you have looks like it should work.

Have you read back the data to see what was the result? I’d be curious if the data you’re writing is corrupt or not.

Have you also, for sanity, probed the GPIO pins you’re using for SPI? (Also which pins are you using?) It’s always good to double check hardware fundamentals even if the problem doesn’t seem to manifest that way. It also doesn’t hurt to have a probe on the SPI CLK line to see if you can, indeed, modify the speed.

zirunhong I wonder if it is correct to say SPI only run at a maximum speed of 8MHz on nrf9160?

I’m running the onboard flash (supposedly) at 40MHz. I did see the 8bps though. I’ve yet to probe these signals as they run on inner layers. Now I’m curious to see if it truly is 40MHz! I know that 8bps doesn’t translate to 8MHz as there’s overhead, delays, finite buffers for transferring, etc.

One thing that you may want to turn on if you’re using logs as measurements is the CONFIG_LOG_IMMEDIATE option. Otherwise the log thread will display the messages when the system is not busy processing higher priority tasks.

Similarly, back to the probing part, you could probe the CLK line to check for the start and end of transfer and make sure you’re hardware measurements agree with the software output.

Hope that helps!

    jaredwolff Hi thanks for reply so fast!

    What’s the rated max speed of the card?

    Have you read back the data to see what was the result?

    • I haven’t look back on this test data for accuracy, but it is show the exact same size as written(32256 Bytes). Additionally, I was trying to writing some PDM data into a wav file on sd card and it playback with clear recording voice so I assume the data is correct.

    Have you also, for sanity, probed the GPIO pins you’re using for SPI? (Also which pins are you using?)

    • CLK-P0.01(D5); MOSI-P0.30(D3); MISO-P0.0(D4); CS-P0.29(D2).
    • I try probe the CLK line on my scope and wired thing happens. The file system is not working (it can’t mount the SD card, read or write), when I remove the probe, it works as normal. When probing other line, it works perfect as well. I thought it might because of my scope goes wrong but I try with another scope and still the same. Still debugging…

    if you’re using logs as measurements is the CONFIG_LOG_IMMEDIATE option.

    • Thanks for the suggestion. I will add it on configuration

      zirunhong I try probe the CLK line on my scope and wired thing happens. The file system is not working (it can’t mount the SD card, read or write), when I remove the probe, it works as normal. When probing other line, it works perfect as well. I thought it might because of my scope goes wrong but I try with another scope and still the same. Still debugging…

      There’s also a mention of changing the drive strength of the GPIO when using higher speeds. I’m assuming it’s done in Nordic’s driver but unsure.

      Also make sure that you’re using high impedance mode on your probes. It should still drive the signal no matter what but I guess it depends on the probe you’re using.

        zirunhong I’ve also noticed that the SD breakout you’re using has “3V Only” Witten all over it. The nRF9160 Feather is a 3.3V board so that could also be related to your speed issues. You can look at the data sheet for your card to confirm it’s operating range. Though, in this case, I’m not sure this is the main issue.

          jaredwolff Thanks for your reply.

          There’s also a mention of changing the drive strength of the GPIO when using higher speeds. I’m assuming it’s done in Nordic’s driver but unsure.

          • I see, so can I change the drive strength manually? If so, not sure if it is the correct, but I added the following after initial the file system:
                    spim = device_get_binding(GPIO0);
            	if (spim == NULL) {
            		LOG_ERR("Error: device_get_binding()");
            		return 0;
            	}
            	res = gpio_pin_configure(spim, SCK_PIN, GPIO_OUTPUT | GPIO_DS_ALT_HIGH | GPIO_DS_ALT_LOW);
            	if (res < 0) {
            		LOG_ERR("set pin mode error\n");
            		return 0;
            	}

          Also make sure that you’re using high impedance mode on your probes. It should still drive the signal no matter what but I guess it depends on the probe you’re using.

          • The input impedance of the channel is marked as 1MOhms/15pF, so I think that should be in high impedance mode

          jaredwolff

          I’ve also noticed that the SD breakout you’re using has “3V Only” Witten all over it. The nRF9160 Feather is a 3.3V board so that could also be related to your speed issues. You can look at the data sheet for your card to confirm it’s operating range. Though, in this case, I’m not sure this is the main issue.

          • I checked the data sheet and it says: MicroSD cards must use 3.3V, so take care to only hook 3.3V to this pin. Hooking 5V or VBAT to this pin will damage your microSD card.

          @jaredwolff Zirun and I are working on this together. It would be amazing if you could confirm if we can get max SPI speed on this board or not. We need to be able to stream a number of seconds of audio data to the SD card or, possibly an external RAM chip but that would also be on the SPI bus. I spent a little bit of time troubleshooting this also but nothing stood out as a root cause. Perhaps it’s a limitation of the nRF9160?

          Thanks,
          Steven

            imoon Got it. I’ll reach out on Devzone to see if I get an answer from Nordic.

            jaredwolff
            Hey, I got the probe working and the frequency show on the scope is 250kHz not matter what frequency I was set

              zirunhong I saw your comment in the Nordic thread. Best to keep it going with them. I won’t be able to help you much more here.

                13 days later

                Hi,

                I finally figure out how to change the speed, not 100% sure if it is the best way to do so. I am still waiting for someone from Nodic DevZone to confirm on this thread.

                So what I have done is changing the SDHC_SPI_INITIAL_SPEED under zephyr\subsys\disk\disk_access_spi_sdhc.c to any frequency you want, in my case 4000000 (4MHz).

                Theoretically, I should change SDHC_SPI_SPEED instead, since it is the SPI speed after initialization, but somehow it is not working according to my observation. I guess that is because the SPI hardware register is not updated with the new speed according to the code, even though it is assigned to the data struct, so it keep using the initial speed all the time.

                Here is the result with the same code I posted:

                From the log:
                [00:00:00.480,438] <inf> main: Opening file path
                [00:00:00.569,824] <inf> main: Done opening file path
                [00:00:00.688,964] <inf> main: Done writing

                Speed is 32256 Bytes / (0.688964 - 0.569824) sec ~= 270 KB/s, almost ten times faster!

                From the scope:
                scope

                Hope that helps someone facing the similar issue!! Also thanks for your help @jaredwolff !

                Zirun

                  zirunhong Thanks for sharing. Glad you’re making some progress. Sorry it’s not exactly what you wanted but it’s nice to see that you’re getting some improvement!

                  Terms and Conditions | Privacy Policy