I tried in vain to connect my nRF51822 eval board with LIS3DH using SPI. Currently I am using the SPI example in PCA10001 with spi_master_init and spi_master_tx_rx function. After some initialisation code, I would like to read the xyz value from LIS3DH, but all the rx_data are 0xFF, which is not normal. Below are my codes:
int main(void)
{
uint32_t * p_spi_base_address;
uint8_t CTRL_REG1_DATA[2]={0x20,0xA7};
uint8_t CTRL_REG2_DATA[2]={0x21,0x00};
uint8_t CTRL_REG3_DATA[2]={0x22,0x40};
uint8_t CTRL_REG4_DATA[2]={0x23,0x00};
uint8_t CTRL_REG5_DATA[2]={0x24,0x00};
uint8_t INT1_THS_DATA[2]={0x32,0x10};
uint8_t INT1_DURATION_DATA[2]={0x33,0x10};
uint8_t INT1_CFG_DATA[2]={0x30,0x0A};
uint8_t rx_data[2]={0x00,0x00};
p_spi_base_address = spi_master_init(SPI0, SPI_MODE0, false);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, CTRL_REG1_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, CTRL_REG2_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, CTRL_REG3_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, CTRL_REG4_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, CTRL_REG5_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, INT1_THS_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, INT1_DURATION_DATA, rx_data);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, INT1_CFG_DATA, rx_data);
nrf_delay_ms(10);
uint8_t tx_x1[2]={0xA8,0xFF};
uint8_t tx_x2[2]={0xA9,0xFF};
uint8_t tx_y1[2]={0xAA,0xFF};
uint8_t tx_y2[2]={0xAB,0xFF};
uint8_t tx_z1[2]={0xAC,0xFF};
uint8_t tx_z2[2]={0xAD,0xFF};
uint8_t rx_x1[2]={0x00,0x00};
uint8_t rx_x2[2]={0x00,0x00};
uint8_t rx_y1[2]={0x00,0x00};
uint8_t rx_y2[2]={0x00,0x00};
uint8_t rx_z1[2]={0x00,0x00};
uint8_t rx_z2[2]={0x00,0x00};
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_x1, rx_x1);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_x2, rx_x2);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_y1, rx_y1);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_y2, rx_y2);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_z1, rx_z1);
nrf_delay_ms(10);
spi_success = spi_master_tx_rx(p_spi_base_address, 2, tx_z2, rx_z2);
nrf_delay_ms(10);
while(true)
{
}
}
For reference, below is how I connect nRF51822 to LIS3DH:
nRF51822 LIS3DH
MISO SDO
MOSI SDA
SCK SCL
Am I doing anything wrong? Is there any reference I could follow on? Im not sure whether I was wrong in the first place when doing the initialisation. Could you please guide me to a simple example of how it could be done? Thanks so much!!