- 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 */