關(guān)于BF51x TWI的應(yīng)用的問題:
TWI用作EEPROM的驅(qū)動(IIC)
/**************************************************************************** * 名稱 : Reset_TWI * 功能 : 復(fù)位 TWI接口 * 入口參數(shù) :無 * 返回值 :無 ****************************************************************************/ void Reset_TWI(void) { //RESET_TWI CONTROLLER *pTWI_CONTROL = RESET_TWI; ssync();
//CLEAR ALL ERRONOUS CONDITIONS BEFORE ENABLING TWI *pTWI_MASTER_STAT = BUFWRERR | BUFRDERR | LOSTARB | ANAK | DNAK; ssync();
//CLEAR ALL INTERRUPTS BEFORE ENABLING TWI *pTWI_INT_STAT = SINIT | SCOMP | SERR | SOVF | MCOMP | MERR | XMTSERV | RCVSERV; ssync();
//FLUSH THE FIFOs - BOTH TX AND RX. *pTWI_FIFO_CTL = XMTFLUSH | RCVFLUSH; ssync(); }
/**************************************************************************** * 名稱 : TWI_MasterMode_Write * 功能 : TWI接口寫數(shù)據(jù) * 入口參數(shù) :無 * 返回值 :無 ****************************************************************************/ void TWI_MasterMode_Write(unsigned short DeviceAddr, unsigned short *TWI_Data_Pointer, unsigned short TX_Count, unsigned short TWI_TX_Length) { int i, j; //FLUSH THE FIFOs - BOTH TX AND RX. *pTWI_FIFO_CTL = XMTFLUSH | RCVFLUSH; ssync();
*pTWI_MASTER_STAT = BUFWRERR | BUFRDERR | LOSTARB | ANAK | DNAK; ssync();
*pTWI_FIFO_CTL = 0; // Clear the bit manually *pTWI_CONTROL = TWI_ENA | PRESCALE_VALUE; // PRESCALE = fsclk/10MHz *pTWI_CLKDIV = ((CLKDIV_HI) << 8) | (CLKDIV_LO); // For 100KHz SCL speed: CLKDIV = (1/100KHz)/(1/10MHz) = 100 -> SCL symetric: CLKHI = 50, CLKLOW = 50 *pTWI_MASTER_ADDR = DeviceAddr; // Target address (7-bits plus the read/write bit the TWI controls for (i = 0; i < TX_Count; i++) { delay(); // # of configurations to send to the sensor *pTWI_XMT_DATA8 = *TWI_Data_Pointer++; // Pointer to an array and load a value where a list of data is located *pTWI_MASTER_CTL = (TWI_TX_Length<<6) | MEN;// | FAST; // Start transmission for (j = 0; j < (TWI_TX_Length-1); j++) { // # of transfers before stop condition while (*pTWI_FIFO_STAT == XMTSTAT) // wait to load the next sample into the TX FIFO ssync();
*pTWI_XMT_DATA8 = *TWI_Data_Pointer++; // Load the next sample into the TX FIFO. Pointer to an array where a list of data is located ssync(); } while ((*pTWI_INT_STAT & MCOMP) == 0) // Wait until transmission complete and MCOMP is set ssync(); *pTWI_INT_STAT = XMTSERV | MCOMP; // service TWI for next transmission delay(); } asm("nop;"); asm("nop;"); asm("nop;");
}
這個是我在論壇里看到的TWI的驅(qū)動。有幾點不太明白:
1:TWI_TX_Length是發(fā)送的數(shù)據(jù)長度,是否包括從機的地址?
2:按照IIC協(xié)議第一個字節(jié)應(yīng)該是從機的地址+讀/寫位;那么TWI_Data_Pointer第一個字節(jié)是否是從機的地址+讀/寫位?
如果我想向從地址為0x01發(fā)送0x02,0x03,0x04三個數(shù)據(jù)TWI_TX_Length是多少?TWI_Data_Pointer指向的數(shù)據(jù)是怎樣的?
主機發(fā)送過程中,運行完 *pTWI_MASTER_CTL = (TWI_TX_Length<<6) | MEN;// | FAST; // Start transmission
TWI_MASTER_STAT(Master Mode Status Register)就會出現(xiàn)BUFRDERR (Buffer Read Error);TWI_INT_STAT(TWI Interrupt Status Register) 就會出現(xiàn) MERR (Master Transfer Error)。同時TWI_INT_STAT 還有MCOMP (Master Transfer Complete)就是發(fā)送完成的標志,這是問什么。請求高手 幫忙解決
答:
1. 不包括,你看下 TWI_MasterMode_Write 函數(shù),
*pTWI_MASTER_ADDR = DeviceAddr; // Target address (7-bits plus the read/write bit the TWI controls 這里是送設(shè)備地址,送完后才進入循環(huán)傳數(shù)據(jù)。 2.不是,TWI_MasterMode_Write 函數(shù)變量中,已經(jīng)有一個設(shè)備地址的參數(shù)傳進去了。這個地址不包括讀寫位,采用7bit的地址。
TWI_TX_Length 是3,TX_Count為1,TWI_Data_Pointer是你數(shù)據(jù)所在的指針地址或者數(shù)組名稱。
出現(xiàn)錯誤可能是沒有哦得到設(shè)備的握手ACK信號。
給你一個簡單例子看看。
問:
在主機的寫操作中: // # of configurations to send to the sensor *pTWI_XMT_DATA8 = *TWI_Data_Pointer++; // Pointer to an array and load a value where a list of data is located *pTWI_MASTER_CTL = (TWI_TX_Length<<6) | MEN;// | FAST; // Start transmission
為什么要先寫*pTWI_XMT_DATA8 再使能*pTWI_MASTER_CTL;按照芯片手冊上的操作是應(yīng)該先寫使能*pTWI_MASTER_CTL;從機地址 發(fā)送完成之后再寫*pTWI_XMT_DATA8。如果按照手冊上的這種操作就會出現(xiàn) TWI_MASTER_STAT(Master Mode Status Register)就會出現(xiàn)BUFRDERR (Buffer Read Error);TWI_INT_STAT(TWI Interrupt Status Register) 就會出現(xiàn) MERR (Master Transfer Error)。同時TWI_INT_STAT 還有MCOMP (Master Transfer Complete)就是發(fā)送完成的標志。 請教斑竹有沒有遇到同樣問題。
答:WI_XMT_DATA8 是向TWI數(shù)據(jù)寄存器送需要傳輸?shù)臄?shù)據(jù),然后使能傳輸,ADI官方的代碼一直是這么寫的,沒有仔細看過他的接口資料。按官方的代碼來吧。 感覺TWitter接口做的不是很好,有的時候傳輸會出錯,我在官方的代碼中,曾經(jīng)看到有一個TWI程序,采用的循環(huán)傳輸?shù)姆绞,就是傳輸?shù)據(jù)后接收設(shè)備應(yīng)答,設(shè)備不應(yīng)答就連續(xù)傳輸。所以我認為官方也應(yīng)該遇到了TWI傳輸失敗的事情。 感覺TWI還不如直接用IO寫個IIC模式的穩(wěn)定。經(jīng)常遇到得不到響應(yīng)信號死掉或者超時的情況。 |