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

app_sdc_init() not allowed to use XL1 (P0.0) as sck pin? / Proper setup of SD Card with other SPI usage

NRF52840, S140, SDK 14.2

I am trying to write to an SD Card. But whenever I insert the card, I get a failure on ASSERT. I believe this is because I am using P0.0 as the SCK pin, and anything &-ed with 0 is 0. 

Does this mean I am not allowed to use the 0.0 SCK pin? That seems wrong. Was this fixed in a later SDK update? 

When I comment out the &&  p_config->sck_pin, I no longer get the Assert. however, I get this error instead (triggered from nrf_drv_spi_init()) 

which is NRF_ERROR_INVALID_STATE (Invalid state, operation disallowed in this state)

I suspect this is because now that I am trying to use the SD Card, there are 2 SPI resources on the system. I configured the SD Card as follows:

Whereas the other SPI device was configured "normally":

 (i do app_error_check in the fn that calls spi_config). 

This error happens and the application freezes immediately when I use SPI 0 for the second SPI device. When I use SPI 1 for the second SPI device without changing the GPIO pins, I get strange behavior: my main loop stops running entirely (seems to hit power manage and just stop? but I have interrupts coming in), and garbage data (instead of the data that is normally sent) goes out on my SPI line to the second device. I am able to still connect over BLE though. Why is this happening?

I read this and this. My other questions:

  1. What SPI instance is the SD Card using? SPI 0? 
  2. Is each SPI instance supposed to use its own separate GPIO?

If this is the case that I have to use a separate SPI instance with separate GPIO, it is a bit disappointing that NRF doesn't let you use SPI for its intended purpose (multiplexing data on a bus). I am a little low on board space and would prefer not to route 3 extra lines if possible. Please advise. What should my path forward be, to use SPI with SD card and a different device?

Thank you!

  • Thanks for this example code. 

    1. How do you know the sector size, and is it likely to be variable between different SD cards?

    2. It seemed to happen randomly, so it is hard to say. It could very well be related to that other bug, but the behavior I saw was different - sometimes it does fail to initialize, but it doesn't continue to fail if it fails on first init. But maybe this is only because of my "bandaid" bugfix? I haven't had any issues since I put in this fix. 

    1. Looks like most are 512 bytes, but you should be able to check it like this:
      DWORD sector_size = 0;
      res = disk_ioctl(0, GET_SECTOR_SIZE, &sector_size);
      if(res != RES_OK)
      {
          NRF_LOG_INFO("Error getting sector size: %d", res);
      }
      NRF_LOG_INFO("sector_size: %d", sector_size);
    2. Ok, sounds like it could be something else. We would need to be able to reproduce it in order to report and fix the bug. If you are able to reproduce it later, please let me know the steps needed in order to get into this state.
Related