I am using SPI0 of nrf52.Following is the code:
I am using SPI0 of nrf52.Following is the code:
That code just looks like a small modification of the spi_master example in SDK 0.9.2? If you want our help you will need to describe your problem in more detail.
Can you please try to tidy up your code? Remove all the code that is not necessary and show us how exactly you are initiating the SPI and how you do read and write operations.
Hi, I am able to read the register value of ADXL362 accelerometer into nrf52 with slight modification with previous code.Modifications are:
void read(nrf_drv_spi_t const * p_instance, uint8_t reg){
m_tx_data_spi[0] = 0x0B;
m_tx_data_spi[1] = reg;
m_tx_data_spi[2] = 0x00;
// m_rx_data_spi[2] = 0;
uint32_t err_code = nrf_drv_spi_transfer(p_instance,
m_tx_data_spi, 3, m_rx_data_spi, 3);
print(m_rx_data_spi[2]);
}
void print(unsigned char p)
{
static char buff[4]={0};
sprintf(buff,"%x",p);
app_uart_put(buff[0]);
app_uart_put(buff[1]);
app_uart_put(0x0A);
app_uart_put(0x0D);
nrf_delay_ms(DELAY_MS*10);
}
But I am facing Problem in writing the values into the ADXL362 registers using SPI_master code of nrf52. I am using UART for displaying register values on terminal.This is my main function:
int main(void)
{
// Setup bsp module.
bsp_configuration();
LEDS_CONFIGURE(LEDS_MASK);
LEDS_OFF(LEDS_MASK);
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
for (;;)
{
#if (SPI0_ENABLED == 1)
if (m_transfer_completed)
{
m_transfer_completed = false;
switch_state();
nrf_delay_ms(DELAY_MS);
}
#endif // (SPI0_ENABLED == 1)
}
}
I am writing the values in ADXL362 registers using following function.When I write value in particular register individually one at time and then read it back then it working.But when when I perform write operation for all register simultaneously an then read any one of them then it giving me zero value.Following is the code:
static void switch_state(void)
{
nrf_drv_spi_t const * p_instance;
nrf_drv_spi_config_t const * p_config;
switch (m_spi_master_ex_state)
{
#if (SPI0_ENABLED == 1)
case TEST_STATE_SPI0_LSB:
p_instance = &m_spi_master_0;
spi_master_init(p_instance, true);
break;
case TEST_STATE_SPI0_MSB:
p_instance = &m_spi_master_0;
spi_master_init(p_instance, false);
break;
#endif // (SPI0_ENABLED == 1)
default:
return;
}
write(p_instance, 0x25,0x03);
nrf_delay_ms(DELAY_MS);
write(p_instance, 0x23,0x58);
nrf_delay_ms(DELAY_MS);
read(p_instance, 0x23);
}
#endif // (SPI0_ENABLED == 1)
I don't see the write()
function you have in your code. Also in your read()
function you start a transfer with nrf_drv_spi_transfer()
and then already in the next line you try to read the rx buffer. At this point the SPI will not yet have completed the transfer and hence, you will not be able to read new data. You need to wait until the SPI interrupt handler is called with a message saying the transfer is complete. Or you can initiate the SPI driver in blocking mode. Then nrf_drv_spi_transfer()
will not return before the transfer is done and it is safe to read the data in the next line. You can use blocking mode by writing nrf_drv_spi_init(p_instance, &config, NULL);