forked
drivers/FlashIAP.h@168:9672193075cf, 2017-07-06 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Jul 06 15:42:05 2017 +0100
- Revision:
- 168:9672193075cf
- Parent:
- 167:e84263d55307
This updates the lib to the mbed lib v 146
Who changed what in which revision?
User | Revision | Line number | New 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 | 167:e84263d55307 | 25 | #if defined (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" |
<> | 160:d5399cc887bb | 31 | |
<> | 160:d5399cc887bb | 32 | namespace mbed { |
<> | 160:d5399cc887bb | 33 | |
<> | 160:d5399cc887bb | 34 | /** \addtogroup drivers */ |
<> | 160:d5399cc887bb | 35 | |
<> | 160:d5399cc887bb | 36 | /** Flash IAP driver. It invokes flash HAL functions. |
<> | 160:d5399cc887bb | 37 | * |
AnnaBridge | 167:e84263d55307 | 38 | * @note Synchronization level: Thread safe |
AnnaBridge | 167:e84263d55307 | 39 | * @ingroup drivers |
<> | 160:d5399cc887bb | 40 | */ |
AnnaBridge | 168:9672193075cf | 41 | class FlashIAP : private NonCopyable<FlashIAP> { |
<> | 160:d5399cc887bb | 42 | public: |
<> | 160:d5399cc887bb | 43 | FlashIAP(); |
<> | 160:d5399cc887bb | 44 | ~FlashIAP(); |
<> | 160:d5399cc887bb | 45 | |
<> | 160:d5399cc887bb | 46 | /** Initialize a flash IAP device |
<> | 160:d5399cc887bb | 47 | * |
<> | 160:d5399cc887bb | 48 | * Should be called once per lifetime of the object. |
<> | 160:d5399cc887bb | 49 | * @return 0 on success or a negative error code on failure |
<> | 160:d5399cc887bb | 50 | */ |
<> | 160:d5399cc887bb | 51 | int init(); |
<> | 160:d5399cc887bb | 52 | |
<> | 160:d5399cc887bb | 53 | /** Deinitialize a flash IAP device |
<> | 160:d5399cc887bb | 54 | * |
<> | 160:d5399cc887bb | 55 | * @return 0 on success or a negative error code on failure |
<> | 160:d5399cc887bb | 56 | */ |
<> | 160:d5399cc887bb | 57 | int deinit(); |
<> | 160:d5399cc887bb | 58 | |
<> | 160:d5399cc887bb | 59 | /** Read data from a flash device. |
<> | 160:d5399cc887bb | 60 | * |
<> | 160:d5399cc887bb | 61 | * This method invokes memcpy - reads number of bytes from the address |
<> | 160:d5399cc887bb | 62 | * |
<> | 160:d5399cc887bb | 63 | * @param buffer Buffer to write to |
<> | 160:d5399cc887bb | 64 | * @param addr Flash address to begin reading from |
<> | 160:d5399cc887bb | 65 | * @param size Size to read in bytes |
<> | 160:d5399cc887bb | 66 | * @return 0 on success, negative error code on failure |
<> | 160:d5399cc887bb | 67 | */ |
<> | 160:d5399cc887bb | 68 | int read(void *buffer, uint32_t addr, uint32_t size); |
<> | 160:d5399cc887bb | 69 | |
<> | 160:d5399cc887bb | 70 | /** Program data to pages |
<> | 160:d5399cc887bb | 71 | * |
<> | 160:d5399cc887bb | 72 | * The sectors must have been erased prior to being programmed |
<> | 160:d5399cc887bb | 73 | * |
<> | 160:d5399cc887bb | 74 | * @param buffer Buffer of data to be written |
<> | 160:d5399cc887bb | 75 | * @param addr Address of a page to begin writing to, must be a multiple of program and sector sizes |
<> | 160:d5399cc887bb | 76 | * @param size Size to write in bytes, must be a multiple of program and sector sizes |
<> | 160:d5399cc887bb | 77 | * @return 0 on success, negative error code on failure |
<> | 160:d5399cc887bb | 78 | */ |
<> | 160:d5399cc887bb | 79 | int program(const void *buffer, uint32_t addr, uint32_t size); |
<> | 160:d5399cc887bb | 80 | |
<> | 160:d5399cc887bb | 81 | /** Erase sectors |
<> | 160:d5399cc887bb | 82 | * |
<> | 160:d5399cc887bb | 83 | * The state of an erased sector is undefined until it has been programmed |
<> | 160:d5399cc887bb | 84 | * |
<> | 160:d5399cc887bb | 85 | * @param addr Address of a sector to begin erasing, must be a multiple of the sector size |
<> | 160:d5399cc887bb | 86 | * @param size Size to erase in bytes, must be a multiple of the sector size |
<> | 160:d5399cc887bb | 87 | * @return 0 on success, negative error code on failure |
<> | 160:d5399cc887bb | 88 | */ |
<> | 160:d5399cc887bb | 89 | int erase(uint32_t addr, uint32_t size); |
<> | 160:d5399cc887bb | 90 | |
<> | 160:d5399cc887bb | 91 | /** Get the sector size at the defined address |
<> | 160:d5399cc887bb | 92 | * |
<> | 160:d5399cc887bb | 93 | * Sector size might differ at address ranges. |
<> | 160:d5399cc887bb | 94 | * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048> |
<> | 160:d5399cc887bb | 95 | * |
<> | 160:d5399cc887bb | 96 | * @param addr Address of or inside the sector to query |
<> | 160:d5399cc887bb | 97 | * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped |
<> | 160:d5399cc887bb | 98 | */ |
<> | 160:d5399cc887bb | 99 | uint32_t get_sector_size(uint32_t addr) const; |
<> | 160:d5399cc887bb | 100 | |
<> | 160:d5399cc887bb | 101 | /** Get the flash start address |
<> | 160:d5399cc887bb | 102 | * |
<> | 160:d5399cc887bb | 103 | * @return Flash start address |
<> | 160:d5399cc887bb | 104 | */ |
<> | 160:d5399cc887bb | 105 | uint32_t get_flash_start() const; |
<> | 160:d5399cc887bb | 106 | |
<> | 160:d5399cc887bb | 107 | /** Get the flash size |
<> | 160:d5399cc887bb | 108 | * |
<> | 160:d5399cc887bb | 109 | * @return Flash size |
<> | 160:d5399cc887bb | 110 | */ |
<> | 160:d5399cc887bb | 111 | uint32_t get_flash_size() const; |
<> | 160:d5399cc887bb | 112 | |
<> | 160:d5399cc887bb | 113 | /** Get the program page size |
<> | 160:d5399cc887bb | 114 | * |
<> | 160:d5399cc887bb | 115 | * @return Size of a program page in bytes |
<> | 160:d5399cc887bb | 116 | */ |
<> | 160:d5399cc887bb | 117 | uint32_t get_page_size() const; |
<> | 160:d5399cc887bb | 118 | |
<> | 160:d5399cc887bb | 119 | private: |
<> | 160:d5399cc887bb | 120 | |
AnnaBridge | 167:e84263d55307 | 121 | /* Check if address and size are aligned to a sector |
<> | 160:d5399cc887bb | 122 | * |
<> | 160:d5399cc887bb | 123 | * @param addr Address of block to check for alignment |
<> | 160:d5399cc887bb | 124 | * @param size Size of block to check for alignment |
<> | 160:d5399cc887bb | 125 | * @return true if the block is sector aligned, false otherwise |
<> | 160:d5399cc887bb | 126 | */ |
<> | 160:d5399cc887bb | 127 | bool is_aligned_to_sector(uint32_t addr, uint32_t size); |
<> | 160:d5399cc887bb | 128 | |
<> | 160:d5399cc887bb | 129 | flash_t _flash; |
<> | 160:d5399cc887bb | 130 | static SingletonPtr<PlatformMutex> _mutex; |
<> | 160:d5399cc887bb | 131 | }; |
<> | 160:d5399cc887bb | 132 | |
<> | 160:d5399cc887bb | 133 | } /* namespace mbed */ |
<> | 160:d5399cc887bb | 134 | |
<> | 160:d5399cc887bb | 135 | #endif /* DEVICE_FLASH */ |
<> | 160:d5399cc887bb | 136 | |
<> | 160:d5399cc887bb | 137 | #endif /* MBED_FLASHIAP_H */ |