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