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