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