SPI library used to communicate with an altera development board attached to four zigbee-header pins.
Diff: mmSPI.cpp
- Revision:
- 16:0e422fd263c6
- Parent:
- 15:d6cc57c4e23d
- Child:
- 17:b81c0c1f312f
diff -r d6cc57c4e23d -r 0e422fd263c6 mmSPI.cpp --- a/mmSPI.cpp Mon Aug 19 04:41:29 2013 +0000 +++ b/mmSPI.cpp Mon Aug 19 16:26:29 2013 +0000 @@ -76,19 +76,8 @@ // MSB out/in first. void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes) { - // the first SPI pulse after the - // CPU clock goes low is used for - // parallel-load of the SPI shadow - // registers, not for shifting. - if (0) - { - *pSCLK = 1; - wait(fSPIquarterP); - wait(fSPIquarterP); - *pSCLK = 0; - wait(fSPIquarterP); - wait(fSPIquarterP); - } + + for (dLoop02 = (cNumBytes - 1); dLoop02 >= 0; dLoop02--) transceive_byte(&(cReceive[dLoop02]), &(cSend[dLoop02])); @@ -101,6 +90,76 @@ *pCPUclk = 0; wait(fSPIquarterP); wait(fSPIquarterP); + + + + if (0) + { + *pSCLK = 1; + wait(fSPIquarterP); + wait(fSPIquarterP); + *pSCLK = 0; + wait(fSPIquarterP); + wait(fSPIquarterP); + } + } +//----------------------------------------------//------------------------------ + // transceive a character array. + // limit is 256 characters. + // MSB out/in first. + void mmSPI::transceive_vector2(char *pcReceive, char *pcSend, int dNumBytes) + { + int dClear; + int dIndex; + int dMosiByteIndex; + int dMosiBitIndex; + int dMisoByteIndex; + int dMisoBitIndex; + + dIndex = (dNumBytes * 8) - 1; + dMosiByteIndex = dIndex / 8; + dMosiBitIndex = dIndex % 8; + + for (dClear = 0; dClear < dNumBytes; dClear++) pcReceive[dClear] = 0; + + + *pCPUclk = 1; // pulse the CPU clock. + wait(fSPIquarterP); + wait(fSPIquarterP); + *pCPUclk = 0; + wait(fSPIquarterP); + wait(fSPIquarterP); + + *pSCLK = 1; // pulse the SPI clock for parallel load. + wait(fSPIquarterP); + wait(fSPIquarterP); + *pSCLK = 0; + // pre-assert MOSI. + *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; + wait(fSPIquarterP); + wait(fSPIquarterP); + + + for (dIndex = (dNumBytes * 8) - 1; dIndex >= 0; dIndex--) + { + dMisoByteIndex = dIndex / 8; + dMisoBitIndex = dIndex % 8; + pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex); + +// pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (0x23 << dMisoBitIndex); + + *pSCLK = 1; + wait(fSPIquarterP); + wait(fSPIquarterP); + *pSCLK = 0; + + if (dIndex < 0) dIndex = 0; + dMosiByteIndex = (dIndex - 1) / 8; + dMosiBitIndex = (dIndex - 1) % 8; + *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; + wait(fSPIquarterP); + wait(fSPIquarterP); + } } //----------------------------------------------//------------------------------ // transceive a character array. @@ -191,6 +250,15 @@ } //----------------------------------------------//------------------------------ + void mmSPI::write_register(char cRegister, char cValue, char * pcReceive, char * pcSend) + { + pcSend[7] = 0x02; + pcSend[1] = ((cRegister & 0x07) << 2) | 0xA0; + pcSend[0] = cValue & 0xFF; + + // transceive_vector(pcReceive, pcSend, 8); + } +//----------------------------------------------//------------------------------ @@ -208,10 +276,3 @@ - - - - - - -