O7/TOF/App/Example_4_ultra_low_power.c

181 lines
5.7 KiB
C

/*
Copyright (c) 2021, STMicroelectronics - All Rights Reserved
This file : part of VL53L1X ULP and : dual licensed,
either 'STMicroelectronics
Proprietary license'
or 'BSD 3-clause "New" or "Revised" License' , at your option.
*******************************************************************************
'STMicroelectronics Proprietary license'
*******************************************************************************
License terms: STMicroelectronics Proprietary in accordance with licensing
terms at www.st.com/sla0081
STMicroelectronics confidential
Reproduction and Communication of this document : strictly prohibited unless
specifically authorized in writing by STMicroelectronics.
*******************************************************************************
Alternatively, VL53L1X ULP may be distributed under the terms of
'BSD 3-clause "New" or "Revised" License', in which case the following
provisions apply instead of the ones mentioned above :
*******************************************************************************
License terms: BSD 3-clause "New" or "Revised" License.
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 the copyright holder 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.
*******************************************************************************
*/
/****************************************/
/* VL53L1X Low Power basic ranging */
/****************************************/
/*
* This example shows how to use the VL53L1X Ultra Low Power driver with the
* lowest possible power consumption.
* The example initializes the VL53L1X driver, configure the sensor and
* starts a ranging to capture 200 frames.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "../../STM32CubeIDE/Drivers/BSP/vl53l1x_uld/VL53L1X_ULP_api.h"
uint8_t IsInterruptDetected(uint16_t dev)
{
// To be filled with customer HW. This function should
// return 1 when an interrupt is raised by the ToF on GPIO1 pin (pin7)
}
uint8_t Example_4_ultra_low_power(void)
{
/*********************************/
/* VL53L1X ranging variables */
/*********************************/
uint8_t status, loop;
uint8_t dev;
uint16_t sensor_id;
/*********************************/
/* Customer platform */
/*********************************/
/* Default VL53L1X Ultra Low Power I2C address */
dev = 0x52;
/* (Optional) Change I2C address */
// status = VL53L1X_ULP_SetI2CAddress(dev, 0x20);
// dev = 0x20;
/*********************************/
/* Power on sensor and init */
/*********************************/
/* (Optional) Check if there is a VL53L1X sensor connected */
status = VL53L1X_ULP_GetSensorId(dev, &sensor_id);
if(status || (sensor_id != 0xEACC))
{
printf("VL53L1X not detected at requested address\n");
return status;
}
/* (Mandatory) Init VL53L1X sensor */
status = VL53L1X_ULP_SensorInit(dev);
if(status)
{
printf("VL53L1X ultra low power Loading failed\n");
return status;
}
printf("VL53L1X ultra low power ready !\n");
/*********************************/
/* Sensor configuration */
/*********************************/
/* (Optional) Program sensor to raise an interrupt ONLY below 300mm */
status = VL53L1X_ULP_SetInterruptConfiguration(dev, 300, 1);
/* (Optional) Program a 10Hz ranging frequency */
status = VL53L1X_ULP_SetInterMeasurementInMs(dev, 100);
/* Reduce the macro timing to minimum. This is equivalent as reducing the integration time */
status = VL53L1X_ULP_SetMacroTiming(dev, 1);
/* Reduce at maximum the SPADS */
status = VL53L1X_ULP_SetROI(dev, 4);
/*********************************/
/* Ranging loop */
/*********************************/
status = VL53L1X_ULP_StartRanging(dev);
if(status)
{
printf("VL53L1X_ULP_StartRanging failed with status %u\n", status);
return status;
}
printf("Ranging started. Put your hand close to the sensor to generate an interrupt...\n");
loop = 0;
while(loop < 200)
{
/* Use this external function to detect when a hardware interrupt is
* generated on PIN 7 (GPIO1). It means that a new measurement is ready.
*/
if(IsInterruptDetected(dev))
{
/* (Mandatory) Clear HW interrupt to restart measurements */
VL53L1X_ULP_ClearInterrupt(dev);
printf("Target detected! Interrupt raised by sensor\n");
loop++;
}
}
status = VL53L1X_ULP_StopRanging(dev);
printf("End of VL53L1X ultra low power demo\n");
return status;
}