SPI transfer stuck at while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END)){}

I am using a PCA10056 Dev Kit to test the accel sensor(IIS3DWB). But the SPI transfer is stuck at the line below.

while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END)){}

Here is more information. 

1.ST provided an official driver which should can be transplanted to any platform if the SPI write and read function is defined.

2.My implementation is showed below

uint8_t SpiInOut (stmdev_ctx_t* spi_ctx, uint8_t outData)
{
    uint32_t err_code;
    uint8_t inData ;
    
    if ((spi_ctx == NULL) || (&(spi_ctx->spi_handle) == NULL))
    {
        APP_ERROR_CHECK_BOOL (false);
    }

    err_code = nrf_drv_spi_transfer (&(spi_ctx->spi_handle), &outData, 1, &inData, 1);
    APP_ERROR_CHECK(err_code);

    return inData;
}

int32_t SpiWriteRegisters(void* handle, uint8_t reg, const uint8_t* buffer, uint16_t len)
{
    UNUSED_PARAMETER(handle);

    GpioWrite(&iis3dwb_.spi.Nss, 0);
    SpiInOut(&iis3dwb_.spi, reg);
    for(uint16_t i = 0; i < len; i++)
    {
        SpiInOut(&iis3dwb_.spi, buffer[i]);
    }
    GpioWrite(&iis3dwb_.spi.Nss, 1);

    return 0;
}

int32_t SpiReadRegisters(void* handle, uint8_t reg, uint8_t* buffer, uint16_t len)
{
    UNUSED_PARAMETER(handle);
    
    // Set first bit to 1;
    reg |= 0x80;
    GpioWrite(&iis3dwb_.spi.Nss, 0);
    SpiInOut(&iis3dwb_.spi, reg);
    
    for(uint16_t i = 0; i < len; i++)
    {
        buffer[i] = SpiInOut(&iis3dwb_.spi, 0);
    }

    GpioWrite(&iis3dwb_.spi.Nss, 1);

    return 0;
}

3.I wrote the program with reference to this example. But the program is stuck at the second loop of 

 iis3dwb_fifo_out_multi_raw_get(&dev_ctx, fifo_data, num);

Data can be retrieved in the first loop. 

4. I tried to modify the ReadRegister function as below, it won't be stuck, but the data is all wrong which the rxBuffer is full of zeros.

//  for(uint16_t i = 0; i < len; i++)
//  {
//      buffer[i] = SpiInOut(&iis3dwb_.spi, 0);
//  }
    
err_code = nrf_drv_spi_transfer (&(iis3dwb_.spi.Spi), NULL , 0 , buffer, len);
APP_ERROR_CHECK(err_code);

Parents Reply Children
No Data
Related