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