SPI library used to communicate with an altera development board attached to four zigbee-header pins.
Diff: mmSPI.cpp
- Revision:
- 32:5a5d9525c6c4
- Parent:
- 31:ea7b25e494b5
- Child:
- 33:59e13ec04ea8
- Child:
- 34:d5553509f31a
--- a/mmSPI.cpp Sun Aug 25 20:12:32 2013 +0000 +++ b/mmSPI.cpp Mon Aug 26 22:39:44 2013 +0000 @@ -1,22 +1,24 @@ /*----------------------------------------------//------------------------------ student : m-moore - class : external SPI interface + email : gated.clock@gmail.com + class : usb device drivers directory : mmSPI file : mmSPI.cpp + date : september 3, 2013. ------------------------------------------------//----------------------------*/ #include "mmSPI.h" /*----------------------------------------------//------------------------------ ------------------------------------------------//----------------------------*/ //==============================================//============================== -// 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. - } + *pSCLK = 0; // initialize. + *pCPUclk = 0; // initialize. + ulSPIclkCount = 0; // initialize. + ulCPUclkCount = 0; // initialize. + } // constructor. //----------------------------------------------//------------------------------ mmSPI::~mmSPI() // destructor. { @@ -25,22 +27,22 @@ if (pMISO) {delete pMISO; pMISO = NULL;} if (pSCLK) {delete pSCLK; pSCLK = NULL;} if (pCPUclk) {delete pCPUclk; pCPUclk = NULL;} - } + } // destructor. //----------------------------------------------//------------------------------ void mmSPI::allocations(void) // object allocations. { - pMOSI = new DigitalOut(mmSPI_MOSI); // SPI MOSI pin object. + pMOSI = new DigitalOut(mmSPI_MOSI); // SPI MOSI pin object. if (!pMOSI) error("\n\r mmSPI::allocations : FATAL malloc error for pMOSI. \n\r"); - pMISO = new DigitalOut(mmSPI_MISO); // SPI MISO pin object. + pMISO = new DigitalOut(mmSPI_MISO); // SPI MISO pin object. if (!pMISO) error("\n\r mmSPI::allocations : FATAL malloc error for pMISO. \n\r"); - pSCLK = new DigitalOut(mmSPI_SCLK); // SPI SCLK pin object. + pSCLK = new DigitalOut(mmSPI_SCLK); // SPI SCLK pin object. if (!pSCLK) error("\n\r mmSPI::allocations : FATAL malloc error for pSCLK. \n\r"); - pCPUclk = new DigitalOut(mmCPU_CLK); // SPI SCLK pin object. + pCPUclk = new DigitalOut(mmCPU_CLK); // SPI SCLK pin object. if (!pCPUclk) error("\n\r mmSPI::allocations : FATAL malloc error for pCPUclk. \n\r"); - } + } // allocations. //----------------------------------------------//------------------------------ void mmSPI::setSPIfrequency(float fFreq) // set SPI clock frequency. { @@ -54,19 +56,19 @@ void mmSPI::setSendBuffer(char * pcSendBuffer) { pcSend = pcSendBuffer; // promote to object scope. - } + } // setSendBuffer. //----------------------------------------------//------------------------------ // obtain SPI receive buffer pointer. void mmSPI::setReceiveBuffer(char * pcReceiveBuffer) { pcReceive = pcReceiveBuffer; // promote to object scope. - } + } // setReceiveBuffer. //----------------------------------------------//------------------------------ // obtain number of SPI bytes. void mmSPI::setNumberOfBytes(int dNumberOfBytes) { dNumBytes = dNumberOfBytes; // promote to object scope. - } + } // setNumberOfBytes. //----------------------------------------------//------------------------------ // transceive a character array. // MSB out/in first. @@ -95,6 +97,7 @@ *pCPUclk = 0; wait(fSPIquarterP); wait(fSPIquarterP); + ulCPUclkCount++; } // if pre-CPU clock. if (cPreSPI) // if pre-SPI pulse. @@ -103,6 +106,7 @@ wait(fSPIquarterP); wait(fSPIquarterP); *pSCLK = 0; + ulSPIclkCount++; } // if pre-SPI pulse. if (cScan) // if cScan. @@ -113,6 +117,7 @@ wait(fSPIquarterP); + // main SPI scan loop. for (dIndex = (dNumBytes * 8) - 1; dIndex >= 0; dIndex--) { dMisoByteIndex = dIndex / 8; @@ -128,7 +133,8 @@ *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; wait(fSPIquarterP); wait(fSPIquarterP); - } + ulSPIclkCount++; + } // main SPI scan loop. } // if cScan. if (cPostCPU) // if post-CPU pulse. @@ -138,14 +144,16 @@ wait(fSPIquarterP); *pCPUclk = 0; wait(fSPIquarterP); - wait(fSPIquarterP); + wait(fSPIquarterP); + ulCPUclkCount++; *pSCLK = 1; // clear-out the SPI parallel-load enable. wait(fSPIquarterP); wait(fSPIquarterP); *pSCLK = 0; + ulSPIclkCount++; } // if post-CPU pulse. - } + } // transceive_vector. //----------------------------------------------//------------------------------ // cRegister -> CPU_register // 0 R0 @@ -172,7 +180,7 @@ transceive_vector(1,1,1,0); // transmit command. clear_transmit_vector(); // clear transmit vector. - } + } // write_register. //----------------------------------------------//------------------------------ // cRegister -> CPU_register // 0 -> R0 @@ -194,7 +202,7 @@ transceive_vector(1,1,1,0); // snap & scan-out reg contents. return (pcReceive[6 - cRegister]); // return the particular reg value. - } + } // read_register. //----------------------------------------------//------------------------------ void mmSPI::write_memory(char cHData, char cLdata, char cAddress) { @@ -215,7 +223,7 @@ transceive_vector(1,1,1,0); clear_transmit_vector(); // clear transmit vector. - } + } // write_memory. //----------------------------------------------//------------------------------ // fetch a word from main memory. unsigned int mmSPI::read_memory(char cAddress) @@ -246,7 +254,7 @@ clear_transmit_vector(); // clear transmit vector. return udMemoryContent; // return the memory word. - } + } // read_memory. //----------------------------------------------//------------------------------ void mmSPI::step(void) // step the CPU. { @@ -255,13 +263,17 @@ transceive_vector(0,0,0,1); // advance CPU, clear shadow-load. pcSend[7] = 0x02; // ready to disable CPU mode. transceive_vector(0,0,1,0); // disable CPU mode. - } + } // step. //----------------------------------------------//------------------------------ void mmSPI::clear_transmit_vector(void) // fill transmit buffer with 0. { int dLoop; for (dLoop = 0; dLoop < dNumBytes; dLoop++) pcSend[dLoop] = 0x00; - } + } // clear_transmit_vector. +//----------------------------------------------//------------------------------ + // getters. + unsigned long mmSPI::SPIClockCount() {return ulSPIclkCount;} + unsigned long mmSPI::CPUClockCount() {return ulCPUclkCount;} //----------------------------------------------//------------------------------ @@ -271,8 +283,3 @@ - - - - -