mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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