This repository has been archived on 2025-07-31. You can view files and clone it, but cannot push or open issues or pull requests.
SP713_Upper_C/drivers/spi/drv_spi.cpp
2025-07-17 17:54:51 +08:00

103 lines
3.1 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "drv_spi.h"
#include <QDebug> // 用于调试输出
DRV_Spi::DRV_Spi(QObject *parent)
: QObject(parent) // 假设有8个通道
{
}
DRV_Spi::~DRV_Spi()
{
}
//枚举当前的所有SPI设备返回搜索到的设备数目
ULONG DRV_Spi::spi_enumDevice()
{
ULONG i;
mDeviceInforS DevInfor = {0};
UCHAR iDriverVer = 0;
UCHAR iDLLVer = 0;
UCHAR ibcdDevice = 0;
UCHAR iChipType = 0;
// 当前设备数目清零
this->m_DRV_Spi_Infors.ulDevCnt = 0;
// 依次搜索设备
for(i=0;i<16;i++)
{
// 打开设备
if(CH347OpenDevice(i) != INVALID_HANDLE_VALUE)
{
// 获取设备信息
CH347GetDeviceInfor(i,&DevInfor);
// 输出设备信息
DbgPrint("Manufacturer:%s Product:%s.", DevInfor.ManufacturerString, DevInfor.ProductString);
if(DevInfor.ChipMode == 3) //模式3此接口为JTAG/I2C
continue;
// 格式化设备名称
sprintf(this->m_DRV_Spi_Infors.bDeviceName, "%d# %s",i,DevInfor.FuncDescStr);
// 输出设备名称
DbgPrint("Dev Name:%s.", this->m_DRV_Spi_Infors.bDeviceName);
// copy设备信息至SpiI2cDevInfor结构体
memcpy(&this->m_DRV_Spi_Infors.SpiI2cDevInfor[this->m_DRV_Spi_Infors.ulDevCnt], &DevInfor,sizeof(DevInfor));
// 设备数量自增
this->m_DRV_Spi_Infors.ulDevCnt++;
// 获取版本号
CH347GetVersion(i, &iDriverVer, &iDLLVer, &ibcdDevice, &iChipType);
// 输出版本号
DbgPrint("Version: DV:%02x DLLV:%02x BCD:%02x CT:%02x.", iDriverVer, iDLLVer, ibcdDevice, iChipType);
}
// 关闭设备
CH347CloseDevice(i);
}
return this->m_DRV_Spi_Infors.ulDevCnt;
}
//打开设备
bool DRV_Spi::spi_openDevice(ULONG SpiI2cGpioDevIndex)
{
// 获取所选的设备序号
if((SpiI2cGpioDevIndex < 0) || (SpiI2cGpioDevIndex > this->m_DRV_Spi_Infors.ulDevCnt))
{
DbgPrint("device index error!");
return false;
}
// 打开设备
this->m_DRV_Spi_Infors.devIsOpened = (CH347OpenDevice(SpiI2cGpioDevIndex) != INVALID_HANDLE_VALUE);
// 设置USB数据读写超时时间
CH347SetTimeout(SpiI2cGpioDevIndex,500,500);
// 输出调试信息
DbgPrint("Open the device...%s",this->m_DRV_Spi_Infors.devIsOpened?"Success":"Failed");
if(!this->m_DRV_Spi_Infors.devIsOpened)
return false;
// 设置当前打开设备的索引号
this->m_DRV_Spi_Infors.opendDevIndex = SpiI2cGpioDevIndex;
return true;
// CH347InitSpi();
}
//关闭设备
bool DRV_Spi::spi_closeDevice()
{
// 判断当前设备是否开启
if(!this->m_DRV_Spi_Infors.devIsOpened)
{
DbgPrint("The Device not open!");
return false;
}
// 关闭当前正在开启的设备
CH347CloseDevice(this->m_DRV_Spi_Infors.opendDevIndex);
// 设置设备打开标志位为FALSE
this->m_DRV_Spi_Infors.devIsOpened = FALSE;
// 输出信息
DbgPrint("Close the Device!");
return TRUE;
}