Today I will introduce to you how to quickly implement the drive test of ST sensors through the STM32 microcontroller . The sensor we use this time is LSM6DSOX . Because the SensorTile.box development board integrates this sensor, I will use SensorTile.box to demonstrate how to drive LSM6DSOX through the STM32L4R9 microcontroller on the development board to realize the data reading of the acceleration gyroscope.
Because ST sensors provide very powerful sensor drivers and demonstration projects, sensor testing can be completed without even writing a line of code in the STM32 microcontroller.
This post is suitable for novices who are not particularly familiar with sensors or STM32 microcontrollers. For netizens with certain development experience, the techniques taught in this post can be completed through simple exploration by themselves. After reading this, you should not need to read the following content.
The following content is also applicable to other ST sensors and other STM32 microcontrollers. The difference is that the port connecting the sensor should be configured according to the actual connection situation of the sensor.
First, find the LSM6DSOX product page on the ST official website , then click the Tool&Software tab, find the C-Driver-MEMS software resource, and click it. Find Get from Github to jump to the Github website, then select Code->Download ZIP to download the STMems_Standard_C_drivers driver package to your local computer.
Unzip the downloaded STMems_Standard_C_drivers , find the LSM6DSOX folder, and the two files under drivers are the driver files of lsm6dsox . Open the example folder, you can see a lot of demonstration codes. This time, the lsm6dsox_read_data_polling.c file is used to implement the polling reading of sensor data, and then the data is sent to the computer through the USB virtual serial port of STM32L4R9 .
Open STM32CubeMX , create a project, select the corresponding MCU model and click Start Project , find SPI1 under Connectivity in Pinout&Configuration , select Full-Duplex Master Mode , modify the Prescaler value to make the clock frequency meet the requirements, and set Data Size to 8 Bit . Set the corresponding pins of SPI according to the SensorTile.box schematic diagram , set the CS pin of LSM6DSOX , and set the label of the CS pin, such as CS_LSM6DSOX .
Then find USB_OTG_FS under Connectivity in Pinout&Configuration , select Device_Only for Mode , and leave the rest as default. Find USB_DEVICE in Middleware , set Class For FS IP to Communication Device Class (Virtual Port Com) , and leave the rest as default. The USB virtual serial port is now set.
Change the clock to 120MHz in Clock Configuration , select HSI48 for CLK48 Clock Mux , then give the project a name in Project of Project Manager , select the corresponding project path, and select the corresponding IDE . Here I chose STM32CubeIDE , which is a free tool officially launched by ST based on eclipse development, which is very easy to use.
Then in Code Generator, change Copy all used libraries into the project folder to Copy only the necessary library files , so that only the used libraries are copied, and the generated project will be smaller. Click the GENERATE CODE button in the upper right corner to generate the project files and initialization code.
After the project is generated, click Open Project and use the corresponding IDE to open the project file. In STM32CubeIDE, copy lsm6dsox_read_data_polling.c and lsm6dsox_reg.c to the Src folder , and copy lsm6dsox_reg.h to the Inc folder.
Open lsm6dsox_read_data_polling.c and reference the main.h header file, add a reference to hspi1 , set dev_ctx.handle to hspi1 , remove the code not related to SPI in the platform_write and platform_read functions, change the CS operation to the port and pin corresponding to CS_LSM6DSOX , and remove the code except CDC_Transmit_FS in the tx_com function .
Reference the lsm6dsox_read_data_polling function in the main.c file and execute it in the main function . In the project settings, check use float with printf from newlib-nano (-u _printf_float) to remove the compiler warning, and then compile the project.
Connect the debugger and development board, download the firmware to the STM32 microcontroller, and open the serial port software to see the corresponding sensor data.
I will do a complete demonstration via video next time.
Netizens who are familiar with the STM32CubeMX tool should find that the X-CUBE-MEMS1 expansion package integrated in STM32CubeMX has integrated almost all ST sensor drivers. It is also convenient to use STM32CubeMX to add corresponding sensor drivers. Which method to choose depends on personal habits.