Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of flash25spi by
flash25spi.h@3:318fabd6708c, 2014-04-25 (annotated)
- Committer:
- Tomo2k
- Date:
- Fri Apr 25 12:25:08 2014 +0000
- Revision:
- 3:318fabd6708c
- Parent:
- 1:aa6409c599cb
- Child:
- 4:af870c53c0e9
Now supports two M25P* flash chips that do not have Device ID info
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 |
| 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 | |
| Tomo2k | 3:318fabd6708c | 36 | class CDI; |
| stonie | 0:d07f90d3c670 | 37 | |
| stonie | 0:d07f90d3c670 | 38 | /** |
| Tomo2k | 3:318fabd6708c | 39 | A class to read and write M25P* serial SPI flash devices. |
| stonie | 0:d07f90d3c670 | 40 | */ |
| Tomo2k | 3:318fabd6708c | 41 | class FlashM25PSpi |
| stonie | 0:d07f90d3c670 | 42 | { |
| Tomo2k | 3:318fabd6708c | 43 | public: |
| Tomo2k | 3:318fabd6708c | 44 | /** |
| Tomo2k | 3:318fabd6708c | 45 | Create the handler class. it tries to autodetect your device. If your device is not recognized, add the devices parameters |
| Tomo2k | 3:318fabd6708c | 46 | to the device data structure at the library sources. |
| Tomo2k | 3:318fabd6708c | 47 | @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 |
| Tomo2k | 3:318fabd6708c | 48 | @param enable the pin name for the port where /CS is connected |
| Tomo2k | 3:318fabd6708c | 49 | */ |
| Tomo2k | 3:318fabd6708c | 50 | FlashM25PSpi(SPI *spi, PinName enable, CDI *cdi = 0); |
| Tomo2k | 3:318fabd6708c | 51 | |
| Tomo2k | 3:318fabd6708c | 52 | /** |
| Tomo2k | 3:318fabd6708c | 53 | Destroy the handler and powers 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 | 3:318fabd6708c | 59 | The buffer must be pre-allocated by the caller |
| Tomo2k | 3:318fabd6708c | 60 | @param startAdr 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 | 3:318fabd6708c | 68 | Write the give buffer into the memory. |
| Tomo2k | 3:318fabd6708c | 69 | This function handles dividing the write into pages until the physical write has finished. |
| Tomo2k | 3:318fabd6708c | 70 | @note The flash must be erased before starting a write cycle. |
| Tomo2k | 3:318fabd6708c | 71 | @param startAdr Address to start writing. Doesn't need to match a page boundary |
| Tomo2k | 3:318fabd6708c | 72 | @param data Source data buffer |
| Tomo2k | 3:318fabd6708c | 73 | @param len the number of bytes to write (must not exceed the end of memory) |
| Tomo2k | 3:318fabd6708c | 74 | @return false if the adresses are out of range |
| Tomo2k | 3:318fabd6708c | 75 | */ |
| Tomo2k | 3:318fabd6708c | 76 | bool write(uint32_t startAddr, const void* data, size_t len); |
| Tomo2k | 3:318fabd6708c | 77 | |
| Tomo2k | 3:318fabd6708c | 78 | /** |
| Tomo2k | 3:318fabd6708c | 79 | Erases the sector containing the given address to 0xFF - this erases several pages. |
| Tomo2k | 3:318fabd6708c | 80 | Refer to the IC datasheet or Size() methods for memory organisation. |
| Tomo2k | 3:318fabd6708c | 81 | @param addr Any address within the sector to be cleared |
| Tomo2k | 3:318fabd6708c | 82 | */ |
| Tomo2k | 3:318fabd6708c | 83 | void eraseSector(uint32_t addr); |
| stonie | 0:d07f90d3c670 | 84 | |
| Tomo2k | 3:318fabd6708c | 85 | //! Erases the whole flash to 0xFF |
| Tomo2k | 3:318fabd6708c | 86 | void eraseMem(); |
| Tomo2k | 3:318fabd6708c | 87 | |
| Tomo2k | 3:318fabd6708c | 88 | //! Read detected flash size |
| Tomo2k | 3:318fabd6708c | 89 | size_t flashSize() const {return _size;} |
| Tomo2k | 3:318fabd6708c | 90 | |
| Tomo2k | 3:318fabd6708c | 91 | //! Read detected flash sector size |
| Tomo2k | 3:318fabd6708c | 92 | size_t sectorSize() const {return _sectorSize;} |
| Tomo2k | 3:318fabd6708c | 93 | |
| Tomo2k | 3:318fabd6708c | 94 | //! Read detected flash page size |
| Tomo2k | 3:318fabd6708c | 95 | size_t pageSize() const {return _pageSize;} |
| stonie | 0:d07f90d3c670 | 96 | |
| Tomo2k | 3:318fabd6708c | 97 | private: |
| Tomo2k | 3:318fabd6708c | 98 | bool writePage(uint32_t startAddr, const char* data, size_t len); |
| Tomo2k | 3:318fabd6708c | 99 | int readStatus(); |
| Tomo2k | 3:318fabd6708c | 100 | void waitForWrite(); |
| Tomo2k | 3:318fabd6708c | 101 | void enableWrite(); |
| stonie | 0:d07f90d3c670 | 102 | |
| stonie | 0:d07f90d3c670 | 103 | |
| Tomo2k | 3:318fabd6708c | 104 | SPI* _spi; |
| Tomo2k | 3:318fabd6708c | 105 | DigitalOut _enable; |
| Tomo2k | 3:318fabd6708c | 106 | size_t _size, _pageSize, _sectorSize; |
| Tomo2k | 3:318fabd6708c | 107 | }; |
