|
|
/*
|
|
|
******************************************************************************
|
|
|
* @file driver_ST7735S.c
|
|
|
* @author <20>Ϻ<EFBFBD><CFBA><EFBFBD><EFBFBD><EFBFBD>
|
|
|
* @version V1.0.0
|
|
|
* @date 2023
|
|
|
* @brief ST7735S driver.
|
|
|
* This file provides firmware functions to manage gc9c01
|
|
|
* with SPI and DMA. User should set GPIO, dma channel, spi
|
|
|
* channel according actual resource allocation.
|
|
|
******************************************************************************
|
|
|
* @attention
|
|
|
*
|
|
|
* Copyright (c) 2023 Bixdo.
|
|
|
* All rights reserved.
|
|
|
******************************************************************************
|
|
|
*/
|
|
|
#include "ALLinclude.h"
|
|
|
|
|
|
#include "vl53l0x_i2c.h"
|
|
|
#include "FunctionPressure.h"
|
|
|
#include "PMDS_Fx.h"
|
|
|
#include "IIC.h"
|
|
|
|
|
|
static void delay_us(uint32_t delay_data)
|
|
|
{
|
|
|
uint8_t delay_data_;
|
|
|
delay_data_= delay_data;
|
|
|
while(delay_data_)
|
|
|
{
|
|
|
delay_data_--;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void VL53L0X_i2c_init(void)
|
|
|
{
|
|
|
VL_SDA_OUT();
|
|
|
VL_IIC_SCL();
|
|
|
// GPIO_SetBits(GPIOD, GPIO_Pin_1|GPIO_Pin_2);//PD1,PD2 OUT HIGHT
|
|
|
HAL_GPIO_WritePin(P_PORT_SDA,P_PIN_SDA, GPIO_PIN_SET);
|
|
|
HAL_GPIO_WritePin(P_PORT_SCL,P_PIN_SCL, GPIO_PIN_SET);
|
|
|
}
|
|
|
|
|
|
void VL_SDA_IN(void) //PD1 SDA
|
|
|
{
|
|
|
#if MCU_STM32G030 ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_PY32F002A ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_STM32G431 ==1
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void VL_SDA_OUT(void) //PD1 SDA
|
|
|
{
|
|
|
#if MCU_STM32G030 ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_PY32F002A ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_STM32G431 ==1
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SDA;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
HAL_GPIO_Init(P_PORT_SDA, &GPIO_InitStruct);
|
|
|
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void VL_IIC_SCL(void) //PD2 SCL
|
|
|
{
|
|
|
|
|
|
#if MCU_STM32G030 ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
#if MCU_PY32F002A ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_STM32G431 ==1
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
void VL_IIC_SCL_IN(void)
|
|
|
{
|
|
|
|
|
|
#if MCU_STM32G030 ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
#if MCU_PY32F002A ==1
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
#endif
|
|
|
|
|
|
#if MCU_STM32G431 ==1
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
GPIO_InitStruct.Pin = P_PIN_SCL;
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
HAL_GPIO_Init(P_PORT_SCL, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>IIC<49><43>ʼ<EFBFBD>ź<EFBFBD>
|
|
|
void VL_IIC_Start(void)
|
|
|
{
|
|
|
VL_SDA_OUT();//sda<64><61><EFBFBD><EFBFBD><EFBFBD>
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(P_PORT_SDA,P_PIN_SDA, GPIO_PIN_SET);
|
|
|
HAL_GPIO_WritePin(P_PORT_SCL,P_PIN_SCL, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,1);//VL_IIC_SDA=1;
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
|
|
|
delay_us(140);
|
|
|
HAL_GPIO_WritePin(P_PORT_SDA,P_PIN_SDA, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,0);//VL_IIC_SDA=0;//START:when CLK is high,DATA change form high to low
|
|
|
delay_us(140);
|
|
|
HAL_GPIO_WritePin(P_PORT_SCL,P_PIN_SCL, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;//ǯסI2C<32><43><EFBFBD>ߣ<EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>IICֹͣ<CDA3>ź<EFBFBD>
|
|
|
void VL_IIC_Stop(void)
|
|
|
{
|
|
|
VL_SDA_OUT();//sda<64><61><EFBFBD><EFBFBD><EFBFBD>
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(P_PORT_SDA,P_PIN_SDA, GPIO_PIN_RESET);
|
|
|
HAL_GPIO_WritePin(P_PORT_SCL,P_PIN_SCL, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,0);//VL_IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
|
|
|
delay_us(140);
|
|
|
HAL_GPIO_WritePin(P_PORT_SCL,P_PIN_SCL, GPIO_PIN_SET);
|
|
|
delay_us(140);
|
|
|
HAL_GPIO_WritePin(P_PORT_SDA,P_PIN_SDA, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,1);//VL_IIC_SDA=1;//<2F><><EFBFBD><EFBFBD>I2C<32><43><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ź<EFBFBD>
|
|
|
delay_us(140);
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F>ȴ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>źŵ<C5BA><C5B5><EFBFBD>
|
|
|
//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ʧ<EFBFBD><CAA7>
|
|
|
// 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ɹ<EFBFBD>
|
|
|
u8 VL_IIC_Wait_Ack(void)
|
|
|
{
|
|
|
u8 ucErrTime=0;
|
|
|
VL_SDA_IN(); //SDA<44><41><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(GPIO_SDA,GPIO_PIN_SDA, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,1);//VL_IIC_SDA=1;//
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
delay_us(120);
|
|
|
// while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1))
|
|
|
while(HAL_GPIO_ReadPin(GPIO_SDA,GPIO_PIN_SDA))
|
|
|
{
|
|
|
ucErrTime++;
|
|
|
if(ucErrTime>250)
|
|
|
{
|
|
|
VL_IIC_Stop();
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;//ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ACKӦ<4B><D3A6>
|
|
|
void VL_IIC_Ack(void)
|
|
|
{
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
VL_SDA_OUT();
|
|
|
HAL_GPIO_WritePin(GPIO_SDA,GPIO_PIN_SDA, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,0);//VL_IIC_SDA=0;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
#endif
|
|
|
|
|
|
#if MCU_STM32G431 ==1
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<4B><D3A6>
|
|
|
void VL_IIC_NAck(void)
|
|
|
{
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
VL_SDA_OUT();
|
|
|
HAL_GPIO_WritePin(GPIO_SDA,GPIO_PIN_SDA, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,1);//VL_IIC_SDA=1;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
//IIC<49><43><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
|
|
|
//<2F><><EFBFBD>شӻ<D8B4><D3BB><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|
|
//1<><31><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|
|
//0<><30><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|
|
void VL_IIC_Send_Byte(uint8_t txd)
|
|
|
{
|
|
|
uint8_t t;
|
|
|
VL_SDA_OUT();
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>ӿ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
|
|
|
for(t=0;t<8;t++)
|
|
|
{
|
|
|
if((txd&0x80)>>7)
|
|
|
HAL_GPIO_WritePin(GPIO_SDA,GPIO_PIN_SDA, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,1);// VL_IIC_SDA=1;
|
|
|
else
|
|
|
HAL_GPIO_WritePin(GPIO_SDA,GPIO_PIN_SDA, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_1,0);// VL_IIC_SDA=0;
|
|
|
txd<<=1;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
delay_us(120);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
delay_us(120);
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F><>1<EFBFBD><31><EFBFBD>ֽڣ<D6BD>ack=1ʱ<31><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK<43><4B>ack=0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
|
|
|
uint8_t VL_IIC_Read_Byte(unsigned char ack)
|
|
|
{
|
|
|
unsigned char i,receive=0;
|
|
|
VL_SDA_IN();//SDA<44><41><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|
|
#if MCU_STM32G030 ==1 || MCU_PY32F002A ==1 || MCU_STM32G431 ==1
|
|
|
for(i=0;i<8;i++ )
|
|
|
{
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_RESET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,0);//VL_IIC_SCL=0;
|
|
|
delay_us(140);
|
|
|
HAL_GPIO_WritePin(GPIO_CLK,GPIO_PIN_CLK, GPIO_PIN_SET);
|
|
|
// GPIO_WriteBit(GPIOD,GPIO_Pin_2,1);//VL_IIC_SCL=1;
|
|
|
receive<<=1;
|
|
|
if(HAL_GPIO_ReadPin(GPIO_SDA,GPIO_PIN_SDA))receive++;
|
|
|
// if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1))receive++;
|
|
|
delay_us(140); //1
|
|
|
}
|
|
|
if (!ack)
|
|
|
VL_IIC_NAck();//<2F><><EFBFBD><EFBFBD>nACK
|
|
|
else
|
|
|
VL_IIC_Ack(); //<2F><><EFBFBD><EFBFBD>ACK
|
|
|
#endif
|
|
|
return receive;
|
|
|
}
|
|
|
|
|
|
//IICдһ<D0B4><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
uint8_t VL_IIC_Write_1Byte(uint8_t SlaveAddress, uint8_t REG_Address,uint8_t REG_data)
|
|
|
{
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress);
|
|
|
if(VL_IIC_Wait_Ack())
|
|
|
{
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 1;//ûӦ<C3BB><D3A6><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
|
|
|
}
|
|
|
VL_IIC_Send_Byte(REG_Address);
|
|
|
VL_IIC_Wait_Ack();
|
|
|
VL_IIC_Send_Byte(REG_data);
|
|
|
VL_IIC_Wait_Ack();
|
|
|
VL_IIC_Stop();
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//IIC<49><43>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
uint8_t VL_IIC_Read_1Byte(uint8_t SlaveAddress, uint8_t REG_Address,uint8_t *REG_data)
|
|
|
{
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress);//<2F><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
|
|
if(VL_IIC_Wait_Ack())
|
|
|
{
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 1;//ûӦ<C3BB><D3A6><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
}
|
|
|
VL_IIC_Send_Byte(REG_Address);
|
|
|
VL_IIC_Wait_Ack();
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress|0x01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
VL_IIC_Wait_Ack();
|
|
|
*REG_data = VL_IIC_Read_Byte(0);
|
|
|
VL_IIC_Stop();
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//IICдn<D0B4>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
uint8_t VL_IIC_Write_nByte(uint8_t SlaveAddress, uint8_t REG_Address,u16 len, uint8_t *buf)
|
|
|
{
|
|
|
//#ifdef iic_soft
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress);//<2F><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
|
|
if(VL_IIC_Wait_Ack())
|
|
|
{
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 1;//ûӦ<C3BB><D3A6><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
}
|
|
|
VL_IIC_Send_Byte(REG_Address);
|
|
|
VL_IIC_Wait_Ack();
|
|
|
while(len--)
|
|
|
{
|
|
|
VL_IIC_Send_Byte(*buf++);//<2F><><EFBFBD><EFBFBD>buff<66><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
VL_IIC_Wait_Ack();
|
|
|
}
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 0;
|
|
|
//#endif
|
|
|
|
|
|
#ifdef iic_hard_ware
|
|
|
HAL_StatusTypeDef ret = HAL_ERROR;
|
|
|
ret=I2C_Write(SlaveAddress,REG_Address,buf,len);
|
|
|
return ret;
|
|
|
#endif
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//IIC<49><43>n<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
uint8_t VL_IIC_Read_nByte(uint8_t SlaveAddress, uint8_t REG_Address,uint8_t len,uint8_t *buf)
|
|
|
{
|
|
|
uint8_t SlaveAddress_;
|
|
|
#if IICAddress_7BIT ==1
|
|
|
SlaveAddress_=SlaveAddress<<1;
|
|
|
#endif
|
|
|
#if IICAddress_8BIT ==1
|
|
|
SlaveAddress_=SlaveAddress;
|
|
|
#endif
|
|
|
//#ifdef iic_soft
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress_);//<2F><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
|
|
if(VL_IIC_Wait_Ack())
|
|
|
{
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 1;//ûӦ<C3BB><D3A6><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
}
|
|
|
VL_IIC_Send_Byte(REG_Address);
|
|
|
VL_IIC_Wait_Ack();
|
|
|
|
|
|
VL_IIC_Start();
|
|
|
VL_IIC_Send_Byte(SlaveAddress_|0x01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
VL_IIC_Wait_Ack();
|
|
|
while(len)
|
|
|
{
|
|
|
if(len==1)
|
|
|
{
|
|
|
*buf = VL_IIC_Read_Byte(0);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
*buf = VL_IIC_Read_Byte(1);
|
|
|
}
|
|
|
buf++;
|
|
|
len--;
|
|
|
}
|
|
|
VL_IIC_Stop();//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
return 0;
|
|
|
//#endif
|
|
|
|
|
|
#ifdef iic_hard_ware
|
|
|
HAL_StatusTypeDef ret = HAL_ERROR;
|
|
|
ret=I2C_Read(SlaveAddress,®_Address,1,buf,(uint8_t)(len));
|
|
|
return ret;
|
|
|
#endif
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//VL53L0X д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//pdata:<3A><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
|
|
//count:<3A><><EFBFBD><EFBFBD> <20><><EFBFBD>65535
|
|
|
uint8_t VL53L0X_write_multi(uint8_t address, uint8_t index,uint8_t *pdata,uint16_t count)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
if(VL_IIC_Write_nByte(address,index,count,pdata))
|
|
|
{
|
|
|
status = STATUS_FAIL;
|
|
|
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
|
|
|
//VL53L0X <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//pdata:<3A><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
|
|
//count:<3A><><EFBFBD><EFBFBD> <20><><EFBFBD>65535
|
|
|
uint8_t VL53L0X_read_multi(uint8_t address,uint8_t index,uint8_t *pdata,uint16_t count)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
if(VL_IIC_Read_nByte(address,index,count,pdata))
|
|
|
{
|
|
|
status = STATUS_FAIL;
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
//VL53L0X д1<D0B4><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(8λ)
|
|
|
uint8_t VL53L0X_write_byte(uint8_t address,uint8_t index,uint8_t data)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
status = VL53L0X_write_multi(address,index,&data,1);
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
//VL53L0X д1<D0B4><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(˫<>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(16λ)
|
|
|
uint8_t VL53L0X_write_word(uint8_t address,uint8_t index,uint16_t data)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
uint8_t buffer[2];
|
|
|
|
|
|
//<2F><>16λ<36><CEBB><EFBFBD>ݲ<EFBFBD>ֳ<EFBFBD>8λ
|
|
|
buffer[0] = (uint8_t)(data>>8);//<2F>߰<EFBFBD>λ
|
|
|
buffer[1] = (uint8_t)(data&0xff);//<2F>Ͱ<EFBFBD>λ
|
|
|
|
|
|
if(index%2==1)
|
|
|
{
|
|
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD>Ų<EFBFBD><C5B2>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD>Է<EFBFBD>2<EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
status = VL53L0X_write_multi(address,index,&buffer[0],1);
|
|
|
status = VL53L0X_write_multi(address,index,&buffer[0],1);
|
|
|
}else
|
|
|
{
|
|
|
status = VL53L0X_write_multi(address,index,buffer,2);
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
//VL53L0X д1<D0B4><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(32λ)
|
|
|
uint8_t VL53L0X_write_dword(uint8_t address,uint8_t index,uint32_t data)
|
|
|
{
|
|
|
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
uint8_t buffer[4];
|
|
|
|
|
|
//<2F><>32λ<32><CEBB><EFBFBD>ݲ<EFBFBD>ֳ<EFBFBD>8λ
|
|
|
buffer[0] = (uint8_t)(data>>24);
|
|
|
buffer[1] = (uint8_t)((data&0xff0000)>>16);
|
|
|
buffer[2] = (uint8_t)((data&0xff00)>>8);
|
|
|
buffer[3] = (uint8_t)(data&0xff);
|
|
|
|
|
|
status = VL53L0X_write_multi(address,index,buffer,4);
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
//VL53L0X <20><>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(8λ)
|
|
|
uint8_t VL53L0X_read_byte(uint8_t address,uint8_t index,uint8_t *pdata)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
status = VL53L0X_read_multi(address,index,pdata,1);
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
//VL53L0X <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(˫<>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(16λ)
|
|
|
uint8_t VL53L0X_read_word(uint8_t address,uint8_t index,uint16_t *pdata)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
uint8_t buffer[2];
|
|
|
|
|
|
status = VL53L0X_read_multi(address,index,buffer,2);
|
|
|
|
|
|
*pdata = ((u16)buffer[0]<<8)+(u16)buffer[1];
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
//VL53L0X <20><>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>ֽ<EFBFBD>)
|
|
|
//address:<3A><>ַ
|
|
|
//index:ƫ<>Ƶ<EFBFBD>ַ
|
|
|
//data:<3A><><EFBFBD><EFBFBD>(32λ)
|
|
|
uint8_t VL53L0X_read_dword(uint8_t address,uint8_t index,uint32_t *pdata)
|
|
|
{
|
|
|
uint8_t status = STATUS_OK;
|
|
|
|
|
|
uint8_t buffer[4];
|
|
|
|
|
|
status = VL53L0X_read_multi(address,index,buffer,4);
|
|
|
|
|
|
*pdata = ((u32)buffer[0]<<24)+((u32)buffer[1]<<16)+((u32)buffer[2]<<8)+((u32)buffer[3]);
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/*********************************************END OF FILE*********************************************/ |