00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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