This is an involuntary fork, created because the repository would not update mmSPI. SPI library used to communicate with an altera development board attached to four zigbee-header pins.

Dependents:   Embedded_RTOS_Project

Fork of mmSPI by Mike Moore

Committer:
gatedClock
Date:
Sat Aug 17 23:13:12 2013 +0000
Revision:
12:a1b7ce9c1d64
Parent:
11:17207edac925
Child:
13:3e6886a96aea
looping with char as index doesn't work, switched to int.

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 8:e2d8bbc3e659 18 // deallocations.
gatedClock 8:e2d8bbc3e659 19 if (pMOSI) {delete pMOSI; pMOSI = NULL;}
gatedClock 8:e2d8bbc3e659 20 if (pMISO) {delete pMISO; pMISO = NULL;}
gatedClock 8:e2d8bbc3e659 21 if (pSCLK) {delete pSCLK; pSCLK = NULL;}
gatedClock 8:e2d8bbc3e659 22 if (pCPUclk) {delete pCPUclk; pCPUclk = NULL;}
gatedClock 3:de99451ab3c0 23 }
gatedClock 3:de99451ab3c0 24 //----------------------------------------------//------------------------------
gatedClock 3:de99451ab3c0 25 void mmSPI::allocations(void) // object allocations.
gatedClock 3:de99451ab3c0 26 {
gatedClock 8:e2d8bbc3e659 27 pMOSI = new DigitalOut(mmSPI_MOSI); // SPI MOSI pin object.
gatedClock 3:de99451ab3c0 28 if (!pMOSI) error("\n\r mmSPI::allocations : FATAL malloc error for pMOSI. \n\r");
gatedClock 3:de99451ab3c0 29
gatedClock 8:e2d8bbc3e659 30 pMISO = new DigitalOut(mmSPI_MISO); // SPI MISO pin object.
gatedClock 3:de99451ab3c0 31 if (!pMISO) error("\n\r mmSPI::allocations : FATAL malloc error for pMISO. \n\r");
gatedClock 3:de99451ab3c0 32
gatedClock 8:e2d8bbc3e659 33 pSCLK = new DigitalOut(mmSPI_SCLK); // SPI SCLK pin object.
gatedClock 3:de99451ab3c0 34 if (!pSCLK) error("\n\r mmSPI::allocations : FATAL malloc error for pSCLK. \n\r");
gatedClock 8:e2d8bbc3e659 35
gatedClock 8:e2d8bbc3e659 36 pCPUclk = new DigitalOut(mmCPU_CLK); // SPI SCLK pin object.
gatedClock 8:e2d8bbc3e659 37 if (!pCPUclk) error("\n\r mmSPI::allocations : FATAL malloc error for pCPUclk. \n\r");
gatedClock 3:de99451ab3c0 38 }
gatedClock 4:aa1fe8707bef 39 //----------------------------------------------//------------------------------
gatedClock 4:aa1fe8707bef 40 void mmSPI::setSPIfrequency(float fFreq) // set SPI clock frequency.
gatedClock 4:aa1fe8707bef 41 {
gatedClock 4:aa1fe8707bef 42 fSPIfreq = fFreq; // promote to object scope.
gatedClock 4:aa1fe8707bef 43 if (fSPIfreq < .05) // don't get near divide-by-zero.
gatedClock 4:aa1fe8707bef 44 error("\n\r mmSPI::setSPIfrequency : FATAL SPI frequency set too low. \n\r");
gatedClock 4:aa1fe8707bef 45 fSPIquarterP = (1 / fSPIfreq) / 4; // figure quarter-cycle period.
gatedClock 4:aa1fe8707bef 46 }
gatedClock 0:fb42c5acf810 47 //----------------------------------------------//------------------------------
gatedClock 5:b14dcaae260e 48 // we're not going for speed, so lets go for good setup / hold.
gatedClock 6:b480fc4e87e5 49
gatedClock 6:b480fc4e87e5 50 // send/receive a byte over SPI.
gatedClock 7:b3e8b537d5c2 51 // MSB out/in first.
gatedClock 6:b480fc4e87e5 52 void mmSPI::transceive_byte(char *cReceive, char *cSend)
gatedClock 1:15706d15d123 53 {
gatedClock 6:b480fc4e87e5 54 *cReceive = 0; // clear receive byte.
gatedClock 12:a1b7ce9c1d64 55 for (dLoop01 = 7; dLoop01 >= 0; dLoop01--)// loop for 8 bits in the byte.
gatedClock 5:b14dcaae260e 56 {
gatedClock 5:b14dcaae260e 57 *pSCLK = 0; // SPI clock negedge.
gatedClock 5:b14dcaae260e 58 wait(fSPIquarterP); // until middle of clock low.
gatedClock 12:a1b7ce9c1d64 59 *pMOSI = (*cSend >> dLoop01) & 1; // assert MOSI.
gatedClock 5:b14dcaae260e 60 wait(fSPIquarterP); // MOSI setup time
gatedClock 5:b14dcaae260e 61 *pSCLK = 1; // SPI clock posedge.
gatedClock 5:b14dcaae260e 62 wait(fSPIquarterP); // MISO setup time.
gatedClock 12:a1b7ce9c1d64 63 *cReceive = *cReceive | (*pMISO << dLoop01);
gatedClock 5:b14dcaae260e 64 wait(fSPIquarterP); // finish-out cycle.
gatedClock 5:b14dcaae260e 65 }
gatedClock 1:15706d15d123 66 }
gatedClock 5:b14dcaae260e 67 //----------------------------------------------//------------------------------
gatedClock 7:b3e8b537d5c2 68 // transceive a character array.
gatedClock 7:b3e8b537d5c2 69 // limit is 256 characters.
gatedClock 7:b3e8b537d5c2 70 // MSB out/in first.
gatedClock 7:b3e8b537d5c2 71 void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes)
gatedClock 7:b3e8b537d5c2 72 {
gatedClock 12:a1b7ce9c1d64 73 for (dLoop02 = (cNumBytes - 1); dLoop02 >= 0; dLoop02--)
gatedClock 12:a1b7ce9c1d64 74 transceive_byte(&(cReceive[dLoop02]), &(cSend[dLoop02]));
gatedClock 7:b3e8b537d5c2 75 }
gatedClock 7:b3e8b537d5c2 76 //----------------------------------------------//------------------------------
gatedClock 9:0551307e3b15 77 // transceive a character array.
gatedClock 9:0551307e3b15 78 // limit is 256 characters.
gatedClock 9:0551307e3b15 79 // MSB out/in first.
gatedClock 9:0551307e3b15 80 void mmSPI::test_toggle_cpu_clock(void)
gatedClock 9:0551307e3b15 81 {
gatedClock 11:17207edac925 82 DigitalOut led0(LED4);
gatedClock 9:0551307e3b15 83 while (1)
gatedClock 9:0551307e3b15 84 {
gatedClock 11:17207edac925 85 *pCPUclk = 1; led0 = 1;
gatedClock 9:0551307e3b15 86 wait(1.0);
gatedClock 11:17207edac925 87 *pCPUclk = 0; led0 = 0;
gatedClock 9:0551307e3b15 88 wait(1.0);
gatedClock 9:0551307e3b15 89 }
gatedClock 9:0551307e3b15 90 }
gatedClock 9:0551307e3b15 91 //----------------------------------------------//------------------------------
gatedClock 5:b14dcaae260e 92
gatedClock 5:b14dcaae260e 93
gatedClock 5:b14dcaae260e 94
gatedClock 5:b14dcaae260e 95
gatedClock 5:b14dcaae260e 96
gatedClock 5:b14dcaae260e 97
gatedClock 5:b14dcaae260e 98
gatedClock 5:b14dcaae260e 99
gatedClock 5:b14dcaae260e 100
gatedClock 7:b3e8b537d5c2 101
gatedClock 7:b3e8b537d5c2 102
gatedClock 7:b3e8b537d5c2 103
gatedClock 7:b3e8b537d5c2 104
gatedClock 7:b3e8b537d5c2 105
gatedClock 7:b3e8b537d5c2 106
gatedClock 7:b3e8b537d5c2 107
gatedClock 7:b3e8b537d5c2 108
gatedClock 7:b3e8b537d5c2 109
gatedClock 7:b3e8b537d5c2 110
gatedClock 7:b3e8b537d5c2 111
gatedClock 7:b3e8b537d5c2 112