cancel
Showing results for 
Search instead for 
Did you mean: 

I use LSM9DS1 with STM32L433 and get strange data.

Vladimir Lobanov
Associate II

I use LSM9DS1 with STM32L433 and get strange data.0690X0000060QR1QAM.png

Initialization code:

HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG4;
 
 data = 0x38; //0b00111000
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG5_XL;
 
 data = 0x38; //0b00111000
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG1_G;
 
 data = 0x80; //0b01000000
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
 
 address = FIFO_CTRL;
 
 data = 0xC0; //0b11000000
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG9;
 
 data = 0x06; //0b00000110
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG1_M;
 
 data = 0xF8; //0b11111000
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG3_M;
 
 data = 0x04; //0b00000100
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_SET);
 
  
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_RESET);
 
 address = CTRL_REG4_M;
 
 data = 0x0C; //0b00001100
 
 HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
 
 HAL_SPI_Transmit(&hspi1, &data, sizeof(address), 0x1000);
 
 HAL_GPIO_WritePin(CS_M_GPIO_Port, CS_M_Pin, GPIO_PIN_SET);

FIFO read code:

{
            HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
            uint8_t address = FIFO_SRC | 0x80;
            uint8_t data;
            uint16_t gyro_temp[3];
            uint16_t accel_temp[3];
            uint8_t data_temp[2];
            HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
            HAL_SPI_Receive(&hspi1, &data, sizeof(data), 0x1000);
            HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
            uint8_t count = data & 0x1F;
            HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_RESET);
            address = OUT_X_L_G | 0x80;
            HAL_SPI_Transmit(&hspi1, &address, sizeof(address), 0x1000);
            for (uint8_t i = 0; i < count; i++)
            {          
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                gyro_temp[0] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                gyro_temp[1] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                gyro_temp[2] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                accel_temp[0] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                accel_temp[1] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
                HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
                accel_temp[2] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
                
                uint8_t sym_count = sprintf(wtext, "%u;%u;%u;%u;%u;%u;\r\n", accel_temp[0], accel_temp[1], accel_temp[2], gyro_temp[0], gyro_temp[1], gyro_temp[2]);
				f_write(&file, wtext, sym_count+1 , (void *)&wbytes);
            }
            HAL_GPIO_WritePin(CS_A_G_GPIO_Port, CS_A_G_Pin, GPIO_PIN_SET);
}

Can someone explain what I'm doing wrong?

8 REPLIES 8
T J
Lead

interpretation needs more information...

what is strange about the data ?

Vladimir Lobanov
Associate II

On all channels, data jumps from 0 to ~ 60000, although the accelerometer is at rest.

For example, X axys of gyro:0690X0000060QcxQAE.png

T J
Lead

ooo,

you have negative numbers

+ and - 0

like this:

5,4,3,2,1,0,0xffff,0xfffe these are negative numbers just next to zero

I am working on the LIS3DE today, same negative numbers

Vladimir Lobanov
Associate II

Well, if we take into account the negative values, then the amplitudes are still large (although the accelerometer is stationary)0690X0000060RHNQA2.png

T J
Lead

do you have the bytes swapped ?

i use this code

    
    ReadACC[0] = (int8_t)readLis3DEByte(OutX) & 0xFF;           
    ReadACC[1] = (int8_t)readLis3DEByte(OutY) & 0xFF;  
    ReadACC[2] = (int8_t)readLis3DEByte(OutZ) & 0xFF; 
int8_t readLis3DEByte(char address)
{
    address &= 0x3F;     // 6bit address
    address += 0x80;     // Read
    address += 0x40;     // Auto Increment
    HAL_GPIO_WritePin(LIS3DE_nSS_GPIO_Port, LIS3DE_nSS_Pin, GPIO_PIN_RESET);
    
    transfer(address);
    char dummyByte = 0;
    char RxSPI = transfer_receive(dummyByte);
    
    HAL_GPIO_WritePin(LIS3DE_nSS_GPIO_Port, LIS3DE_nSS_Pin, GPIO_PIN_SET);
    return RxSPI;
}
void transfer(unsigned short data) {
    //    Clear_SPI1_nSS_Out();
    char RxSPI;
    //read SPI SR register;
    int32_t readSPI_SR = hspi1.Instance->SR;
    
    while (!(hspi1.Instance->SR  & SPI_FLAG_TXE)) ;
    *((__IO uint8_t *)&hspi1.Instance->DR) =  data;
    
    while (!(hspi1.Instance->SR  & SPI_FLAG_TXE)) ;
    while ((hspi1.Instance->SR  & SPI_FLAG_BSY)) ;
    while ((hspi1.Instance->SR  & SPI_FLAG_RXNE))
        RxSPI = hspi1.Instance->DR + readSPI_SR;    // read Rx byte and discard, only clearing the buffer
//    Set_SPI1_nSS_Out();        
}
char transfer_receive(unsigned short data) {
 
    char RxSPI;                                                     
 //   Clear_SPI1_nSS_Out();
    while(!(hspi1.Instance->SR  & SPI_FLAG_TXE))
         ;
    while ((hspi1.Instance->SR  & SPI_FLAG_RXNE))   // load all the bytes received, only keep the last one
     RxSPI = hspi1.Instance->DR;      //empty fifo
 
    *((__IO uint8_t *)&hspi1.Instance->DR) = data;  // force the SPI to transceive 8 bit
    while(!(hspi1.Instance->SR  & SPI_FLAG_TXE))    // wait to transmitter double buffer to shift into transmitter
         ;                   
    while ((hspi1.Instance->SR  & SPI_FLAG_BSY)) ;  // wait for data to shift out of the processor pins
    while((hspi1.Instance->SR  & SPI_FLAG_RXNE))   // load all the bytes received, only keep the last one
        RxSPI = hspi1.Instance->DR;     // we only want the last byte
 //   Set_SPI1_nSS_Out();        
        
    return RxSPI;
}

T J
Lead

do you have a noisy power supply ?

Vladimir Lobanov
Associate II

I read Lo and Hi bytes and convert it to int16:

HAL_SPI_Receive(&hspi1, &data_temp[0], sizeof(data_temp[0]), 0x1000);
HAL_SPI_Receive(&hspi1, &data_temp[1], sizeof(data_temp[1]), 0x1000);
gyro_temp[0] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);

T J
Lead

I cant see the values, so how can I help...

are you sure the value are not reversed ?

gyro_temp[0] = (int16_t) (((int16_t)data_temp[1] << 8) | data_temp[0]);
 
or 
gyro_temp[0] = (int16_t) (((int16_t)data_temp[0] << 8) | data_temp[1]);