SPI library used to communicate with an altera development board attached to four zigbee-header pins.
mmSPI.cpp@24:d3b8c68f41f2, 2013-08-20 (annotated)
- Committer:
- gatedClock
- Date:
- Tue Aug 20 14:38:50 2013 +0000
- Revision:
- 24:d3b8c68f41f2
- Parent:
- 23:dbd89a56716d
- Child:
- 26:26a8f31a31b8
cleanup.
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 | 15:d6cc57c4e23d | 11 | // consider resetting the fpga around here, because |
gatedClock | 15:d6cc57c4e23d | 12 | // the micro may be wiggling these signals before here. |
gatedClock | 0:fb42c5acf810 | 13 | mmSPI::mmSPI() // constructor. |
gatedClock | 0:fb42c5acf810 | 14 | { |
gatedClock | 3:de99451ab3c0 | 15 | allocations(); // object allocations. |
gatedClock | 15:d6cc57c4e23d | 16 | |
gatedClock | 15:d6cc57c4e23d | 17 | *pSCLK = 0; // initialize. |
gatedClock | 15:d6cc57c4e23d | 18 | *pCPUclk = 0; // initialize. |
gatedClock | 0:fb42c5acf810 | 19 | } |
gatedClock | 0:fb42c5acf810 | 20 | //----------------------------------------------//------------------------------ |
gatedClock | 0:fb42c5acf810 | 21 | mmSPI::~mmSPI() // destructor. |
gatedClock | 0:fb42c5acf810 | 22 | { |
gatedClock | 8:e2d8bbc3e659 | 23 | // deallocations. |
gatedClock | 8:e2d8bbc3e659 | 24 | if (pMOSI) {delete pMOSI; pMOSI = NULL;} |
gatedClock | 8:e2d8bbc3e659 | 25 | if (pMISO) {delete pMISO; pMISO = NULL;} |
gatedClock | 8:e2d8bbc3e659 | 26 | if (pSCLK) {delete pSCLK; pSCLK = NULL;} |
gatedClock | 8:e2d8bbc3e659 | 27 | if (pCPUclk) {delete pCPUclk; pCPUclk = NULL;} |
gatedClock | 3:de99451ab3c0 | 28 | } |
gatedClock | 3:de99451ab3c0 | 29 | //----------------------------------------------//------------------------------ |
gatedClock | 3:de99451ab3c0 | 30 | void mmSPI::allocations(void) // object allocations. |
gatedClock | 3:de99451ab3c0 | 31 | { |
gatedClock | 8:e2d8bbc3e659 | 32 | pMOSI = new DigitalOut(mmSPI_MOSI); // SPI MOSI pin object. |
gatedClock | 3:de99451ab3c0 | 33 | if (!pMOSI) error("\n\r mmSPI::allocations : FATAL malloc error for pMOSI. \n\r"); |
gatedClock | 3:de99451ab3c0 | 34 | |
gatedClock | 8:e2d8bbc3e659 | 35 | pMISO = new DigitalOut(mmSPI_MISO); // SPI MISO pin object. |
gatedClock | 3:de99451ab3c0 | 36 | if (!pMISO) error("\n\r mmSPI::allocations : FATAL malloc error for pMISO. \n\r"); |
gatedClock | 3:de99451ab3c0 | 37 | |
gatedClock | 8:e2d8bbc3e659 | 38 | pSCLK = new DigitalOut(mmSPI_SCLK); // SPI SCLK pin object. |
gatedClock | 3:de99451ab3c0 | 39 | if (!pSCLK) error("\n\r mmSPI::allocations : FATAL malloc error for pSCLK. \n\r"); |
gatedClock | 8:e2d8bbc3e659 | 40 | |
gatedClock | 8:e2d8bbc3e659 | 41 | pCPUclk = new DigitalOut(mmCPU_CLK); // SPI SCLK pin object. |
gatedClock | 8:e2d8bbc3e659 | 42 | if (!pCPUclk) error("\n\r mmSPI::allocations : FATAL malloc error for pCPUclk. \n\r"); |
gatedClock | 3:de99451ab3c0 | 43 | } |
gatedClock | 4:aa1fe8707bef | 44 | //----------------------------------------------//------------------------------ |
gatedClock | 4:aa1fe8707bef | 45 | void mmSPI::setSPIfrequency(float fFreq) // set SPI clock frequency. |
gatedClock | 4:aa1fe8707bef | 46 | { |
gatedClock | 4:aa1fe8707bef | 47 | fSPIfreq = fFreq; // promote to object scope. |
gatedClock | 4:aa1fe8707bef | 48 | if (fSPIfreq < .05) // don't get near divide-by-zero. |
gatedClock | 4:aa1fe8707bef | 49 | error("\n\r mmSPI::setSPIfrequency : FATAL SPI frequency set too low. \n\r"); |
gatedClock | 4:aa1fe8707bef | 50 | fSPIquarterP = (1 / fSPIfreq) / 4; // figure quarter-cycle period. |
gatedClock | 4:aa1fe8707bef | 51 | } |
gatedClock | 22:7524dee5c753 | 52 | //----------------------------------------------//------------------------------ |
gatedClock | 22:7524dee5c753 | 53 | // obtain SPI send buffer pointer. |
gatedClock | 22:7524dee5c753 | 54 | void mmSPI::setSendBuffer(char * pcSendBuffer) |
gatedClock | 22:7524dee5c753 | 55 | { |
gatedClock | 22:7524dee5c753 | 56 | pcSend = pcSendBuffer; // promote to object scope. |
gatedClock | 22:7524dee5c753 | 57 | } |
gatedClock | 22:7524dee5c753 | 58 | //----------------------------------------------//------------------------------ |
gatedClock | 22:7524dee5c753 | 59 | // obtain SPI receive buffer pointer. |
gatedClock | 22:7524dee5c753 | 60 | void mmSPI::setReceiveBuffer(char * pcReceiveBuffer) |
gatedClock | 22:7524dee5c753 | 61 | { |
gatedClock | 22:7524dee5c753 | 62 | pcReceive = pcReceiveBuffer; // promote to object scope. |
gatedClock | 22:7524dee5c753 | 63 | } |
gatedClock | 0:fb42c5acf810 | 64 | //----------------------------------------------//------------------------------ |
gatedClock | 22:7524dee5c753 | 65 | // obtain number of SPI bytes. |
gatedClock | 22:7524dee5c753 | 66 | void mmSPI::setNumberOfBytes(int dNumberOfBytes) |
gatedClock | 22:7524dee5c753 | 67 | { |
gatedClock | 22:7524dee5c753 | 68 | dNumBytes = dNumberOfBytes; // promote to object scope. |
gatedClock | 22:7524dee5c753 | 69 | } |
gatedClock | 22:7524dee5c753 | 70 | //----------------------------------------------//------------------------------ |
gatedClock | 16:0e422fd263c6 | 71 | // transceive a character array. |
gatedClock | 16:0e422fd263c6 | 72 | // MSB out/in first. |
gatedClock | 23:dbd89a56716d | 73 | void mmSPI::transceive_vector(void) |
gatedClock | 16:0e422fd263c6 | 74 | { |
gatedClock | 16:0e422fd263c6 | 75 | int dClear; |
gatedClock | 16:0e422fd263c6 | 76 | int dIndex; |
gatedClock | 16:0e422fd263c6 | 77 | int dMosiByteIndex; |
gatedClock | 16:0e422fd263c6 | 78 | int dMosiBitIndex; |
gatedClock | 16:0e422fd263c6 | 79 | int dMisoByteIndex; |
gatedClock | 16:0e422fd263c6 | 80 | int dMisoBitIndex; |
gatedClock | 16:0e422fd263c6 | 81 | |
gatedClock | 16:0e422fd263c6 | 82 | dIndex = (dNumBytes * 8) - 1; |
gatedClock | 16:0e422fd263c6 | 83 | dMosiByteIndex = dIndex / 8; |
gatedClock | 16:0e422fd263c6 | 84 | dMosiBitIndex = dIndex % 8; |
gatedClock | 16:0e422fd263c6 | 85 | |
gatedClock | 16:0e422fd263c6 | 86 | for (dClear = 0; dClear < dNumBytes; dClear++) pcReceive[dClear] = 0; |
gatedClock | 16:0e422fd263c6 | 87 | |
gatedClock | 16:0e422fd263c6 | 88 | |
gatedClock | 16:0e422fd263c6 | 89 | *pCPUclk = 1; // pulse the CPU clock. |
gatedClock | 16:0e422fd263c6 | 90 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 91 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 92 | *pCPUclk = 0; |
gatedClock | 16:0e422fd263c6 | 93 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 94 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 95 | |
gatedClock | 16:0e422fd263c6 | 96 | *pSCLK = 1; // pulse the SPI clock for parallel load. |
gatedClock | 16:0e422fd263c6 | 97 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 98 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 99 | *pSCLK = 0; |
gatedClock | 16:0e422fd263c6 | 100 | // pre-assert MOSI. |
gatedClock | 16:0e422fd263c6 | 101 | *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; |
gatedClock | 16:0e422fd263c6 | 102 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 103 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 104 | |
gatedClock | 16:0e422fd263c6 | 105 | |
gatedClock | 16:0e422fd263c6 | 106 | for (dIndex = (dNumBytes * 8) - 1; dIndex >= 0; dIndex--) |
gatedClock | 16:0e422fd263c6 | 107 | { |
gatedClock | 16:0e422fd263c6 | 108 | dMisoByteIndex = dIndex / 8; |
gatedClock | 16:0e422fd263c6 | 109 | dMisoBitIndex = dIndex % 8; |
gatedClock | 18:4a29cad91540 | 110 | pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex); |
gatedClock | 16:0e422fd263c6 | 111 | *pSCLK = 1; |
gatedClock | 16:0e422fd263c6 | 112 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 113 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 114 | *pSCLK = 0; |
gatedClock | 16:0e422fd263c6 | 115 | |
gatedClock | 16:0e422fd263c6 | 116 | if (dIndex < 0) dIndex = 0; |
gatedClock | 16:0e422fd263c6 | 117 | dMosiByteIndex = (dIndex - 1) / 8; |
gatedClock | 16:0e422fd263c6 | 118 | dMosiBitIndex = (dIndex - 1) % 8; |
gatedClock | 16:0e422fd263c6 | 119 | *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; |
gatedClock | 16:0e422fd263c6 | 120 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 121 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 122 | } |
gatedClock | 7:b3e8b537d5c2 | 123 | } |
gatedClock | 7:b3e8b537d5c2 | 124 | //----------------------------------------------//------------------------------ |
gatedClock | 23:dbd89a56716d | 125 | void mmSPI::write_register(char cRegister, char cValue) |
gatedClock | 16:0e422fd263c6 | 126 | { |
gatedClock | 18:4a29cad91540 | 127 | int dLoop; // loop index. |
gatedClock | 18:4a29cad91540 | 128 | |
gatedClock | 24:d3b8c68f41f2 | 129 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 130 | |
gatedClock | 18:4a29cad91540 | 131 | pcSend[7] = 0x02; // mbed sends a command. |
gatedClock | 18:4a29cad91540 | 132 | |
gatedClock | 18:4a29cad91540 | 133 | // align into instruction word. |
gatedClock | 16:0e422fd263c6 | 134 | pcSend[1] = ((cRegister & 0x07) << 2) | 0xA0; |
gatedClock | 18:4a29cad91540 | 135 | pcSend[0] = cValue & 0xFF; // immediate value to i.w. |
gatedClock | 17:b81c0c1f312f | 136 | |
gatedClock | 23:dbd89a56716d | 137 | transceive_vector(); // transmit command. |
gatedClock | 18:4a29cad91540 | 138 | |
gatedClock | 24:d3b8c68f41f2 | 139 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 17:b81c0c1f312f | 140 | } |
gatedClock | 17:b81c0c1f312f | 141 | //----------------------------------------------//------------------------------ |
gatedClock | 17:b81c0c1f312f | 142 | // returns the content of |
gatedClock | 17:b81c0c1f312f | 143 | // a CPU register. |
gatedClock | 23:dbd89a56716d | 144 | char mmSPI::read_register(char cRegister) |
gatedClock | 17:b81c0c1f312f | 145 | { |
gatedClock | 24:d3b8c68f41f2 | 146 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 147 | |
gatedClock | 23:dbd89a56716d | 148 | transceive_vector(); // snap & scan-out reg contents. |
gatedClock | 17:b81c0c1f312f | 149 | |
gatedClock | 19:c2b753533b93 | 150 | return (pcReceive[6 - cRegister]); // return the particular reg value. |
gatedClock | 17:b81c0c1f312f | 151 | } |
gatedClock | 17:b81c0c1f312f | 152 | //----------------------------------------------//------------------------------ |
gatedClock | 23:dbd89a56716d | 153 | void mmSPI::write_memory(char cHData, char cLdata, char cAddress) |
gatedClock | 18:4a29cad91540 | 154 | { |
gatedClock | 24:d3b8c68f41f2 | 155 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 24:d3b8c68f41f2 | 156 | |
gatedClock | 24:d3b8c68f41f2 | 157 | write_register(0x03,cAddress); // R3 <- address. |
gatedClock | 24:d3b8c68f41f2 | 158 | write_register(0x02,cHData); // R2 <- high-data. |
gatedClock | 24:d3b8c68f41f2 | 159 | write_register(0x01,cLdata); // R1 <- low-data. |
gatedClock | 18:4a29cad91540 | 160 | |
gatedClock | 20:2d5cd38047ca | 161 | pcSend[7] = 0x00; // write-enable high. |
gatedClock | 20:2d5cd38047ca | 162 | pcSend[1] = 0x02; |
gatedClock | 20:2d5cd38047ca | 163 | pcSend[0] = 0x00; |
gatedClock | 23:dbd89a56716d | 164 | transceive_vector(); |
gatedClock | 18:4a29cad91540 | 165 | |
gatedClock | 20:2d5cd38047ca | 166 | pcSend[7] = 0x00; // write-enable low. |
gatedClock | 20:2d5cd38047ca | 167 | pcSend[1] = 0x00; |
gatedClock | 20:2d5cd38047ca | 168 | pcSend[0] = 0x00; |
gatedClock | 23:dbd89a56716d | 169 | transceive_vector(); |
gatedClock | 24:d3b8c68f41f2 | 170 | |
gatedClock | 24:d3b8c68f41f2 | 171 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 172 | } |
gatedClock | 18:4a29cad91540 | 173 | //----------------------------------------------//------------------------------ |
gatedClock | 18:4a29cad91540 | 174 | // fetch a word from main memory. |
gatedClock | 23:dbd89a56716d | 175 | unsigned int mmSPI::read_memory(char cAddress) |
gatedClock | 24:d3b8c68f41f2 | 176 | { |
gatedClock | 18:4a29cad91540 | 177 | unsigned int udMemoryContent; // return variable. |
gatedClock | 18:4a29cad91540 | 178 | char cHData; // returned data-high. |
gatedClock | 18:4a29cad91540 | 179 | char cLData; // returned data-low. |
gatedClock | 24:d3b8c68f41f2 | 180 | |
gatedClock | 24:d3b8c68f41f2 | 181 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 21:e90dd0f8aaa1 | 182 | |
gatedClock | 24:d3b8c68f41f2 | 183 | write_register(0x03,cAddress); // R3 <= address. |
gatedClock | 18:4a29cad91540 | 184 | |
gatedClock | 18:4a29cad91540 | 185 | pcSend[7] = 0x02; // mbed sends command. |
gatedClock | 20:2d5cd38047ca | 186 | pcSend[1] = 0xC8; // R2 <- MM[R3] |
gatedClock | 18:4a29cad91540 | 187 | pcSend[0] = 0x00; |
gatedClock | 23:dbd89a56716d | 188 | transceive_vector(); // send command. |
gatedClock | 18:4a29cad91540 | 189 | |
gatedClock | 18:4a29cad91540 | 190 | pcSend[7] = 0x02; // mbed sends command. |
gatedClock | 20:2d5cd38047ca | 191 | pcSend[1] = 0xC4; // R1 <- MM[R3] |
gatedClock | 18:4a29cad91540 | 192 | pcSend[0] = 0x00; |
gatedClock | 23:dbd89a56716d | 193 | transceive_vector(); // send command. |
gatedClock | 18:4a29cad91540 | 194 | |
gatedClock | 24:d3b8c68f41f2 | 195 | cHData = read_register(0x02); // obtain MM high-data-byte. |
gatedClock | 24:d3b8c68f41f2 | 196 | cLData = read_register(0x01); // obtain MM low-data-byte. |
gatedClock | 18:4a29cad91540 | 197 | |
gatedClock | 18:4a29cad91540 | 198 | |
gatedClock | 18:4a29cad91540 | 199 | udMemoryContent = (cHData << 8) + cLData; // build the memory word. |
gatedClock | 24:d3b8c68f41f2 | 200 | |
gatedClock | 24:d3b8c68f41f2 | 201 | clear_transmit_vector(); // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 202 | |
gatedClock | 18:4a29cad91540 | 203 | return udMemoryContent; // return the memory word. |
gatedClock | 18:4a29cad91540 | 204 | } |
gatedClock | 18:4a29cad91540 | 205 | //----------------------------------------------//------------------------------ |
gatedClock | 24:d3b8c68f41f2 | 206 | void mmSPI::clear_transmit_vector(void) // fill transmit buffer with 0. |
gatedClock | 24:d3b8c68f41f2 | 207 | { |
gatedClock | 24:d3b8c68f41f2 | 208 | int dLoop; |
gatedClock | 24:d3b8c68f41f2 | 209 | for (dLoop = 0; dLoop < dNumBytes; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 24:d3b8c68f41f2 | 210 | } |
gatedClock | 24:d3b8c68f41f2 | 211 | //----------------------------------------------//------------------------------ |
gatedClock | 17:b81c0c1f312f | 212 | |
gatedClock | 17:b81c0c1f312f | 213 | |
gatedClock | 5:b14dcaae260e | 214 | |
gatedClock | 5:b14dcaae260e | 215 | |
gatedClock | 5:b14dcaae260e | 216 | |
gatedClock | 5:b14dcaae260e | 217 | |
gatedClock | 5:b14dcaae260e | 218 | |
gatedClock | 5:b14dcaae260e | 219 | |
gatedClock | 5:b14dcaae260e | 220 | |
gatedClock | 7:b3e8b537d5c2 | 221 | |
gatedClock | 7:b3e8b537d5c2 | 222 | |
gatedClock | 7:b3e8b537d5c2 | 223 | |
gatedClock | 7:b3e8b537d5c2 | 224 |