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