Dependencies:   mbed

Committer:
ms523
Date:
Tue Jan 17 18:48:29 2012 +0000
Revision:
1:e4104703c457
Parent:
0:7953b8ea1d53
Read and write to FeRAM - code optimised

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e4104703c457 7 #define TFE 0x01
ms523 0:7953b8ea1d53 8 #define RNE 0x04
ms523 0:7953b8ea1d53 9
ms523 1:e4104703c457 10 #define WREN 0x06
ms523 1:e4104703c457 11 #define WRITE 0x02
ms523 1:e4104703c457 12 #define READ 0x03
ms523 1:e4104703c457 13
ms523 0:7953b8ea1d53 14 /*** My method to write to FeRAM ***/
ms523 0:7953b8ea1d53 15 void spi_write (unsigned char data) {
ms523 0:7953b8ea1d53 16 // First don't write to the FIFO buffer if it is full
ms523 0:7953b8ea1d53 17 while (!(LPC_SSP1->SR & TNF)) // While TNF-Bit = 0 (FIFO full)...
ms523 0:7953b8ea1d53 18 ; // Wait
ms523 0:7953b8ea1d53 19 LPC_SSP1->DR = data; // Write to FIFO buffer
ms523 0:7953b8ea1d53 20 }
ms523 0:7953b8ea1d53 21
ms523 0:7953b8ea1d53 22 /*** Write a byte to FeRAM ***/
ms523 0:7953b8ea1d53 23 void write_byte (int address, unsigned char data) {
ms523 1:e4104703c457 24 spi_write(WREN);
ms523 1:e4104703c457 25 while (!(LPC_SSP1->SR & TFE)) // While TFE-Bit = 1 (FIFO empty)...
ms523 1:e4104703c457 26 ; // Wait
ms523 1:e4104703c457 27 spi_write(WRITE); // Send write command
ms523 0:7953b8ea1d53 28 spi_write(address >> 16); // Send top address byte to write to
ms523 0:7953b8ea1d53 29 spi_write((address >> 8) & 0xFF); // Send Middle address byte to write to
ms523 0:7953b8ea1d53 30 spi_write(address & 0xFF); // Send Bottom address byte to write to
ms523 0:7953b8ea1d53 31 spi_write(data); // Send data to be write
ms523 0:7953b8ea1d53 32 // Now I need to check that the FIFO transmit buffer is empty exiting the method
ms523 1:e4104703c457 33 while (!(LPC_SSP1->SR & TFE)) // While TFE-Bit = 0 (FIFO not empty)...
ms523 0:7953b8ea1d53 34 ; // Wait
ms523 0:7953b8ea1d53 35 }
ms523 0:7953b8ea1d53 36
ms523 0:7953b8ea1d53 37 /*** Read a byte from FeRAM ***/
ms523 0:7953b8ea1d53 38 unsigned char read_byte (int address) {
ms523 1:e4104703c457 39 unsigned char my_val; // Variable to store the read data
ms523 1:e4104703c457 40 spi_write(READ); // Send read command
ms523 0:7953b8ea1d53 41 spi_write(address >> 16); // Send top address byte to read from
ms523 0:7953b8ea1d53 42 spi_write((address >> 8) & 0xFF); // Send Middle address byte to read from
ms523 0:7953b8ea1d53 43 spi_write(address & 0xFF); // Send Bottom address byte to read from
ms523 0:7953b8ea1d53 44 // Now the buffer is empty send out a dummy byte and read the buffer
ms523 0:7953b8ea1d53 45 spi_write(0x00); // Send the dummy byte
ms523 0:7953b8ea1d53 46 // Now I need to empty the FIFO receive buffer...
ms523 0:7953b8ea1d53 47 while (LPC_SSP1->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)...
ms523 1:e4104703c457 48 my_val = LPC_SSP1->DR; // Read the byte in the buffer
ms523 0:7953b8ea1d53 49 return (my_val); // Return the last byte read
ms523 0:7953b8ea1d53 50 }
ms523 0:7953b8ea1d53 51
ms523 0:7953b8ea1d53 52 int main() {
ms523 1:e4104703c457 53 unsigned char my_val[19200];
ms523 0:7953b8ea1d53 54
ms523 0:7953b8ea1d53 55 // Set up the SPI port...
ms523 0:7953b8ea1d53 56 my_spi.frequency(32000000); // 32MHz is the fastest mbed frequency supported by the FeRAM
ms523 0:7953b8ea1d53 57 my_spi.format(8,3); // Set for mode 3
ms523 0:7953b8ea1d53 58 LPC_PINCON->PINSEL0 |= 0x00002000; // Set up SSEL1
ms523 1:e4104703c457 59
ms523 1:e4104703c457 60 // To start with I need the chip select high and wait 1ms after powering up
ms523 0:7953b8ea1d53 61 wait(0.001); // Must wait 1ms after power on before using FeRAM
ms523 1:e4104703c457 62
ms523 1:e4104703c457 63 // Create timers
ms523 1:e4104703c457 64 Timer write_timer, read_timer;
ms523 0:7953b8ea1d53 65
ms523 1:e4104703c457 66 int val = 88;
ms523 1:e4104703c457 67
ms523 1:e4104703c457 68 write_timer.start();
ms523 1:e4104703c457 69 // Write the bytes...
ms523 1:e4104703c457 70 for (int i = 0; i < 19200; i++) {
ms523 1:e4104703c457 71 write_byte (i, val);
ms523 1:e4104703c457 72 }
ms523 1:e4104703c457 73 write_timer.stop();
ms523 0:7953b8ea1d53 74
ms523 1:e4104703c457 75 read_timer.start();
ms523 1:e4104703c457 76 // Read the bytes...
ms523 1:e4104703c457 77 for (int i = 0; i < 19200; i++) {
ms523 1:e4104703c457 78 my_val[i] = read_byte (i);
ms523 1:e4104703c457 79 }
ms523 1:e4104703c457 80 read_timer.stop();
ms523 0:7953b8ea1d53 81
ms523 1:e4104703c457 82 // Print the data read on screen
ms523 1:e4104703c457 83 for (int i = 0; i < 19200; i++) {
ms523 1:e4104703c457 84 if(my_val[i] != val)
ms523 1:e4104703c457 85 pc.printf("\n\r Error at element %d [%d]",i,my_val[i]);
ms523 1:e4104703c457 86 }
ms523 1:e4104703c457 87
ms523 1:e4104703c457 88 pc.printf("\n\n\r Write Time = %f ms\n\r Read Time = %f ms",write_timer.read()*1000,read_timer.read()*1000);
ms523 0:7953b8ea1d53 89 }