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