使用
CH347StreamSPI4函數(shù)與SPI從機(jī)通信時(shí)?偶爾會(huì)出現(xiàn)函數(shù)不返回阻塞導(dǎo)致進(jìn)程卡死的現(xiàn)象。 此時(shí)只有拔掉CH347的USB口才能解決卡死問(wèn)題(函數(shù)可直接返回錯(cuò)誤)
程序已經(jīng)在CH347OpenDevice后設(shè)置了超時(shí)
CH347SetTimeout(mDevIndex,500,500);
以下是源碼:
#include?"ch347.h"
#include?"CH347DLL.H"
#include?<QDebug>
#include?"QsLog.h"
#include?"mytime.h"
//pins?0-7??isRisingEdge?是否為上升沿觸發(fā)
bool?Ch347::gpioSetIRQCallback(uint8_t?pins,bool?isRisingEdge,mPCH347_INT_ROUTINE?call)
{
????uint8_t?Int0TripMode=0;
????if(isRisingEdge)
????{
????????Int0TripMode=1;
????}
????//?設(shè)定GPIO中斷服務(wù)程序
????bool?isSetIrq=CH347SetIntRoutine(mDevIndex,???????????//?指定設(shè)備序號(hào)
?????????????????????????????????????pins,??????????//?中斷0?GPIO引腳號(hào),大于7:不啟用此中斷源;?為0-7對(duì)應(yīng)gpio0-7
?????????????????????????????????????Int0TripMode,??????//?中斷0類型:?00:下降沿觸發(fā);?01:上升沿觸發(fā);?02:雙邊沿觸發(fā);?03:保留;
?????????????????????????????????????9,??????????//?中斷1?GPIO引腳號(hào),大于7則不啟用此中斷源,為0-7對(duì)應(yīng)gpio0-7
?????????????????????????????????????0,??????//?中斷1類型:?00:下降沿觸發(fā);?01:上升沿觸發(fā);?02:雙邊沿觸發(fā);?03:保留;
?????????????????????????????????????call);//?指定中斷服務(wù)程序,為NULL則取消中斷服務(wù),否則在中斷時(shí)調(diào)用該程序
????return?isSetIrq;
}
Ch347::Ch347(int?mode)
{
????mMode=mode;
????QLOG_DEBUG()<<"mMode="<<mMode;
????int?count=enumDevice();
????QLOG_DEBUG()<<"設(shè)備數(shù)量:"<<count;
????if(count<=0)
????{
????????QLOG_ERROR()<<"枚舉失敗";
????}
????for?(auto?it?=?qAsConst(mDevInfoMap).begin();?it?!=?qAsConst(mDevInfoMap).end();?++it)
????{
????????QString?str(it.value().FuncDescStr);
????????int?key=it.key();
????????QLOG_DEBUG()<<"枚舉設(shè)備?index:"<<key<<"信息:"<<str;
????}
????QLOG_DEBUG()<<"CH347?構(gòu)造完成";
}
Ch347::~Ch347()
{
????closeDev();
}
//枚舉設(shè)備
int?Ch347::enumDevice()
{
????ULONG?i,DevCnt?=?0;
????mDevIsOpened?=?(CH347OpenDevice(mDevIndex)?!=?INVALID_HANDLE_VALUE);
????QLOG_DEBUG()<<"devOpen:"<<mDevIsOpened;
????if(mDevIsOpened==false)
????{
????????QLOG_ERROR()<<"打開(kāi)CH347設(shè)備出錯(cuò)";
????}
????CH347SetTimeout(mDevIndex,500,500);
????mDeviceInforS?DevInfor;
????for(i=0;i<16;i++)
????{
????????if(CH347Uart_Open(i)?!=?INVALID_HANDLE_VALUE)
????????{
????????????CH347Uart_GetDeviceInfor(i,&DevInfor);
????????????mDevInfoMap[DevCnt]=DevInfor;
????????????DevCnt++;
????????}
????????CH347Uart_Close(i);
????}
????return?DevCnt;
}
void?Ch347::closeDev(void)
{
????if(mDevIsOpened)
????{
????????QLOG_DEBUG()<<"關(guān)閉CH347設(shè)備";
????????CH347CloseDevice(mDevIndex);
????????mDevIsOpened=false;
????}
}
bool?Ch347::SPITest()
{
????mSpiCfgS?readCfg?=?{0};
????bool?rec=CH347SPI_GetCfg(0,&readCfg);
????return?rec;
}
bool?Ch347::SPIOpen()
{
????bool?RetVal?=?FALSE;
????mSpiCfgS?SpiCfg?=?{0};
????UCHAR?SpiDatabits?=?0; //?設(shè)置的數(shù)據(jù)位?默認(rèn)0
????SpiCfg.iMode?=?(UCHAR)1;
????SpiCfg.iClock?=?(UCHAR)5;
????SpiCfg.iByteOrder?=?(UCHAR)1;
????SpiCfg.iSpiWriteReadInterval?=0;
????SpiCfg.iSpiOutDefaultData?=?0xFF;
????SpiCfg.iChipSelect?=?0;
????SpiCfg.iChipSelect?|=?0x80;
????SpiCfg.CS1Polarity?=?(UCHAR)0;
????SpiCfg.CS2Polarity?=?(UCHAR)0;
????SpiCfg.iIsAutoDeativeCS?=?0;
????SpiCfg.iActiveDelay?=?0;
????SpiCfg.iDelayDeactive?=0;
????//?設(shè)置SPI?數(shù)據(jù)位
????//SpiDatabits?=?(UCHAR)0;//SendDlgItemMessage(SpiI2cGpioDebugHwnd,IDC_SpiCfg_Databits,CB_GETCURSEL,0,0);
????RetVal?=?CH347SPI_SetDataBits(mDevIndex,?SpiDatabits);
????RetVal?=?CH347SPI_Init(mDevIndex,&SpiCfg);
????QLOG_DEBUG()<<"CH347SPI_Init?"<<RetVal;
????return?RetVal;
}
bool?Ch347::gpioGet(uint8_t?pins)
{
????bool?RetVal;
????uint8_t?iDir?=?0,iData?=?0;
????RetVal?=?CH347GPIO_Get(mDevIndex,&iDir,&iData);
????if(RetVal)
????{
????????mPinsDir=iDir;
????????mPinsVol=iData;
????????//顯示方向
????????uint8_t?Dir?=?(iDir&(1<<pins))?BST_CHECKED:BST_UNCHECKED;
????????//電平值
????????uint8_t?Sel?=?(iData&(1<<pins))?BST_CHECKED:BST_UNCHECKED;
????????bool?vol=(bool)Sel;
????????return?vol;
????}
????return?false;
}
bool?Ch347::gpioSet(uint8_t?pins,bool?isHigh,bool?isEnable)
{
????if(pins>7)
????{
????????return?false;
????}
????uint8_t?enable=(uint8_t)isEnable;
????uint8_t?dir=1;
????uint8_t?data=(uint8_t)isHigh;
????mPinsEnable|=(enable<<pins);
????mPinsDir|=(dir<<pins);
????mPinsVol|=(data<<pins);
????bool?rec=CH347GPIO_Set(mDevIndex,//?指定設(shè)備序號(hào)
??????????????????????mPinsEnable,//?數(shù)據(jù)有效標(biāo)志:對(duì)應(yīng)位0-7,對(duì)應(yīng)GPIO0-7.
??????????????????????mPinsDir,//?設(shè)置I/O方向,某位清0則對(duì)應(yīng)引腳為輸入,某位置1則對(duì)應(yīng)引腳為輸出.GPIO0-7對(duì)應(yīng)位0-7.
??????????????????????mPinsVol);??//?輸出數(shù)據(jù),如果I/O方向?yàn)檩敵?那么某位清0時(shí)對(duì)應(yīng)引腳輸出低電平,某位置1時(shí)對(duì)應(yīng)引腳輸出高電平
????return?rec;
}
bool?Ch347::SPIWrite(Bytes&?bytes)
{
????uint8_t?buf[1024*3]={0};
????int?len=bytes.length();
????memcpy(buf,bytes.toBuffer(),len);
????QLOG_DEBUG()<<"SPI?寫入:"<<bytes.toString();
????UCHAR?ChipSelect=0x80;
????//事實(shí)上單獨(dú)的SPI寫或讀函數(shù)是可以直接被?CH347SPI_WriteRead?函數(shù)替代使用的。
????//之所以提供單獨(dú)的?CH347SPI_Read?和?CH347SPI_Write?函數(shù)
????//是考慮到實(shí)際應(yīng)用有需要單向操作的場(chǎng)景,此類函數(shù)使用會(huì)更直觀些
????bool?rec=CH347SPI_Write(mDevIndex,ChipSelect,len,512,buf);
????if(rec==false)
????{
????????QLOG_ERROR()<<"SPI寫失敗";
????}
????return?rec;
}
bool?Ch347::SPIWriteRead(Bytes&?bytes)
{
????Mytime::delayMs(10);
????uint8_t?buf[1024*3]={0};
????int?len=bytes.length();
????memcpy(buf,bytes.toBuffer(),len);
????QLOG_DEBUG()<<"SPI?寫入:"<<bytes.toString();
????UCHAR?ChipSelect=0x80;
????//CH347StreamSPI4其API為適應(yīng)此前調(diào)用方式,功能與CH347SPI_WriteRead一致
????bool?rec=CH347StreamSPI4(mDevIndex,ChipSelect,len,buf);
????memcpy(bytes.toBuffer(),buf,len);
????QLOG_DEBUG()<<"SPI?讀出:"<<bytes.toString();
????if(rec==false)
????{
????????QLOG_ERROR()<<"SPI寫失敗";
????}
????return?rec;
}
bool?Ch347::IICOpen()
{
????mDevIsOpened?=?(CH347OpenDevice(mDevIndex)?!=?INVALID_HANDLE_VALUE);
????QLOG_DEBUG()<<"devOpen:"<<mDevIsOpened;
????if(mDevIsOpened==false)
????{
????????return?false;
????}
????bool?retVal?=?CH347I2C_Set(mDevIndex,?mIICSpeedMode);
????QLOG_DEBUG()<<"CH347I2C?Set?clock:"<<retVal;
????retVal?=?CH347I2C_SetStretch(mDevIndex,?false);
????QLOG_DEBUG()<<"CH347?I2C?set?stetching:"<<retVal;
????uint32_t?I2CDelayMs?=?100;
????if?(I2CDelayMs?>?0)
????{
????????retVal?=?CH347I2C_SetDelaymS(mDevIndex,?I2CDelayMs);
????????QLOG_DEBUG()<<"CH347InitI2C"<<retVal;
????}
????return?retVal;
}
bool?Ch347::IICReadWrite(uint8_t*?outBuffer,int&?outLen,uint8_t*?inBuffer,int&?inLen)
{
????bool?RetVal?=?CH347StreamI2C(mDevIndex,outLen,outBuffer,inLen,inBuffer);
????QLOG_DEBUG()<<"read?write?iic:"<<RetVal;
????if(RetVal)
????{
????????QLOG_DEBUG()<<"inlen="<<inLen;
????}
????return?RetVal;
}
熱門產(chǎn)品 :
CH397: 高集成度、低功耗USB網(wǎng)卡芯片