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