Interface library for Ramtron FRAM memory, FM25VXX series.
Code sample:
#include "mbed.h" #include "FRAMSPI.h" SPI spi(p5,p6,p7); FRAMSPI fram(spi,p8); Serial pc(USBTX, USBRX); int main() { char wdata[] = "Hello world!"; char rdata[14]; fram.write(0, wdata, 13); // 12 symbols + zero terminator fram.read(0, rdata, 13); pc.printf("data: %s", rdata); }
FRAMSPI.h@0:27d10d94f7d9, 2012-10-21 (annotated)
- Committer:
- Decimus
- Date:
- Sun Oct 21 17:10:30 2012 +0000
- Revision:
- 0:27d10d94f7d9
Upload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Decimus | 0:27d10d94f7d9 | 1 | /* FRAM_SPI - Ramtron FRAM FM24VXX series driver |
Decimus | 0:27d10d94f7d9 | 2 | * Copyright (c) 2012 Oleg Evsegneev |
Decimus | 0:27d10d94f7d9 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
Decimus | 0:27d10d94f7d9 | 4 | * |
Decimus | 0:27d10d94f7d9 | 5 | * Datasheet http://www.ramtron.com/files/datasheets/FM25V02_ds.pdf |
Decimus | 0:27d10d94f7d9 | 6 | */ |
Decimus | 0:27d10d94f7d9 | 7 | |
Decimus | 0:27d10d94f7d9 | 8 | #ifndef FRAMSPI_ |
Decimus | 0:27d10d94f7d9 | 9 | #define FRAMSPI_ |
Decimus | 0:27d10d94f7d9 | 10 | |
Decimus | 0:27d10d94f7d9 | 11 | #include "mbed.h" |
Decimus | 0:27d10d94f7d9 | 12 | |
Decimus | 0:27d10d94f7d9 | 13 | // commands |
Decimus | 0:27d10d94f7d9 | 14 | #define READ 0x03 |
Decimus | 0:27d10d94f7d9 | 15 | #define WRITE 0x02 |
Decimus | 0:27d10d94f7d9 | 16 | #define READ_STATUS 0x05 // RDSR |
Decimus | 0:27d10d94f7d9 | 17 | #define WRITE_ENABLED 0x06 // WREN |
Decimus | 0:27d10d94f7d9 | 18 | #define WRITE_STATUS 0x01 // WRSR |
Decimus | 0:27d10d94f7d9 | 19 | #define READ_ID 0x9F // RDID |
Decimus | 0:27d10d94f7d9 | 20 | #define READ_SN 0xC3 // RSN |
Decimus | 0:27d10d94f7d9 | 21 | |
Decimus | 0:27d10d94f7d9 | 22 | /** An interface for the Ramtron FM25VXX FRAM over SPI |
Decimus | 0:27d10d94f7d9 | 23 | * |
Decimus | 0:27d10d94f7d9 | 24 | * |
Decimus | 0:27d10d94f7d9 | 25 | * |
Decimus | 0:27d10d94f7d9 | 26 | * @code |
Decimus | 0:27d10d94f7d9 | 27 | * #include "mbed.h" |
Decimus | 0:27d10d94f7d9 | 28 | * #include "FRAMSPI.h" |
Decimus | 0:27d10d94f7d9 | 29 | * |
Decimus | 0:27d10d94f7d9 | 30 | * SPI spi(p5,p6,p7); |
Decimus | 0:27d10d94f7d9 | 31 | * FRAMSPI fram(spi,p8); |
Decimus | 0:27d10d94f7d9 | 32 | * Serial pc(USBTX, USBRX); |
Decimus | 0:27d10d94f7d9 | 33 | * |
Decimus | 0:27d10d94f7d9 | 34 | * int main() { |
Decimus | 0:27d10d94f7d9 | 35 | * char wdata[] = "Hello world!"; |
Decimus | 0:27d10d94f7d9 | 36 | * char rdata[14]; |
Decimus | 0:27d10d94f7d9 | 37 | * |
Decimus | 0:27d10d94f7d9 | 38 | * fram.write(0, wdata, 13); // 12 symbols + zero terminator |
Decimus | 0:27d10d94f7d9 | 39 | * fram.read(0, rdata, 13); |
Decimus | 0:27d10d94f7d9 | 40 | * |
Decimus | 0:27d10d94f7d9 | 41 | * pc.printf("data: %s", rdata); |
Decimus | 0:27d10d94f7d9 | 42 | * } |
Decimus | 0:27d10d94f7d9 | 43 | * @endcode |
Decimus | 0:27d10d94f7d9 | 44 | * connections: |
Decimus | 0:27d10d94f7d9 | 45 | * chip pin 1 to any GPIO. Chip select (!S) |
Decimus | 0:27d10d94f7d9 | 46 | * chip pin 2 SO to MISO. Write (!W) |
Decimus | 0:27d10d94f7d9 | 47 | * chip pin 3 to Vout or N/C |
Decimus | 0:27d10d94f7d9 | 48 | * chip pin 4 to Gnd |
Decimus | 0:27d10d94f7d9 | 49 | * chip pin 5 SI to MOSI |
Decimus | 0:27d10d94f7d9 | 50 | * chip pin 6 SCK to sck |
Decimus | 0:27d10d94f7d9 | 51 | * chip pin 7 to Vout. !Hold |
Decimus | 0:27d10d94f7d9 | 52 | * chip pin 8 to Vout |
Decimus | 0:27d10d94f7d9 | 53 | */ |
Decimus | 0:27d10d94f7d9 | 54 | |
Decimus | 0:27d10d94f7d9 | 55 | class FRAMSPI { |
Decimus | 0:27d10d94f7d9 | 56 | |
Decimus | 0:27d10d94f7d9 | 57 | public: |
Decimus | 0:27d10d94f7d9 | 58 | /** Create an interface |
Decimus | 0:27d10d94f7d9 | 59 | * |
Decimus | 0:27d10d94f7d9 | 60 | * @param spi An SPI object |
Decimus | 0:27d10d94f7d9 | 61 | * @param ncs Chip select pin |
Decimus | 0:27d10d94f7d9 | 62 | */ |
Decimus | 0:27d10d94f7d9 | 63 | FRAMSPI(SPI& spi, PinName ncs); |
Decimus | 0:27d10d94f7d9 | 64 | |
Decimus | 0:27d10d94f7d9 | 65 | /** read chip 8 byte device ID |
Decimus | 0:27d10d94f7d9 | 66 | */ |
Decimus | 0:27d10d94f7d9 | 67 | void readID(char *buff); |
Decimus | 0:27d10d94f7d9 | 68 | |
Decimus | 0:27d10d94f7d9 | 69 | /** read chip 2 byte serial number |
Decimus | 0:27d10d94f7d9 | 70 | */ |
Decimus | 0:27d10d94f7d9 | 71 | void readSN(char *buff); |
Decimus | 0:27d10d94f7d9 | 72 | |
Decimus | 0:27d10d94f7d9 | 73 | /** read a byte from FRAM |
Decimus | 0:27d10d94f7d9 | 74 | * @param addr The address to read from |
Decimus | 0:27d10d94f7d9 | 75 | * @return the byte at that address |
Decimus | 0:27d10d94f7d9 | 76 | */ |
Decimus | 0:27d10d94f7d9 | 77 | char read(int addr); |
Decimus | 0:27d10d94f7d9 | 78 | |
Decimus | 0:27d10d94f7d9 | 79 | /** read multiple bytes from FRAM into a buffer |
Decimus | 0:27d10d94f7d9 | 80 | * @param addr The FRAM address to read from |
Decimus | 0:27d10d94f7d9 | 81 | * @param buff The buffer to read into (must be big enough!) |
Decimus | 0:27d10d94f7d9 | 82 | * @param cnt The number of bytes to read |
Decimus | 0:27d10d94f7d9 | 83 | */ |
Decimus | 0:27d10d94f7d9 | 84 | void read(int addr, char *buff, int cnt); |
Decimus | 0:27d10d94f7d9 | 85 | |
Decimus | 0:27d10d94f7d9 | 86 | /** write a byte to FRAM |
Decimus | 0:27d10d94f7d9 | 87 | * @param addr The address SFRAM to write to |
Decimus | 0:27d10d94f7d9 | 88 | * @param data The byte to write there |
Decimus | 0:27d10d94f7d9 | 89 | */ |
Decimus | 0:27d10d94f7d9 | 90 | void write(int addr, char data); |
Decimus | 0:27d10d94f7d9 | 91 | |
Decimus | 0:27d10d94f7d9 | 92 | /** write multiple bytes to FRAM from a buffer |
Decimus | 0:27d10d94f7d9 | 93 | * @param addr The FRAM address write to |
Decimus | 0:27d10d94f7d9 | 94 | * @param buff The buffer to write from |
Decimus | 0:27d10d94f7d9 | 95 | * @param cnt The number of bytes to write |
Decimus | 0:27d10d94f7d9 | 96 | */ |
Decimus | 0:27d10d94f7d9 | 97 | void write(int addr, char *buff, int cnt); |
Decimus | 0:27d10d94f7d9 | 98 | |
Decimus | 0:27d10d94f7d9 | 99 | private: |
Decimus | 0:27d10d94f7d9 | 100 | SPI& _spi; |
Decimus | 0:27d10d94f7d9 | 101 | DigitalOut _ncs; |
Decimus | 0:27d10d94f7d9 | 102 | void prepareCommand(char cmd, int addr); |
Decimus | 0:27d10d94f7d9 | 103 | void select(); |
Decimus | 0:27d10d94f7d9 | 104 | void deselect(); |
Decimus | 0:27d10d94f7d9 | 105 | }; |
Decimus | 0:27d10d94f7d9 | 106 | |
Decimus | 0:27d10d94f7d9 | 107 | #endif |