Martin Smith
/
SPI_Investigation
Revision 0:7953b8ea1d53, committed 2012-01-15
- Comitter:
- ms523
- Date:
- Sun Jan 15 15:11:31 2012 +0000
- Child:
- 1:e4104703c457
- Commit message:
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jan 15 15:11:31 2012 +0000 @@ -0,0 +1,66 @@ +#include "mbed.h" + +SPI my_spi(p5, p6, p7); +Serial pc(USBTX,USBRX); + +#define TNF 0x02 +#define TNE 0x01 +#define RNE 0x04 + +/*** My method to write to FeRAM ***/ +void spi_write (unsigned char data) { + // First don't write to the FIFO buffer if it is full + while (!(LPC_SSP1->SR & TNF)) // While TNF-Bit = 0 (FIFO full)... + ; // Wait + LPC_SSP1->DR = data; // Write to FIFO buffer +} + +/*** Write a byte to FeRAM ***/ +void write_byte (int address, unsigned char data) { + spi_write(0x02); // Send write command + spi_write(address >> 16); // Send top address byte to write to + spi_write((address >> 8) & 0xFF); // Send Middle address byte to write to + spi_write(address & 0xFF); // Send Bottom address byte to write to + spi_write(data); // Send data to be write + // Now I need to check that the FIFO transmit buffer is empty exiting the method + while (!(LPC_SSP1->SR & TNE)) // While TNE-Bit = 0 (FIFO not empty)... + ; // Wait +} + +/*** Read a byte from FeRAM ***/ +unsigned char read_byte (int address) { + unsigned char my_val = 0; // Variable to store the read data + spi_write(0x03); // Send read command + spi_write(address >> 16); // Send top address byte to read from + spi_write((address >> 8) & 0xFF); // Send Middle address byte to read from + spi_write(address & 0xFF); // Send Bottom address byte to read from + // Now the buffer is empty send out a dummy byte and read the buffer + spi_write(0x00); // Send the dummy byte + // Now I need to empty the FIFO receive buffer... + while (LPC_SSP1->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... + my_val = LPC_SSP1->DR; // Read the byte in the buffer + return (my_val); // Return the last byte read +} + +int main() { + + // Set up the SPI port... + my_spi.frequency(32000000); // 32MHz is the fastest mbed frequency supported by the FeRAM + my_spi.format(8,3); // Set for mode 3 + LPC_PINCON->PINSEL0 |= 0x00002000; // Set up SSEL1 + wait(0.001); // Must wait 1ms after power on before using FeRAM + my_spi.write(0x06); // Send WREN command to write enable + + // Thought I would wait a bit to get a cleaner scope trace + wait_us(2); + + // Write the byte... + write_byte (100, 0x4C); + + // Thought I would wait a bit again + wait_us(2); + + // read the byte and display on TeraTerm + int my_val = read_byte (100); + pc.printf(" Value = 0x%X",my_val); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jan 15 15:11:31 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/5364839841bd