Dependencies:   DM_FATFileSystem EthernetInterface HTTPClient mbed-rtos mbed-src

Fork of DMSupport by Embedded Artists

Memory/InternalEEPROM.h

Committer:
embeddedartists
Date:
2014-11-21
Revision:
0:6b68dac0d986
Child:
17:4ea2509445ac

File content as of revision 0:6b68dac0d986:

/*
 *  Copyright 2014 Embedded Artists AB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef INTERNAL_EEPROM_H
#define INTERNAL_EEPROM_H

#include "mbed.h"

/**
 * Internal EEPROM Example
 *
 * @code
 * #include "mbed.h"
 * #include "InternalEEPROM.h"
 *
 * int main(void) {
 *    InternalEEPROM mem;
 *
 *    err = SPIFI::instance().init();
 *    if (err != SPIFI::Ok) {
 *        printf("Failed to initialize SPIFI, error %d\n", err);
 *    }
 *    
 *    // Write "Hello World!" into the first bytes of the SPIFI
 *    char buff[20] = "Hello World!";
 *    err = SPIFI::instance().program(0, strlen(buff)+1, buff, SPIFI::EraseAsRequired);
 *    if (err != SPIFI::Ok) {
 *        printf("Failed to write to SPIFI, error %d\n", err);
 *    }
 *
 *    // Now verify that it can be read
 *    if (memcmp((char*)SPIFI::SpifiMemBase, buff, strlen(buff)+1) == 0) {
 *        printf("Readback from memory OK: '%s'\n", SPIFI::SpifiMemBase);
 *    } else {
 *        printf("Spifi does not contain the correct data!\n");
 *    }
 * }
 * @endcode
 */
class InternalEEPROM {
public:
    
    enum Constants {
        EEPROM_MEMORY_SIZE = 4032,
        EEPROM_PAGE_SIZE   =   64,
        EEPROM_NUM_PAGES   = EEPROM_MEMORY_SIZE/EEPROM_PAGE_SIZE,
    };
  
    InternalEEPROM();
    ~InternalEEPROM();

    /** Initializes the SPIFI ROM driver making the content of the external serial flash available
     *
     *  @returns
     *       Ok on success
     *       An error code on failure
     */
    void init();
    
    void powerDown();
    
    int read(uint32_t pageAddr, uint32_t pageOffset, uint8_t* data, uint32_t size);
    int write(uint32_t pageAddr, uint32_t pageOffset, const uint8_t* data, uint32_t size);
    void erasePage(uint32_t pageAddr);
  
    /** Returns the size (in bytes) of the internal EEPROM
     *
     *  @returns
     *       The size in bytes
     */
    uint32_t memorySize() { return EEPROM_MEMORY_SIZE; }
  
    /** Returns the size of a page (in bytes) of the internal EEPROM
     *
     *  @returns
     *       The page size in bytes
     */
    uint32_t pageSize() { return EEPROM_PAGE_SIZE; }
  
    /** Returns the number of pages in the internal EEPROM
     *
     *  @returns
     *       The number of pages
     */
    uint32_t numPages() { return EEPROM_NUM_PAGES; }
  
private:

    bool _initialized;
    
    void powerUp();
    void clearInterrupt(uint32_t mask);
    void waitForInterrupt(uint32_t mask);
    void setAddr(uint32_t pageAddr, uint32_t pageOffset);
    void setCmd(uint32_t cmd);
    void readPage(uint32_t pageAddr, uint32_t pageOffset, uint8_t* buf, uint32_t size);
    void writePage(uint32_t pageAddr, uint32_t pageOffset, const uint8_t* buf, uint32_t size);
    void eraseOrProgramPage(uint32_t pageAddr);
};

#endif