Files
QDAC-firmware/libraries/zf_device/zf_device_dl1b.c

218 lines
8.5 KiB
C
Raw Permalink Normal View History

2023-12-11 21:45:06 +08:00
/*********************************************************************************************************************
* MM32F527X-E9P Opensourec Library MM32F527X-E9P SDK
* Copyright (c) 2022 SEEKFREE
*
* MM32F527X-E9P
*
* MM32F527X-E9P
* GPLGNU General Public License GNU通用公共许可证
* GPL 3 GPL3.0/
*
*
*
* GPL
*
* GPL
* <https://www.gnu.org/licenses/>
*
*
* 使 GPL3.0
* libraries/doc GPL3_permission_statement.txt
* libraries LICENSE
* 使
*
* zf_device_dl1b
*
* libraries/doc version
* MounRiver Studio V1.8.1
* CH32V307VCT6
* https://seekfree.taobao.com/
*
*
*
* 2022-08-10 Teternal first version
********************************************************************************************************************/
/*********************************************************************************************************************
* 线
* ------------------------------------
*
* SCL zf_device_dl1b.h DL1B_SCL_PIN
* SDA zf_device_dl1b.h DL1B_SDA_PIN
* XS zf_device_dl1b.h DL1B_XS_PIN
* VCC 5V
* GND
* ------------------------------------
********************************************************************************************************************/
#include "zf_common_debug.h"
#include "zf_driver_delay.h"
#include "zf_driver_exti.h"
#include "zf_driver_soft_iic.h"
#include "zf_device_dl1b.h"
#include "zf_device_config.h"
#include "zf_device_type.h"
static uint8 dl1b_init_flag = 0;
uint8 dl1b_finsh_flag = 0;
uint16 dl1b_distance_mm = 8192;
#if DL1B_USE_SOFT_IIC
static soft_iic_info_struct dl1b_iic_struct;
#define dl1b_transfer_8bit_array(tdata, tlen, rdata, rlen) (soft_iic_transfer_8bit_array(&dl1b_iic_struct, (tdata), (tlen), (rdata), (rlen)))
#else
#define dl1b_transfer_8bit_array(tdata, tlen, rdata, rlen) (iic_transfer_8bit_array(DL1B_IIC, DL1B_DEV_ADDR, (tdata), (tlen), (rdata), (rlen)))
#endif
//-------------------------------------------------------------------------------------------------------------------
// 函数简介 返回以毫米为单位的范围读数
// 参数说明 void
// 返回参数 void
// 使用示例 dl1b_get_distance();
// 备注信息 在开始单次射程测量后也调用此函数
//-------------------------------------------------------------------------------------------------------------------
void dl1b_get_distance (void)
{
if(dl1b_init_flag)
{
uint8 data_buffer[3];
int16 dl1b_distance_temp = 0;
data_buffer[0] = DL1B_GPIO__TIO_HV_STATUS >> 8;
data_buffer[1] = DL1B_GPIO__TIO_HV_STATUS & 0xFF;
dl1b_transfer_8bit_array(data_buffer, 2, &data_buffer[2], 1);
if(data_buffer[2])
{
data_buffer[0] = DL1B_SYSTEM__INTERRUPT_CLEAR >> 8;
data_buffer[1] = DL1B_SYSTEM__INTERRUPT_CLEAR & 0xFF;
data_buffer[2] = 0x01;
dl1b_transfer_8bit_array(data_buffer, 3, data_buffer, 0);// clear Interrupt
data_buffer[0] = DL1B_RESULT__RANGE_STATUS >> 8;
data_buffer[1] = DL1B_RESULT__RANGE_STATUS & 0xFF;
dl1b_transfer_8bit_array(data_buffer, 2, &data_buffer[2], 1);
if(0x89 == data_buffer[2])
{
data_buffer[0] = DL1B_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 >> 8;
data_buffer[1] = DL1B_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 & 0xFF;
dl1b_transfer_8bit_array(data_buffer, 2, data_buffer, 2);
dl1b_distance_temp = data_buffer[0];
dl1b_distance_temp = (dl1b_distance_temp << 8) | data_buffer[1];
if(dl1b_distance_temp > 4000 || dl1b_distance_temp < 0)
{
dl1b_distance_mm = 8192;
dl1b_finsh_flag = 0;
}
else
{
dl1b_distance_mm = dl1b_distance_temp;
dl1b_finsh_flag = 1;
}
}
else
{
dl1b_distance_mm = 8192;
dl1b_finsh_flag = 0;
}
}
else
{
dl1b_distance_mm = 8192;
dl1b_finsh_flag = 0;
}
}
}
//-------------------------------------------------------------------------------------------------------------------
// 函数简介 DL1B INT 中断响应处理函数
// 参数说明 void
// 返回参数 void
// 使用示例 dl1b_int_handler();
// 备注信息 本函数需要在 DL1B_INT_PIN 对应的外部中断处理函数中调用
//-------------------------------------------------------------------------------------------------------------------
void dl1b_int_handler (void)
{
#if DL1B_INT_ENABLE
dl1b_get_distance();
#endif
}
//-------------------------------------------------------------------------------------------------------------------
// 函数简介 初始化 DL1B
// 参数说明 void
// 返回参数 uint8 1-初始化失败 0-初始化成功
// 使用示例 dl1b_init();
// 备注信息
//-------------------------------------------------------------------------------------------------------------------
uint8 dl1b_init (void)
{
uint8 return_state = 0;
uint8 data_buffer[2 + sizeof(dl1b_config_file)];
uint16 time_out_count = 0;
#if DL1B_USE_SOFT_IIC
soft_iic_init(&dl1b_iic_struct, DL1B_DEV_ADDR, DL1B_SOFT_IIC_DELAY, DL1B_SCL_PIN, DL1B_SDA_PIN);
#else
iic_init(DL1B_IIC, DL1B_DEV_ADDR, DL1B_IIC_SPEED, DL1B_SCL_PIN, DL1B_SDA_PIN);
#endif
gpio_init(DL1B_XS_PIN, GPO, GPIO_HIGH, GPO_PUSH_PULL);
do
{
system_delay_ms(50);
gpio_low(DL1B_XS_PIN);
system_delay_ms(10);
gpio_high(DL1B_XS_PIN);
system_delay_ms(50);
data_buffer[0] = DL1B_FIRMWARE__SYSTEM_STATUS >> 8;
data_buffer[1] = DL1B_FIRMWARE__SYSTEM_STATUS & 0xFF;
dl1b_transfer_8bit_array(data_buffer, 2, &data_buffer[2], 1);
return_state = (0x01 == (data_buffer[2] & 0x01)) ? (0) : (1);
if(1 == return_state)
{
break;
}
data_buffer[0] = DL1B_I2C_SLAVE__DEVICE_ADDRESS >> 8;
data_buffer[1] = DL1B_I2C_SLAVE__DEVICE_ADDRESS & 0xFF;
memcpy(&data_buffer[2], (uint8 *)dl1b_config_file, sizeof(dl1b_config_file));
dl1b_transfer_8bit_array(data_buffer, 2 + sizeof(dl1b_config_file), data_buffer, 0);
while(1)
{
data_buffer[0] = DL1B_GPIO__TIO_HV_STATUS >> 8;
data_buffer[1] = DL1B_GPIO__TIO_HV_STATUS & 0xFF;
dl1b_transfer_8bit_array(data_buffer, 2, &data_buffer[2], 1);
if(0x00 == (data_buffer[2] & 0x01))
{
time_out_count = 0;
break;
}
if(DL1B_TIMEOUT_COUNT < time_out_count ++)
{
return_state = 1;
break;
}
system_delay_ms(1);
}
dl1b_init_flag = 1;
#if DL1B_INT_ENABLE
exti_init(DL1B_INT_PIN, EXTI_TRIGGER_FALLING);
dl1b_int_handler();
dl1b_finsh_flag = 0;
#endif
set_tof_type(TOF_DL1B, dl1b_int_handler);
}while(0);
return return_state;
}