1.前言
物联网部分先放一下,先来搞传感器的移植
这里选用的传感器是LSM6DSO16IS,内部集成了ISPU,是做震动开发的不二选择。
正好手上有一片IKS4A1自带这个传感器,就直接插在板子背面来用一下
2.初始配置
首先新建一个CUBEMX空工程,选择板卡为H573I-DK
BSP部分先不要配置,容易和自带的接口冲突
在中间固件这里选择x-cube-MEMS包,下载好之后选择IKS4A1这个子板,也可以直接选择LSM6DSO16IS这个传感器。
之后选择I2C1接口,配置成FAST快速模式,这个I2C正好是板子上对应的传感器接口。M33核的DMA配置有点不同,这里先不配置DMA。
回到中间固件这里,把板子的I2C接口选中为I2C1。
时钟配置如图:
生成工程,初始配置完成
3.代码编辑
生成的代码有一个,这里面包含了大部分板子上传感器的参数。
IKS4A1_CONF.H:
#include "stm32h5xx_hal.h" #include "stm32h573i_dk_bus.h" #include "stm32h573i_dk_errno.h" /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef IKS4A1_CONF_H #define IKS4A1_CONF_H #ifdef __cplusplus extern "C" { #endif #define USE_IKS4A1_ENV_SENSOR_SHT40AD1B_0 1U #define USE_IKS4A1_ENV_SENSOR_LPS22DF_0 1U #define USE_IKS4A1_ENV_SENSOR_STTS22H_0 1U #define USE_IKS4A1_MOTION_SENSOR_LSM6DSV16X_0 1U #define USE_IKS4A1_MOTION_SENSOR_LIS2DUXS12_0 1U #define USE_IKS4A1_MOTION_SENSOR_LIS2MDL_0 1U #define USE_IKS4A1_MOTION_SENSOR_LSM6DSO16IS_0 1U #define IKS4A1_I2C_INIT BSP_I2C1_Init #define IKS4A1_I2C_DEINIT BSP_I2C1_DeInit #define IKS4A1_I2C_READ_REG BSP_I2C1_ReadReg #define IKS4A1_I2C_WRITE_REG BSP_I2C1_WriteReg #define IKS4A1_I2C_READ BSP_I2C1_Recv #define IKS4A1_I2C_WRITE BSP_I2C1_Send #define IKS4A1_GET_TICK BSP_GetTick #define IKS4A1_DELAY HAL_Delay #define BUS_IKS4A1_INSTANCE BUS_I2C1_INSTANCE #define BUS_IKS4A1_CLK_DISABLE() __HAL_RCC_I2C1_CLK_DISABLE() #define BUS_IKS4A1_CLK_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE() #define BUS_IKS4A1_SCL_GPIO_PORT BUS_I2C1_SCL_GPIO_PORT #define BUS_IKS4A1_SCL_GPIO_AF BUS_I2C1_SCL_GPIO_AF #define BUS_IKS4A1_SCL_GPIO_CLK_ENABLE() BUS_I2C1_SCL_GPIO_CLK_ENABLE() #define BUS_IKS4A1_SCL_GPIO_CLK_DISABLE() BUS_I2C1_SCL_GPIO_CLK_DISABLE() #define BUS_IKS4A1_SCL_GPIO_PIN BUS_I2C1_SCL_GPIO_PIN #define BUS_IKS4A1_SDA_GPIO_PIN BUS_I2C1_SDA_GPIO_PIN #define BUS_IKS4A1_SDA_GPIO_CLK_DISABLE() BUS_I2C1_SDA_GPIO_CLK_DISABLE() #define BUS_IKS4A1_SDA_GPIO_PORT BUS_I2C1_SDA_GPIO_PORT #define BUS_IKS4A1_SDA_GPIO_AF BUS_I2C1_SDA_GPIO_AF #define BUS_IKS4A1_SDA_GPIO_CLK_ENABLE() BUS_I2C1_SDA_GPIO_CLK_ENABLE() #ifdef __cplusplus } #endif #endif /* IKS4A1_CONF_H*/
只有这些是远远不够的,还要移植一些其他代码
只展示部分.C的代码,头文件请自行定义。
#include "iks4a1_mems_control.h" #include "iks4a1_motion_sensors.h" extern void *MotionCompObj[IKS4A1_MOTION_INSTANCES_NBR]; /** * @brief Initializes accelerometer * @param None * @retval None */ void BSP_SENSOR_LSM6DSO16IS_ACC_Init(void) { (void) IKS4A1_MOTION_SENSOR_Init(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO); } void BSP_SENSOR_LSM6DSO16IS_GYR_Init(void) { (void) IKS4A1_MOTION_SENSOR_Init(IKS4A1_LSM6DSO16IS_0, MOTION_GYRO); } void BSP_SENSOR_LSM6DSO16IS_ACC_Enable(void) { (void)IKS4A1_MOTION_SENSOR_Enable(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO); } void BSP_SENSOR_LSM6DSO16IS_GYR_Enable(void) { (void)IKS4A1_MOTION_SENSOR_Enable(IKS4A1_LSM6DSO16IS_0, MOTION_GYRO); } /** * @brief Disables accelerometer * @param None * @retval None */ void BSP_SENSOR_LSM6DSO16IS_ACC_Disable(void) { (void)IKS4A1_MOTION_SENSOR_Disable(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO); } /** * @brief Disables gyroscope * @param None * @retval None */ void BSP_SENSOR_LSM6DSO16IS_GYR_Disable(void) { (void)IKS4A1_MOTION_SENSOR_Disable(IKS4A1_LSM6DSO16IS_0, MOTION_GYRO); } /** * @brief Get accelerometer data * @param Axes pointer to axes data structure * @retval None */ void BSP_SENSOR_LSM6DSO16IS_ACC_GetAxes(IKS4A1_MOTION_SENSOR_Axes_t *Axes) { (void)IKS4A1_MOTION_SENSOR_GetAxes(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO, Axes); } /** * @brief Get gyroscope data * @param Axes pointer to axes data structure * @retval None */ void BSP_SENSOR_LSM6DSO16IS_GYR_GetAxes(IKS4A1_MOTION_SENSOR_Axes_t *Axes) { (void)IKS4A1_MOTION_SENSOR_GetAxes(IKS4A1_LSM6DSO16IS_0, MOTION_GYRO, Axes); } /** * @brief Set output data rate for accelerometer * @param Odr Output Data Rate value to be set * @retval None */ void BSP_SENSOR_LSM6DSO16IS_ACC_SetOutputDataRate(float Odr) { (void)IKS4A1_MOTION_SENSOR_SetOutputDataRate(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO, Odr); } void BSP_SENSOR_LSM6DSO16IS_ACC_SetFullScale(int32_t Fullscale) { (void)IKS4A1_MOTION_SENSOR_SetFullScale(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO, Fullscale); }
另外,需要定义一些传感器参数:
#define ALGO_FREQ 50U /* Algorithm frequency >= 50Hz */ #define ACC_ODR ((float)ALGO_FREQ) #define ACC_FS 4 /* FS = <-4g, 4g> */ #define GYR_ODR ((float)ALGO_FREQ) #define GYR_FS 200 /* FS = <+_250> */
最后,把init 和Process定义,放到主函数中运行即可:
void MX_MEMS_Process(void) { BSP_SENSOR_LSM6DSO16IS_ACC_Enable(); BSP_SENSOR_LSM6DSO16IS_GYR_Enable(); BSP_SENSOR_LSM6DSO16IS_ACC_GetAxes(&AccValue); BSP_SENSOR_LSM6DSO16IS_GYR_GetAxes(&GyrValue); } static void Init_Sensors(void) { BSP_SENSOR_LSM6DSO16IS_ACC_Init(); BSP_SENSOR_LSM6DSO16IS_GYR_Init(); BSP_SENSOR_LSM6DSO16IS_ACC_SetOutputDataRate(ACC_ODR); BSP_SENSOR_LSM6DSO16IS_ACC_SetFullScale(ACC_FS); //BSP_SENSOR_GYR_SetOutputDataRate(GYR_ODR); //BSP_SENSOR_GYR_SetFullScale(GYR_FS); }
4.成果展示
在DEBUG里观察数据,可以看到传感器输出的原始数据如图: