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