SPI library used to communicate with an altera development board attached to four zigbee-header pins.
Diff: mmSPI.cpp
- Revision:
- 18:4a29cad91540
- Parent:
- 17:b81c0c1f312f
- Child:
- 19:c2b753533b93
diff -r b81c0c1f312f -r 4a29cad91540 mmSPI.cpp --- a/mmSPI.cpp Mon Aug 19 18:26:39 2013 +0000 +++ b/mmSPI.cpp Mon Aug 19 20:45:29 2013 +0000 @@ -144,10 +144,7 @@ { dMisoByteIndex = dIndex / 8; dMisoBitIndex = dIndex % 8; - pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex); - -// pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (0x23 << dMisoBitIndex); - + pcReceive[dMisoByteIndex] = pcReceive[dMisoByteIndex] | (*pMISO << dMisoBitIndex); *pSCLK = 1; wait(fSPIquarterP); wait(fSPIquarterP); @@ -252,20 +249,21 @@ //----------------------------------------------//------------------------------ void mmSPI::write_register(char cRegister, char cValue, char * pcReceive, char * pcSend) { - pcSend[7] = 0x02; + int dLoop; // loop index. + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + + pcSend[7] = 0x02; // mbed sends a command. + + // align into instruction word. pcSend[1] = ((cRegister & 0x07) << 2) | 0xA0; - pcSend[0] = cValue & 0xFF; - - transceive_vector2(pcReceive, pcSend, 8); + pcSend[0] = cValue & 0xFF; // immediate value to i.w. - pcSend[7] = 0x00; - pcSend[6] = 0x00; - pcSend[5] = 0x00; - pcSend[4] = 0x00; - pcSend[3] = 0x00; - pcSend[2] = 0x00; - pcSend[1] = 0x00; - pcSend[0] = 0x00; + transceive_vector2(pcReceive, pcSend, 8); // transmit command. + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; } //----------------------------------------------//------------------------------ // returns the content of @@ -273,14 +271,80 @@ char mmSPI::read_register(char cRegister, char * pcReceive, char * pcSend) { int dLoop; // send all 0. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend = 0x00; - - + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + transceive_vector2(pcReceive, pcSend, 8); // snap & scan-out reg contents. return (pcReceive[cRegister]); // return the particular reg value. } //----------------------------------------------//------------------------------ + void mmSPI::write_memory(char cHData, char cLdata, char cAddress, char * pcReceive, char * pcSend) + { + int dLoop; // loop index. + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + + // R3 <- address. + // R2 <- high-data. + // R1 <- low-data. + write_register(0x03,cAddress, pcReceive, pcSend); + write_register(0x02,cHData, pcReceive, pcSend); + write_register(0x01,cLdata, pcReceive, pcSend); + + pcSend[7] = 0x02; // write-enable high. + pcSend[1] = 0x02; + pcSend[0] = 0x00; + + + transceive_vector2(pcReceive, pcSend, 8); + + pcSend[7] = 0x02; // write-enable low. + pcSend[1] = 0x00; + pcSend[0] = 0x00; + transceive_vector2(pcReceive, pcSend, 8); + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + } +//----------------------------------------------//------------------------------ + // fetch a word from main memory. + unsigned int mmSPI::read_memory(char cAddress, char * pcReceive, char * pcSend) + { + int dLoop; // loop index. + unsigned int udMemoryContent; // return variable. + char cHData; // returned data-high. + char cLData; // returned data-low. + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + + // R3 <- address. + write_register(0x03,cAddress, pcReceive, pcSend); + + pcSend[7] = 0x02; // mbed sends command. + pcSend[1] = 0x68; // R2 <- MM[R3] + pcSend[0] = 0x00; + transceive_vector2(pcReceive, pcSend, 8); // send command. + + pcSend[7] = 0x02; // mbed sends command. + pcSend[1] = 0x64; // R1 <- MM[R3] + pcSend[0] = 0x00; + transceive_vector2(pcReceive, pcSend, 8); // send command. + + // obtain MM content. + cHData = read_register(0x02, pcReceive, pcSend); + cLData = read_register(0x01, pcReceive, pcSend); + + + udMemoryContent = (cHData << 8) + cLData; // build the memory word. + + // clear transmit vector. + for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + + return udMemoryContent; // return the memory word. + } +//----------------------------------------------//------------------------------ void mmSPI::write_pulse(char * pcReceive, char * pcSend)