Zephyr SPIM does not receive any data from MISO pin

 Hello,

 I'm trying to move from nRF5 SDK with nRF52840 to NCS with nRF5340 and experiencing a lot of problems replacing the nrf driver functions with nrfx.

 Thanks to the other precedents in the devzone, I implemented all the functions except the SPIM Rx.

 The problem is that I see the SPI slave (nRF52840-DK and I checked it works well with another nRF52840-DK) toggles the MISO pin, but the SPI master (nRF5340) cannot read the input.

 Following is the code how I configured the SPIM.

 

nrfx_spim_t spim_adc = NRFX_SPIM_INSTANCE(2);
#define spim_adc_m_length  240
static uint8_t spim_adc_rx_buf[spim_adc_m_length+1];
nrfx_spim_xfer_desc_t spim_adc_desc = NRFX_SPIM_XFER_RX(spim_adc_rx_buf, spim_adc_m_length);
static volatile bool spim_adc_xfer_done;

(...)

void gpiote_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrfx_err_t err_code = nrfx_spim_xfer(&spim_adc, &spim_adc_desc, 0);
}

void gpiote_init(void)
{
	nrfx_err_t err_code;
	
    err_code = nrfx_gpiote_init(6);

	nrfx_gpiote_in_config_t gpiote_cfg = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(false);

	err_code = nrfx_gpiote_in_init(pin_spim_fpga_ind, &gpiote_cfg, gpiote_handler);
	if (NRFX_SUCCESS != err_code)
	{
		printk("Init error\n");
	}
	nrfx_gpiote_in_event_enable(pin_spim_fpga_ind, true);
}

(...)

void spim_adc_event_handler(nrfx_spim_evt_t const *p_event, void *p_context)
{
	if (p_event->type == NRFX_SPIM_EVENT_DONE)
	{
		spim_adc_xfer_done = true;
	}
}

void spi_init(void)
{
	nrfx_spim_config_t spim_adc_config = NRFX_SPIM_DEFAULT_CONFIG(pin_spim_adc_CLK, pin_spim_adc_MOSI, pin_spim_adc_MISO, pin_spim_adc_CS);
	spim_adc_config.frequency = NRF_SPIM_FREQ_8M;
	spim_adc_config.mode = NRF_SPIM_MODE_3;
	// nrfx_spim_init(&spim_adc, &spim_adc_config, spim_adc_event_handler, NULL);
	if (NRFX_SUCCESS != nrfx_spim_init(&spim_adc, &spim_adc_config, spim_adc_event_handler, NULL))
	{
		printk("Init error\n");
	}
	IRQ_DIRECT_CONNECT(SPIM2_SPIS2_TWIM2_TWIS2_UARTE2_IRQn, 2, nrfx_spim_2_irq_handler, 0);
	irq_enable(SPIM2_SPIS2_TWIM2_TWIS2_UARTE2_IRQn);
}

(...)

void main(void)
{
	// Set CPU clock to 64 MHz
	HFCLKCTL_R &= ~(0x01);

	// Init
	ble_init();
	nus_init();
	gpio_pin_init();
	spi_init();
	gpiote_init();

	// Load configuration
	if (IS_ENABLED(CONFIG_SETTINGS)) {
		settings_load();
	}

	// Start BLE advertising
	advertising_init();
	
	// Enable cache and hit/miss tracking
    while((NVMC_READY_R&0x1) == 0);
    NVMC_ICACHECNF_R = 0x101;

	// Main loop responses to the BLE RX event after the callback
	for(;;)
	{
		if (spim_adc_xfer_done)
		{
			uint16_t length = spim_adc_m_length;
			bt_nus_send(NULL, spim_adc_rx_buf, length);
			spim_adc_xfer_done = false;
		}
	}
}

 As you can see, when the SPI transaction is over, I send the Rx buffer data via Bluetooth. The result is a 240 bytes length array with only zero value.

 Please let me know if I missed something required to enable SPI Rx.

 P.S. The code crashes when I locate the bt_nus_data_send() in the SPIM handler but there was no problem doing so with ble_nus_data_send(). Is this the problem of priority which was managed by SoftDevice before?

Parents
  • Hi,

    Which pin are you using for MISO on the nRF5340? Typically if you are not able to read the pin, it is configured for use/assigned to another peripheral.

    Can you post your overlay and prj.conf files?

     P.S. The code crashes when I locate the bt_nus_data_send() in the SPIM handler but there was no problem doing so with ble_nus_data_send(). Is this the problem of priority which was managed by SoftDevice before?

    Sounds like it could be a priority issue, yes. What is the error when it crashes? You have the SPIM priority set to 2, so that is quite high. Have you tried lowering the priority?

    Best regards,
    Jørgen

  • Also, I defined the pins for GPIO and SPI in the main.c file as following

    // Pins for electrical recording and stimulation
    #define pin_stim_enable                 NRF_GPIO_PIN_MAP(0, 17)
    #define pin_mux_rec_sel0                NRF_GPIO_PIN_MAP(0, 21)
    #define pin_mux_rec_sel1                NRF_GPIO_PIN_MAP(0, 16)
    #define pin_mux_rec_sel2                NRF_GPIO_PIN_MAP(0, 14)
    #define pin_mux_stim_sel0               NRF_GPIO_PIN_MAP(0, 6)
    #define pin_mux_stim_sel1               NRF_GPIO_PIN_MAP(0, 24)
    #define pin_mux_stim_sel2               NRF_GPIO_PIN_MAP(0, 22)
    #define pin_mux_rec_enable              NRF_GPIO_PIN_MAP(0, 20)
    #define pin_mux_stim_enable             NRF_GPIO_PIN_MAP(0, 8)
    #define pin_vstart						NRF_GPIO_PIN_MAP(0, 26)
    
    // Pins for SPI with variable resistor to control electrical stimulation waveform
    #define pin_spim_stim_CS				NRF_GPIO_PIN_MAP(1, 12)
    #define pin_spim_stim_MISO				NRF_GPIO_PIN_MAP(1, 3)
    #define pin_spim_stim_MOSI				NRF_GPIO_PIN_MAP(1, 7)
    #define pin_spim_stim_CLK				NRF_GPIO_PIN_MAP(1, 1)
    
    // Pins for SPI with FPGA which decimates ADC's output
    #define pin_spim_adc_CS					NRF_GPIO_PIN_MAP(0, 9)
    #define pin_spim_adc_MISO				NRF_GPIO_PIN_MAP(0, 10)
    #define pin_spim_adc_MOSI				NRF_GPIO_PIN_MAP(0, 11)
    #define pin_spim_adc_CLK				NRF_GPIO_PIN_MAP(0, 12)
    #define pin_spim_fpga_ind				NRF_GPIO_PIN_MAP(0, 19)
    #define pin_fpga_osc_enable				NRF_GPIO_PIN_MAP(0, 27)
    

Reply
  • Also, I defined the pins for GPIO and SPI in the main.c file as following

    // Pins for electrical recording and stimulation
    #define pin_stim_enable                 NRF_GPIO_PIN_MAP(0, 17)
    #define pin_mux_rec_sel0                NRF_GPIO_PIN_MAP(0, 21)
    #define pin_mux_rec_sel1                NRF_GPIO_PIN_MAP(0, 16)
    #define pin_mux_rec_sel2                NRF_GPIO_PIN_MAP(0, 14)
    #define pin_mux_stim_sel0               NRF_GPIO_PIN_MAP(0, 6)
    #define pin_mux_stim_sel1               NRF_GPIO_PIN_MAP(0, 24)
    #define pin_mux_stim_sel2               NRF_GPIO_PIN_MAP(0, 22)
    #define pin_mux_rec_enable              NRF_GPIO_PIN_MAP(0, 20)
    #define pin_mux_stim_enable             NRF_GPIO_PIN_MAP(0, 8)
    #define pin_vstart						NRF_GPIO_PIN_MAP(0, 26)
    
    // Pins for SPI with variable resistor to control electrical stimulation waveform
    #define pin_spim_stim_CS				NRF_GPIO_PIN_MAP(1, 12)
    #define pin_spim_stim_MISO				NRF_GPIO_PIN_MAP(1, 3)
    #define pin_spim_stim_MOSI				NRF_GPIO_PIN_MAP(1, 7)
    #define pin_spim_stim_CLK				NRF_GPIO_PIN_MAP(1, 1)
    
    // Pins for SPI with FPGA which decimates ADC's output
    #define pin_spim_adc_CS					NRF_GPIO_PIN_MAP(0, 9)
    #define pin_spim_adc_MISO				NRF_GPIO_PIN_MAP(0, 10)
    #define pin_spim_adc_MOSI				NRF_GPIO_PIN_MAP(0, 11)
    #define pin_spim_adc_CLK				NRF_GPIO_PIN_MAP(0, 12)
    #define pin_spim_fpga_ind				NRF_GPIO_PIN_MAP(0, 19)
    #define pin_fpga_osc_enable				NRF_GPIO_PIN_MAP(0, 27)
    

Children
No Data
Related