SPI library used to communicate with an altera development board attached to four zigbee-header pins.

mmSPI.cpp

Committer:
gatedClock
Date:
2013-08-17
Revision:
9:0551307e3b15
Parent:
8:e2d8bbc3e659
Child:
10:8c0b8722cfcb
Child:
11:17207edac925

File content as of revision 9:0551307e3b15:

/*----------------------------------------------//------------------------------
    student   : m-moore
    class     : external SPI interface
    directory : mmSPI
    file      : mmSPI.cpp
------------------------------------------------//----------------------------*/
    #include "mmSPI.h"
/*----------------------------------------------//------------------------------
------------------------------------------------//----------------------------*/
//==============================================//==============================
    mmSPI::mmSPI()                              // constructor.
    {
      allocations();                            // object allocations.
    }
//----------------------------------------------//------------------------------    
    mmSPI::~mmSPI()                             // destructor.
    {
                                                // deallocations.
      if (pMOSI)   {delete pMOSI;   pMOSI   = NULL;}
      if (pMISO)   {delete pMISO;   pMISO   = NULL;}
      if (pSCLK)   {delete pSCLK;   pSCLK   = NULL;}
      if (pCPUclk) {delete pCPUclk; pCPUclk = NULL;}
    } 
//----------------------------------------------//------------------------------
    void mmSPI::allocations(void)               // object allocations.
    {
      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.
      if (!pMISO) error("\n\r mmSPI::allocations : FATAL malloc error for pMISO. \n\r"); 
    
      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.
      if (!pCPUclk) error("\n\r mmSPI::allocations : FATAL malloc error for pCPUclk. \n\r");
    } 
//----------------------------------------------//------------------------------    
    void mmSPI::setSPIfrequency(float fFreq)    // set SPI clock frequency.
    {
      fSPIfreq = fFreq;                         // promote to object scope.
      if (fSPIfreq < .05)                       // don't get near divide-by-zero.
      error("\n\r mmSPI::setSPIfrequency : FATAL SPI frequency set too low. \n\r"); 
      fSPIquarterP = (1 / fSPIfreq) / 4;        // figure quarter-cycle period.
    }
//----------------------------------------------//------------------------------
//  we're not going for speed, so lets go for good setup / hold.

                                                // send/receive a byte over SPI.
                                                // MSB out/in first.
    void mmSPI::transceive_byte(char *cReceive, char *cSend)
    {
      *cReceive = 0;                            // clear receive byte.                                    
      for (cLoop01 = 7; cLoop01 >= 0; cLoop01--)// loop for 8 bits in the byte.
      {
        *pSCLK = 0;                             // SPI clock negedge.
        wait(fSPIquarterP);                     // until middle of clock low.
        *pMOSI = (*cSend >> cLoop01) & 1;       // assert MOSI.
        wait(fSPIquarterP);                     // MOSI setup time
        *pSCLK = 1;                             // SPI clock posedge.
        wait(fSPIquarterP);                     // MISO setup time.
        *cReceive = *cReceive | (*pMISO << cLoop01);
        wait(fSPIquarterP);                     // finish-out cycle.
      }
    }
//----------------------------------------------//------------------------------
                                                // transceive a character array.
                                                // limit is 256 characters.
                                                // MSB out/in first.
    void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes)
    {    
      for (cLoop02 = (cNumBytes - 1); cLoop02 >= 0; cLoop02--)
      transceive_byte(&(cReceive[cLoop02]), &(cSend[cLoop02]));
    }
//----------------------------------------------//------------------------------
                                                // transceive a character array.
                                                // limit is 256 characters.
                                                // MSB out/in first.
    void mmSPI::test_toggle_cpu_clock(void)
    {    
      while (1)
      {
        *pCPUclk = 1;
        wait(1.0);
        *pCPUclk = 0;
        wait(1.0);
      }
    }
//----------------------------------------------//------------------------------