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