A library to read and write all 25* serial SPI flash devices from various manufacturers.
flash25spi.h@0:d07f90d3c670, 2011-02-20 (annotated)
- Committer:
- stonie
- Date:
- Sun Feb 20 09:19:04 2011 +0000
- Revision:
- 0:d07f90d3c670
- Child:
- 1:aa6409c599cb
Initial Release
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:d07f90d3c670 | 44 | create the handler class |
stonie | 0:d07f90d3c670 | 45 | @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 | 46 | @param enable the pin name for the port where /CS is connected |
stonie | 0:d07f90d3c670 | 47 | @param bytes the size of you flash in bytes (NOT bits) |
stonie | 0:d07f90d3c670 | 48 | @param pagesize the size of a single page, to provide overruns |
stonie | 0:d07f90d3c670 | 49 | @param sectorsize the size of a single sector, for erasing |
stonie | 0:d07f90d3c670 | 50 | */ |
stonie | 0:d07f90d3c670 | 51 | flash25spi(SPI *spi, PinName enable, unsigned int bytes, unsigned int pagesize, unsigned int sectorsize); |
stonie | 0:d07f90d3c670 | 52 | |
stonie | 0:d07f90d3c670 | 53 | /** |
stonie | 0:d07f90d3c670 | 54 | destroys the handler, and frees the /CS pin |
stonie | 0:d07f90d3c670 | 55 | */ |
stonie | 0:d07f90d3c670 | 56 | ~flash25spi(); |
stonie | 0:d07f90d3c670 | 57 | |
stonie | 0:d07f90d3c670 | 58 | /** |
stonie | 0:d07f90d3c670 | 59 | read a part of the flashs memory. The buffer will be allocated here, and must be freed by the user |
stonie | 0:d07f90d3c670 | 60 | @param startAdr the adress where to start reading. Doesn't need to match a page boundary |
stonie | 0:d07f90d3c670 | 61 | @param len the number of bytes to read (must not exceed the end of memory) |
stonie | 0:d07f90d3c670 | 62 | @return NULL in case of an error or if the adresses are out of range, the pointer to the data otherwise |
stonie | 0:d07f90d3c670 | 63 | */ |
stonie | 0:d07f90d3c670 | 64 | char* read(unsigned int startAdr, unsigned int len); |
stonie | 0:d07f90d3c670 | 65 | |
stonie | 0:d07f90d3c670 | 66 | /** |
stonie | 0:d07f90d3c670 | 67 | writes the give buffer into the memory. This function handles dividing the write into |
stonie | 0:d07f90d3c670 | 68 | pages, and waites until the phyiscal write has finished |
stonie | 0:d07f90d3c670 | 69 | @param startAdr the adress where to start writing. Doesn't need to match a page boundary |
stonie | 0:d07f90d3c670 | 70 | @param len the number of bytes to read (must not exceed the end of memory) |
stonie | 0:d07f90d3c670 | 71 | @return false if the adresses are out of range |
stonie | 0:d07f90d3c670 | 72 | */ |
stonie | 0:d07f90d3c670 | 73 | bool write(unsigned int startAdr, unsigned int len, const char* data); |
stonie | 0:d07f90d3c670 | 74 | |
stonie | 0:d07f90d3c670 | 75 | /** |
stonie | 0:d07f90d3c670 | 76 | fills the sector containing the given address with 0xFF - this erases several pages. |
stonie | 0:d07f90d3c670 | 77 | Refer to the flashes datasheet about the memory organisation. |
stonie | 0:d07f90d3c670 | 78 | @param addr an address within the sector to be cleared |
stonie | 0:d07f90d3c670 | 79 | */ |
stonie | 0:d07f90d3c670 | 80 | void clearSector(unsigned int addr); |
stonie | 0:d07f90d3c670 | 81 | |
stonie | 0:d07f90d3c670 | 82 | /** |
stonie | 0:d07f90d3c670 | 83 | fills the block containing the given address with 0xFF - this erases several pages and several sectors. |
stonie | 0:d07f90d3c670 | 84 | Refer to the flashes datasheet about the memory organisation. |
stonie | 0:d07f90d3c670 | 85 | @param addr an address within the sector to be cleared |
stonie | 0:d07f90d3c670 | 86 | */ |
stonie | 0:d07f90d3c670 | 87 | void clearBlock(unsigned int addr); |
stonie | 0:d07f90d3c670 | 88 | |
stonie | 0:d07f90d3c670 | 89 | |
stonie | 0:d07f90d3c670 | 90 | /** |
stonie | 0:d07f90d3c670 | 91 | fills the whole flash with 0xFF |
stonie | 0:d07f90d3c670 | 92 | */ |
stonie | 0:d07f90d3c670 | 93 | void clearMem(); |
stonie | 0:d07f90d3c670 | 94 | |
stonie | 0:d07f90d3c670 | 95 | private: |
stonie | 0:d07f90d3c670 | 96 | bool writePage(unsigned int startAdr, unsigned int len, const char* data); |
stonie | 0:d07f90d3c670 | 97 | int readStatus(); |
stonie | 0:d07f90d3c670 | 98 | void waitForWrite(); |
stonie | 0:d07f90d3c670 | 99 | void enableWrite(); |
stonie | 0:d07f90d3c670 | 100 | |
stonie | 0:d07f90d3c670 | 101 | |
stonie | 0:d07f90d3c670 | 102 | SPI* _spi; |
stonie | 0:d07f90d3c670 | 103 | DigitalOut* _enable; |
stonie | 0:d07f90d3c670 | 104 | unsigned int _size,_pageSize,_sectorSize,_blockSize; |
stonie | 0:d07f90d3c670 | 105 | |
stonie | 0:d07f90d3c670 | 106 | }; |
stonie | 0:d07f90d3c670 | 107 | |
stonie | 0:d07f90d3c670 | 108 | |
stonie | 0:d07f90d3c670 | 109 | |
stonie | 0:d07f90d3c670 | 110 | #endif |