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

Committer:
gatedClock
Date:
Wed Aug 14 15:13:05 2013 +0000
Revision:
5:b14dcaae260e
Parent:
4:aa1fe8707bef
Child:
6:b480fc4e87e5
void mmSPI::transceive_byte(void)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gatedClock 0:fb42c5acf810 1 /*----------------------------------------------//------------------------------
gatedClock 0:fb42c5acf810 2 student : m-moore
gatedClock 0:fb42c5acf810 3 class : external SPI interface
gatedClock 0:fb42c5acf810 4 directory : mmSPI
gatedClock 0:fb42c5acf810 5 file : mmSPI.cpp
gatedClock 0:fb42c5acf810 6 ------------------------------------------------//----------------------------*/
gatedClock 0:fb42c5acf810 7 #include "mmSPI.h"
gatedClock 0:fb42c5acf810 8 /*----------------------------------------------//------------------------------
gatedClock 0:fb42c5acf810 9 ------------------------------------------------//----------------------------*/
gatedClock 0:fb42c5acf810 10 //==============================================//==============================
gatedClock 0:fb42c5acf810 11 mmSPI::mmSPI() // constructor.
gatedClock 0:fb42c5acf810 12 {
gatedClock 3:de99451ab3c0 13 allocations(); // object allocations.
gatedClock 0:fb42c5acf810 14 }
gatedClock 0:fb42c5acf810 15 //----------------------------------------------//------------------------------
gatedClock 0:fb42c5acf810 16 mmSPI::~mmSPI() // destructor.
gatedClock 0:fb42c5acf810 17 {
gatedClock 3:de99451ab3c0 18 if (pMOSI) {delete pMOSI; pMOSI = NULL;} // delete allocation.
gatedClock 3:de99451ab3c0 19 if (pMISO) {delete pMISO; pMISO = NULL;} // delete allocation.
gatedClock 3:de99451ab3c0 20 if (pSCLK) {delete pSCLK; pSCLK = NULL;} // delete allocation.
gatedClock 3:de99451ab3c0 21 }
gatedClock 3:de99451ab3c0 22 //----------------------------------------------//------------------------------
gatedClock 3:de99451ab3c0 23 void mmSPI::allocations(void) // object allocations.
gatedClock 3:de99451ab3c0 24 {
gatedClock 3:de99451ab3c0 25 pMOSI = new DigitalOut(mmSPI_MOSI); // SPI MOSI pin object.
gatedClock 3:de99451ab3c0 26 if (!pMOSI) error("\n\r mmSPI::allocations : FATAL malloc error for pMOSI. \n\r");
gatedClock 3:de99451ab3c0 27
gatedClock 3:de99451ab3c0 28 pMISO = new DigitalOut(mmSPI_MISO); // SPI MISO pin object.
gatedClock 3:de99451ab3c0 29 if (!pMISO) error("\n\r mmSPI::allocations : FATAL malloc error for pMISO. \n\r");
gatedClock 3:de99451ab3c0 30
gatedClock 3:de99451ab3c0 31 pSCLK = new DigitalOut(mmSPI_SCLK); // SPI SCLK pin object.
gatedClock 3:de99451ab3c0 32 if (!pSCLK) error("\n\r mmSPI::allocations : FATAL malloc error for pSCLK. \n\r");
gatedClock 3:de99451ab3c0 33 }
gatedClock 4:aa1fe8707bef 34 //----------------------------------------------//------------------------------
gatedClock 4:aa1fe8707bef 35 void mmSPI::setSPIfrequency(float fFreq) // set SPI clock frequency.
gatedClock 4:aa1fe8707bef 36 {
gatedClock 4:aa1fe8707bef 37 fSPIfreq = fFreq; // promote to object scope.
gatedClock 4:aa1fe8707bef 38 if (fSPIfreq < .05) // don't get near divide-by-zero.
gatedClock 4:aa1fe8707bef 39 error("\n\r mmSPI::setSPIfrequency : FATAL SPI frequency set too low. \n\r");
gatedClock 4:aa1fe8707bef 40 fSPIquarterP = (1 / fSPIfreq) / 4; // figure quarter-cycle period.
gatedClock 4:aa1fe8707bef 41 }
gatedClock 4:aa1fe8707bef 42
gatedClock 0:fb42c5acf810 43 //----------------------------------------------//------------------------------
gatedClock 5:b14dcaae260e 44 // we're not going for speed, so lets go for good setup / hold.
gatedClock 5:b14dcaae260e 45 void mmSPI::transceive_byte(void) // send/receive a byte.
gatedClock 1:15706d15d123 46 {
gatedClock 5:b14dcaae260e 47 cReceive = 0; // clear receive byte.
gatedClock 5:b14dcaae260e 48 for (cLoop01 = 7; cLoop01 >= 0; cLoop01++)// loop for 8 bits in the byte.
gatedClock 5:b14dcaae260e 49 {
gatedClock 5:b14dcaae260e 50 *pSCLK = 0; // SPI clock negedge.
gatedClock 5:b14dcaae260e 51 wait(fSPIquarterP); // until middle of clock low.
gatedClock 5:b14dcaae260e 52 *pMOSI = (cSend >> cLoop01) & 1; // assert MOSI.
gatedClock 5:b14dcaae260e 53 wait(fSPIquarterP); // MOSI setup time
gatedClock 5:b14dcaae260e 54 *pSCLK = 1; // SPI clock posedge.
gatedClock 5:b14dcaae260e 55 wait(fSPIquarterP); // MISO setup time.
gatedClock 5:b14dcaae260e 56 cReceive = cReceive | (*pMISO << cLoop01);
gatedClock 5:b14dcaae260e 57 wait(fSPIquarterP); // finish-out cycle.
gatedClock 5:b14dcaae260e 58 }
gatedClock 1:15706d15d123 59 }
gatedClock 5:b14dcaae260e 60 //----------------------------------------------//------------------------------
gatedClock 5:b14dcaae260e 61
gatedClock 5:b14dcaae260e 62
gatedClock 5:b14dcaae260e 63
gatedClock 5:b14dcaae260e 64
gatedClock 5:b14dcaae260e 65
gatedClock 5:b14dcaae260e 66
gatedClock 5:b14dcaae260e 67
gatedClock 5:b14dcaae260e 68
gatedClock 5:b14dcaae260e 69
gatedClock 5:b14dcaae260e 70
gatedClock 5:b14dcaae260e 71