1232
drivers/FlashIAP.h@0:06036f8bee2d, 2022-10-24 (annotated)
- Committer:
- ganlikun
- Date:
- Mon Oct 24 15:19:39 2022 +0000
- Revision:
- 0:06036f8bee2d
11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:06036f8bee2d | 1 | /* mbed Microcontroller Library |
ganlikun | 0:06036f8bee2d | 2 | * Copyright (c) 2017 ARM Limited |
ganlikun | 0:06036f8bee2d | 3 | * |
ganlikun | 0:06036f8bee2d | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
ganlikun | 0:06036f8bee2d | 5 | * of this software and associated documentation files (the "Software"), to deal |
ganlikun | 0:06036f8bee2d | 6 | * in the Software without restriction, including without limitation the rights |
ganlikun | 0:06036f8bee2d | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ganlikun | 0:06036f8bee2d | 8 | * copies of the Software, and to permit persons to whom the Software is |
ganlikun | 0:06036f8bee2d | 9 | * furnished to do so, subject to the following conditions: |
ganlikun | 0:06036f8bee2d | 10 | * |
ganlikun | 0:06036f8bee2d | 11 | * The above copyright notice and this permission notice shall be included in |
ganlikun | 0:06036f8bee2d | 12 | * all copies or substantial portions of the Software. |
ganlikun | 0:06036f8bee2d | 13 | * |
ganlikun | 0:06036f8bee2d | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ganlikun | 0:06036f8bee2d | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ganlikun | 0:06036f8bee2d | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ganlikun | 0:06036f8bee2d | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ganlikun | 0:06036f8bee2d | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ganlikun | 0:06036f8bee2d | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
ganlikun | 0:06036f8bee2d | 20 | * SOFTWARE. |
ganlikun | 0:06036f8bee2d | 21 | */ |
ganlikun | 0:06036f8bee2d | 22 | #ifndef MBED_FLASHIAP_H |
ganlikun | 0:06036f8bee2d | 23 | #define MBED_FLASHIAP_H |
ganlikun | 0:06036f8bee2d | 24 | |
ganlikun | 0:06036f8bee2d | 25 | #if defined (DEVICE_FLASH) || defined(DOXYGEN_ONLY) |
ganlikun | 0:06036f8bee2d | 26 | |
ganlikun | 0:06036f8bee2d | 27 | #include "flash_api.h" |
ganlikun | 0:06036f8bee2d | 28 | #include "platform/SingletonPtr.h" |
ganlikun | 0:06036f8bee2d | 29 | #include "platform/PlatformMutex.h" |
ganlikun | 0:06036f8bee2d | 30 | #include "platform/NonCopyable.h" |
ganlikun | 0:06036f8bee2d | 31 | |
ganlikun | 0:06036f8bee2d | 32 | namespace mbed { |
ganlikun | 0:06036f8bee2d | 33 | |
ganlikun | 0:06036f8bee2d | 34 | /** \addtogroup drivers */ |
ganlikun | 0:06036f8bee2d | 35 | |
ganlikun | 0:06036f8bee2d | 36 | /** Flash IAP driver. It invokes flash HAL functions. |
ganlikun | 0:06036f8bee2d | 37 | * |
ganlikun | 0:06036f8bee2d | 38 | * @note Synchronization level: Thread safe |
ganlikun | 0:06036f8bee2d | 39 | * @ingroup drivers |
ganlikun | 0:06036f8bee2d | 40 | */ |
ganlikun | 0:06036f8bee2d | 41 | class FlashIAP : private NonCopyable<FlashIAP> { |
ganlikun | 0:06036f8bee2d | 42 | public: |
ganlikun | 0:06036f8bee2d | 43 | FlashIAP(); |
ganlikun | 0:06036f8bee2d | 44 | ~FlashIAP(); |
ganlikun | 0:06036f8bee2d | 45 | |
ganlikun | 0:06036f8bee2d | 46 | /** Initialize a flash IAP device |
ganlikun | 0:06036f8bee2d | 47 | * |
ganlikun | 0:06036f8bee2d | 48 | * Should be called once per lifetime of the object. |
ganlikun | 0:06036f8bee2d | 49 | * @return 0 on success or a negative error code on failure |
ganlikun | 0:06036f8bee2d | 50 | */ |
ganlikun | 0:06036f8bee2d | 51 | int init(); |
ganlikun | 0:06036f8bee2d | 52 | |
ganlikun | 0:06036f8bee2d | 53 | /** Deinitialize a flash IAP device |
ganlikun | 0:06036f8bee2d | 54 | * |
ganlikun | 0:06036f8bee2d | 55 | * @return 0 on success or a negative error code on failure |
ganlikun | 0:06036f8bee2d | 56 | */ |
ganlikun | 0:06036f8bee2d | 57 | int deinit(); |
ganlikun | 0:06036f8bee2d | 58 | |
ganlikun | 0:06036f8bee2d | 59 | /** Read data from a flash device. |
ganlikun | 0:06036f8bee2d | 60 | * |
ganlikun | 0:06036f8bee2d | 61 | * This method invokes memcpy - reads number of bytes from the address |
ganlikun | 0:06036f8bee2d | 62 | * |
ganlikun | 0:06036f8bee2d | 63 | * @param buffer Buffer to write to |
ganlikun | 0:06036f8bee2d | 64 | * @param addr Flash address to begin reading from |
ganlikun | 0:06036f8bee2d | 65 | * @param size Size to read in bytes |
ganlikun | 0:06036f8bee2d | 66 | * @return 0 on success, negative error code on failure |
ganlikun | 0:06036f8bee2d | 67 | */ |
ganlikun | 0:06036f8bee2d | 68 | int read(void *buffer, uint32_t addr, uint32_t size); |
ganlikun | 0:06036f8bee2d | 69 | |
ganlikun | 0:06036f8bee2d | 70 | /** Program data to pages |
ganlikun | 0:06036f8bee2d | 71 | * |
ganlikun | 0:06036f8bee2d | 72 | * The sectors must have been erased prior to being programmed |
ganlikun | 0:06036f8bee2d | 73 | * |
ganlikun | 0:06036f8bee2d | 74 | * @param buffer Buffer of data to be written |
ganlikun | 0:06036f8bee2d | 75 | * @param addr Address of a page to begin writing to, must be a multiple of program and sector sizes |
ganlikun | 0:06036f8bee2d | 76 | * @param size Size to write in bytes, must be a multiple of program and sector sizes |
ganlikun | 0:06036f8bee2d | 77 | * @return 0 on success, negative error code on failure |
ganlikun | 0:06036f8bee2d | 78 | */ |
ganlikun | 0:06036f8bee2d | 79 | int program(const void *buffer, uint32_t addr, uint32_t size); |
ganlikun | 0:06036f8bee2d | 80 | |
ganlikun | 0:06036f8bee2d | 81 | /** Erase sectors |
ganlikun | 0:06036f8bee2d | 82 | * |
ganlikun | 0:06036f8bee2d | 83 | * The state of an erased sector is undefined until it has been programmed |
ganlikun | 0:06036f8bee2d | 84 | * |
ganlikun | 0:06036f8bee2d | 85 | * @param addr Address of a sector to begin erasing, must be a multiple of the sector size |
ganlikun | 0:06036f8bee2d | 86 | * @param size Size to erase in bytes, must be a multiple of the sector size |
ganlikun | 0:06036f8bee2d | 87 | * @return 0 on success, negative error code on failure |
ganlikun | 0:06036f8bee2d | 88 | */ |
ganlikun | 0:06036f8bee2d | 89 | int erase(uint32_t addr, uint32_t size); |
ganlikun | 0:06036f8bee2d | 90 | |
ganlikun | 0:06036f8bee2d | 91 | /** Get the sector size at the defined address |
ganlikun | 0:06036f8bee2d | 92 | * |
ganlikun | 0:06036f8bee2d | 93 | * Sector size might differ at address ranges. |
ganlikun | 0:06036f8bee2d | 94 | * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048> |
ganlikun | 0:06036f8bee2d | 95 | * |
ganlikun | 0:06036f8bee2d | 96 | * @param addr Address of or inside the sector to query |
ganlikun | 0:06036f8bee2d | 97 | * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped |
ganlikun | 0:06036f8bee2d | 98 | */ |
ganlikun | 0:06036f8bee2d | 99 | uint32_t get_sector_size(uint32_t addr) const; |
ganlikun | 0:06036f8bee2d | 100 | |
ganlikun | 0:06036f8bee2d | 101 | /** Get the flash start address |
ganlikun | 0:06036f8bee2d | 102 | * |
ganlikun | 0:06036f8bee2d | 103 | * @return Flash start address |
ganlikun | 0:06036f8bee2d | 104 | */ |
ganlikun | 0:06036f8bee2d | 105 | uint32_t get_flash_start() const; |
ganlikun | 0:06036f8bee2d | 106 | |
ganlikun | 0:06036f8bee2d | 107 | /** Get the flash size |
ganlikun | 0:06036f8bee2d | 108 | * |
ganlikun | 0:06036f8bee2d | 109 | * @return Flash size |
ganlikun | 0:06036f8bee2d | 110 | */ |
ganlikun | 0:06036f8bee2d | 111 | uint32_t get_flash_size() const; |
ganlikun | 0:06036f8bee2d | 112 | |
ganlikun | 0:06036f8bee2d | 113 | /** Get the program page size |
ganlikun | 0:06036f8bee2d | 114 | * |
ganlikun | 0:06036f8bee2d | 115 | * The page size defines the writable page size |
ganlikun | 0:06036f8bee2d | 116 | * @return Size of a program page in bytes |
ganlikun | 0:06036f8bee2d | 117 | */ |
ganlikun | 0:06036f8bee2d | 118 | uint32_t get_page_size() const; |
ganlikun | 0:06036f8bee2d | 119 | |
ganlikun | 0:06036f8bee2d | 120 | private: |
ganlikun | 0:06036f8bee2d | 121 | |
ganlikun | 0:06036f8bee2d | 122 | /* Check if address and size are aligned to a sector |
ganlikun | 0:06036f8bee2d | 123 | * |
ganlikun | 0:06036f8bee2d | 124 | * @param addr Address of block to check for alignment |
ganlikun | 0:06036f8bee2d | 125 | * @param size Size of block to check for alignment |
ganlikun | 0:06036f8bee2d | 126 | * @return true if the block is sector aligned, false otherwise |
ganlikun | 0:06036f8bee2d | 127 | */ |
ganlikun | 0:06036f8bee2d | 128 | bool is_aligned_to_sector(uint32_t addr, uint32_t size); |
ganlikun | 0:06036f8bee2d | 129 | |
ganlikun | 0:06036f8bee2d | 130 | flash_t _flash; |
ganlikun | 0:06036f8bee2d | 131 | static SingletonPtr<PlatformMutex> _mutex; |
ganlikun | 0:06036f8bee2d | 132 | }; |
ganlikun | 0:06036f8bee2d | 133 | |
ganlikun | 0:06036f8bee2d | 134 | } /* namespace mbed */ |
ganlikun | 0:06036f8bee2d | 135 | |
ganlikun | 0:06036f8bee2d | 136 | #endif /* DEVICE_FLASH */ |
ganlikun | 0:06036f8bee2d | 137 | |
ganlikun | 0:06036f8bee2d | 138 | #endif /* MBED_FLASHIAP_H */ |
ganlikun | 0:06036f8bee2d | 139 |