LSM6DS3 _ timing issue ?
Dear Community,
I am trying to setup the LSM6DS3 Accelerometer/Gyrochip. The datasheet states the following startup procedure for this device:
1 Write CTRL9_XL = 38h // Acc X, Y, Z axes enabled
2 Write CTRL1_XL = 60h // Acc = 416Hz (High-Performance mode)
3 Write INT1_CTRL = 01h // Acc Data Ready interrupt on INT1
My interpretation of this procedure is the following code (which runs on aSTM32F302 µC):
//APB1 @36 MHz
//SPI3 setup
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
//IRQ Handler
char Rx_Idx = 0x00;
char Tx_Idx = 0x00;
void SPI3_IRQHandler() {
/* SPI in Master Tramitter mode--------------------------------------- */
if (SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_TXE) == SET)
{
SPI_SendData8(SPI3, Tx_Idx);
SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, DISABLE);
}
// SPI in Master Receiver mode-----------------------------------
if (SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_RXNE) == SET)
{
Rx_Idx = SPI_ReceiveData8(SPI3);
}
// SPI Error interrupt---------------------------------------
if (SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_OVR) == SET) {
SPI_ReceiveData8(SPI3);
SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_OVR);
}
}
char SPI_transfer(char data) {
Tx_Idx = data;
//start the SPI interupt handler...
SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE); //the send routine disables the interupt if all bytes have been sen
/* Waiting until TX FIFO is empty */
while (SPI_GetTransmissionFIFOStatus(SPI3) != SPI_TransmissionFIFOStatus_Empty)
{}
/* Wait busy flag */
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET)
{}
/* Waiting until RX FIFO is empty */
while (SPI_GetReceptionFIFOStatus(SPI3) != SPI_ReceptionFIFOStatus_Empty)
{}
return Rx_Idx;
}
void main() {
//some periph intitialization
Delay1sec();
//Register CTRL9_XL
SPI_transfer(0x18);
//Data
SPI_transfer(0x38);
//Register CTRL1_XL
SPI_transfer(0x10);
//Data
SPI_transfer(0x60);
//Register INT1_CTRL
SPI_transfer(0x0D);
//Data
SPI_transfer(0x01);
while(1) {
// 3 2 1 0
//EV_BOOT TDA GDA XLDA
retn = 0;
while( !(retn & (1 << 0))) {
// Send_Message(retn);
retn = SPI_transfer(0x1E);
}
/*
3 Read OUTX_L_XL 28h
4 Read OUTX_H_XL 29h
5 Read OUTY_L_XL 2Ah
6 Read OUTY_H_XL 2Bh
7 Read OUTZ_L_XL 2CH
8 Read OUTZ_H_XL 2DH
*/
tempX = 0;
tempX = SPI_transfer(0x28);
tempX = tempX + 256* SPI_transfer(0x29);
tempY = 0;
tempY = SPI_transfer(0x2A);
tempY = tempY + 256* SPI_transfer(0x2B);
tempZ = 0;
tempZ = SPI_transfer(0x2C);
tempZ = tempZ + 256* SPI_transfer(0x2D);
Send_X2bytes( tempX); //sends the values to the PC
Send_Y2bytes( tempY);
Send_Z2bytes( tempZ);
Delay100ms();
};
This code pulls out some data from the registers but the values are confusing and have probably nothing to do with acceleration (although the values change if the chip is turned around). I doubt there is a timing issue and I would be glad if someone could give a hint.
