SPI library used to communicate with an altera development board attached to four zigbee-header pins.

Committer:
gatedClock
Date:
Mon Aug 19 16:26:29 2013 +0000
Revision:
16:0e422fd263c6
Parent:
15:d6cc57c4e23d
Child:
17:b81c0c1f312f
first evidence loaded shadows from registers & read-out to mbed.

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 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 16:0e422fd263c6 147 pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex);
gatedClock 16:0e422fd263c6 148
gatedClock 16:0e422fd263c6 149 // pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (0x23 << dMisoBitIndex);
gatedClock 16:0e422fd263c6 150
gatedClock 16:0e422fd263c6 151 *pSCLK = 1;
gatedClock 16:0e422fd263c6 152 wait(fSPIquarterP);
gatedClock 16:0e422fd263c6 153 wait(fSPIquarterP);
gatedClock 16:0e422fd263c6 154 *pSCLK = 0;
gatedClock 16:0e422fd263c6 155
gatedClock 16:0e422fd263c6 156 if (dIndex < 0) dIndex = 0;
gatedClock 16:0e422fd263c6 157 dMosiByteIndex = (dIndex - 1) / 8;
gatedClock 16:0e422fd263c6 158 dMosiBitIndex = (dIndex - 1) % 8;
gatedClock 16:0e422fd263c6 159 *pMOSI = ((pcSend[dMosiByteIndex]) >> dMosiBitIndex) & 1;
gatedClock 16:0e422fd263c6 160 wait(fSPIquarterP);
gatedClock 16:0e422fd263c6 161 wait(fSPIquarterP);
gatedClock 16:0e422fd263c6 162 }
gatedClock 7:b3e8b537d5c2 163 }
gatedClock 7:b3e8b537d5c2 164 //----------------------------------------------//------------------------------
gatedClock 9:0551307e3b15 165 // transceive a character array.
gatedClock 9:0551307e3b15 166 // limit is 256 characters.
gatedClock 9:0551307e3b15 167 // MSB out/in first.
gatedClock 9:0551307e3b15 168 void mmSPI::test_toggle_cpu_clock(void)
gatedClock 9:0551307e3b15 169 {
gatedClock 11:17207edac925 170 DigitalOut led0(LED4);
gatedClock 9:0551307e3b15 171 while (1)
gatedClock 9:0551307e3b15 172 {
gatedClock 11:17207edac925 173 *pCPUclk = 1; led0 = 1;
gatedClock 9:0551307e3b15 174 wait(1.0);
gatedClock 11:17207edac925 175 *pCPUclk = 0; led0 = 0;
gatedClock 9:0551307e3b15 176 wait(1.0);
gatedClock 9:0551307e3b15 177 }
gatedClock 9:0551307e3b15 178 }
gatedClock 9:0551307e3b15 179 //----------------------------------------------//------------------------------
gatedClock 15:d6cc57c4e23d 180 void mmSPI::force_write(char cDataHIgh, char cDataLow, char cAddress)
gatedClock 15:d6cc57c4e23d 181 {
gatedClock 15:d6cc57c4e23d 182 char pcReceive[8];
gatedClock 15:d6cc57c4e23d 183 char pcSend [8];
gatedClock 15:d6cc57c4e23d 184 int dLoop;
gatedClock 15:d6cc57c4e23d 185
gatedClock 15:d6cc57c4e23d 186 for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0;
gatedClock 15:d6cc57c4e23d 187
gatedClock 15:d6cc57c4e23d 188
gatedClock 15:d6cc57c4e23d 189 // high data to R2.
gatedClock 15:d6cc57c4e23d 190 pcSend[7] = 0x02; pcSend[1] = 0xA8; pcSend[0] = cDataHIgh;
gatedClock 15:d6cc57c4e23d 191 transceive_vector(pcReceive, pcSend, 8);
gatedClock 5:b14dcaae260e 192
gatedClock 5:b14dcaae260e 193
gatedClock 15:d6cc57c4e23d 194 // low data to R1.
gatedClock 15:d6cc57c4e23d 195 pcSend[7] = 0x02; pcSend[1] = 0xA4; pcSend[0] = cDataLow;
gatedClock 15:d6cc57c4e23d 196 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 197
gatedClock 15:d6cc57c4e23d 198
gatedClock 15:d6cc57c4e23d 199 // address to R3.
gatedClock 15:d6cc57c4e23d 200 pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress;
gatedClock 15:d6cc57c4e23d 201 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 202
gatedClock 15:d6cc57c4e23d 203
gatedClock 15:d6cc57c4e23d 204
gatedClock 15:d6cc57c4e23d 205 pcSend[7] = 0x02; pcSend[1] = 0x02; pcSend[0] = 0; // WE high.
gatedClock 15:d6cc57c4e23d 206 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 207
gatedClock 15:d6cc57c4e23d 208 pcSend[7] = 0x02; pcSend[1] = 0x00; pcSend[0] = 0; // WE low.
gatedClock 15:d6cc57c4e23d 209 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 210
gatedClock 15:d6cc57c4e23d 211 }
gatedClock 15:d6cc57c4e23d 212 //----------------------------------------------//------------------------------
gatedClock 14:35717622a4fb 213
gatedClock 15:d6cc57c4e23d 214 void mmSPI::force_read(char cAddress)
gatedClock 15:d6cc57c4e23d 215 {
gatedClock 15:d6cc57c4e23d 216 char pcReceive[8];
gatedClock 15:d6cc57c4e23d 217 char pcSend [8];
gatedClock 15:d6cc57c4e23d 218 int dLoop;
gatedClock 15:d6cc57c4e23d 219
gatedClock 15:d6cc57c4e23d 220 for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0;
gatedClock 15:d6cc57c4e23d 221
gatedClock 15:d6cc57c4e23d 222
gatedClock 15:d6cc57c4e23d 223
gatedClock 15:d6cc57c4e23d 224
gatedClock 15:d6cc57c4e23d 225 // address to R3.
gatedClock 15:d6cc57c4e23d 226 pcSend[7] = 0x02; pcSend[1] = 0xAC; pcSend[0] = cAddress;
gatedClock 15:d6cc57c4e23d 227 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 228
gatedClock 15:d6cc57c4e23d 229 // R2 gets data-H from memory.
gatedClock 15:d6cc57c4e23d 230 pcSend[7] = 0x02; pcSend[1] = 0xC8; pcSend[0] = cAddress;
gatedClock 15:d6cc57c4e23d 231 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 232
gatedClock 15:d6cc57c4e23d 233 // R1 gets data-L from memory.
gatedClock 15:d6cc57c4e23d 234 pcSend[7] = 0x02; pcSend[1] = 0xC4; pcSend[0] = cAddress;
gatedClock 15:d6cc57c4e23d 235 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 236
gatedClock 15:d6cc57c4e23d 237
gatedClock 15:d6cc57c4e23d 238
gatedClock 15:d6cc57c4e23d 239
gatedClock 15:d6cc57c4e23d 240 // pcSend[7] = 0x02; // force IR.
gatedClock 15:d6cc57c4e23d 241 // pcSend[1] = 0xA4; // R1 <- immediate.
gatedClock 15:d6cc57c4e23d 242 // pcSend[0] = 0xEE; // immediate value.
gatedClock 15:d6cc57c4e23d 243 /// transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 244
gatedClock 15:d6cc57c4e23d 245
gatedClock 15:d6cc57c4e23d 246
gatedClock 15:d6cc57c4e23d 247 // no-op scan.
gatedClock 15:d6cc57c4e23d 248 pcSend[7] = 0x02; pcSend[1] = 0x0; pcSend[0] = 0;
gatedClock 15:d6cc57c4e23d 249 transceive_vector(pcReceive, pcSend, 8);
gatedClock 15:d6cc57c4e23d 250
gatedClock 15:d6cc57c4e23d 251 }
gatedClock 14:35717622a4fb 252 //----------------------------------------------//------------------------------
gatedClock 16:0e422fd263c6 253 void mmSPI::write_register(char cRegister, char cValue, char * pcReceive, char * pcSend)
gatedClock 16:0e422fd263c6 254 {
gatedClock 16:0e422fd263c6 255 pcSend[7] = 0x02;
gatedClock 16:0e422fd263c6 256 pcSend[1] = ((cRegister & 0x07) << 2) | 0xA0;
gatedClock 16:0e422fd263c6 257 pcSend[0] = cValue & 0xFF;
gatedClock 16:0e422fd263c6 258
gatedClock 16:0e422fd263c6 259 // transceive_vector(pcReceive, pcSend, 8);
gatedClock 16:0e422fd263c6 260 }
gatedClock 16:0e422fd263c6 261 //----------------------------------------------//------------------------------
gatedClock 5:b14dcaae260e 262
gatedClock 5:b14dcaae260e 263
gatedClock 5:b14dcaae260e 264
gatedClock 5:b14dcaae260e 265
gatedClock 5:b14dcaae260e 266
gatedClock 5:b14dcaae260e 267
gatedClock 5:b14dcaae260e 268
gatedClock 7:b3e8b537d5c2 269
gatedClock 7:b3e8b537d5c2 270
gatedClock 7:b3e8b537d5c2 271
gatedClock 7:b3e8b537d5c2 272
gatedClock 7:b3e8b537d5c2 273
gatedClock 15:d6cc57c4e23d 274
gatedClock 15:d6cc57c4e23d 275
gatedClock 15:d6cc57c4e23d 276
gatedClock 15:d6cc57c4e23d 277
gatedClock 15:d6cc57c4e23d 278