Stm32f7 - Disco get data from IMU LSM9DS1 (SPI interface)

Description:

In last post , I was show how to get data of LSM9DS1 sensor with I2C interface. In this post , I used SPI interface to get data. SPI interface is faster than I2C but more wires to connect .
  LSM9DS1_DATASHEET
 Connection
 Sensor                  MPU
3.3V-----------------------3.3V
 GND---------------------GND
SCL----------------------PI_1
SDA----------------------MOSI (PB_15)
SDO_AG----------------MISO (PB_14)
CS_AG------------------- PA_8

1. Pin out setting
RCC -> High speed clock -> Crystal ceramic resonator
Up system clock to 216Mhz
SPI2 ->Mode ->Full Duplex Master
PA_8->Output
2. SPI configuration
Frame format ->Motorola
Data size-> 8bit
Prescaler -> 16
3. Source generation and edition
In file Src/main.c add

/* Includes ------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
#define ACT_THS             0x04
#define ACT_DUR             0x05
#define INT_GEN_CFG_XL      0x06
#define INT_GEN_THS_X_XL    0x07
#define INT_GEN_THS_Y_XL    0x08
#define INT_GEN_THS_Z_XL    0x09
#define INT_GEN_DUR_XL      0x0A
#define REFERENCE_G         0x0B
#define INT1_CTRL           0x0C
#define INT2_CTRL           0x0D
#define WHO_AM_I_XG         0x0F
#define CTRL_REG1_G         0x10
#define CTRL_REG2_G         0x11
#define CTRL_REG3_G         0x12
#define ORIENT_CFG_G        0x13
#define INT_GEN_SRC_G       0x14
#define OUT_TEMP_L          0x15
#define OUT_TEMP_H          0x16
#define STATUS_REG_0        0x17
#define OUT_X_L_G           0x18
#define OUT_X_H_G           0x19
#define OUT_Y_L_G           0x1A
#define OUT_Y_H_G           0x1B
#define OUT_Z_L_G           0x1C
#define OUT_Z_H_G           0x1D
#define CTRL_REG4           0x1E
#define CTRL_REG5_XL        0x1F
#define CTRL_REG6_XL        0x20
#define CTRL_REG7_XL        0x21
#define CTRL_REG8           0x22
#define CTRL_REG9           0x23
#define CTRL_REG10          0x24
#define INT_GEN_SRC_XL      0x26
#define STATUS_REG_1        0x27
#define OUT_X_L_XL          0x28
#define OUT_X_H_XL          0x29
#define OUT_Y_L_XL          0x2A
#define OUT_Y_H_XL          0x2B
#define OUT_Z_L_XL          0x2C
#define OUT_Z_H_XL          0x2D
#define FIFO_CTRL           0x2E
#define FIFO_SRC            0x2F
#define INT_GEN_CFG_G       0x30
#define INT_GEN_THS_XH_G    0x31
#define INT_GEN_THS_XL_G    0x32
#define INT_GEN_THS_YH_G    0x33
#define INT_GEN_THS_YL_G    0x34
#define INT_GEN_THS_ZH_G    0x35
#define INT_GEN_THS_ZL_G    0x36
#define INT_GEN_DUR_G       0x37


///////////////////////////////
// LSM9DS1 Magneto Registers //
///////////////////////////////
#define OFFSET_X_REG_L_M    0x05
#define OFFSET_X_REG_H_M    0x06
#define OFFSET_Y_REG_L_M    0x07
#define OFFSET_Y_REG_H_M    0x08
#define OFFSET_Z_REG_L_M    0x09
#define OFFSET_Z_REG_H_M    0x0A
#define WHO_AM_I_M          0x0F
#define CTRL_REG1_M         0x20
#define CTRL_REG2_M         0x21
#define CTRL_REG3_M         0x22
#define CTRL_REG4_M         0x23
#define CTRL_REG5_M         0x24
#define STATUS_REG_M        0x27
#define OUT_X_L_M           0x28
#define OUT_X_H_M           0x29
#define OUT_Y_L_M           0x2A
#define OUT_Y_H_M           0x2B
#define OUT_Z_L_M           0x2C
#define OUT_Z_H_M           0x2D
#define INT_CFG_M           0x30
#define INT_SRC_M           0x30
#define INT_THS_L_M         0x32
#define INT_THS_H_M         0x33

------------------------------------------------------------------------------------

void write_data(uint8_t regedit , uint8_t data){
    uint8_t cmd[1];
    HAL_GPIO_WritePin (GPIOA, GPIO_PIN_8, 0);
    cmd[0]= regedit&0x7f;
    HAL_SPI_Transmit(&hspi2, cmd,1,0x1000);
    cmd[0]=data;
    HAL_SPI_Transmit(&hspi2, cmd,1,0x1000);
    HAL_GPIO_WritePin (GPIOA, GPIO_PIN_8, 1);
}
uint8_t read_1data(uint8_t reg){
    uint8_t data[1];
    uint8_t cmd[1];
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 0);
    cmd[0]=reg|0x80;
    HAL_SPI_Transmit(&hspi2, &cmd[0],1,0x1000);
    cmd[0]=0x00;
    HAL_SPI_TransmitReceive(&hspi2,&cmd[0], &data[0], 1,0x1000);
    //HAL_SPI_TransmitReceive(&hspi2, cmd, data,1,10);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 1);
    return data[0];
}
void read_ndata(uint8_t reg,uint8_t* data,uint8_t n){
    uint8_t cmd[1];
    uint8_t i;
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 0);
    cmd[0]=reg|0x80;
    HAL_SPI_Transmit(&hspi2, &cmd[0],1,0x1000);
    for(i=0;i<n;i++){
        cmd[0]=0x00;
        HAL_SPI_TransmitReceive(&hspi2,&cmd[0], &data[i], 1,0x1000);
    }
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 1);
}


int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_SPI2_Init(); 


    BSP_LCD_Init();
    BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
    BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
    BSP_LCD_DisplayOn();
    BSP_LCD_SelectLayer(0);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
    BSP_LCD_SelectLayer(1);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
    BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
    BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
    HAL_Delay(100);
     write_data(CTRL_REG1_G, 0x00); 
    write_data(CTRL_REG1_G, 0xC0);// 952 Hz OD, 33 Hz cutoff
    write_data(CTRL_REG2_G, 0x00);   
    write_data(CTRL_REG3_G, 0x00);
    write_data(CTRL_REG4, 0x38);
    write_data(CTRL_REG5_XL, 0x38);
    write_data(CTRL_REG6_XL, 0x00);
    write_data(CTRL_REG7_XL, 0x00);
    HAL_Delay(100);
    double gyro[3],acc[3];
     uint8_t data[6];
    int i=0;
    char buffer[30];
    int16_t gyro_data[3],acc_data[3];

  while (1)
  {
    BSP_LCD_Clear(LCD_COLOR_WHITE);
    read_ndata(0x18,data,6);       
    for(i=0;i<3;i++){
        gyro_data[i]=(data[2*i+1]<<8)|data[2*i];  
        gyro[i]=(double)gyro_data[i]*0.00875;
    }
    sprintf(buffer,"vx=%2.2lf vy=%2.2lf vz=%2.2lf", gyro[0],gyro[1],gyro[2]);
     BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"GyroScope:", LEFT_MODE);
     BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)buffer, LEFT_MODE);
    read_ndata(0x28,data,6);
      for(i=0;i<3;i++){
        acc_data[i]=(data[2*i+1]<<8)|data[2*i];  
        acc[i]=(double)acc_data[i]/16384*9.8;
    }
    sprintf(buffer,"ax=%2.2lf ay=%2.2lf az=%2.2lf", acc[0],acc[1],acc[2]);
     BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"Accelerometer:", LEFT_MODE);
      BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)buffer, LEFT_MODE);
     HAL_Delay(200);
    BSP_LCD_Clear(LCD_COLOR_WHITE);


  }

}