5614 views|4 replies

9702

Posts

24

Resources
The OP
 

LSM6DSO(X) sensor driver key code analysis: read_data_polling routine (Part 2) [Copy link]

 
This post was last edited by littleshrimp on 2021-2-28 18:21

Through the previous posts, we have a general understanding of the Standard C driver and read_data_polling of ST sensors. Next, I will continue to give a more in-depth introduction to the read_data_polling routine.

https://en.eeworld.com/bbs/thread-1157700-1-1.html

In the lsm6dso_read_data_polling function, after resetting the sensor and judging that the reset is successful, the sensor will be configured. First, the lsm6dso_i3c_disable_set function is executed. This function is used to configure and disable the I3C function. As for what I3C is, you can search for "MIPI I3C" by yourself. The I3C function is disabled in the routine. Looking at the lsm6dso_i3c_disable_set function, it is still a bit complicated. Let's first look at the val in the function parameter, which corresponds to the enumeration type lsm6dso_i3c_disable_t. LSM6DSO_I3C_DISABLE is to turn off I3C, and the rest are ENABLE and the corresponding time. The value of LSM6DSO_I3C_DISABLE is 0x80, and the others are 0, 1, 2, and 3 respectively. Back to the lsm6dso_i3c_disable_set function, the function first executes lsm6dso_read_reg to read the CTRL9_XL register, and then sets ctrl9_xl.i3c_disabl, ((uint8_t)val & 0x80U) >> 7 is used to set val to 1 when the 7th bit is 1 (LSM6DSO_I3C_DISABLE), otherwise it is 0. Other methods can also be used here, such as val == LSM6DSO_I3C_DISABLE ? 1 : 0, or ((uint8_t)val & 0x80U) == 0x80U, etc. Checking the data sheet, we can see that the I3C_disable bit of CTRL9_XL is used to enable or disable the I3C interface, and it is disabled when it is 1.

After configuring i3c_disable of CTRL9_XL, the I3C_BUS_AVB register is read through the lsm6dso_read_reg function, and then the 0-1 bits of val are assigned to i3c_bus_avb_sel. Looking at the I3C_BUS_AVB in the data sheet, it corresponds to 50us, 2ms, 1ms and 5ms. I don’t know much about this parameter related to I3C, so I won’t explain it here.

Going back to the lsm6dso_read_data_polling function, after executing lsm6dso_i3c_disable_set to turn off the I3C function, lsm6dso_block_data_update_set is executed. This function is used by many sensors. Looking inside lsm6dso_block_data_update_set, we can see that it is used to set the bdu bit of CTRL3_C, which is also read first and then set. BDU stands for Block Data Update. Because the sensor resolution is 16 bits, each set of data consists of the high byte MSB and the low byte LSB. For example, just after the MCU has just read the MSB or LSB of the first data, the two register data of the sensor are updated immediately. Then the other byte (MSB or LSB) read by the MCU is actually the second data. In this way, the 16-bit data composed of these two bytes is equivalent to including the first data and the second data. To prevent this from happening, BDU can be set to 1, so that the sensor will determine whether the MSB and LSB of a group of registers have been read completely, and will only update these two registers after they have been read completely.

After talking about BDU, the rest of the code is easier to understand. lsm6dso_xl_data_rate_set is used to set the ODR of acceleration, lsm6dso_gy_data_rate_set is used to set the ODR of gyroscope, lsm6dso_xl_full_scale_set is used to set the full scale of acceleration, lsm6dso_gy_full_scale_set is used to set the full scale of gyroscope. The driver header file lsm6dso_reg.h has been defined. Just pass the corresponding definition as a parameter to the corresponding function to achieve the corresponding configuration.

After setting the data output rate and full scale of the sensor, the filter of the sensor is configured. The input parameter of the lsm6dso_xl_hp_path_on_out_set function is LSM6DSO_LP_ODR_DIV_100, which literally means setting the acceleration low-pass filter frequency to ODR/100. Because ODR was previously set to 12.5Hz, the filter allows signals below 0.125Hz to pass. lsm6dso_xl_hp_path_on_out_set sets the hp_slope_xl_en, hp_ref_mode_xl and hpcf_xl of CTRL8_XL respectively. Check the CTRL8_XL in the data sheet and the AN5192 application note. HP_SLOPE_ XL_EN is the high-pass filter enable. When it is 1, it is a high-pass filter, and when it is 0, it is a low-pass filter. hp_ref_mode_xl is the high-pass filter reference mode. To enable this mode, you must also set HP_SLOPE_ XL_EN to 1 (high-pass mode), and the HPCF_XL value cannot be 0 (that is, ODR / 4 (slope filter) is not supported). After enabling the reference mode, the sensor will save the current acceleration value, and the data read later will be the difference between the current value and the current value. For example, it is easier to understand. Suppose the acceleration value is X=0, Y=0, Z=1000mg when the reference mode is enabled. This value will be saved all the time. If the sensor remains stationary, the data obtained is still X=0, Y=0, Z=1000mg in theory. At this time, the data read is X=0, Y=0, Z=0, which is the result of subtracting two sets of data. hpcf_xl corresponds to BIT7-BIT5 of CTRL8_XL, occupies 3 bits, and the corresponding cutoff frequency is ODR/4 to ODR/800.

Then the lsm6dso_xl_filter_lp2_set function is executed, and the function is set to PROPERTY_ENABLE. By observing the lsm6dso_xl_filter_lp2_set function, it is found that it is used to set the lpf2_xl_en bit of the CTRL1_XL register. From the table in the figure below, we can see that lpf2_xl_en is only effective for the low-pass filter, and when it is 0, it is equivalent to shielding LPF2.

At this point, the sensor configuration tool of the entire routine has been completed. The remaining code is repeated in the while block, which is also relatively easy to understand. The lsm6dso_xl_flag_data_ready_get function is used to read the xlda bit of the STATUS_REG register. Bits 2, 1, and 0 of STATUS_REG correspond to valid temperature data, valid angular velocity data, and valid acceleration data, respectively. xlda indicates that the acceleration data is valid, and when it is 1, it means that the acceleration data conversion is completed. Then use memset to clear the data of data_raw_acceleration, and then read the x, y, and z axis data of acceleration to data_raw_acceleration through the lsm6dso_acceleration_raw_get function.

The lsm6dso_acceleration_raw_get function reads 6 bytes of data from the OUTX_L_A register at a time, corresponding to the LSB and MSB of X, Y, and Z respectively, and then merges the 2 bytes into a 16-bit data. Next, the 16-bit acceleration raw data read out is converted into the corresponding acceleration value in mg through the lsm6dso_from_fs2_to_mg function. Note that the code here does not judge the acceleration range, but uses a fixed writing method. lsm6dso_from_fs2_to_mg is only valid when the range is 2g. If the range is 4g, the lsm6dso_from_fs4_to_mg function is required. If you want to be more flexible here, you can use macro definitions yourself, or save the range information when configuring the range and make judgments, so as to facilitate code modification in the future. lsm6dso_from_fs2_to_mg multiplies the current 16-bit acceleration data by 0.061f. The origin of 0.061 can be found in the data sheet, which is the last picture below. Finally, sprintf is used to convert the floating-point acceleration data into a string and the data is sent to the host computer through tx_com for analysis.

The remaining gyroscope and temperature data are all related to the acceleration type, so I won't repeat them here. So far, the entire read_data_polling routine has been introduced. If you have any questions, please post a reply.

Latest reply

Have you solved this problem?   Details Published on 2023-12-11 01:33
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 

6549

Posts

0

Resources
2
 

Thank you for sharing, it will be very useful to learn from them in the future

 
 

24

Posts

0

Resources
3
 
 
 

3

Posts

0

Resources
4
 

Dear experts, I am now using LSM6DSO for low-power applications, such as motion/stillness detection, tilt detection, and 6D direction detection. The data output rate is 26hz, and the SPI bus is used to communicate with the microcontroller. However, I now find that the current of the whole machine is very large (after the microcontroller enters STOP mode, the current of the whole machine is still 570uA. All other modules in my circuit have been removed, and the current is still so large). I have also seen relevant information on the Internet, which says that this chip has a problem with the shared interface of IIC and SPI. I have also tried many ways, but still cannot solve it. I wonder if anyone has a good way!

 
 
 

4

Posts

0

Resources
5
 
luobote posted on 2023-11-13 14:22 Dear experts, I am currently using LSM6DSO for low-power applications, detecting motion/stillness, tilt detection, 6D direction detection, the data output rate is 26hz, and single...

Have you solved this problem?

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list