[应用相关] STM32C0移植LSM6DS3数据卡死

[复制链接]
1341|17
 楼主| 电子烂人 发表于 2025-2-21 18:01 | 显示全部楼层 |阅读模式

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


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

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

  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration--------------------------------------------------------*/

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

  8.   /* USER CODE BEGIN Init */

  9.   /* USER CODE END Init */

  10.   /* Configure the system clock */
  11.   SystemClock_Config();

  12.   /* USER CODE BEGIN SysInit */

  13.   /* USER CODE END SysInit */

  14.   /* Initialize all configured peripherals */
  15.   MX_GPIO_Init();
  16.   MX_ADC1_Init();
  17.   MX_USART1_UART_Init();
  18.   MX_I2C1_Init();
  19.   /* USER CODE BEGIN 2 */

  20.   /* USER CODE END 2 */

  21.   /* Infinite loop */
  22.   /* USER CODE BEGIN WHILE */
  23.   while (1)
  24.   {
  25.     /* USER CODE END WHILE */

  26.     /* USER CODE BEGIN 3 */
  27.           lsm6ds3tr_c_read_data_polling();
  28.   }
  29.   /* USER CODE END 3 */
  30. }

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

  39.   __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);

  40.   /** Initializes the RCC Oscillators according to the specified parameters
  41.   * in the RCC_OscInitTypeDef structure.
  42.   */
  43.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  44.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  45.   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  46.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  47.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  48.   {
  49.     Error_Handler();
  50.   }

  51.   /** Initializes the CPU, AHB and APB buses clocks
  52.   */
  53.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  54.                               |RCC_CLOCKTYPE_PCLK1;
  55.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  56.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  57.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  58.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;

  59.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  60.   {
  61.     Error_Handler();
  62.   }
  63. }

  64. /* USER CODE BEGIN 4 */
  65. void lsm6ds3tr_c_read_data_polling(void)
  66. {
  67.   /* Initialize mems driver interface */
  68.   stmdev_ctx_t dev_ctx;
  69.   dev_ctx.write_reg = platform_write;
  70.   dev_ctx.read_reg = platform_read;
  71. //  dev_ctx.mdelay = platform_delay;
  72.   dev_ctx.handle = &hi2c1;

  73.   /* Wait sensor boot time */
  74.   HAL_Delay(BOOT_TIME);
  75.   /* Check device ID */
  76.   whoamI = 0x6a;
  77.   lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);

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

  80.   /* Restore default configuration*/
  81.   lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  82.   do {
  83.     lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  84.     lsm6ds3tr_c_reset_set(&dev_ctx, 0);
  85.   } while (rst);

  86.   /* Enable Block Data Update */
  87. // lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  88.   /* Set Output Data Rate */
  89.   lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  90.   lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  91.   /* Set full scale */
  92.   lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, LSM6DS3TR_C_2g);
  93.   lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, LSM6DS3TR_C_2000dps);
  94.   /* Configure filtering chain(No aux interface) */

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

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

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

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

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

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

  111.     if (reg.status_reg.xlda) {
  112.       /* Read magnetic field data */
  113.       memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
  114.       lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  115.                                        data_raw_acceleration);
  116.       acceleration_mg[0] = lsm6ds3tr_c_from_fs2g_to_mg(
  117.                              data_raw_acceleration[0]);
  118.       acceleration_mg[1] = lsm6ds3tr_c_from_fs2g_to_mg(
  119.                              data_raw_acceleration[1]);
  120.       acceleration_mg[2] = lsm6ds3tr_c_from_fs2g_to_mg(
  121.                              data_raw_acceleration[2]);
  122.     }
  123.     if (reg.status_reg.gda) {
  124.       /* Read magnetic field data */
  125.       memset(data_raw_angular_rate, 0x00, 3 * sizeof(int16_t));
  126.       lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx,
  127.                                        data_raw_angular_rate);
  128.       angular_rate_mdps[0] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  129.                                data_raw_angular_rate[0]);
  130.       angular_rate_mdps[1] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  131.                                data_raw_angular_rate[1]);
  132.       angular_rate_mdps[2] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  133.                                data_raw_angular_rate[2]);
  134.     }

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

  141.     }
  142. }
  143. }

  144. /*
  145. * @brief  Write generic device register (platform dependent)
  146. *
  147. * @param  handle    customizable argument. In this examples is used in
  148. *                   order to select the correct sensor bus handler.
  149. * @param  reg       register to write
  150. * @param  bufp      pointer to data to write in register reg
  151. * @param  len       number of consecutive register to write
  152. *
  153. */
  154. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  155.                               uint16_t len)
  156. {

  157.   HAL_I2C_Mem_Write(handle, LSM6DS3TR_C_I2C_ADD_L, reg,
  158.                     I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  159.   return 0;
  160. }

  161. /*
  162. * @brief  Read generic device register (platform dependent)
  163. *
  164. * @param  handle    customizable argument. In this examples is used in
  165. *                   order to select the correct sensor bus handler.
  166. * @param  reg       register to read
  167. * @param  bufp      pointer to buffer that store the data read
  168. * @param  len       number of consecutive register to read
  169. *
  170. */
  171. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  172.                              uint16_t len)
  173. {
  174.   HAL_I2C_Mem_Read(handle, LSM6DS3TR_C_I2C_ADD_L, reg,
  175.                    I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  176.   return 0;
  177. }

  178. /*
  179. * @brief  Write generic device register (platform dependent)
  180. *
  181. * @param  tx_buffer     buffer to transmit
  182. * @param  len           number of byte to send
  183. *
  184. */


  185. /* USER CODE END 4 */


wangjiahao88 发表于 2025-3-6 15:00 | 显示全部楼层
LSM6DS3;用的是硬件IIC吗?

评论

对的,硬件i2c fast模式  发表于 2025-3-7 12:01
对的,硬件i2c fast模式  发表于 2025-3-7 11:59
wangjiahao88 发表于 2025-3-6 15:01 | 显示全部楼层
看你的程序,没有使用中断方法读写吗?没有开启中断么?

评论

我看有很多开发者的代码也有提到中断,请问是要在IO给出中断信号之后再采集吗?十分感谢  发表于 2025-3-7 11:58
没有,只用了轮询SPI  发表于 2025-3-6 16:24
laocuo1142 发表于 2025-3-7 10:29 | 显示全部楼层
DEBUG时发现卡在了I2C的timeOUT函数中意味着I2C通信没有正常进行。
powerantone 发表于 2025-3-7 11:20 | 显示全部楼层
检查程序逻辑是否存在问题
flycamelaaa 发表于 2025-3-7 12:00 | 显示全部楼层
可能是由于I2C总线上的信号质量不佳、时序不匹配或地址冲突等问题导致的。
stormwind123 发表于 2025-3-7 15:03 | 显示全部楼层
可以尝试修改复位相关的代码或重新配置复位引脚。
probedog 发表于 2025-3-7 16:03 | 显示全部楼层
验证I2C的时序设置是否与LSM6DS3的要求相匹配。如果不匹配,需要调整STM32C0的I2C时序设置。
classroom 发表于 2025-3-7 17:23 | 显示全部楼层
检查I2C地址是否正确设置,避免地址冲突导致的通信失败。
wangjiahao88 发表于 2025-3-25 13:47 | 显示全部楼层
是这样的,硬件 IIC中断,I2C_GET_TIMEOUT_FLAG就要清除中断;
wangjiahao88 发表于 2025-3-25 13:49 | 显示全部楼层
I2C_MasterRx;I2C_MasterTx里面看一下;0x08,18,28,等等,就不要管他。f8,58 都有可能出现,这种情况,只管去读数据就可以了!
过期的塔头 发表于 2025-3-31 23:06 | 显示全部楼层
程序在等待某个操作完成时超时了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

75

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部