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