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

How to communicate with LSM9DS1 and SPI with nrf52840

HI.

I had previously implemented spi communication using the STM32 chip and the LSM9DS1.

Now I want to implement the same functionality using nrf52840 DK.

But it does not work.

STM32

SpiHandle.Instance               = SPIx;
  SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; //SPI_BAUDRATEPRESCALER_2 는 값 튐. pre8
  SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;
  SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;
  SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;
  SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;
  SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;
  SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLE;
  SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
  SpiHandle.Init.CRCPolynomial     = 7;
  SpiHandle.Init.NSS               = SPI_NSS_SOFT;
  SpiHandle.Init.Mode = SPI_MODE_MASTER;

  if(HAL_SPI_Init(&SpiHandle) != HAL_OK)  
  {
    Error_Handler();
  }

void AG_read_ndata(uint8_t reg,uint8_t* data,uint8_t n){
    uint8_t cmd[1];
    uint8_t i;

    LSM9DS1_AG_L();
    cmd[0]=reg|0x80;
    HAL_SPI_Transmit(&SpiHandle, &cmd[0],1,0x1000);
    for(i=0;i<n;i++){
        cmd[0]=0x00;
        HAL_SPI_TransmitReceive(&SpiHandle,&cmd[0], &data[i], 1,0x1000);
    }
    LSM9DS1_AG_H();
}

nRF52840

void spi_configure(void){
 
  nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  spi_config.ss_pin   = SPI_SS_PIN;
  spi_config.miso_pin = SPI_MISO_PIN;
  spi_config.mosi_pin = SPI_MOSI_PIN;
  spi_config.sck_pin  = SPI_SCK_PIN;
  APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
}


void AG_read_data(uint8_t reg, uint8_t* data, uint8_t n){
  uint8_t cmd[1];
  uint8_t i;
  
  cmd[0] = reg|0x80;
  nrf_drv_spi_transfer(&spi, &cmd[0], 1, 0, 0);
  for(i=0;i<n;i++){
    cmd[0]=0x00;
    nrf_drv_spi_transfer(&spi,&cmd[0],1,&data[i],1);
  }
}

What's wrong?

Thank you.

Parents
  • Hello,

    If you look at the definition of NRF_DRV_SPI_DEFAULT_CONFIG (found on line 214 in nrf_drv_spi.h), you will see what it defines. You can use this default config for initializing the SPI, and change the settings that you want to use, like it is done in the spi example in the SDK.

     

    If you are still having trouble getting the two chips to work together, try out the different modes

        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        spi_config.mode     = NRF_DRV_SPI_MODE_0;   //also test modes 1, 2 and 3
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    
        NRF_LOG_INFO("SPI example started.");

     

    Best regards,

    Edvin

Reply
  • Hello,

    If you look at the definition of NRF_DRV_SPI_DEFAULT_CONFIG (found on line 214 in nrf_drv_spi.h), you will see what it defines. You can use this default config for initializing the SPI, and change the settings that you want to use, like it is done in the spi example in the SDK.

     

    If you are still having trouble getting the two chips to work together, try out the different modes

        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        spi_config.mode     = NRF_DRV_SPI_MODE_0;   //also test modes 1, 2 and 3
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    
        NRF_LOG_INFO("SPI example started.");

     

    Best regards,

    Edvin

Children
No Data
Related