Flash handler for M25P* chips with no Device ID.
Fork of flash25spi by
flash25spi.h@12:63399e648bfc, 2014-08-19 (annotated)
- Committer:
- Tomo2k
- Date:
- Tue Aug 19 16:56:06 2014 +0000
- Revision:
- 12:63399e648bfc
- Parent:
- 8:7b09546cb412
Switched to private RTOS_SPI_Clean
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 | 8:7b09546cb412 | 34 | |
Tomo2k | 8:7b09546cb412 | 35 | // RTOS DMA-enabled version |
Tomo2k | 8:7b09546cb412 | 36 | #include "rtos.h" |
Tomo2k | 8:7b09546cb412 | 37 | #include "RTOS_SPI.h" |
Tomo2k | 8:7b09546cb412 | 38 | |
Tomo2k | 3:318fabd6708c | 39 | #include "mbed.h" |
stonie | 0:d07f90d3c670 | 40 | |
stonie | 0:d07f90d3c670 | 41 | /** |
Tomo2k | 8:7b09546cb412 | 42 | An RTOS interface class to read and write M25P* serial SPI flash devices. |
Tomo2k | 5:3fe5c97a223b | 43 | Supports M25P10-A and M25P40 ICs with no Device ID. |
stonie | 0:d07f90d3c670 | 44 | */ |
Tomo2k | 3:318fabd6708c | 45 | class FlashM25PSpi |
stonie | 0:d07f90d3c670 | 46 | { |
Tomo2k | 3:318fabd6708c | 47 | public: |
Tomo2k | 3:318fabd6708c | 48 | /** |
Tomo2k | 5:3fe5c97a223b | 49 | Create the flash interface class. |
Tomo2k | 5:3fe5c97a223b | 50 | It will autodetect the Signature of your device. If your device is not recognized, add the devices parameters to the device data structure. |
Tomo2k | 8:7b09546cb412 | 51 | @param spi the RTOS_SPI port where the flash is connected. Must be set to speed matching your device. |
Tomo2k | 3:318fabd6708c | 52 | @param enable the pin name for the port where /CS is connected |
Tomo2k | 8:7b09546cb412 | 53 | @param bits SPI bits format. If zero, leave SPI Format unchanged |
Tomo2k | 8:7b09546cb412 | 54 | @param mode SPI mode. |
Tomo2k | 3:318fabd6708c | 55 | */ |
Tomo2k | 8:7b09546cb412 | 56 | FlashM25PSpi(RTOS_SPI *spi, PinName enable, int bits = 8, int mode = 3); |
Tomo2k | 3:318fabd6708c | 57 | |
Tomo2k | 3:318fabd6708c | 58 | /** |
Tomo2k | 5:3fe5c97a223b | 59 | Destroy the interface and power down the flash chip |
Tomo2k | 3:318fabd6708c | 60 | */ |
Tomo2k | 3:318fabd6708c | 61 | ~FlashM25PSpi(); |
Tomo2k | 3:318fabd6708c | 62 | |
Tomo2k | 3:318fabd6708c | 63 | /** |
Tomo2k | 3:318fabd6708c | 64 | Read a part of the flash memory into destination. |
Tomo2k | 5:3fe5c97a223b | 65 | The buffer must be pre-allocated by the caller. |
Tomo2k | 5:3fe5c97a223b | 66 | @param startAddr Flash address to start reading from. Doesn't need to match a page boundary |
Tomo2k | 3:318fabd6708c | 67 | @param destination Destination buffer |
Tomo2k | 3:318fabd6708c | 68 | @param len Number of bytes to read (must not exceed the end of memory) |
Tomo2k | 3:318fabd6708c | 69 | @return true if succeeded |
Tomo2k | 3:318fabd6708c | 70 | */ |
Tomo2k | 3:318fabd6708c | 71 | bool read(uint32_t startAddr, void* destination, size_t len); |
stonie | 0:d07f90d3c670 | 72 | |
Tomo2k | 3:318fabd6708c | 73 | /** |
Tomo2k | 5:3fe5c97a223b | 74 | Write the provided buffer into the flash memory. |
Tomo2k | 3:318fabd6708c | 75 | This function handles dividing the write into pages until the physical write has finished. |
Tomo2k | 5:3fe5c97a223b | 76 | Blocks until write completes. |
Tomo2k | 5:3fe5c97a223b | 77 | @note The flash must be in the erased (0xFF) state before starting a write cycle. This is not checked. |
Tomo2k | 5:3fe5c97a223b | 78 | @param startAddr Address to start writing. Doesn't need to match a page boundary |
Tomo2k | 3:318fabd6708c | 79 | @param data Source data buffer |
Tomo2k | 5:3fe5c97a223b | 80 | @param len the number of bytes to write (must not exceed the end of flash memory) |
Tomo2k | 5:3fe5c97a223b | 81 | @return false if the addresses are out of range |
Tomo2k | 3:318fabd6708c | 82 | */ |
Tomo2k | 3:318fabd6708c | 83 | bool write(uint32_t startAddr, const void* data, size_t len); |
Tomo2k | 3:318fabd6708c | 84 | |
Tomo2k | 3:318fabd6708c | 85 | /** |
Tomo2k | 5:3fe5c97a223b | 86 | Erases the sector containing the given address to 0xFF. |
Tomo2k | 5:3fe5c97a223b | 87 | This erases several pages, refer to the IC datasheet or |
Tomo2k | 5:3fe5c97a223b | 88 | sectorSize() and pageSize() methods for memory organisation. |
Tomo2k | 3:318fabd6708c | 89 | @param addr Any address within the sector to be cleared |
Tomo2k | 3:318fabd6708c | 90 | */ |
Tomo2k | 3:318fabd6708c | 91 | void eraseSector(uint32_t addr); |
stonie | 0:d07f90d3c670 | 92 | |
Tomo2k | 3:318fabd6708c | 93 | //! Erases the whole flash to 0xFF |
Tomo2k | 3:318fabd6708c | 94 | void eraseMem(); |
Tomo2k | 5:3fe5c97a223b | 95 | |
Tomo2k | 5:3fe5c97a223b | 96 | //! Flash size |
Tomo2k | 4:af870c53c0e9 | 97 | size_t flashSize() const { |
Tomo2k | 4:af870c53c0e9 | 98 | return _size; |
Tomo2k | 4:af870c53c0e9 | 99 | } |
Tomo2k | 5:3fe5c97a223b | 100 | |
Tomo2k | 5:3fe5c97a223b | 101 | //! Flash sector size |
Tomo2k | 5:3fe5c97a223b | 102 | size_t sectorSize() const { |
Tomo2k | 5:3fe5c97a223b | 103 | return _sectorSize; |
Tomo2k | 5:3fe5c97a223b | 104 | } |
Tomo2k | 5:3fe5c97a223b | 105 | |
Tomo2k | 5:3fe5c97a223b | 106 | //! Flash page size |
Tomo2k | 5:3fe5c97a223b | 107 | size_t pageSize() const { |
Tomo2k | 5:3fe5c97a223b | 108 | return _pageSize; |
Tomo2k | 5:3fe5c97a223b | 109 | } |
stonie | 0:d07f90d3c670 | 110 | |
Tomo2k | 3:318fabd6708c | 111 | private: |
Tomo2k | 8:7b09546cb412 | 112 | bool writePage(uint32_t startAddr, const uint8_t* data, size_t len); |
Tomo2k | 3:318fabd6708c | 113 | int readStatus(); |
Tomo2k | 3:318fabd6708c | 114 | void waitForWrite(); |
Tomo2k | 3:318fabd6708c | 115 | void enableWrite(); |
stonie | 0:d07f90d3c670 | 116 | |
Tomo2k | 8:7b09546cb412 | 117 | RTOS_SPI* _spi; |
stonie | 0:d07f90d3c670 | 118 | |
Tomo2k | 3:318fabd6708c | 119 | DigitalOut _enable; |
Tomo2k | 3:318fabd6708c | 120 | size_t _size, _pageSize, _sectorSize; |
Tomo2k | 3:318fabd6708c | 121 | }; |