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

nrf52:SPI master with softDevice can not work

Hi, nrf52832, softdevice:S132 My SPI master program can initialize the SD Card succefully. But, when I put it into the examples of ble_app_uart on nRF5_SDK_11.0.0_89a8197, it can not work. It write a data through SPI, but can not receive any ack. Its spi_event_handler not been called. Why? The following is some code of the main function:

int main(void)
{
    uint32_t err_code;
    bool erase_bonds;
	
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();
    buttons_leds_init(&erase_bonds);
	   
    ble_stack_init();	
    gap_params_init();	 
    services_init();	
    advertising_init(); 
    conn_params_init();	

    printf("\r\nUART Start!\r\n");
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
   SDHost_Test();

for();
}
........

void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
    spi_xfer_done = true;
}

u8 SPI1_WriteOneByte(u8 TxData)
{
    uint8_t spi_txbuf = TxData;

	
	spi_xfer_done = false;		
    nrf_drv_spi_transfer(&m_spi_master, &spi_txbuf, 1, NULL, 0);
	while (!spi_xfer_done)
	{
	   __WFE();
	}

	return true;

}

image description

  • I tried adding your code to the ble_app_uart example and printed a string to UART in spi_event_handler. This seems to work fine for me. Are you sure that you do not receive any SPI events?

  • Yes,I'm sure. It can not enter into void spi_event_handler(nrf_drv_spi_evt_t const * p_event). The programme stop in while (!spi_xfer_done) { __WFE(); }

  • //the code my added is the following:

    //my add start -----------------------------
    
    #define SPI_MASTER_0_ENABLE 
    #define SD_CS  	30 
    #define SD_MISO	28
    #define SD_MOSI	27
    #define SD_SCK	29
    
    static nrf_drv_spi_t	m_spi_master = NRF_DRV_SPI_INSTANCE(0);
    static volatile bool spi_xfer_done = true;  
    
    /**
     * @brief SPI user event handler.
     * @param event
     */
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
    {
    	printf("p_event->type=%d\r\n",p_event->type);
    
        spi_xfer_done = true;
    }
    
    /**
     * @brief spi_master_init.
     * @param spi_master_instance,spi_speed
     */
    static uint32_t spi_master_init(nrf_drv_spi_t * spi_master_instance)
    {
        uint32_t err_code = NRF_SUCCESS;
    
    	nrf_drv_spi_config_t spi_config =
    			{
    				.ss_pin = SD_CS,
    				.irq_priority = APP_IRQ_PRIORITY_LOW,
    				.orc = 0xFF,
    				.frequency = NRF_DRV_SPI_FREQ_4M, 
    				.mode = NRF_DRV_SPI_MODE_3,
    				.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
    				.sck_pin = SD_SCK,
    				.mosi_pin = SD_MOSI,
    				.miso_pin = SD_MISO
    			};
    
    	err_code = nrf_drv_spi_init(spi_master_instance, &spi_config, spi_event_handler);		
    
        return err_code;
        
    }
    
    //SPI Write one byte data
    uint8_t SPI1_WriteOneByte(uint8_t TxData)
    {
        uint8_t spi_txbuf = TxData;
    	
    	spi_xfer_done = false;		
        uint32_t err_code = nrf_drv_spi_transfer(&m_spi_master, &spi_txbuf, 1, NULL, 0);
    
    	//wait for the data to transfer complete.
    	while(!spi_xfer_done)
    	{
    		__WFE();
    	}
    	
    	printf("\r\n SPI1_WriteOneByte transfer complete\r\n"); 	
    		
    	return true;
    
    }
    //my add end -----------------------------
    
    
    /**@brief Application main function.
     */
    int main(void)
    {
        uint32_t err_code;
        bool erase_bonds;
    
        // Initialize.
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
        uart_init();
    
        buttons_leds_init(&erase_bonds);
        ble_stack_init();
        gap_params_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        printf("\r\nUART Start!\r\n");
        err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
        APP_ERROR_CHECK(err_code);
    
    	//my add start -----------------------------
    	uint8_t i;
    	spi_master_init(&m_spi_master);		
    	nrf_delay_ms(500);	
     	for(i=0;i<10;i++)
    		SPI1_WriteOneByte(0XFF);
        //my add end -----------------------------
    	
        // Enter main loop.
        for (;;)
        {
            power_manage();
        }
    }
    
    
    /** 
     * @}
     */
    
  • OK,I have find the problems. With the softDevice, the SPI frequency can only be set once.

  • Hello, what's the meaning of "With the softDevice, the SPI frequency can only be set once."? I notice you just set the SPI frequence once in your code.

Related