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
mmSPI.cpp@14:35717622a4fb, 2013-08-18 (annotated)
- Committer:
- gatedClock
- Date:
- Sun Aug 18 04:50:54 2013 +0000
- Revision:
- 14:35717622a4fb
- Parent:
- 13:3e6886a96aea
- Child:
- 15:d6cc57c4e23d
AC immediate value is seen from R0 LED monitor.
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 | 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 | 13:3e6886a96aea | 72 | { |
gatedClock | 13:3e6886a96aea | 73 | // the first SPI pulse after the |
gatedClock | 13:3e6886a96aea | 74 | // CPU clock goes low is used for |
gatedClock | 13:3e6886a96aea | 75 | // parallel-load of the SPI shadow |
gatedClock | 13:3e6886a96aea | 76 | // registers, not for shifting. |
gatedClock | 13:3e6886a96aea | 77 | if (0) |
gatedClock | 13:3e6886a96aea | 78 | { |
gatedClock | 13:3e6886a96aea | 79 | *pSCLK = 1; |
gatedClock | 13:3e6886a96aea | 80 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 81 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 82 | *pSCLK = 0; |
gatedClock | 13:3e6886a96aea | 83 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 84 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 85 | } |
gatedClock | 13:3e6886a96aea | 86 | |
gatedClock | 12:a1b7ce9c1d64 | 87 | for (dLoop02 = (cNumBytes - 1); dLoop02 >= 0; dLoop02--) |
gatedClock | 12:a1b7ce9c1d64 | 88 | transceive_byte(&(cReceive[dLoop02]), &(cSend[dLoop02])); |
gatedClock | 13:3e6886a96aea | 89 | |
gatedClock | 13:3e6886a96aea | 90 | *pSCLK = 0; // SPI clock rests low. |
gatedClock | 13:3e6886a96aea | 91 | |
gatedClock | 13:3e6886a96aea | 92 | *pCPUclk = 1; // pulse the CPU clock. |
gatedClock | 13:3e6886a96aea | 93 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 94 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 95 | *pCPUclk = 0; |
gatedClock | 13:3e6886a96aea | 96 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 97 | wait(fSPIquarterP); |
gatedClock | 7:b3e8b537d5c2 | 98 | } |
gatedClock | 7:b3e8b537d5c2 | 99 | //----------------------------------------------//------------------------------ |
gatedClock | 9:0551307e3b15 | 100 | // transceive a character array. |
gatedClock | 9:0551307e3b15 | 101 | // limit is 256 characters. |
gatedClock | 9:0551307e3b15 | 102 | // MSB out/in first. |
gatedClock | 9:0551307e3b15 | 103 | void mmSPI::test_toggle_cpu_clock(void) |
gatedClock | 9:0551307e3b15 | 104 | { |
gatedClock | 11:17207edac925 | 105 | DigitalOut led0(LED4); |
gatedClock | 9:0551307e3b15 | 106 | while (1) |
gatedClock | 9:0551307e3b15 | 107 | { |
gatedClock | 11:17207edac925 | 108 | *pCPUclk = 1; led0 = 1; |
gatedClock | 9:0551307e3b15 | 109 | wait(1.0); |
gatedClock | 11:17207edac925 | 110 | *pCPUclk = 0; led0 = 0; |
gatedClock | 9:0551307e3b15 | 111 | wait(1.0); |
gatedClock | 9:0551307e3b15 | 112 | } |
gatedClock | 9:0551307e3b15 | 113 | } |
gatedClock | 9:0551307e3b15 | 114 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 115 | |
gatedClock | 5:b14dcaae260e | 116 | |
gatedClock | 14:35717622a4fb | 117 | |
gatedClock | 14:35717622a4fb | 118 | |
gatedClock | 14:35717622a4fb | 119 | |
gatedClock | 14:35717622a4fb | 120 | |
gatedClock | 14:35717622a4fb | 121 | |
gatedClock | 14:35717622a4fb | 122 | |
gatedClock | 14:35717622a4fb | 123 | |
gatedClock | 14:35717622a4fb | 124 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 125 | |
gatedClock | 5:b14dcaae260e | 126 | |
gatedClock | 5:b14dcaae260e | 127 | |
gatedClock | 5:b14dcaae260e | 128 | |
gatedClock | 5:b14dcaae260e | 129 | |
gatedClock | 5:b14dcaae260e | 130 | |
gatedClock | 5:b14dcaae260e | 131 | |
gatedClock | 7:b3e8b537d5c2 | 132 | |
gatedClock | 7:b3e8b537d5c2 | 133 | |
gatedClock | 7:b3e8b537d5c2 | 134 | |
gatedClock | 7:b3e8b537d5c2 | 135 | |
gatedClock | 7:b3e8b537d5c2 | 136 |