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

  • Sorry for the late reply :(

    So, this is my overlay file for the nrf5340dk

    // To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
    // For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
    &led0 {
        gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
    };
    
    &button1 {
        gpios = <&gpio0 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
    };
    
    
    &qspi {
        status = "disabled";
    };
    
    &pwm0 {
        status = "disabled";
    };
    
    &uart0 {
        status = "okay";
        tx-pin = <2>;
        rx-pin = <3>;
        rts-pin = <0>;
        cts-pin = <1>;
    };
    
    // I had to disable these to use the requested pins.
    &spi2 {
        status = "disabled";
    };
    
    &i2c1 {
        status = "disabled";
    };
    
    &button2 {
        status = "disabled";
    };
    
    

    And this is the prj.conf file

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    CONFIG_GPIO=y
    CONFIG_PRINTK=y
    
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="BCC_SAC_TEST"
    CONFIG_BT_DEVICE_APPEARANCE=833
    CONFIG_BT_NUS_THREAD_STACK_SIZE=2048
    CONFIG_THREAD_NAME=y
    
    
    CONFIG_BT_USER_DATA_LEN_UPDATE=y
    CONFIG_BT_DATA_LEN_UPDATE=y
    CONFIG_BT_USER_PHY_UPDATE=y
    CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
    
    CONFIG_BT_BUF_ACL_RX_SIZE=251
    CONFIG_BT_GATT_CLIENT=y
    CONFIG_BT_ATT_PREPARE_COUNT=2
    CONFIG_BT_CONN_TX_MAX=10
    CONFIG_BT_MAX_CONN=1
    CONFIG_BT_L2CAP_TX_BUF_COUNT=10
    CONFIG_BT_L2CAP_TX_MTU=247
    CONFIG_BT_BUF_ACL_TX_COUNT=10
    CONFIG_BT_BUF_ACL_TX_SIZE=251
    
    # Enable the NUS service
    CONFIG_BT_NUS=y
    
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
    CONFIG_DEBUG_OPTIMIZATIONS=y
    CONFIG_DEBUG_THREAD_INFO=y
    
    # Config logger
    CONFIG_LOG=n
    
    CONFIG_ASSERT=y
    CONFIG_ENTROPY_GENERATOR=y
    CONFIG_TEST_RANDOM_GENERATOR=y
    
    CONFIG_SPI=n
    CONFIG_NRFX_SPIM=y
    CONFIG_NRFX_SPIM1=y
    CONFIG_NRFX_SPIM2=y
    CONFIG_NRFX_GPIOTE=y

     Should I disable something else??

Reply
  • Sorry for the late reply :(

    So, this is my overlay file for the nrf5340dk

    // To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
    // For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
    &led0 {
        gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
    };
    
    &button1 {
        gpios = <&gpio0 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
    };
    
    
    &qspi {
        status = "disabled";
    };
    
    &pwm0 {
        status = "disabled";
    };
    
    &uart0 {
        status = "okay";
        tx-pin = <2>;
        rx-pin = <3>;
        rts-pin = <0>;
        cts-pin = <1>;
    };
    
    // I had to disable these to use the requested pins.
    &spi2 {
        status = "disabled";
    };
    
    &i2c1 {
        status = "disabled";
    };
    
    &button2 {
        status = "disabled";
    };
    
    

    And this is the prj.conf file

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    CONFIG_GPIO=y
    CONFIG_PRINTK=y
    
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="BCC_SAC_TEST"
    CONFIG_BT_DEVICE_APPEARANCE=833
    CONFIG_BT_NUS_THREAD_STACK_SIZE=2048
    CONFIG_THREAD_NAME=y
    
    
    CONFIG_BT_USER_DATA_LEN_UPDATE=y
    CONFIG_BT_DATA_LEN_UPDATE=y
    CONFIG_BT_USER_PHY_UPDATE=y
    CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
    
    CONFIG_BT_BUF_ACL_RX_SIZE=251
    CONFIG_BT_GATT_CLIENT=y
    CONFIG_BT_ATT_PREPARE_COUNT=2
    CONFIG_BT_CONN_TX_MAX=10
    CONFIG_BT_MAX_CONN=1
    CONFIG_BT_L2CAP_TX_BUF_COUNT=10
    CONFIG_BT_L2CAP_TX_MTU=247
    CONFIG_BT_BUF_ACL_TX_COUNT=10
    CONFIG_BT_BUF_ACL_TX_SIZE=251
    
    # Enable the NUS service
    CONFIG_BT_NUS=y
    
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
    CONFIG_DEBUG_OPTIMIZATIONS=y
    CONFIG_DEBUG_THREAD_INFO=y
    
    # Config logger
    CONFIG_LOG=n
    
    CONFIG_ASSERT=y
    CONFIG_ENTROPY_GENERATOR=y
    CONFIG_TEST_RANDOM_GENERATOR=y
    
    CONFIG_SPI=n
    CONFIG_NRFX_SPIM=y
    CONFIG_NRFX_SPIM1=y
    CONFIG_NRFX_SPIM2=y
    CONFIG_NRFX_GPIOTE=y

     Should I disable something else??

Children
No Data
Related