Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2017 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32xxx_hal.h"
- /* USER CODE BEGIN Includes */
- #include "main.h"
- #include "vl53l1_api.h"
- #include "X-NUCLEO-53L1A1.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- I2C_HandleTypeDef hi2c1;
- UART_HandleTypeDef huart2;
- VL53L1_Dev_t devCenter;
- VL53L1_Dev_t devLeft;
- VL53L1_Dev_t devRight;
- VL53L1_DEV Dev = &devCenter;
- uint32_t status;
- volatile int IntCount;
- #define isAutonomousExample 1 /* Allow to select either autonomous ranging or fast ranging example */
- #define isInterrupt 0 /* If isInterrupt = 1 then device working in interrupt mode, else device working in polling mode */
- uint32_t tabResults [3];
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handler(void);
- static void MX_GPIO_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_I2C1_Init(void);
- void AutonomousLowPowerRangingTest(void); /* see Autonomous ranging example implementation in USER CODE BEGIN 4 section */
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- PUTCHAR_PROTOTYPE
- {
- HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, 0xFFFF);
- return ch;
- }
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- if (GPIO_Pin==VL53L1X_INT_Pin)
- {
- IntCount++;
- }
- }
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint16_t wordData;
- uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
- static VL53L1_RangingMeasurementData_t RangingData;
- uint8_t newI2C = 0x52;
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART2_UART_Init();
- MX_I2C1_Init();
- XNUCLEO53L1A1_Init();
- /* USER CODE END 1 */
- /* USER CODE BEGIN 2 */
- /* An example here below shows how to manage multi-sensor operation.
- In this example the sensors range sequentially. Several sensors range simultanously is also possible */
- /* Reset the 3 ToF sensors on the expansion board */
- for (ToFSensor=0;ToFSensor<3;ToFSensor++){
- status = XNUCLEO53L1A1_ResetId(ToFSensor, 0);
- }
- /* Bring the sensors out of the reset stage one by one and set the new I2C address */
- for (ToFSensor=0;ToFSensor<3;ToFSensor++){
- switch(ToFSensor){
- case 0:
- Dev=&devLeft;
- break;
- case 1:
- Dev=&devCenter;
- break;
- case 2:
- Dev=&devRight;
- break;
- }
- status = XNUCLEO53L1A1_ResetId(ToFSensor, 1);
- Dev->comms_speed_khz = 400;
- Dev->I2cHandle = &hi2c1;
- Dev->comms_type = 1;
- Dev->I2cDevAddr=0x52; /* default ToF sensor I2C address*/
- VL53L1_RdWord(Dev, 0x010F, &wordData);
- printf("VL53L1X: %02X\n\r", wordData);
- newI2C = Dev->I2cDevAddr + (ToFSensor+1)*2;
- status = VL53L1_SetDeviceAddress(Dev, newI2C);
- Dev->I2cDevAddr=newI2C;
- VL53L1_RdWord(Dev, 0x010F, &wordData);
- printf("VL53L1X: %02X\n\r", wordData);
- /* Device Initialization and setting */
- status = VL53L1_WaitDeviceBooted(Dev);
- status = VL53L1_DataInit(Dev);
- status = VL53L1_StaticInit(Dev);
- status = VL53L1_SetDistanceMode(Dev, VL53L1_DISTANCEMODE_LONG);
- status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(Dev, 50000);
- status = VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev, 100);
- }
- /* Sequential ranging loop */
- while(1){
- for (ToFSensor=0;ToFSensor<3;ToFSensor++){
- switch(ToFSensor){
- case 0:
- Dev=&devLeft;
- break;
- case 1:
- Dev=&devCenter;
- break;
- case 2:
- Dev=&devRight;
- break;
- }
- status = VL53L1_StartMeasurement(Dev);
- status = VL53L1_WaitMeasurementDataReady(Dev);
- if(!status)
- {
- status = VL53L1_GetRangingMeasurementData(Dev, &RangingData);
- }
- tabResults[ToFSensor] = RangingData.RangeMilliMeter;
- status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
- }
- printf("%u\t%u\t%u\n", tabResults[0], tabResults[1], tabResults[2]);
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- #ifdef STM32F401xE
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 16;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- #endif
- #ifdef STM32L476xx
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 10;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
- PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
- PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the main internal regulator output voltage
- */
- if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- #endif
- #ifdef STM32F401xE
- /* I2C1 init function */
- static void MX_I2C1_Init(void)
- {
- hi2c1.Instance = I2C1;
- hi2c1.Init.ClockSpeed = 100000;
- hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- #endif
- #ifdef STM32L476xx
- /* I2C1 init function */
- static void MX_I2C1_Init(void)
- {
- hi2c1.Instance = I2C1;
- hi2c1.Init.Timing = 0x10909CEC;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Analogue filter
- */
- if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Digital filter
- */
- if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
- {
- Error_Handler();
- }
- }
- #endif
- /* USART2 init function */
- static void MX_USART2_UART_Init(void)
- {
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : VL53L1X_INT_Pin */
- GPIO_InitStruct.Pin = VL53L1X_INT_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(VL53L1X_INT_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI4_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /* Autonomous ranging loop*/
- void AutonomousLowPowerRangingTest(void)
- {
- static VL53L1_RangingMeasurementData_t RangingData;
- status = VL53L1_WaitDeviceBooted(Dev);
- status = VL53L1_DataInit(Dev);
- status = VL53L1_StaticInit(Dev);
- status = VL53L1_SetDistanceMode(Dev, VL53L1_DISTANCEMODE_LONG);
- status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(Dev, 50000);
- status = VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev, 500);
- status = VL53L1_StartMeasurement(Dev);
- if(status){
- printf("VL53L1_StartMeasurement failed \n");
- while(1);
- }
- if (isInterrupt){
- do // interrupt mode
- {
- __WFI();
- if(IntCount !=0 ){
- IntCount=0;
- status = VL53L1_GetRangingMeasurementData(Dev, &RangingData);
- if(status==0){
- printf("%d,%d,%.2f,%.2f\n", RangingData.RangeStatus,RangingData.RangeMilliMeter,
- RangingData.SignalRateRtnMegaCps/65536.0,RangingData.AmbientRateRtnMegaCps/65336.0);
- }
- status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
- }
- }
- while(1);
- }
- else{
- do // polling mode
- {
- status = VL53L1_WaitMeasurementDataReady(Dev);
- if(!status)
- {
- status = VL53L1_GetRangingMeasurementData(Dev, &RangingData);
- if(status==0){
- printf("%d,%d,%.2f,%.2f\n", RangingData.RangeStatus,RangingData.RangeMilliMeter,
- (RangingData.SignalRateRtnMegaCps/65536.0),RangingData.AmbientRateRtnMegaCps/65336.0);
- }
- status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
- }
- }
- while (1);
- }
- // return status;
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement