打印
[应用相关]

STM32C0移植LSM6DS3数据卡死

[复制链接]
998|17
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
电子烂人|  楼主 | 2025-2-21 18:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

遇到了之前做评测相似的问题,STM32C0移植LSM6DS3时候,数据不通过RST检测
感觉是一直卡在了复位上
手动设置复位寄存器之后,还是只会显示一次数据后卡死,每次显示数据一致


具体DEBUG发现是卡在了timeOUT 函数里

贴上具体的部分关键代码:
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
          lsm6ds3tr_c_read_data_polling();
  }
  /* USER CODE END 3 */
}

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void lsm6ds3tr_c_read_data_polling(void)
{
  /* Initialize mems driver interface */
  stmdev_ctx_t dev_ctx;
  dev_ctx.write_reg = platform_write;
  dev_ctx.read_reg = platform_read;
//  dev_ctx.mdelay = platform_delay;
  dev_ctx.handle = &hi2c1;

  /* Wait sensor boot time */
  HAL_Delay(BOOT_TIME);
  /* Check device ID */
  whoamI = 0x6a;
  lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);

  if ( whoamI != LSM6DS3TR_C_ID )
    while (1); /*manage here device not found */

  /* Restore default configuration*/
  lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  do {
    lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
    lsm6ds3tr_c_reset_set(&dev_ctx, 0);
  } while (rst);

  /* Enable Block Data Update */
// lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  /* Set Output Data Rate */
  lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  /* Set full scale */
  lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, LSM6DS3TR_C_2g);
  lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, LSM6DS3TR_C_2000dps);
  /* Configure filtering chain(No aux interface) */

  /* Accelerometer - analog filter */
  //lsm6ds3tr_c_xl_filter_analog_set(&dev_ctx,LSM6DS3TR_C_XL_ANA_BW_400Hz);

  /* Accelerometer - LPF1 path ( LPF2 not used )*/
  //lsm6ds3tr_c_xl_lp1_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_LP1_ODR_DIV_4);

  /* Accelerometer - LPF1 + LPF2 path */
  //lsm6ds3tr_c_xl_lp2_bandwidth_set(&dev_ctx,LSM6DS3TR_C_XL_LOW_NOISE_LP_ODR_DIV_100);

  /* Accelerometer - High Pass / Slope path */
  //lsm6ds3tr_c_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);
  //lsm6ds3tr_c_xl_hp_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_HP_ODR_DIV_100);

  /* Gyroscope - filtering chain */
  //lsm6ds3tr_c_gy_band_pass_set(&dev_ctx,LSM6DS3TR_C_HP_260mHz_LP1_STRONG);

  /* Read samples in polling mode (no int) */
  while (1) {
    /* Read output only if new value is available */
    lsm6ds3tr_c_reg_t reg;
    lsm6ds3tr_c_status_reg_get(&dev_ctx, ®.status_reg);

    if (reg.status_reg.xlda) {
      /* Read magnetic field data */
      memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
      lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
                                       data_raw_acceleration);
      acceleration_mg[0] = lsm6ds3tr_c_from_fs2g_to_mg(
                             data_raw_acceleration[0]);
      acceleration_mg[1] = lsm6ds3tr_c_from_fs2g_to_mg(
                             data_raw_acceleration[1]);
      acceleration_mg[2] = lsm6ds3tr_c_from_fs2g_to_mg(
                             data_raw_acceleration[2]);
    }
    if (reg.status_reg.gda) {
      /* Read magnetic field data */
      memset(data_raw_angular_rate, 0x00, 3 * sizeof(int16_t));
      lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx,
                                       data_raw_angular_rate);
      angular_rate_mdps[0] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
                               data_raw_angular_rate[0]);
      angular_rate_mdps[1] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
                               data_raw_angular_rate[1]);
      angular_rate_mdps[2] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
                               data_raw_angular_rate[2]);
    }

    if (reg.status_reg.tda) {
      /* Read temperature data */
      memset(&data_raw_temperature, 0x00, sizeof(int16_t));
      lsm6ds3tr_c_temperature_raw_get(&dev_ctx, &data_raw_temperature);
      temperature_degC = lsm6ds3tr_c_from_lsb_to_celsius(
                           data_raw_temperature );

    }
}
}

/*
* @brief  Write generic device register (platform dependent)
*
* @param  handle    customizable argument. In this examples is used in
*                   order to select the correct sensor bus handler.
* @param  reg       register to write
* @param  bufp      pointer to data to write in register reg
* @param  len       number of consecutive register to write
*
*/
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
                              uint16_t len)
{

  HAL_I2C_Mem_Write(handle, LSM6DS3TR_C_I2C_ADD_L, reg,
                    I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  return 0;
}

/*
* @brief  Read generic device register (platform dependent)
*
* @param  handle    customizable argument. In this examples is used in
*                   order to select the correct sensor bus handler.
* @param  reg       register to read
* @param  bufp      pointer to buffer that store the data read
* @param  len       number of consecutive register to read
*
*/
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
                             uint16_t len)
{
  HAL_I2C_Mem_Read(handle, LSM6DS3TR_C_I2C_ADD_L, reg,
                   I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  return 0;
}

/*
* @brief  Write generic device register (platform dependent)
*
* @param  tx_buffer     buffer to transmit
* @param  len           number of byte to send
*
*/


/* USER CODE END 4 */


使用特权

评论回复
沙发
wangjiahao88| | 2025-3-6 15:00 | 只看该作者
LSM6DS3;用的是硬件IIC吗?

使用特权

评论回复
评论
电子烂人 2025-3-7 12:01 回复TA
对的,硬件i2c fast模式 
电子烂人 2025-3-7 11:59 回复TA
对的,硬件i2c fast模式 
板凳
wangjiahao88| | 2025-3-6 15:01 | 只看该作者
看你的程序,没有使用中断方法读写吗?没有开启中断么?

使用特权

评论回复
评论
电子烂人 2025-3-7 11:58 回复TA
我看有很多开发者的代码也有提到中断,请问是要在IO给出中断信号之后再采集吗?十分感谢 
电子烂人 2025-3-6 16:24 回复TA
没有,只用了轮询SPI 
地板
laocuo1142| | 2025-3-7 10:29 | 只看该作者
DEBUG时发现卡在了I2C的timeOUT函数中意味着I2C通信没有正常进行。

使用特权

评论回复
5
powerantone| | 2025-3-7 11:20 | 只看该作者
检查程序逻辑是否存在问题

使用特权

评论回复
6
flycamelaaa| | 2025-3-7 12:00 | 只看该作者
可能是由于I2C总线上的信号质量不佳、时序不匹配或地址冲突等问题导致的。

使用特权

评论回复
7
stormwind123| | 2025-3-7 15:03 | 只看该作者
可以尝试修改复位相关的代码或重新配置复位引脚。

使用特权

评论回复
8
probedog| | 2025-3-7 16:03 | 只看该作者
验证I2C的时序设置是否与LSM6DS3的要求相匹配。如果不匹配,需要调整STM32C0的I2C时序设置。

使用特权

评论回复
9
classroom| | 2025-3-7 17:23 | 只看该作者
检查I2C地址是否正确设置,避免地址冲突导致的通信失败。

使用特权

评论回复
10
wangjiahao88| | 2025-3-25 13:47 | 只看该作者
是这样的,硬件 IIC中断,I2C_GET_TIMEOUT_FLAG就要清除中断;

使用特权

评论回复
11
wangjiahao88| | 2025-3-25 13:49 | 只看该作者
I2C_MasterRx;I2C_MasterTx里面看一下;0x08,18,28,等等,就不要管他。f8,58 都有可能出现,这种情况,只管去读数据就可以了!

使用特权

评论回复
12
过期的塔头| | 2025-3-31 23:06 | 只看该作者
程序在等待某个操作完成时超时了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

71

帖子

0

粉丝