Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FlashIAP.h Source File

FlashIAP.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_FLASHIAP_H
00023 #define MBED_FLASHIAP_H
00024 
00025 #if DEVICE_FLASH || defined(DOXYGEN_ONLY)
00026 
00027 #include "flash_api.h"
00028 #include "platform/SingletonPtr.h"
00029 #include "platform/PlatformMutex.h"
00030 #include "platform/NonCopyable.h"
00031 #include <algorithm>
00032 
00033 // Export ROM end address
00034 #if defined(TOOLCHAIN_GCC_ARM)
00035 extern uint32_t __etext;
00036 extern uint32_t __data_start__;
00037 extern uint32_t __data_end__;
00038 #define FLASHIAP_APP_ROM_END_ADDR (((uint32_t) &__etext) + ((uint32_t) &__data_end__) - ((uint32_t) &__data_start__))
00039 #elif defined(TOOLCHAIN_ARM)
00040 extern uint32_t Load$$LR$$LR_IROM1$$Limit[];
00041 #define FLASHIAP_APP_ROM_END_ADDR ((uint32_t)Load$$LR$$LR_IROM1$$Limit)
00042 #elif defined(TOOLCHAIN_IAR)
00043 #pragma section=".rodata"
00044 #pragma section=".text"
00045 #pragma section=".init_array"
00046 #define FLASHIAP_APP_ROM_END_ADDR std::max(std::max((uint32_t) __section_end(".rodata"), (uint32_t) __section_end(".text")), \
00047                                   (uint32_t) __section_end(".init_array"))
00048 #endif
00049 
00050 namespace mbed {
00051 
00052 /** \addtogroup drivers-public-api */
00053 /** @{*/
00054 
00055 /**
00056  * \defgroup drivers_FlashIAP FlashIAP class
00057  * @{
00058  */
00059 
00060 /** Flash IAP driver. It invokes flash HAL functions.
00061  *
00062  * @note Synchronization level: Thread safe
00063  */
00064 class FlashIAP : private NonCopyable<FlashIAP> {
00065 public:
00066     constexpr FlashIAP() : _flash(), _page_buf(nullptr)
00067     {
00068 
00069     }
00070 
00071     /** Initialize a flash IAP device
00072      *
00073      *  Should be called once per lifetime of the object.
00074      *  @return 0 on success or a negative error code on failure
00075      */
00076     int init();
00077 
00078     /** Deinitialize a flash IAP device
00079      *
00080      *  @return 0 on success or a negative error code on failure
00081      */
00082     int deinit();
00083 
00084     /** Read data from a flash device.
00085      *
00086      *  This method invokes memcpy - reads number of bytes from the address
00087      *
00088      *  @param buffer Buffer to write to
00089      *  @param addr   Flash address to begin reading from
00090      *  @param size   Size to read in bytes
00091      *  @return       0 on success, negative error code on failure
00092      */
00093     int read(void *buffer, uint32_t addr, uint32_t size);
00094 
00095     /** Program data to pages
00096      *
00097      *  The sectors must have been erased prior to being programmed
00098      *
00099      *  @param buffer Buffer of data to be written
00100      *  @param addr   Address of a page to begin writing to
00101      *  @param size   Size to write in bytes, must be a multiple of program size
00102      *  @return       0 on success, negative error code on failure
00103      */
00104     int program(const void *buffer, uint32_t addr, uint32_t size);
00105 
00106     /** Erase sectors
00107      *
00108      *  The state of an erased sector is undefined until it has been programmed
00109      *
00110      *  @param addr Address of a sector to begin erasing, must be a multiple of the sector size
00111      *  @param size Size to erase in bytes, must be a multiple of the sector size
00112      *  @return     0 on success, negative error code on failure
00113      */
00114     int erase(uint32_t addr, uint32_t size);
00115 
00116     /** Get the sector size at the defined address
00117      *
00118      *  Sector size might differ at address ranges.
00119      *  An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
00120      *
00121      *  @param addr Address of or inside the sector to query
00122      *  @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
00123      */
00124     uint32_t get_sector_size(uint32_t addr) const;
00125 
00126     /** Get the flash start address
00127      *
00128      *  @return Flash start address
00129      */
00130     uint32_t get_flash_start() const;
00131 
00132     /** Get the flash size
00133      *
00134      *  @return Flash size
00135      */
00136     uint32_t get_flash_size() const;
00137 
00138     /** Get the program page size
00139      *
00140      *  The page size defines the writable page size
00141      *  @return Size of a program page in bytes
00142      */
00143     uint32_t get_page_size() const;
00144 
00145     /** Get the flash erase value
00146      *
00147      *  Get the value we read after erase operation
00148      *  @return flash erase value
00149      */
00150     uint8_t get_erase_value() const;
00151 
00152 #if !defined(DOXYGEN_ONLY)
00153 private:
00154 
00155     /* Check if address and size are aligned to a sector
00156      *
00157      *  @param addr Address of block to check for alignment
00158      *  @param size Size of block to check for alignment
00159      *  @return true if the block is sector aligned, false otherwise
00160      */
00161     bool is_aligned_to_sector(uint32_t addr, uint32_t size);
00162 
00163     flash_t _flash;
00164     uint8_t *_page_buf;
00165     static SingletonPtr<PlatformMutex>  _mutex;
00166 #endif
00167 };
00168 
00169 /** @}*/
00170 /** @}*/
00171 
00172 } /* namespace mbed */
00173 
00174 #endif  /* DEVICE_FLASH */
00175 
00176 #endif  /* MBED_FLASHIAP_H */