Flash handler for M25P* chips with no Device ID.

Dependencies:   RTOS_SPI_Clean

Fork of flash25spi by Klaus Steinhammer

Committer:
Tomo2k
Date:
Fri Apr 25 12:49:48 2014 +0000
Revision:
5:3fe5c97a223b
Parent:
4:af870c53c0e9
Child:
6:94558d4243f8
Documentation cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stonie 0:d07f90d3c670 1 /* This library is based on the Ser25lcxxx library by Hendrik Lipka
stonie 0:d07f90d3c670 2 * It was adapted to flash memory chips on 19.2.2011 by Klaus Steinhammer
Tomo2k 3:318fabd6708c 3 *
Tomo2k 3:318fabd6708c 4 * It was further adapted to M25P* SPI Flash memory chips that do not
Tomo2k 3:318fabd6708c 5 * support the "RDID" device ID instructions on 25.03.2014 by Richard Thompson
Tomo2k 3:318fabd6708c 6 *
Tomo2k 3:318fabd6708c 7 * The BSD license also applies to this code - have fun.
stonie 0:d07f90d3c670 8 */
stonie 0:d07f90d3c670 9
stonie 0:d07f90d3c670 10 /*
stonie 0:d07f90d3c670 11 * Ser25lcxxx library
stonie 0:d07f90d3c670 12 * Copyright (c) 2010 Hendrik Lipka
Tomo2k 3:318fabd6708c 13 *
stonie 0:d07f90d3c670 14 * Permission is hereby granted, free of charge, to any person obtaining a copy
stonie 0:d07f90d3c670 15 * of this software and associated documentation files (the "Software"), to deal
stonie 0:d07f90d3c670 16 * in the Software without restriction, including without limitation the rights
stonie 0:d07f90d3c670 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
stonie 0:d07f90d3c670 18 * copies of the Software, and to permit persons to whom the Software is
stonie 0:d07f90d3c670 19 * furnished to do so, subject to the following conditions:
Tomo2k 3:318fabd6708c 20 *
stonie 0:d07f90d3c670 21 * The above copyright notice and this permission notice shall be included in
stonie 0:d07f90d3c670 22 * all copies or substantial portions of the Software.
Tomo2k 3:318fabd6708c 23 *
stonie 0:d07f90d3c670 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
stonie 0:d07f90d3c670 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
stonie 0:d07f90d3c670 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
stonie 0:d07f90d3c670 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
stonie 0:d07f90d3c670 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
stonie 0:d07f90d3c670 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
stonie 0:d07f90d3c670 30 * THE SOFTWARE.
stonie 0:d07f90d3c670 31 */
stonie 0:d07f90d3c670 32
Tomo2k 3:318fabd6708c 33 #pragma once
Tomo2k 3:318fabd6708c 34 #include "mbed.h"
stonie 0:d07f90d3c670 35
stonie 0:d07f90d3c670 36 /**
Tomo2k 5:3fe5c97a223b 37 An interface class to read and write M25P* serial SPI flash devices.
Tomo2k 5:3fe5c97a223b 38 Supports M25P10-A and M25P40 ICs with no Device ID.
stonie 0:d07f90d3c670 39 */
Tomo2k 3:318fabd6708c 40 class FlashM25PSpi
stonie 0:d07f90d3c670 41 {
Tomo2k 3:318fabd6708c 42 public:
Tomo2k 3:318fabd6708c 43 /**
Tomo2k 5:3fe5c97a223b 44 Create the flash interface class.
Tomo2k 5:3fe5c97a223b 45 It will autodetect the Signature of your device. If your device is not recognized, add the devices parameters to the device data structure.
Tomo2k 5:3fe5c97a223b 46 @param spi the SPI port where the flash is connected. Must be set to speed matching your device.
Tomo2k 5:3fe5c97a223b 47 Will automatically set it to format(8,0).
Tomo2k 3:318fabd6708c 48 @param enable the pin name for the port where /CS is connected
Tomo2k 3:318fabd6708c 49 */
Tomo2k 4:af870c53c0e9 50 FlashM25PSpi(SPI *spi, PinName enable);
Tomo2k 3:318fabd6708c 51
Tomo2k 3:318fabd6708c 52 /**
Tomo2k 5:3fe5c97a223b 53 Destroy the interface and power down the flash chip
Tomo2k 3:318fabd6708c 54 */
Tomo2k 3:318fabd6708c 55 ~FlashM25PSpi();
Tomo2k 3:318fabd6708c 56
Tomo2k 3:318fabd6708c 57 /**
Tomo2k 3:318fabd6708c 58 Read a part of the flash memory into destination.
Tomo2k 5:3fe5c97a223b 59 The buffer must be pre-allocated by the caller.
Tomo2k 5:3fe5c97a223b 60 @param startAddr Flash address to start reading from. Doesn't need to match a page boundary
Tomo2k 3:318fabd6708c 61 @param destination Destination buffer
Tomo2k 3:318fabd6708c 62 @param len Number of bytes to read (must not exceed the end of memory)
Tomo2k 3:318fabd6708c 63 @return true if succeeded
Tomo2k 3:318fabd6708c 64 */
Tomo2k 3:318fabd6708c 65 bool read(uint32_t startAddr, void* destination, size_t len);
stonie 0:d07f90d3c670 66
Tomo2k 3:318fabd6708c 67 /**
Tomo2k 5:3fe5c97a223b 68 Write the provided buffer into the flash memory.
Tomo2k 3:318fabd6708c 69 This function handles dividing the write into pages until the physical write has finished.
Tomo2k 5:3fe5c97a223b 70 Blocks until write completes.
Tomo2k 5:3fe5c97a223b 71 @note The flash must be in the erased (0xFF) state before starting a write cycle. This is not checked.
Tomo2k 5:3fe5c97a223b 72 @param startAddr Address to start writing. Doesn't need to match a page boundary
Tomo2k 3:318fabd6708c 73 @param data Source data buffer
Tomo2k 5:3fe5c97a223b 74 @param len the number of bytes to write (must not exceed the end of flash memory)
Tomo2k 5:3fe5c97a223b 75 @return false if the addresses are out of range
Tomo2k 3:318fabd6708c 76 */
Tomo2k 3:318fabd6708c 77 bool write(uint32_t startAddr, const void* data, size_t len);
Tomo2k 3:318fabd6708c 78
Tomo2k 3:318fabd6708c 79 /**
Tomo2k 5:3fe5c97a223b 80 Erases the sector containing the given address to 0xFF.
Tomo2k 5:3fe5c97a223b 81 This erases several pages, refer to the IC datasheet or
Tomo2k 5:3fe5c97a223b 82 sectorSize() and pageSize() methods for memory organisation.
Tomo2k 3:318fabd6708c 83 @param addr Any address within the sector to be cleared
Tomo2k 3:318fabd6708c 84 */
Tomo2k 3:318fabd6708c 85 void eraseSector(uint32_t addr);
stonie 0:d07f90d3c670 86
Tomo2k 3:318fabd6708c 87 //! Erases the whole flash to 0xFF
Tomo2k 3:318fabd6708c 88 void eraseMem();
Tomo2k 5:3fe5c97a223b 89
Tomo2k 5:3fe5c97a223b 90 //! Flash size
Tomo2k 4:af870c53c0e9 91 size_t flashSize() const {
Tomo2k 4:af870c53c0e9 92 return _size;
Tomo2k 4:af870c53c0e9 93 }
Tomo2k 5:3fe5c97a223b 94
Tomo2k 5:3fe5c97a223b 95 //! Flash sector size
Tomo2k 5:3fe5c97a223b 96 size_t sectorSize() const {
Tomo2k 5:3fe5c97a223b 97 return _sectorSize;
Tomo2k 5:3fe5c97a223b 98 }
Tomo2k 5:3fe5c97a223b 99
Tomo2k 5:3fe5c97a223b 100 //! Flash page size
Tomo2k 5:3fe5c97a223b 101 size_t pageSize() const {
Tomo2k 5:3fe5c97a223b 102 return _pageSize;
Tomo2k 5:3fe5c97a223b 103 }
stonie 0:d07f90d3c670 104
Tomo2k 3:318fabd6708c 105 private:
Tomo2k 3:318fabd6708c 106 bool writePage(uint32_t startAddr, const char* data, size_t len);
Tomo2k 3:318fabd6708c 107 int readStatus();
Tomo2k 3:318fabd6708c 108 void waitForWrite();
Tomo2k 3:318fabd6708c 109 void enableWrite();
stonie 0:d07f90d3c670 110
stonie 0:d07f90d3c670 111
Tomo2k 3:318fabd6708c 112 SPI* _spi;
Tomo2k 3:318fabd6708c 113 DigitalOut _enable;
Tomo2k 3:318fabd6708c 114 size_t _size, _pageSize, _sectorSize;
Tomo2k 3:318fabd6708c 115 };