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.