Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nRF 52840 SPIM instance

Hello!

I'm trying to set up data transfer between nRF 52840 dongle and KX134 accelerometer from SparkFun. Currently I'm running the WHO_AM_I test only. The code is derived from an SDK example nrf_spim. When the SPIM instance(SPI_INSTANCE) is set to 3, the example works and the LED blinks. However, when the SPIM instance is set to 0,1 or 2, the LED will not blink. I tried changing SPI_USE_EASY_DMA but it makes no difference. 

Down below is the main.c file. All the SPI and SPIM instances in sdk_config.h are enabled.

#include "nrfx_spim.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define NRFX_SPIM_SCK_PIN  NRF_GPIO_PIN_MAP(0,02);
#define NRFX_SPIM_MOSI_PIN NRF_GPIO_PIN_MAP(1,15); //SDA
#define NRFX_SPIM_MISO_PIN NRF_GPIO_PIN_MAP(0,31); //ADR
#define NRFX_SPIM_SS_PIN   NRF_GPIO_PIN_MAP(1,10);
#define NRFX_SPIM_DCX_PIN  NRF_GPIO_PIN_MAP(0,22)

#define ADD_REG_WHO_AM_I            0x13
#define UC_WHO_AM_I_DEFAULT_VALUE   0x46       //0x46

#define SPI_INSTANCE  0                                          /**< SPI instance index. */
#define SPI_BUFSIZE  8

#define SET_READ_SINGLE_CMD(x)  (x | 0x80)

static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */

static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */

uint8_t spi_tx_buf[SPI_BUFSIZE];
uint8_t spi_rx_buf[SPI_BUFSIZE];

static const uint8_t m_length = sizeof(spi_tx_buf);        /**< Transfer length. */

void spim_event_handler(nrfx_spim_evt_t const * p_event,
                       void *                  p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
}

void spi_init(void)
{
    
    nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
    
    spi_config.frequency      = NRF_SPIM_FREQ_4M;
    spi_config.ss_pin         = NRFX_SPIM_SS_PIN;
    spi_config.miso_pin       = NRFX_SPIM_MISO_PIN;
    spi_config.mosi_pin       = NRFX_SPIM_MOSI_PIN;
    spi_config.sck_pin        = NRFX_SPIM_SCK_PIN;
    spi_config.dcx_pin        = NRFX_SPIM_DCX_PIN;
    spi_config.use_hw_ss      = true;
    spi_config.ss_active_high = false;
    
    APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));
    // for (int i=1;i<4;i++)
		// 	{
		// 		bsp_board_led_invert(BSP_BOARD_LED_3);
    //   				nrf_delay_ms(1000);
		// 		bsp_board_led_invert(BSP_BOARD_LED_3);
		// 		nrf_delay_ms(500);
		// 	}
}

int KX134_read_reg(int reg)
{

    spi_tx_buf[0] = SET_READ_SINGLE_CMD(reg);
    spi_xfer_done = false;

		nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(spi_tx_buf, m_length, spi_rx_buf, m_length);
    APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));

    while(spi_xfer_done == false){};

    return spi_rx_buf[1];
}

int main(void)
{
    
    bsp_board_init(BSP_INIT_LEDS);
		int intRegValue;
    
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    
		spi_init();

    NRF_LOG_INFO("NRFX SPIM example started.");
    
	while (1)
	{

    intRegValue = KX134_read_reg(ADD_REG_WHO_AM_I);

    if(intRegValue == UC_WHO_AM_I_DEFAULT_VALUE)
    {
      NRF_LOG_INFO("The WHO_AM_I value is correct for KX134...");
				bsp_board_led_invert(BSP_BOARD_LED_0);
        nrf_delay_ms(2000);
			bsp_board_led_invert(BSP_BOARD_LED_0);
			nrf_delay_ms(1000);
    }
	}
}

I'm currently using nRF SDK 15.0 and nRF connect v3.11.1

I appreciate any help provided in advance.

 25606.sdk_config.h

Parents Reply
  • Hey Jared, thank for such rapid reply! I appreciate the help. By DCX, do you mean line 73 in the code? I tried modify that to nrfx_spim_xfer() but it wouldn't work even with SPIM instance 3, which used to work. I also tried changing the USE_EASY_DMA in sdk_config.h, which also doesn't help. As for the DCX pin at line 52 in spi_init, I didn't use the pin at all, so I doubt changing it would make a difference anyway.

Children
  • JasonZK said:
    By DCX, do you mean line 73 in the code? I tried modify that to nrfx_spim_xfer() but it wouldn't work even with SPIM instance 3, which used to work. I

    Strange that it didn't work. Does it return any error codes?  Can you specify what you mean by didn't work, is there any activity on the SCLK?

    JasonZK said:
    As for the DCX pin at line 52 in spi_init, I didn't use the pin at all, so I doubt changing it would make a difference anyway.

    This feature isn't supported on the other instances so you shouldn't use that for instance 0-2.

    regards

    Jared 

  • Does it return any error codes?  Can you specify what you mean by didn't work

    It compiles fine. There are no error codes. The code above basically flash a LED if the WHO_AM_I value is correct. It worked using SPIM instance 3 and DCX, but after switching to other instances (I'm using instance 0 as a test) and changing DCX to nrfx_spim_xfer (also deleted the 15 at the end so that it compiles), the LED simply doesn't flash.

    is there any activity on the SCLK

    The SCK simply toggles to high for all SPIM instances, whenever the LED is blinking correctly or not blinking at all. 

    This feature isn't supported on the other instances so you shouldn't use that for instance 0-2.

    I also changed the dcx pin on line 52 to NRFX_SPIM_PIN_NOT_USED, which doesn't really have any effect on the results. 

    Thanks for the help.

  • Hey Jared. SPIM is now working. It seems that link 53 and 54 has to be commented out as well for the program to work. Thanks for the help!

Related