Martin Smith
/
SPI_Investigation
main.cpp@0:7953b8ea1d53, 2012-01-15 (annotated)
- Committer:
- ms523
- Date:
- Sun Jan 15 15:11:31 2012 +0000
- Revision:
- 0:7953b8ea1d53
- Child:
- 1:e4104703c457
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ms523 | 0:7953b8ea1d53 | 1 | #include "mbed.h" |
ms523 | 0:7953b8ea1d53 | 2 | |
ms523 | 0:7953b8ea1d53 | 3 | SPI my_spi(p5, p6, p7); |
ms523 | 0:7953b8ea1d53 | 4 | Serial pc(USBTX,USBRX); |
ms523 | 0:7953b8ea1d53 | 5 | |
ms523 | 0:7953b8ea1d53 | 6 | #define TNF 0x02 |
ms523 | 0:7953b8ea1d53 | 7 | #define TNE 0x01 |
ms523 | 0:7953b8ea1d53 | 8 | #define RNE 0x04 |
ms523 | 0:7953b8ea1d53 | 9 | |
ms523 | 0:7953b8ea1d53 | 10 | /*** My method to write to FeRAM ***/ |
ms523 | 0:7953b8ea1d53 | 11 | void spi_write (unsigned char data) { |
ms523 | 0:7953b8ea1d53 | 12 | // First don't write to the FIFO buffer if it is full |
ms523 | 0:7953b8ea1d53 | 13 | while (!(LPC_SSP1->SR & TNF)) // While TNF-Bit = 0 (FIFO full)... |
ms523 | 0:7953b8ea1d53 | 14 | ; // Wait |
ms523 | 0:7953b8ea1d53 | 15 | LPC_SSP1->DR = data; // Write to FIFO buffer |
ms523 | 0:7953b8ea1d53 | 16 | } |
ms523 | 0:7953b8ea1d53 | 17 | |
ms523 | 0:7953b8ea1d53 | 18 | /*** Write a byte to FeRAM ***/ |
ms523 | 0:7953b8ea1d53 | 19 | void write_byte (int address, unsigned char data) { |
ms523 | 0:7953b8ea1d53 | 20 | spi_write(0x02); // Send write command |
ms523 | 0:7953b8ea1d53 | 21 | spi_write(address >> 16); // Send top address byte to write to |
ms523 | 0:7953b8ea1d53 | 22 | spi_write((address >> 8) & 0xFF); // Send Middle address byte to write to |
ms523 | 0:7953b8ea1d53 | 23 | spi_write(address & 0xFF); // Send Bottom address byte to write to |
ms523 | 0:7953b8ea1d53 | 24 | spi_write(data); // Send data to be write |
ms523 | 0:7953b8ea1d53 | 25 | // Now I need to check that the FIFO transmit buffer is empty exiting the method |
ms523 | 0:7953b8ea1d53 | 26 | while (!(LPC_SSP1->SR & TNE)) // While TNE-Bit = 0 (FIFO not empty)... |
ms523 | 0:7953b8ea1d53 | 27 | ; // Wait |
ms523 | 0:7953b8ea1d53 | 28 | } |
ms523 | 0:7953b8ea1d53 | 29 | |
ms523 | 0:7953b8ea1d53 | 30 | /*** Read a byte from FeRAM ***/ |
ms523 | 0:7953b8ea1d53 | 31 | unsigned char read_byte (int address) { |
ms523 | 0:7953b8ea1d53 | 32 | unsigned char my_val = 0; // Variable to store the read data |
ms523 | 0:7953b8ea1d53 | 33 | spi_write(0x03); // Send read command |
ms523 | 0:7953b8ea1d53 | 34 | spi_write(address >> 16); // Send top address byte to read from |
ms523 | 0:7953b8ea1d53 | 35 | spi_write((address >> 8) & 0xFF); // Send Middle address byte to read from |
ms523 | 0:7953b8ea1d53 | 36 | spi_write(address & 0xFF); // Send Bottom address byte to read from |
ms523 | 0:7953b8ea1d53 | 37 | // Now the buffer is empty send out a dummy byte and read the buffer |
ms523 | 0:7953b8ea1d53 | 38 | spi_write(0x00); // Send the dummy byte |
ms523 | 0:7953b8ea1d53 | 39 | // Now I need to empty the FIFO receive buffer... |
ms523 | 0:7953b8ea1d53 | 40 | while (LPC_SSP1->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
ms523 | 0:7953b8ea1d53 | 41 | my_val = LPC_SSP1->DR; // Read the byte in the buffer |
ms523 | 0:7953b8ea1d53 | 42 | return (my_val); // Return the last byte read |
ms523 | 0:7953b8ea1d53 | 43 | } |
ms523 | 0:7953b8ea1d53 | 44 | |
ms523 | 0:7953b8ea1d53 | 45 | int main() { |
ms523 | 0:7953b8ea1d53 | 46 | |
ms523 | 0:7953b8ea1d53 | 47 | // Set up the SPI port... |
ms523 | 0:7953b8ea1d53 | 48 | my_spi.frequency(32000000); // 32MHz is the fastest mbed frequency supported by the FeRAM |
ms523 | 0:7953b8ea1d53 | 49 | my_spi.format(8,3); // Set for mode 3 |
ms523 | 0:7953b8ea1d53 | 50 | LPC_PINCON->PINSEL0 |= 0x00002000; // Set up SSEL1 |
ms523 | 0:7953b8ea1d53 | 51 | wait(0.001); // Must wait 1ms after power on before using FeRAM |
ms523 | 0:7953b8ea1d53 | 52 | my_spi.write(0x06); // Send WREN command to write enable |
ms523 | 0:7953b8ea1d53 | 53 | |
ms523 | 0:7953b8ea1d53 | 54 | // Thought I would wait a bit to get a cleaner scope trace |
ms523 | 0:7953b8ea1d53 | 55 | wait_us(2); |
ms523 | 0:7953b8ea1d53 | 56 | |
ms523 | 0:7953b8ea1d53 | 57 | // Write the byte... |
ms523 | 0:7953b8ea1d53 | 58 | write_byte (100, 0x4C); |
ms523 | 0:7953b8ea1d53 | 59 | |
ms523 | 0:7953b8ea1d53 | 60 | // Thought I would wait a bit again |
ms523 | 0:7953b8ea1d53 | 61 | wait_us(2); |
ms523 | 0:7953b8ea1d53 | 62 | |
ms523 | 0:7953b8ea1d53 | 63 | // read the byte and display on TeraTerm |
ms523 | 0:7953b8ea1d53 | 64 | int my_val = read_byte (100); |
ms523 | 0:7953b8ea1d53 | 65 | pc.printf(" Value = 0x%X",my_val); |
ms523 | 0:7953b8ea1d53 | 66 | } |