SPI library used to communicate with an altera development board attached to four zigbee-header pins.
Diff: mmSPI.cpp
- Revision:
- 24:d3b8c68f41f2
- Parent:
- 23:dbd89a56716d
- Child:
- 26:26a8f31a31b8
diff -r dbd89a56716d -r d3b8c68f41f2 mmSPI.cpp --- a/mmSPI.cpp Tue Aug 20 14:25:47 2013 +0000 +++ b/mmSPI.cpp Tue Aug 20 14:38:50 2013 +0000 @@ -126,8 +126,7 @@ { int dLoop; // loop index. - // clear transmit vector. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + clear_transmit_vector(); // clear transmit vector. pcSend[7] = 0x02; // mbed sends a command. @@ -137,19 +136,14 @@ transceive_vector(); // transmit command. - // clear transmit vector. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + clear_transmit_vector(); // clear transmit vector. } //----------------------------------------------//------------------------------ // returns the content of // a CPU register. char mmSPI::read_register(char cRegister) { - int dLoop; - - - // send all 0. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + clear_transmit_vector(); // clear transmit vector. transceive_vector(); // snap & scan-out reg contents. @@ -158,17 +152,11 @@ //----------------------------------------------//------------------------------ void mmSPI::write_memory(char cHData, char cLdata, char cAddress) { - 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); - write_register(0x02,cHData); - write_register(0x01,cLdata); + clear_transmit_vector(); // clear transmit vector. + + write_register(0x03,cAddress); // R3 <- address. + write_register(0x02,cHData); // R2 <- high-data. + write_register(0x01,cLdata); // R1 <- low-data. pcSend[7] = 0x00; // write-enable high. pcSend[1] = 0x02; @@ -179,26 +167,20 @@ pcSend[1] = 0x00; pcSend[0] = 0x00; transceive_vector(); - - // clear transmit vector. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; - + + clear_transmit_vector(); // clear transmit vector. } //----------------------------------------------//------------------------------ // fetch a word from main memory. unsigned int mmSPI::read_memory(char cAddress) - { - 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; + + clear_transmit_vector(); // clear transmit vector. - - // R3 <- address. - write_register(0x03,cAddress); + write_register(0x03,cAddress); // R3 <= address. pcSend[7] = 0x02; // mbed sends command. pcSend[1] = 0xC8; // R2 <- MM[R3] @@ -210,19 +192,23 @@ pcSend[0] = 0x00; transceive_vector(); // send command. - // obtain MM content. - cHData = read_register(0x02); - cLData = read_register(0x01); + cHData = read_register(0x02); // obtain MM high-data-byte. + cLData = read_register(0x01); // obtain MM low-data-byte. udMemoryContent = (cHData << 8) + cLData; // build the memory word. - - // clear transmit vector. - for (dLoop = 0; dLoop < 8; dLoop++) pcSend[dLoop] = 0x00; + + clear_transmit_vector(); // clear transmit vector. return udMemoryContent; // return the memory word. } //----------------------------------------------//------------------------------ + void mmSPI::clear_transmit_vector(void) // fill transmit buffer with 0. + { + int dLoop; + for (dLoop = 0; dLoop < dNumBytes; dLoop++) pcSend[dLoop] = 0x00; + } +//----------------------------------------------//------------------------------ @@ -236,5 +222,3 @@ - -