SPI library used to communicate with an altera development board attached to four zigbee-header pins.
Diff: mmSPI.cpp
- Revision:
- 15:d6cc57c4e23d
- Parent:
- 14:35717622a4fb
- Child:
- 16:0e422fd263c6
--- a/mmSPI.cpp Sun Aug 18 04:50:54 2013 +0000 +++ b/mmSPI.cpp Mon Aug 19 04:41:29 2013 +0000 @@ -8,9 +8,14 @@ /*----------------------------------------------//------------------------------ ------------------------------------------------//----------------------------*/ //==============================================//============================== +// consider resetting the fpga around here, because +// the micro may be wiggling these signals before here. mmSPI::mmSPI() // constructor. { allocations(); // object allocations. + + *pSCLK = 0; // initialize. + *pCPUclk = 0; // initialize. } //----------------------------------------------//------------------------------ mmSPI::~mmSPI() // destructor. @@ -57,10 +62,11 @@ *pSCLK = 0; // SPI clock negedge. wait(fSPIquarterP); // until middle of clock low. *pMOSI = (*cSend >> dLoop01) & 1; // assert MOSI. - wait(fSPIquarterP); // MOSI setup time + // capture MISO. + *cReceive = *cReceive | (*pMISO << dLoop01); + wait(fSPIquarterP); // finish-out cycle. *pSCLK = 1; // SPI clock posedge. - wait(fSPIquarterP); // MISO setup time. - *cReceive = *cReceive | (*pMISO << dLoop01); + wait(fSPIquarterP); // finish-out cycle. wait(fSPIquarterP); // finish-out cycle. } } @@ -87,7 +93,7 @@ for (dLoop02 = (cNumBytes - 1); dLoop02 >= 0; dLoop02--) transceive_byte(&(cReceive[dLoop02]), &(cSend[dLoop02])); - *pSCLK = 0; // SPI clock rests low. + *pCPUclk = 1; // pulse the CPU clock. wait(fSPIquarterP); @@ -112,15 +118,78 @@ } } //----------------------------------------------//------------------------------ + void mmSPI::force_write(char cDataHIgh, char cDataLow, char cAddress) + { + char pcReceive[8]; + char pcSend [8]; + int dLoop; + + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0; + + + // high data to R2. + pcSend[7] = 0x02; pcSend[1] = 0xA8; pcSend[0] = cDataHIgh; + transceive_vector(pcReceive, pcSend, 8); - - - + // low data to R1. + pcSend[7] = 0x02; pcSend[1] = 0xA4; pcSend[0] = cDataLow; + transceive_vector(pcReceive, pcSend, 8); + + + // address to R3. + pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress; + transceive_vector(pcReceive, pcSend, 8); + + + + pcSend[7] = 0x02; pcSend[1] = 0x02; pcSend[0] = 0; // WE high. + transceive_vector(pcReceive, pcSend, 8); + + pcSend[7] = 0x02; pcSend[1] = 0x00; pcSend[0] = 0; // WE low. + transceive_vector(pcReceive, pcSend, 8); + + } +//----------------------------------------------//------------------------------ - - - + void mmSPI::force_read(char cAddress) + { + char pcReceive[8]; + char pcSend [8]; + int dLoop; + + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0; + + + + + // address to R3. + pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress; + transceive_vector(pcReceive, pcSend, 8); + + // R2 gets data-H from memory. + pcSend[7] = 0x02; pcSend[1] = 0xC8; pcSend[0] = cAddress; + transceive_vector(pcReceive, pcSend, 8); + + // R1 gets data-L from memory. + pcSend[7] = 0x02; pcSend[1] = 0xC4; pcSend[0] = cAddress; + transceive_vector(pcReceive, pcSend, 8); + + + + + // pcSend[7] = 0x02; // force IR. +// pcSend[1] = 0xA4; // R1 <- immediate. +// pcSend[0] = 0xEE; // immediate value. + /// transceive_vector(pcReceive, pcSend, 8); + + + + // no-op scan. + pcSend[7] = 0x02; pcSend[1] = 0x0; pcSend[0] = 0; + transceive_vector(pcReceive, pcSend, 8); + + } //----------------------------------------------//------------------------------ @@ -134,3 +203,15 @@ + + + + + + + + + + + +