最近一段時(shí)間一直在弄SPI接口相關(guān)的東西。用過(guò)幾款不同的CPU芯片,都自帶有SPI硬件接口,之前也用普通I/O口模擬過(guò)SPI接口,和硬件自帶的接口相比操作自由一些,但是速度受限,且不易改變大小。 下面將簡(jiǎn)單的說(shuō)說(shuō)S3C6410的SPI接口。關(guān)于SPI接口的傳輸協(xié)議我就不廢話了,不了解的可以在網(wǎng)上搜一搜。S3C6410的SPI功能相當(dāng)不錯(cuò)。官方文檔上有如下描述: • Full duplex • 8/16/32-bit shift register for TX/RX • 8-bit prescale logic • 3 clocks source • 8-bit/16-bit/32-bit bus interface • The Motorola SPI protocol and National Semiconductor Microwire • Two independent transmit and receive FIFOs, each 16 samples deep by 32-bits wide • Master-mode and Slave-mode • Receive-without-transmit operation • Tx/ Rx maximum frequency at up to 50MHz (CPHA=1 and slave Tx mode maximum frequency at up to 20Mhz)
官方文檔也提供了一個(gè)SPI的操作指南,如下: 1. Set Transfer Type. (CPOL & CPHA set ) 2. Set Clock configuration register. 3. Set SPI MODE configuration register. 4. Set SPI INT_EN register. 5. Set Packet Count configuration register if necessary. 6. Set Tx or Rx Channel on. 7. Set NSSOUT low to start Tx or Rx operation. A. Set NSSOUT Bit to low, then start TX data writing. B. If auto chip selection bit is set, should not control NSSOUT. 在對(duì)S3C6410的整個(gè)調(diào)試過(guò)程中,困擾我的問(wèn)題是它是如何啟動(dòng)一次數(shù)據(jù)的傳輸和接下來(lái)如何判斷它是否完成了數(shù)據(jù)傳輸,以及如何主動(dòng)去接收slave的數(shù)據(jù)。如果之前我沒(méi)有使用過(guò)另外一款MCU,或許不會(huì)產(chǎn)生這樣的疑惑;旧蠀⒄展俜轿臋n的操作說(shuō)明來(lái)做就可以了,而且就那么簡(jiǎn)單。 其實(shí)寫(xiě)數(shù)據(jù)還是很簡(jiǎn)單的,文檔上說(shuō): “CPU must write data on the register SPI_TX_DATA, to write data in FIFO. Data on the register are automatically moved to Tx FIFOs.” 即把要發(fā)送的數(shù)據(jù)寫(xiě)入SPI_TX_DATA后,數(shù)據(jù)會(huì)被自動(dòng)的寫(xiě)入FIFO管道,當(dāng)然這里要設(shè)置FIFO管道的深度。要如何啟動(dòng)FIFO管道里的數(shù)據(jù)往外發(fā)送了?上文的第七步就是描述如何啟動(dòng)spi傳輸。以手動(dòng)方式啟動(dòng)為例,將NSSOUT位置0,就OK了。但是如我上面的疑問(wèn),如何得知傳輸完成否?從文檔里發(fā)現(xiàn)有一個(gè)SPI_STATUS 寄存器,而SPI_STATUS[21]為TX_DONE,有這樣的描述: Indication of transfer done in Shift register(master mode only) 0 : all case except blow case 1 : when tx fifo and shift register are empty 我看見(jiàn)了這個(gè)標(biāo)志位,并以為它就是我要找的答案,但它不是。對(duì)這位的檢測(cè)總是0,無(wú)法作出判斷,正如0的含義“all case except blow case ”。一時(shí)不知到該咋辦,3SC6410也沒(méi)有提供發(fā)送完數(shù)據(jù)后就產(chǎn)生一個(gè)中斷的功能。我又注意到了SPI_STATUS[19:13]的RX_FIFO_LVL位,它顯示了Rx FIFO管道內(nèi)的數(shù)據(jù)量,它如果為0了,不就是說(shuō)寫(xiě)入FIFO中的數(shù)據(jù)已傳送完畢了嗎?果然經(jīng)驗(yàn)正,確實(shí)如此,做如下的: while((v32(SPI_STATUS0)>>6)&0x7f); //wait spi write finish 因?yàn)镾PI是全雙工的串行傳輸協(xié)議,那么對(duì)已經(jīng)知道寫(xiě)數(shù)據(jù),度數(shù)據(jù)就簡(jiǎn)單了,即在寫(xiě)數(shù)據(jù)完成后從SPI_RX_DATA里讀取數(shù)據(jù)就行了。 |