Memory File System Library, for SPI PRAM NP8P128A13 (Micron) see: http://mbed.org/users/okini3939/notebook/extend-memory/
Revision 0:475e05403ad8, committed 2012-11-12
- Comitter:
- okini3939
- Date:
- Mon Nov 12 15:36:45 2012 +0000
- Commit message:
- 1st build
Changed in this revision
PRAMFileSystem.cpp | Show annotated file Show diff for this revision Revisions of this file |
PRAMFileSystem.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 475e05403ad8 PRAMFileSystem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PRAMFileSystem.cpp Mon Nov 12 15:36:45 2012 +0000 @@ -0,0 +1,123 @@ +/* + * Memory File System Library, for SPI PRAM NP8P128A13 (Micron) + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "mbed.h" +#include "PRAMFileSystem.h" + +PRAMFileSystem::PRAMFileSystem (PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) : + FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) { + _cs = 1; +} + +int PRAMFileSystem::_init () { + int i; + + _spi.frequency(16000000); + _cs = 1; + wait_ms(10); + + _cs = 0; + _spi.write(CMD_RDID); + i = _spi.write(0); + i = (_spi.write(0) << 8) | _spi.write(0); + _cs = 1; + if (i == 0 || i == 0xffff) { + return -1; + } + + return 0; +} + +int PRAMFileSystem::disk_initialize() { + if (_init()) { + return 1; + } + _sectors = 128 * 1024 * 1024 / 8 / 512; + return 0; +} + +int PRAMFileSystem::disk_write(const char *buffer, int block_number) { + int i, addr; + + addr = block_number * 512; + for (i = 0; i < 512; i += 64) { + _write(addr + i, &buffer[i], 64); + } + return 0; +} + +int PRAMFileSystem::disk_read(char *buffer, int block_number) { + int i, addr; + + addr = block_number * 512; + for (i = 0; i < 512; i += 64) { + _read(addr + i, &buffer[i], 64); + } + return 0; +} + +int PRAMFileSystem::disk_status() { return 0; } +int PRAMFileSystem::disk_sync() { return 0; } +int PRAMFileSystem::disk_sectors() { return _sectors; } + + +int PRAMFileSystem::_status () { + int r; + + _cs = 0; + _spi.write(CMD_RDSR); + r = _spi.write(0); + _cs = 1; + return r; +} + +int PRAMFileSystem::_write (int addr, const char *buf, int len) { + int i; + + while (_status() & 1) { + // write in progress + wait_us(1); + } + + _cs = 0; + _spi.write(CMD_WREN); + _cs = 1; + wait_us(1); + + _cs = 0; + _spi.write(CMD_PP_BA); + _spi.write((addr >> 16) & 0xff); + _spi.write((addr >> 8) & 0xff); + _spi.write(addr & 0xff); + + len = len - (addr & 0x3f); + for (i = 0; i < len; i ++) { + _spi.write(buf[i]); + } + _cs = 1; + return i; +} + +int PRAMFileSystem::_read (int addr, char *buf, int len) { + int i; + + while (_status() & 1) { + // write in progress + wait_us(1); + } + + _cs = 0; + _spi.write(CMD_READ); + _spi.write((addr >> 16) & 0xff); + _spi.write((addr >> 8) & 0xff); + _spi.write(addr & 0xff); + + for (i = 0; i < len; i ++) { + buf[i] = _spi.write(0); + } + _cs = 1; + return i; +}
diff -r 000000000000 -r 475e05403ad8 PRAMFileSystem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PRAMFileSystem.h Mon Nov 12 15:36:45 2012 +0000 @@ -0,0 +1,57 @@ +/* + * Memory File System Library, for SPI PRAM NP8P128A13 (Micron) + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +#ifndef _PRAMFileSystem_H_ +#define _PRAMFileSystem_H_ + +#include "mbed.h" +#include "FATFileSystem.h" + +#define CMD_WREN 0x06 // Write enable +#define CMD_WRDI 0x04 // Write disable +#define CMD_RDID 0x9f // Read identification +#define CMD_RDSR 0x05 // Read status register +#define CMD_WRSR 0x01 // Write status registe +#define CMD_READ 0x03 // Read data bytes +#define CMD_FREAD 0x0b // Read data bytes at higher speed +#define CMD_PP 0x02 // Page program (legacy program) +#define CMD_PP_BA 0x22 // Page program (bit-alterable write) +#define CMD_PP_1S 0xd1 // Page program (On all 1s) +#define CMD_SE 0xd8 // Sector erase + + +class PRAMFileSystem : public FATFileSystem { +public: + + /** Create the File System for accessing an SD Card using SPI + * + * @param mosi SPI mosi pin connected to SD Card + * @param miso SPI miso pin conencted to SD Card + * @param sclk SPI sclk pin connected to SD Card + * @param cs DigitalOut pin used as SD Card chip select + * @param name The name used to access the virtual filesystem + */ + PRAMFileSystem (PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name); + virtual int disk_initialize (); + virtual int disk_write (const char *buffer, int block_number); + virtual int disk_read (char *buffer, int block_number); + virtual int disk_status (); + virtual int disk_sync (); + virtual int disk_sectors (); + +protected: + + int _init (); + int _status (); + int _read (int addr, char *buf, int len); + int _write (int addr, const char *buf, int len); + int _sectors; + + SPI _spi; + DigitalOut _cs; +}; + +#endif