asdasdasd

Dependencies:   mbed

Fork of FINAL_PROJECT_4180 by Gedeon Nyengele

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MEM.h Source File

MEM.h

00001 #ifndef MEM_H
00002 #define MEM_H
00003 
00004 #include "mbed.h"
00005 
00006 #define MEM_START_ADDR      0
00007 #define MEM_PAGE_OFFSET     (1 << 8)
00008 #define MEM_SECTOR_OFFSET   (1 << 12)
00009 
00010 // Chip Manufacturer Info
00011 typedef struct {
00012     int manuf_id;
00013     int mem_type;
00014     int mem_size;
00015 } FLASH_MEM_INFO;
00016 
00017 // Supported Commands
00018 struct FLASH_MEM_CMD {
00019     static const int WREN         = 0x06;  // Set Write Enable Latch
00020     static const int WRDI         = 0x04;  // Reset Write Enable Latch
00021     static const int RDSR         = 0x05;  // Read Status Register
00022     static const int WRSR         = 0x01;  // Write Status Register
00023     static const int READ         = 0x03;  // Read Data from Memory Array
00024     static const int FAST_READ    = 0x0B;  // Read Data from Memory Array (with dummy cycles)
00025     static const int PROGRAM      = 0x02;  // Program Data Into Memory Array
00026     static const int SECTOR_ERASE = 0x20;  // Erase 1 4KB Sector in Memory
00027     static const int BLOCK_ERASE  = 0x52;  // Erase 1 64KB Block in Memory
00028     static const int CHIP_ERASE   = 0x60;  // Erase Entire Memory Array
00029     static const int RDID         = 0x9F;  // Read Manufacturer and Product ID
00030 };
00031 
00032 // Memory Operations Status
00033 enum MEM_STATUS {
00034     FAIL    = 0,
00035     SUCCESS,
00036     BUSY,
00037     READY,
00038 };
00039 
00040 
00041 class MEM_DEVICE
00042 {
00043     SPI *dev;
00044 
00045 public:
00046      /**
00047      * Constructor
00048      *
00049      * @param mosi MOSI pin for SPI
00050      * @param miso MISO pin for SPI
00051      * @param sck  clock pin for SPI
00052      * @param cs Chip Select pin
00053      */
00054      MEM_DEVICE(PinName mosi, PinName miso, PinName sck, PinName cs);
00055      
00056      
00057      /**
00058      * Destructor
00059      */
00060      ~MEM_DEVICE();
00061      
00062      /**
00063      * Set mode and number of bits to use
00064      *
00065      * @param databits number data bits
00066      * @param mode SPI mode to use (0, 1, 2, 3)
00067      */
00068      void format(int databits, int mode);
00069     
00070      /**
00071      * Read multiple consecutive bytes from memory device
00072      *
00073      * @param startAddr address where to start reading from
00074      * @param numOfBytes number of consecutive bytes to read
00075      * @param destBuffer buffer where to copy bytes to
00076      * @return status of the read operation (FAIL or SUCCESS)
00077      */
00078      MEM_STATUS read_bytes(int startAddr, int numOfBytes, char *destBuffer);
00079 
00080      /**
00081      * Write multiple consecutive bytes to memory device
00082      *
00083      * @param startAddr address where to start writing to
00084      * @param numOfBytes number of consecutive bytes to write
00085      * @param srcBuffer buffer where to write from
00086      * @return status of the write operation (FAIL or SUCCESS)
00087      */
00088      MEM_STATUS write_bytes(int startAddr, int numOfBytes, char *srcBuffer);
00089      
00090      /**
00091      * Clears the entire flash memory
00092      *
00093      * @return status of the clear operation (FAIL or SUCCESS)
00094      */
00095      MEM_STATUS erase();
00096     
00097     /**
00098      * Reads the status of the memory device
00099      *
00100      * @return status of the operation (FAIL, BUSY, READY)
00101      */
00102      MEM_STATUS read_status();
00103 };
00104 
00105 #endif