A library to read and write all 25* serial SPI flash devices from various manufacturers.

Committer:
stonie
Date:
Sun Feb 20 11:16:27 2011 +0000
Revision:
2:14c5db5e54df
Parent:
1:aa6409c599cb
minor fixes

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
stonie 0:d07f90d3c670 3 * the BSD license also applies to this code - have fun.
stonie 0:d07f90d3c670 4 */
stonie 0:d07f90d3c670 5
stonie 0:d07f90d3c670 6 /*
stonie 0:d07f90d3c670 7 * Ser25lcxxx library
stonie 0:d07f90d3c670 8 * Copyright (c) 2010 Hendrik Lipka
stonie 0:d07f90d3c670 9 *
stonie 0:d07f90d3c670 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
stonie 0:d07f90d3c670 11 * of this software and associated documentation files (the "Software"), to deal
stonie 0:d07f90d3c670 12 * in the Software without restriction, including without limitation the rights
stonie 0:d07f90d3c670 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
stonie 0:d07f90d3c670 14 * copies of the Software, and to permit persons to whom the Software is
stonie 0:d07f90d3c670 15 * furnished to do so, subject to the following conditions:
stonie 0:d07f90d3c670 16 *
stonie 0:d07f90d3c670 17 * The above copyright notice and this permission notice shall be included in
stonie 0:d07f90d3c670 18 * all copies or substantial portions of the Software.
stonie 0:d07f90d3c670 19 *
stonie 0:d07f90d3c670 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
stonie 0:d07f90d3c670 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
stonie 0:d07f90d3c670 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
stonie 0:d07f90d3c670 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
stonie 0:d07f90d3c670 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
stonie 0:d07f90d3c670 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
stonie 0:d07f90d3c670 26 * THE SOFTWARE.
stonie 0:d07f90d3c670 27 */
stonie 0:d07f90d3c670 28
stonie 0:d07f90d3c670 29 #ifndef __FLASH25SPI_H__
stonie 0:d07f90d3c670 30 #define __FLASH25SPI_H__
stonie 0:d07f90d3c670 31
stonie 0:d07f90d3c670 32 #include "mbed.h"
stonie 0:d07f90d3c670 33
stonie 0:d07f90d3c670 34 /**
stonie 0:d07f90d3c670 35 A class to read and write all 25* serial SPI flash devices from various manufacturers.
stonie 0:d07f90d3c670 36 It tries to autodetect the connected device. If your flash is not in the device list, but
stonie 0:d07f90d3c670 37 it compatible, feel free to modify the library and add it to the list.
stonie 0:d07f90d3c670 38 */
stonie 0:d07f90d3c670 39
stonie 0:d07f90d3c670 40 class flash25spi
stonie 0:d07f90d3c670 41 {
stonie 0:d07f90d3c670 42 public:
stonie 0:d07f90d3c670 43 /**
stonie 1:aa6409c599cb 44 create the handler class. it tries to autodetect your device. If your device is not recognized, add the devices parameters
stonie 1:aa6409c599cb 45 to the device data structure at the library sources.
stonie 0:d07f90d3c670 46 @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
stonie 0:d07f90d3c670 47 @param enable the pin name for the port where /CS is connected
stonie 0:d07f90d3c670 48 */
stonie 1:aa6409c599cb 49 flash25spi(SPI *spi, PinName enable);
stonie 0:d07f90d3c670 50
stonie 0:d07f90d3c670 51 /**
stonie 0:d07f90d3c670 52 destroys the handler, and frees the /CS pin
stonie 0:d07f90d3c670 53 */
stonie 0:d07f90d3c670 54 ~flash25spi();
stonie 0:d07f90d3c670 55
stonie 0:d07f90d3c670 56 /**
stonie 0:d07f90d3c670 57 read a part of the flashs memory. The buffer will be allocated here, and must be freed by the user
stonie 0:d07f90d3c670 58 @param startAdr the adress where to start reading. Doesn't need to match a page boundary
stonie 0:d07f90d3c670 59 @param len the number of bytes to read (must not exceed the end of memory)
stonie 0:d07f90d3c670 60 @return NULL in case of an error or if the adresses are out of range, the pointer to the data otherwise
stonie 0:d07f90d3c670 61 */
stonie 0:d07f90d3c670 62 char* read(unsigned int startAdr, unsigned int len);
stonie 0:d07f90d3c670 63
stonie 0:d07f90d3c670 64 /**
stonie 0:d07f90d3c670 65 writes the give buffer into the memory. This function handles dividing the write into
stonie 0:d07f90d3c670 66 pages, and waites until the phyiscal write has finished
stonie 0:d07f90d3c670 67 @param startAdr the adress where to start writing. Doesn't need to match a page boundary
stonie 0:d07f90d3c670 68 @param len the number of bytes to read (must not exceed the end of memory)
stonie 0:d07f90d3c670 69 @return false if the adresses are out of range
stonie 0:d07f90d3c670 70 */
stonie 0:d07f90d3c670 71 bool write(unsigned int startAdr, unsigned int len, const char* data);
stonie 0:d07f90d3c670 72
stonie 0:d07f90d3c670 73 /**
stonie 0:d07f90d3c670 74 fills the sector containing the given address with 0xFF - this erases several pages.
stonie 0:d07f90d3c670 75 Refer to the flashes datasheet about the memory organisation.
stonie 0:d07f90d3c670 76 @param addr an address within the sector to be cleared
stonie 0:d07f90d3c670 77 */
stonie 0:d07f90d3c670 78 void clearSector(unsigned int addr);
stonie 0:d07f90d3c670 79
stonie 0:d07f90d3c670 80 /**
stonie 0:d07f90d3c670 81 fills the block containing the given address with 0xFF - this erases several pages and several sectors.
stonie 0:d07f90d3c670 82 Refer to the flashes datasheet about the memory organisation.
stonie 0:d07f90d3c670 83 @param addr an address within the sector to be cleared
stonie 0:d07f90d3c670 84 */
stonie 0:d07f90d3c670 85 void clearBlock(unsigned int addr);
stonie 0:d07f90d3c670 86
stonie 0:d07f90d3c670 87
stonie 0:d07f90d3c670 88 /**
stonie 0:d07f90d3c670 89 fills the whole flash with 0xFF
stonie 0:d07f90d3c670 90 */
stonie 0:d07f90d3c670 91 void clearMem();
stonie 0:d07f90d3c670 92
stonie 0:d07f90d3c670 93 private:
stonie 0:d07f90d3c670 94 bool writePage(unsigned int startAdr, unsigned int len, const char* data);
stonie 0:d07f90d3c670 95 int readStatus();
stonie 0:d07f90d3c670 96 void waitForWrite();
stonie 0:d07f90d3c670 97 void enableWrite();
stonie 0:d07f90d3c670 98
stonie 0:d07f90d3c670 99
stonie 0:d07f90d3c670 100 SPI* _spi;
stonie 0:d07f90d3c670 101 DigitalOut* _enable;
stonie 0:d07f90d3c670 102 unsigned int _size,_pageSize,_sectorSize,_blockSize;
stonie 0:d07f90d3c670 103
stonie 0:d07f90d3c670 104 };
stonie 0:d07f90d3c670 105
stonie 0:d07f90d3c670 106
stonie 0:d07f90d3c670 107
stonie 0:d07f90d3c670 108 #endif