SPI library used to communicate with an altera development board attached to four zigbee-header pins.
mmSPI.cpp@18:4a29cad91540, 2013-08-19 (annotated)
- Committer:
- gatedClock
- Date:
- Mon Aug 19 20:45:29 2013 +0000
- Revision:
- 18:4a29cad91540
- Parent:
- 17:b81c0c1f312f
- Child:
- 19:c2b753533b93
latest write_register.
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 | 0:fb42c5acf810 | 52 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 53 | // we're not going for speed, so lets go for good setup / hold. |
gatedClock | 6:b480fc4e87e5 | 54 | |
gatedClock | 6:b480fc4e87e5 | 55 | // send/receive a byte over SPI. |
gatedClock | 7:b3e8b537d5c2 | 56 | // MSB out/in first. |
gatedClock | 6:b480fc4e87e5 | 57 | void mmSPI::transceive_byte(char *cReceive, char *cSend) |
gatedClock | 1:15706d15d123 | 58 | { |
gatedClock | 6:b480fc4e87e5 | 59 | *cReceive = 0; // clear receive byte. |
gatedClock | 12:a1b7ce9c1d64 | 60 | for (dLoop01 = 7; dLoop01 >= 0; dLoop01--)// loop for 8 bits in the byte. |
gatedClock | 5:b14dcaae260e | 61 | { |
gatedClock | 5:b14dcaae260e | 62 | *pSCLK = 0; // SPI clock negedge. |
gatedClock | 5:b14dcaae260e | 63 | wait(fSPIquarterP); // until middle of clock low. |
gatedClock | 12:a1b7ce9c1d64 | 64 | *pMOSI = (*cSend >> dLoop01) & 1; // assert MOSI. |
gatedClock | 15:d6cc57c4e23d | 65 | // capture MISO. |
gatedClock | 15:d6cc57c4e23d | 66 | *cReceive = *cReceive | (*pMISO << dLoop01); |
gatedClock | 15:d6cc57c4e23d | 67 | wait(fSPIquarterP); // finish-out cycle. |
gatedClock | 5:b14dcaae260e | 68 | *pSCLK = 1; // SPI clock posedge. |
gatedClock | 15:d6cc57c4e23d | 69 | wait(fSPIquarterP); // finish-out cycle. |
gatedClock | 5:b14dcaae260e | 70 | wait(fSPIquarterP); // finish-out cycle. |
gatedClock | 5:b14dcaae260e | 71 | } |
gatedClock | 1:15706d15d123 | 72 | } |
gatedClock | 5:b14dcaae260e | 73 | //----------------------------------------------//------------------------------ |
gatedClock | 7:b3e8b537d5c2 | 74 | // transceive a character array. |
gatedClock | 7:b3e8b537d5c2 | 75 | // limit is 256 characters. |
gatedClock | 7:b3e8b537d5c2 | 76 | // MSB out/in first. |
gatedClock | 7:b3e8b537d5c2 | 77 | void mmSPI::transceive_vector(char *cReceive, char *cSend, char cNumBytes) |
gatedClock | 13:3e6886a96aea | 78 | { |
gatedClock | 16:0e422fd263c6 | 79 | |
gatedClock | 16:0e422fd263c6 | 80 | |
gatedClock | 13:3e6886a96aea | 81 | |
gatedClock | 12:a1b7ce9c1d64 | 82 | for (dLoop02 = (cNumBytes - 1); dLoop02 >= 0; dLoop02--) |
gatedClock | 12:a1b7ce9c1d64 | 83 | transceive_byte(&(cReceive[dLoop02]), &(cSend[dLoop02])); |
gatedClock | 13:3e6886a96aea | 84 | |
gatedClock | 15:d6cc57c4e23d | 85 | |
gatedClock | 13:3e6886a96aea | 86 | |
gatedClock | 13:3e6886a96aea | 87 | *pCPUclk = 1; // pulse the CPU clock. |
gatedClock | 13:3e6886a96aea | 88 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 89 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 90 | *pCPUclk = 0; |
gatedClock | 13:3e6886a96aea | 91 | wait(fSPIquarterP); |
gatedClock | 13:3e6886a96aea | 92 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 93 | |
gatedClock | 16:0e422fd263c6 | 94 | |
gatedClock | 16:0e422fd263c6 | 95 | |
gatedClock | 16:0e422fd263c6 | 96 | if (0) |
gatedClock | 16:0e422fd263c6 | 97 | { |
gatedClock | 16:0e422fd263c6 | 98 | *pSCLK = 1; |
gatedClock | 16:0e422fd263c6 | 99 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 100 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 101 | *pSCLK = 0; |
gatedClock | 16:0e422fd263c6 | 102 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 103 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 104 | } |
gatedClock | 16:0e422fd263c6 | 105 | } |
gatedClock | 16:0e422fd263c6 | 106 | //----------------------------------------------//------------------------------ |
gatedClock | 16:0e422fd263c6 | 107 | // transceive a character array. |
gatedClock | 16:0e422fd263c6 | 108 | // limit is 256 characters. |
gatedClock | 16:0e422fd263c6 | 109 | // MSB out/in first. |
gatedClock | 16:0e422fd263c6 | 110 | void mmSPI::transceive_vector2(char *pcReceive, char *pcSend, int dNumBytes) |
gatedClock | 16:0e422fd263c6 | 111 | { |
gatedClock | 16:0e422fd263c6 | 112 | int dClear; |
gatedClock | 16:0e422fd263c6 | 113 | int dIndex; |
gatedClock | 16:0e422fd263c6 | 114 | int dMosiByteIndex; |
gatedClock | 16:0e422fd263c6 | 115 | int dMosiBitIndex; |
gatedClock | 16:0e422fd263c6 | 116 | int dMisoByteIndex; |
gatedClock | 16:0e422fd263c6 | 117 | int dMisoBitIndex; |
gatedClock | 16:0e422fd263c6 | 118 | |
gatedClock | 16:0e422fd263c6 | 119 | dIndex = (dNumBytes * 8) - 1; |
gatedClock | 16:0e422fd263c6 | 120 | dMosiByteIndex = dIndex / 8; |
gatedClock | 16:0e422fd263c6 | 121 | dMosiBitIndex = dIndex % 8; |
gatedClock | 16:0e422fd263c6 | 122 | |
gatedClock | 16:0e422fd263c6 | 123 | for (dClear = 0; dClear < dNumBytes; dClear++) pcReceive[dClear] = 0; |
gatedClock | 16:0e422fd263c6 | 124 | |
gatedClock | 16:0e422fd263c6 | 125 | |
gatedClock | 16:0e422fd263c6 | 126 | *pCPUclk = 1; // pulse the CPU clock. |
gatedClock | 16:0e422fd263c6 | 127 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 128 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 129 | *pCPUclk = 0; |
gatedClock | 16:0e422fd263c6 | 130 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 131 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 132 | |
gatedClock | 16:0e422fd263c6 | 133 | *pSCLK = 1; // pulse the SPI clock for parallel load. |
gatedClock | 16:0e422fd263c6 | 134 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 135 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 136 | *pSCLK = 0; |
gatedClock | 16:0e422fd263c6 | 137 | // pre-assert MOSI. |
gatedClock | 16:0e422fd263c6 | 138 | *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; |
gatedClock | 16:0e422fd263c6 | 139 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 140 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 141 | |
gatedClock | 16:0e422fd263c6 | 142 | |
gatedClock | 16:0e422fd263c6 | 143 | for (dIndex = (dNumBytes * 8) - 1; dIndex >= 0; dIndex--) |
gatedClock | 16:0e422fd263c6 | 144 | { |
gatedClock | 16:0e422fd263c6 | 145 | dMisoByteIndex = dIndex / 8; |
gatedClock | 16:0e422fd263c6 | 146 | dMisoBitIndex = dIndex % 8; |
gatedClock | 18:4a29cad91540 | 147 | pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex); |
gatedClock | 16:0e422fd263c6 | 148 | *pSCLK = 1; |
gatedClock | 16:0e422fd263c6 | 149 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 150 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 151 | *pSCLK = 0; |
gatedClock | 16:0e422fd263c6 | 152 | |
gatedClock | 16:0e422fd263c6 | 153 | if (dIndex < 0) dIndex = 0; |
gatedClock | 16:0e422fd263c6 | 154 | dMosiByteIndex = (dIndex - 1) / 8; |
gatedClock | 16:0e422fd263c6 | 155 | dMosiBitIndex = (dIndex - 1) % 8; |
gatedClock | 16:0e422fd263c6 | 156 | *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1; |
gatedClock | 16:0e422fd263c6 | 157 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 158 | wait(fSPIquarterP); |
gatedClock | 16:0e422fd263c6 | 159 | } |
gatedClock | 7:b3e8b537d5c2 | 160 | } |
gatedClock | 7:b3e8b537d5c2 | 161 | //----------------------------------------------//------------------------------ |
gatedClock | 9:0551307e3b15 | 162 | // transceive a character array. |
gatedClock | 9:0551307e3b15 | 163 | // limit is 256 characters. |
gatedClock | 9:0551307e3b15 | 164 | // MSB out/in first. |
gatedClock | 9:0551307e3b15 | 165 | void mmSPI::test_toggle_cpu_clock(void) |
gatedClock | 9:0551307e3b15 | 166 | { |
gatedClock | 11:17207edac925 | 167 | DigitalOut led0(LED4); |
gatedClock | 9:0551307e3b15 | 168 | while (1) |
gatedClock | 9:0551307e3b15 | 169 | { |
gatedClock | 11:17207edac925 | 170 | *pCPUclk = 1; led0 = 1; |
gatedClock | 9:0551307e3b15 | 171 | wait(1.0); |
gatedClock | 11:17207edac925 | 172 | *pCPUclk = 0; led0 = 0; |
gatedClock | 9:0551307e3b15 | 173 | wait(1.0); |
gatedClock | 9:0551307e3b15 | 174 | } |
gatedClock | 9:0551307e3b15 | 175 | } |
gatedClock | 9:0551307e3b15 | 176 | //----------------------------------------------//------------------------------ |
gatedClock | 15:d6cc57c4e23d | 177 | void mmSPI::force_write(char cDataHIgh, char cDataLow, char cAddress) |
gatedClock | 15:d6cc57c4e23d | 178 | { |
gatedClock | 15:d6cc57c4e23d | 179 | char pcReceive[8]; |
gatedClock | 15:d6cc57c4e23d | 180 | char pcSend [8]; |
gatedClock | 15:d6cc57c4e23d | 181 | int dLoop; |
gatedClock | 15:d6cc57c4e23d | 182 | |
gatedClock | 15:d6cc57c4e23d | 183 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0; |
gatedClock | 15:d6cc57c4e23d | 184 | |
gatedClock | 15:d6cc57c4e23d | 185 | |
gatedClock | 15:d6cc57c4e23d | 186 | // high data to R2. |
gatedClock | 15:d6cc57c4e23d | 187 | pcSend[7] = 0x02; pcSend[1] = 0xA8; pcSend[0] = cDataHIgh; |
gatedClock | 15:d6cc57c4e23d | 188 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 5:b14dcaae260e | 189 | |
gatedClock | 5:b14dcaae260e | 190 | |
gatedClock | 15:d6cc57c4e23d | 191 | // low data to R1. |
gatedClock | 15:d6cc57c4e23d | 192 | pcSend[7] = 0x02; pcSend[1] = 0xA4; pcSend[0] = cDataLow; |
gatedClock | 15:d6cc57c4e23d | 193 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 194 | |
gatedClock | 15:d6cc57c4e23d | 195 | |
gatedClock | 15:d6cc57c4e23d | 196 | // address to R3. |
gatedClock | 15:d6cc57c4e23d | 197 | pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress; |
gatedClock | 15:d6cc57c4e23d | 198 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 199 | |
gatedClock | 15:d6cc57c4e23d | 200 | |
gatedClock | 15:d6cc57c4e23d | 201 | |
gatedClock | 15:d6cc57c4e23d | 202 | pcSend[7] = 0x02; pcSend[1] = 0x02; pcSend[0] = 0; // WE high. |
gatedClock | 15:d6cc57c4e23d | 203 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 204 | |
gatedClock | 15:d6cc57c4e23d | 205 | pcSend[7] = 0x02; pcSend[1] = 0x00; pcSend[0] = 0; // WE low. |
gatedClock | 15:d6cc57c4e23d | 206 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 207 | |
gatedClock | 15:d6cc57c4e23d | 208 | } |
gatedClock | 15:d6cc57c4e23d | 209 | //----------------------------------------------//------------------------------ |
gatedClock | 14:35717622a4fb | 210 | |
gatedClock | 15:d6cc57c4e23d | 211 | void mmSPI::force_read(char cAddress) |
gatedClock | 15:d6cc57c4e23d | 212 | { |
gatedClock | 15:d6cc57c4e23d | 213 | char pcReceive[8]; |
gatedClock | 15:d6cc57c4e23d | 214 | char pcSend [8]; |
gatedClock | 15:d6cc57c4e23d | 215 | int dLoop; |
gatedClock | 15:d6cc57c4e23d | 216 | |
gatedClock | 15:d6cc57c4e23d | 217 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0; |
gatedClock | 15:d6cc57c4e23d | 218 | |
gatedClock | 15:d6cc57c4e23d | 219 | |
gatedClock | 15:d6cc57c4e23d | 220 | |
gatedClock | 15:d6cc57c4e23d | 221 | |
gatedClock | 15:d6cc57c4e23d | 222 | // address to R3. |
gatedClock | 15:d6cc57c4e23d | 223 | pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress; |
gatedClock | 15:d6cc57c4e23d | 224 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 225 | |
gatedClock | 15:d6cc57c4e23d | 226 | // R2 gets data-H from memory. |
gatedClock | 15:d6cc57c4e23d | 227 | pcSend[7] = 0x02; pcSend[1] = 0xC8; pcSend[0] = cAddress; |
gatedClock | 15:d6cc57c4e23d | 228 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 229 | |
gatedClock | 15:d6cc57c4e23d | 230 | // R1 gets data-L from memory. |
gatedClock | 15:d6cc57c4e23d | 231 | pcSend[7] = 0x02; pcSend[1] = 0xC4; pcSend[0] = cAddress; |
gatedClock | 15:d6cc57c4e23d | 232 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 233 | |
gatedClock | 15:d6cc57c4e23d | 234 | |
gatedClock | 15:d6cc57c4e23d | 235 | |
gatedClock | 15:d6cc57c4e23d | 236 | |
gatedClock | 15:d6cc57c4e23d | 237 | // pcSend[7] = 0x02; // force IR. |
gatedClock | 15:d6cc57c4e23d | 238 | // pcSend[1] = 0xA4; // R1 <- immediate. |
gatedClock | 15:d6cc57c4e23d | 239 | // pcSend[0] = 0xEE; // immediate value. |
gatedClock | 15:d6cc57c4e23d | 240 | /// transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 241 | |
gatedClock | 15:d6cc57c4e23d | 242 | |
gatedClock | 15:d6cc57c4e23d | 243 | |
gatedClock | 15:d6cc57c4e23d | 244 | // no-op scan. |
gatedClock | 15:d6cc57c4e23d | 245 | pcSend[7] = 0x02; pcSend[1] = 0x0; pcSend[0] = 0; |
gatedClock | 15:d6cc57c4e23d | 246 | transceive_vector(pcReceive, pcSend, 8); |
gatedClock | 15:d6cc57c4e23d | 247 | |
gatedClock | 15:d6cc57c4e23d | 248 | } |
gatedClock | 14:35717622a4fb | 249 | //----------------------------------------------//------------------------------ |
gatedClock | 16:0e422fd263c6 | 250 | void mmSPI::write_register(char cRegister, char cValue, char * pcReceive, char * pcSend) |
gatedClock | 16:0e422fd263c6 | 251 | { |
gatedClock | 18:4a29cad91540 | 252 | int dLoop; // loop index. |
gatedClock | 18:4a29cad91540 | 253 | |
gatedClock | 18:4a29cad91540 | 254 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 255 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 256 | |
gatedClock | 18:4a29cad91540 | 257 | pcSend[7] = 0x02; // mbed sends a command. |
gatedClock | 18:4a29cad91540 | 258 | |
gatedClock | 18:4a29cad91540 | 259 | // align into instruction word. |
gatedClock | 16:0e422fd263c6 | 260 | pcSend[1] = ((cRegister & 0x07) << 2) | 0xA0; |
gatedClock | 18:4a29cad91540 | 261 | pcSend[0] = cValue & 0xFF; // immediate value to i.w. |
gatedClock | 17:b81c0c1f312f | 262 | |
gatedClock | 18:4a29cad91540 | 263 | transceive_vector2(pcReceive, pcSend, 8); // transmit command. |
gatedClock | 18:4a29cad91540 | 264 | |
gatedClock | 18:4a29cad91540 | 265 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 266 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 17:b81c0c1f312f | 267 | } |
gatedClock | 17:b81c0c1f312f | 268 | //----------------------------------------------//------------------------------ |
gatedClock | 17:b81c0c1f312f | 269 | // returns the content of |
gatedClock | 17:b81c0c1f312f | 270 | // a CPU register. |
gatedClock | 17:b81c0c1f312f | 271 | char mmSPI::read_register(char cRegister, char * pcReceive, char * pcSend) |
gatedClock | 17:b81c0c1f312f | 272 | { |
gatedClock | 17:b81c0c1f312f | 273 | int dLoop; // send all 0. |
gatedClock | 18:4a29cad91540 | 274 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 275 | |
gatedClock | 17:b81c0c1f312f | 276 | transceive_vector2(pcReceive, pcSend, 8); // snap & scan-out reg contents. |
gatedClock | 17:b81c0c1f312f | 277 | |
gatedClock | 17:b81c0c1f312f | 278 | return (pcReceive[cRegister]); // return the particular reg value. |
gatedClock | 17:b81c0c1f312f | 279 | } |
gatedClock | 17:b81c0c1f312f | 280 | //----------------------------------------------//------------------------------ |
gatedClock | 18:4a29cad91540 | 281 | void mmSPI::write_memory(char cHData, char cLdata, char cAddress, char * pcReceive, char * pcSend) |
gatedClock | 18:4a29cad91540 | 282 | { |
gatedClock | 18:4a29cad91540 | 283 | int dLoop; // loop index. |
gatedClock | 18:4a29cad91540 | 284 | |
gatedClock | 18:4a29cad91540 | 285 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 286 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 287 | |
gatedClock | 18:4a29cad91540 | 288 | // R3 <- address. |
gatedClock | 18:4a29cad91540 | 289 | // R2 <- high-data. |
gatedClock | 18:4a29cad91540 | 290 | // R1 <- low-data. |
gatedClock | 18:4a29cad91540 | 291 | write_register(0x03,cAddress, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 292 | write_register(0x02,cHData, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 293 | write_register(0x01,cLdata, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 294 | |
gatedClock | 18:4a29cad91540 | 295 | pcSend[7] = 0x02; // write-enable high. |
gatedClock | 18:4a29cad91540 | 296 | pcSend[1] = 0x02; |
gatedClock | 18:4a29cad91540 | 297 | pcSend[0] = 0x00; |
gatedClock | 18:4a29cad91540 | 298 | |
gatedClock | 18:4a29cad91540 | 299 | |
gatedClock | 18:4a29cad91540 | 300 | transceive_vector2(pcReceive, pcSend, 8); |
gatedClock | 18:4a29cad91540 | 301 | |
gatedClock | 18:4a29cad91540 | 302 | pcSend[7] = 0x02; // write-enable low. |
gatedClock | 18:4a29cad91540 | 303 | pcSend[1] = 0x00; |
gatedClock | 18:4a29cad91540 | 304 | pcSend[0] = 0x00; |
gatedClock | 18:4a29cad91540 | 305 | transceive_vector2(pcReceive, pcSend, 8); |
gatedClock | 18:4a29cad91540 | 306 | |
gatedClock | 18:4a29cad91540 | 307 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 308 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 309 | } |
gatedClock | 18:4a29cad91540 | 310 | //----------------------------------------------//------------------------------ |
gatedClock | 18:4a29cad91540 | 311 | // fetch a word from main memory. |
gatedClock | 18:4a29cad91540 | 312 | unsigned int mmSPI::read_memory(char cAddress, char * pcReceive, char * pcSend) |
gatedClock | 18:4a29cad91540 | 313 | { |
gatedClock | 18:4a29cad91540 | 314 | int dLoop; // loop index. |
gatedClock | 18:4a29cad91540 | 315 | unsigned int udMemoryContent; // return variable. |
gatedClock | 18:4a29cad91540 | 316 | char cHData; // returned data-high. |
gatedClock | 18:4a29cad91540 | 317 | char cLData; // returned data-low. |
gatedClock | 18:4a29cad91540 | 318 | |
gatedClock | 18:4a29cad91540 | 319 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 320 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 321 | |
gatedClock | 18:4a29cad91540 | 322 | // R3 <- address. |
gatedClock | 18:4a29cad91540 | 323 | write_register(0x03,cAddress, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 324 | |
gatedClock | 18:4a29cad91540 | 325 | pcSend[7] = 0x02; // mbed sends command. |
gatedClock | 18:4a29cad91540 | 326 | pcSend[1] = 0x68; // R2 <- MM[R3] |
gatedClock | 18:4a29cad91540 | 327 | pcSend[0] = 0x00; |
gatedClock | 18:4a29cad91540 | 328 | transceive_vector2(pcReceive, pcSend, 8); // send command. |
gatedClock | 18:4a29cad91540 | 329 | |
gatedClock | 18:4a29cad91540 | 330 | pcSend[7] = 0x02; // mbed sends command. |
gatedClock | 18:4a29cad91540 | 331 | pcSend[1] = 0x64; // R1 <- MM[R3] |
gatedClock | 18:4a29cad91540 | 332 | pcSend[0] = 0x00; |
gatedClock | 18:4a29cad91540 | 333 | transceive_vector2(pcReceive, pcSend, 8); // send command. |
gatedClock | 18:4a29cad91540 | 334 | |
gatedClock | 18:4a29cad91540 | 335 | // obtain MM content. |
gatedClock | 18:4a29cad91540 | 336 | cHData = read_register(0x02, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 337 | cLData = read_register(0x01, pcReceive, pcSend); |
gatedClock | 18:4a29cad91540 | 338 | |
gatedClock | 18:4a29cad91540 | 339 | |
gatedClock | 18:4a29cad91540 | 340 | udMemoryContent = (cHData << 8) + cLData; // build the memory word. |
gatedClock | 18:4a29cad91540 | 341 | |
gatedClock | 18:4a29cad91540 | 342 | // clear transmit vector. |
gatedClock | 18:4a29cad91540 | 343 | for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; |
gatedClock | 18:4a29cad91540 | 344 | |
gatedClock | 18:4a29cad91540 | 345 | return udMemoryContent; // return the memory word. |
gatedClock | 18:4a29cad91540 | 346 | } |
gatedClock | 18:4a29cad91540 | 347 | //----------------------------------------------//------------------------------ |
gatedClock | 17:b81c0c1f312f | 348 | |
gatedClock | 17:b81c0c1f312f | 349 | |
gatedClock | 17:b81c0c1f312f | 350 | void mmSPI::write_pulse(char * pcReceive, char * pcSend) |
gatedClock | 17:b81c0c1f312f | 351 | { |
gatedClock | 17:b81c0c1f312f | 352 | pcSend[7] = 0x02; // write-enable high. |
gatedClock | 17:b81c0c1f312f | 353 | pcSend[1] = 0x02; |
gatedClock | 17:b81c0c1f312f | 354 | pcSend[0] = 0x00; |
gatedClock | 17:b81c0c1f312f | 355 | transceive_vector2(pcReceive, pcSend, 8); |
gatedClock | 17:b81c0c1f312f | 356 | |
gatedClock | 17:b81c0c1f312f | 357 | pcSend[7] = 0x02; // write-enable low. |
gatedClock | 17:b81c0c1f312f | 358 | pcSend[1] = 0x00; |
gatedClock | 17:b81c0c1f312f | 359 | pcSend[0] = 0x00; |
gatedClock | 17:b81c0c1f312f | 360 | transceive_vector2(pcReceive, pcSend, 8); |
gatedClock | 17:b81c0c1f312f | 361 | |
gatedClock | 17:b81c0c1f312f | 362 | pcSend[7] = 0x00; |
gatedClock | 17:b81c0c1f312f | 363 | pcSend[6] = 0x00; |
gatedClock | 17:b81c0c1f312f | 364 | pcSend[5] = 0x00; |
gatedClock | 17:b81c0c1f312f | 365 | pcSend[4] = 0x00; |
gatedClock | 17:b81c0c1f312f | 366 | pcSend[3] = 0x00; |
gatedClock | 17:b81c0c1f312f | 367 | pcSend[2] = 0x00; |
gatedClock | 17:b81c0c1f312f | 368 | pcSend[1] = 0x00; |
gatedClock | 17:b81c0c1f312f | 369 | pcSend[0] = 0x00; |
gatedClock | 16:0e422fd263c6 | 370 | } |
gatedClock | 16:0e422fd263c6 | 371 | //----------------------------------------------//------------------------------ |
gatedClock | 5:b14dcaae260e | 372 | |
gatedClock | 5:b14dcaae260e | 373 | |
gatedClock | 5:b14dcaae260e | 374 | |
gatedClock | 5:b14dcaae260e | 375 | |
gatedClock | 5:b14dcaae260e | 376 | |
gatedClock | 5:b14dcaae260e | 377 | |
gatedClock | 5:b14dcaae260e | 378 | |
gatedClock | 7:b3e8b537d5c2 | 379 | |
gatedClock | 7:b3e8b537d5c2 | 380 | |
gatedClock | 7:b3e8b537d5c2 | 381 | |
gatedClock | 7:b3e8b537d5c2 | 382 | |
gatedClock | 7:b3e8b537d5c2 | 383 | |
gatedClock | 15:d6cc57c4e23d | 384 | |
gatedClock | 15:d6cc57c4e23d | 385 |