Flash handler for M25P* chips with no Device ID.

Dependencies:   RTOS_SPI_Clean

Fork of flash25spi by Klaus Steinhammer

flash25spi.h

Committer:
Tomo2k
Date:
2014-04-25
Revision:
4:af870c53c0e9
Parent:
3:318fabd6708c
Child:
5:3fe5c97a223b

File content as of revision 4:af870c53c0e9:

/* This library is based on the Ser25lcxxx library by Hendrik Lipka
* It was adapted to flash memory chips on 19.2.2011 by Klaus Steinhammer
*
* It was further adapted to M25P* SPI Flash memory chips that do not
* support the "RDID" device ID instructions on 25.03.2014 by Richard Thompson
*
* The BSD license also applies to this code - have fun.
*/

/*
* Ser25lcxxx library
* Copyright (c) 2010 Hendrik Lipka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once
#include "mbed.h"

/**
A class to read and write M25P* serial SPI flash devices.
*/
class FlashM25PSpi
{
public:
    /**
        Create the handler class. it tries to autodetect your device. If your device is not recognized, add the devices parameters
        to the device data structure at the library sources.
        @param spi the SPI port where the flash is connected. Must be set to format(8,3), and with a speed matching the one of your device
        @param enable the pin name for the port where /CS is connected
    */
    FlashM25PSpi(SPI *spi, PinName enable);

    /**
        Destroy the handler and powers down the flash chip
    */
    ~FlashM25PSpi();

    /**
        Read a part of the flash memory into destination.
        The buffer must be pre-allocated by the caller
        @param startAdr Flash address to start reading from. Doesn't need to match a page boundary
        @param destination Destination buffer
        @param len Number of bytes to read (must not exceed the end of memory)
        @return true if succeeded
    */
    bool read(uint32_t startAddr, void* destination, size_t len);

    /**
        Write the give buffer into the memory.
        This function handles dividing the write into pages until the physical write has finished.
        @note The flash must be erased before starting a write cycle.
        @param startAdr Address to start writing. Doesn't need to match a page boundary
        @param data Source data buffer
        @param len the number of bytes to write (must not exceed the end of memory)
        @return false if the adresses are out of range
    */
    bool write(uint32_t startAddr, const void* data, size_t len);

    /**
        Erases the sector containing the given address to 0xFF - this erases several pages.
        Refer to the IC datasheet or Size() methods for memory organisation.
        @param addr Any address within the sector to be cleared
    */
    void eraseSector(uint32_t addr);

    //! Erases the whole flash to 0xFF
    void eraseMem();
    
    //! Read detected flash size
    size_t flashSize() const {
        return _size;
    }
    
    //! Read detected flash sector size
    size_t sectorSize() const {return _sectorSize;}
    
    //! Read detected flash page size
    size_t pageSize() const {return _pageSize;}

private:
    bool writePage(uint32_t startAddr, const char* data, size_t len);
    int readStatus();
    void waitForWrite();
    void enableWrite();


    SPI* _spi;
    DigitalOut _enable;
    size_t _size, _pageSize, _sectorSize;
};