150 lines
5.2 KiB
C
150 lines
5.2 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.
|
|
*******************************************************************************
|
|
*/
|
|
|
|
#include "VL53L1X_ULP_platform.h"
|
|
|
|
extern I2C_HandleTypeDef hi2c2;
|
|
|
|
uint8_t VL53L1X_ULP_RdDWord(uint16_t dev, uint16_t RegisterAdress, uint32_t *value)
|
|
{
|
|
uint8_t status = 0;
|
|
uint8_t data_write[2];
|
|
uint8_t data_read[4];
|
|
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 2, 100);
|
|
status = HAL_I2C_Master_Receive(&hi2c2, dev, data_read, 4, 100);
|
|
*value = ((data_read[0] << 24) | (data_read[1] << 16) |
|
|
(data_read[2] << 8) | (data_read[3]));
|
|
return status;
|
|
}
|
|
|
|
uint8_t VL53L1X_ULP_RdWord(uint16_t dev, uint16_t RegisterAdress, uint16_t *value)
|
|
{
|
|
uint8_t status = 0;
|
|
uint8_t data_write[2];
|
|
uint8_t data_read[2];
|
|
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 2, 100);
|
|
status = HAL_I2C_Master_Receive(&hi2c2, dev, data_read, 2, 100);
|
|
*value = (data_read[0] << 8) | (data_read[1]);
|
|
return status;
|
|
}
|
|
|
|
uint8_t VL53L1X_ULP_RdByte(uint16_t dev, uint16_t RegisterAdress, uint8_t *value)
|
|
{
|
|
uint8_t status = 0;
|
|
uint8_t data_write[2];
|
|
uint8_t data_read[1];
|
|
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 2, 100);
|
|
status = HAL_I2C_Master_Receive(&hi2c2, dev, data_read, 1, 100);
|
|
*value = data_read[0];
|
|
return status;
|
|
}
|
|
|
|
uint8_t VL53L1X_ULP_WrByte(uint16_t dev, uint16_t RegisterAdress, uint8_t value)
|
|
{
|
|
uint8_t data_write[3];
|
|
uint8_t status = 0;
|
|
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
data_write[2] = value & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 3, 100);
|
|
return status;
|
|
}
|
|
|
|
uint8_t VL53L1X_ULP_WrWord(uint16_t dev, uint16_t RegisterAdress, uint16_t value)
|
|
{
|
|
uint8_t data_write[4];
|
|
uint8_t status = 0;
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
data_write[2] = (value >> 8) & 0xFF;
|
|
data_write[3] = value & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 4, 100);
|
|
return status;
|
|
}
|
|
|
|
uint8_t VL53L1X_ULP_WrDWord(uint16_t dev, uint16_t RegisterAdress, uint32_t value)
|
|
{
|
|
uint8_t data_write[6];
|
|
uint8_t status = 0;
|
|
|
|
data_write[0] = (RegisterAdress >> 8) & 0xFF;
|
|
data_write[1] = RegisterAdress & 0xFF;
|
|
data_write[2] = (value >> 24) & 0xFF;
|
|
data_write[3] = (value >> 16) & 0xFF;
|
|
data_write[4] = (value >> 8) & 0xFF;
|
|
data_write[5] = value & 0xFF;
|
|
status = HAL_I2C_Master_Transmit(&hi2c2, dev, data_write, 6, 100);
|
|
return status;
|
|
}
|
|
|
|
void VL53L1X_ULP_WaitMs(uint32_t TimeMs)
|
|
{
|
|
HAL_Delay(TimeMs);
|
|
}
|