09-27-2019 10:03 PM - edited 09-27-2019 10:05 PM
I am trying to have SPI connectio as interface protocol between BMI160 shuttle board/MSP432P401R. What I need is the accel/gyroscope data continuously. The main functions I am using is from BMI160 drivers found on github:
https://github.com/BoschSensortec/BMI160_driver
I have defined the user_spi_read/user_spi_write as below:
int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
SPI_BMI160_BEGIN;
/* send register address byte */
SPI_BMI160_TX(reg_addr);
/* Storing chip status */
// SPI_BMI160_RX(rslt);
for (i = 0; i < len; i++)
{
SPI_BMI160_TX(*reg_data);
reg_data++;
}
return (rslt);
}
And
int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
int i;
/* Pull CS_N low*/
SPI_BMI160_BEGIN;
/* send register address byte */
SPI_BMI160_TX(reg_addr);
/* Storing chip status */
//SPI_BMI160_RX(rslt);/////////////////?????
/* Communicate len number of bytes: if RX - the procedure sends 0x00 to push bytes from slave*/
for (i = 0; i < len; i++)
{
SPI_BMI160_TX(0); /* Possible to combining read and write as one access type */
// SPI_WAIT_DONE();
SPI_BMI160_RX(*reg_data); /* Store pData from last pData RX */
reg_data++;
}
//}
/* Pull CS_N low*/
SPI_BMI160_END;
/* return the status byte value */
return(rslt);
}
The functions for SPI (in MSP432p401R) abbreviated as below:
#define SPI_BMI160_BEGIN P6OUT &= ~BIT2;
#define SPI_BMI160_TX(x) do{while(!(UCTXIFG & UCB1IFG));UCB1TXBUF= (x);}while(1==-1)
#define SPI_BMI160_RX(x) do{while(!(UCRXIFG & UCB1IFG));(x)=UCB1RXBUF;}while(1==-1)
#define SPI_BMI160_END P6OUT |= BIT2;
Is this correct form for SPI wirte/read functions? Seems SPI is not working.
Thanks
Saber
09-29-2019 10:35 AM - edited 09-30-2019 04:40 AM
It is recommended to observe the waveform using a logic analyzer or oscilloscope.
Here are some suggestions in terms of the code:
10-12-2019 12:04 AM
Thanks for your previous reply.
On 2 and 3, you mentioned that for read/write operations, the registers need to be ORed/ANDed with 0x7F/0x40 respectively. Does it apply to I2C as well?
Thanks
Saber
10-14-2019 02:59 PM
10-18-2019 11:02 PM - edited 10-19-2019 12:09 AM
Hello
Thanks for your previous answer. Maybe a trivial question 🙂:
In #1, you mentioned to switch to SPI, a dummy read of 0x00 should be done. Can you please explain how I can do it?
The SPI write and read instances are like below:
int8_t user_spi_write(int8_t dev_id, int8_t reg_addr, int8_t *reg_data, int8_tlength)
{
int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
SPI_BMI160_BEGIN;
SPI_BMI160_TX(reg_addr & 0x7F);
__delay_cycles(200);
for (i = 0; i < length; i++)
{
SPI_BMI160_TX(*reg_data);
reg_data++;
}
SPI_BMI160_END;
return rslt;
}
int8_t user_spi_read(int8_t dev_id, int8_t reg_addr, int8_t *reg_data, int8_tlength)
{
int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
SPI_BMI160_BEGIN;
SPI_BMI160_TX(reg_addr | 0x80);
__delay_cycles(200);
for (i = 0; i < length; i++)
{
SPI_BMI160_TX(0);
SPI_BMI160_RX(*reg_data); /* Store pData from last pData RX */
reg_data++;
}
SPI_BMI160_END;
return rslt;
}
Thanks
Saber