Sharing SPI interface between SD card and other peripheral

Dear DevZone,

I am trying to share a SPI instance between an SD card and an other peripheral. I am working with nRF52840. 

I already opened a ticket back in time in devzone.nordicsemi.com/.../sd-card-disk_initialization-fails-when-the-fatfs-example-is-integrated-in-my-fw-project for initial configuration of the fatfs example.

Now, the fatfs example is correctly initializing the board if no other SPI periherals are initialized.

However, if I try to initialize my other peripheral before the fatfs example, the other peripheral correctly initializes, but the fatfs example goes in BREAKPOINT condition when trying to initialize the disk (disk_initialization) during spi setup.  Here the SPI initialization from the other peripheral. I deassert the CS pin after the other peripheral initialization

// spi_master_init: function that initializes the SPI master, its pins and the order in which it sends the bits on the bus
uint32_t spi_master_init(void)
{
	uint32_t err_code;
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = NRF_DRV_SPI_PIN_NOT_USED;                             // unconnected, we are going to use a custom function to switch between SSx
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
	spi_config.bit_order = SPI_CONFIG_ORDER_MsbFirst;
	
        err_code = nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL);
        APP_ERROR_CHECK(err_code);
}

The same (but specular) happens if I try to run before the fatfs example and then initialize the other peripheral: the fatfs correctly initializes the SD card, but then the SPI initialization fails with a BREAKPOINT condition when it tryies to do a transfer. Here the two sequences of operations:

int main(void)
{
    status = INIT;
    ret_code_t err_code;
    uint32_t time_ticks;

    init_message_data();

    bsp_board_init(BSP_INIT_LEDS);


    gpio_init();


//    // initialization of SPI protocol
    err_code = spi_master_init();

//******************AD7147 initialization*********************************

    // this call initializes the converters
    CDCInit();                    

//*******************************************************************************************





    // some delay needed to allow the previous operations to be successfull
    nrf_delay_ms(1000);
//******************SD card initialization*********************************
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    // this call initializes the SD card
    fatfs_example();                  

//*******************************************************************************************

Here the breakpoint condition - NRF_LOG_ERROR("ERROR %u [%s] at %s:%u\r\nPC at: 0x%08x",:

        case NRF_FAULT_ID_SDK_ERROR:
        {
            error_info_t * p_info = (error_info_t *)info;
            NRF_LOG_ERROR("ERROR %u [%s] at %s:%u\r\nPC at: 0x%08x",
                          p_info->err_code,
                          nrf_strerror_get(p_info->err_code),
                          p_info->p_file_name,
                          p_info->line_num,
                          pc);
             NRF_LOG_ERROR("End of error report");
            break;
        }
        default:
            NRF_LOG_ERROR("UNKNOWN FAULT at 0x%08X", pc);
            break;
    }
#endif

    NRF_BREAKPOINT_COND;

Which can be the issue? 

Thank you very much,

best regards

Gianluca

Parents Reply Children
  • I found out that in the fatfs SPI initialization function there is this configuration:

    #define APP_SDCARD_CONFIG(MOSI_PIN, MISO_PIN, SCK_PIN, CS_PIN) {    \
                .mosi_pin = MOSI_PIN,                                   \
                .miso_pin = MISO_PIN,                                   \
                .sck_pin  = SCK_PIN,                                    \
                .cs_pin   = CS_PIN                                      \
            }
    

    Can this be the problem? In the sense that I set the CS to be a specific pin (while in the other case the CS pin was not set). Anyways, I would expect that the fatfs example work after this initialization

Related