SPI library used to communicate with an altera development board attached to four zigbee-header pins.
mmSPI.cpp@7:b3e8b537d5c2, 2013-08-14 (annotated)
- Committer:
- gatedClock
- Date:
- Wed Aug 14 15:34:10 2013 +0000
- Revision:
- 7:b3e8b537d5c2
- Parent:
- 6:b480fc4e87e5
- Child:
- 8:e2d8bbc3e659
void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes)
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:fb42c5acf810 | 42 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 43 | // we're not going for speed, so lets go for good setup / hold. |
gatedClock | 6:b480fc4e87e5 | 44 | |
gatedClock | 6:b480fc4e87e5 | 45 | // send/receive a byte over SPI. |
gatedClock | 7:b3e8b537d5c2 | 46 | // MSB out/in first. |
gatedClock | 6:b480fc4e87e5 | 47 | void mmSPI::transceive_byte(char *cReceive, char *cSend) |
gatedClock | 1:15706d15d123 | 48 | { |
gatedClock | 6:b480fc4e87e5 | 49 | *cReceive = 0; // clear receive byte. |
gatedClock | 7:b3e8b537d5c2 | 50 | for (cLoop01 = 7; cLoop01 >= 0; cLoop01--)// loop for 8 bits in the byte. |
gatedClock | 5:b14dcaae260e | 51 | { |
gatedClock | 5:b14dcaae260e | 52 | *pSCLK = 0; // SPI clock negedge. |
gatedClock | 5:b14dcaae260e | 53 | wait(fSPIquarterP); // until middle of clock low. |
gatedClock | 6:b480fc4e87e5 | 54 | *pMOSI = (*cSend >> cLoop01) & 1; // assert MOSI. |
gatedClock | 5:b14dcaae260e | 55 | wait(fSPIquarterP); // MOSI setup time |
gatedClock | 5:b14dcaae260e | 56 | *pSCLK = 1; // SPI clock posedge. |
gatedClock | 5:b14dcaae260e | 57 | wait(fSPIquarterP); // MISO setup time. |
gatedClock | 6:b480fc4e87e5 | 58 | *cReceive = *cReceive | (*pMISO << cLoop01); |
gatedClock | 5:b14dcaae260e | 59 | wait(fSPIquarterP); // finish-out cycle. |
gatedClock | 5:b14dcaae260e | 60 | } |
gatedClock | 1:15706d15d123 | 61 | } |
gatedClock | 5:b14dcaae260e | 62 | //----------------------------------------------//------------------------------ |
gatedClock | 7:b3e8b537d5c2 | 63 | // transceive a character array. |
gatedClock | 7:b3e8b537d5c2 | 64 | // limit is 256 characters. |
gatedClock | 7:b3e8b537d5c2 | 65 | // MSB out/in first. |
gatedClock | 7:b3e8b537d5c2 | 66 | void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes) |
gatedClock | 7:b3e8b537d5c2 | 67 | { |
gatedClock | 7:b3e8b537d5c2 | 68 | for (cLoop02 = (cNumBytes - 1); cLoop02 >= 0; cLoop02--) |
gatedClock | 7:b3e8b537d5c2 | 69 | transceive_byte(&(cReceive[cLoop02]), &(cSend[cLoop02])); |
gatedClock | 7:b3e8b537d5c2 | 70 | } |
gatedClock | 7:b3e8b537d5c2 | 71 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 72 | |
gatedClock | 5:b14dcaae260e | 73 | |
gatedClock | 5:b14dcaae260e | 74 | |
gatedClock | 5:b14dcaae260e | 75 | |
gatedClock | 5:b14dcaae260e | 76 | |
gatedClock | 5:b14dcaae260e | 77 | |
gatedClock | 5:b14dcaae260e | 78 | |
gatedClock | 5:b14dcaae260e | 79 | |
gatedClock | 5:b14dcaae260e | 80 | |
gatedClock | 7:b3e8b537d5c2 | 81 | |
gatedClock | 7:b3e8b537d5c2 | 82 | |
gatedClock | 7:b3e8b537d5c2 | 83 | |
gatedClock | 7:b3e8b537d5c2 | 84 | |
gatedClock | 7:b3e8b537d5c2 | 85 | |
gatedClock | 7:b3e8b537d5c2 | 86 | |
gatedClock | 7:b3e8b537d5c2 | 87 | |
gatedClock | 7:b3e8b537d5c2 | 88 | |
gatedClock | 7:b3e8b537d5c2 | 89 | |
gatedClock | 7:b3e8b537d5c2 | 90 | |
gatedClock | 7:b3e8b537d5c2 | 91 | |
gatedClock | 7:b3e8b537d5c2 | 92 |