takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 defined (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 #define FLASHIAP_ROM_END ((uint32_t) &__etext)
00037 #elif defined(TOOLCHAIN_ARM)
00038 extern uint32_t Load$$LR$$LR_IROM1$$Limit[];
00039 #define FLASHIAP_ROM_END ((uint32_t)Load$$LR$$LR_IROM1$$Limit)
00040 #elif defined(TOOLCHAIN_IAR)
00041 #pragma section=".rodata"
00042 #pragma section=".text"
00043 #define FLASHIAP_ROM_END (std::max((uint32_t) __section_end(".rodata"), (uint32_t) __section_end(".text")))
00044 #endif
00045 
00046 namespace mbed {
00047 
00048 /** \addtogroup drivers */
00049 
00050 /** Flash IAP driver. It invokes flash HAL functions.
00051  *
00052  * @note Synchronization level: Thread safe
00053  * @ingroup drivers
00054  */
00055 class FlashIAP : private NonCopyable<FlashIAP> {
00056 public:
00057     FlashIAP();
00058     ~FlashIAP();
00059 
00060     /** Initialize a flash IAP device
00061      *
00062      *  Should be called once per lifetime of the object.
00063      *  @return 0 on success or a negative error code on failure
00064      */
00065     int init();
00066 
00067     /** Deinitialize a flash IAP device
00068      *
00069      *  @return 0 on success or a negative error code on failure
00070      */
00071     int deinit();
00072 
00073     /** Read data from a flash device.
00074      *
00075      *  This method invokes memcpy - reads number of bytes from the address
00076      *
00077      *  @param buffer Buffer to write to
00078      *  @param addr   Flash address to begin reading from
00079      *  @param size   Size to read in bytes
00080      *  @return       0 on success, negative error code on failure
00081      */
00082     int read(void *buffer, uint32_t addr, uint32_t size);
00083 
00084     /** Program data to pages
00085      *
00086      *  The sectors must have been erased prior to being programmed
00087      *
00088      *  @param buffer Buffer of data to be written
00089      *  @param addr   Address of a page to begin writing to
00090      *  @param size   Size to write in bytes, must be a multiple of program size
00091      *  @return       0 on success, negative error code on failure
00092      */
00093     int program(const void *buffer, uint32_t addr, uint32_t size);
00094 
00095     /** Erase sectors
00096      *
00097      *  The state of an erased sector is undefined until it has been programmed
00098      *
00099      *  @param addr Address of a sector to begin erasing, must be a multiple of the sector size
00100      *  @param size Size to erase in bytes, must be a multiple of the sector size
00101      *  @return     0 on success, negative error code on failure
00102      */
00103     int erase(uint32_t addr, uint32_t size);
00104 
00105     /** Get the sector size at the defined address
00106      *
00107      *  Sector size might differ at address ranges.
00108      *  An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
00109      *
00110      *  @param addr Address of or inside the sector to query
00111      *  @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
00112      */
00113     uint32_t get_sector_size(uint32_t addr) const;
00114 
00115     /** Get the flash start address
00116      *
00117      *  @return Flash start address
00118      */
00119     uint32_t get_flash_start() const;
00120 
00121     /** Get the flash size
00122      *
00123      *  @return Flash size
00124      */
00125     uint32_t get_flash_size() const;
00126 
00127     /** Get the program page size
00128      *
00129      *  The page size defines the writable page size
00130      *  @return Size of a program page in bytes
00131      */
00132     uint32_t get_page_size() const;
00133 
00134 private:
00135 
00136     /* Check if address and size are aligned to a sector
00137      *
00138      *  @param addr Address of block to check for alignment
00139      *  @param size Size of block to check for alignment
00140      *  @return true if the block is sector aligned, false otherwise
00141      */
00142     bool is_aligned_to_sector(uint32_t addr, uint32_t size);
00143 
00144     flash_t _flash;
00145     uint8_t *_page_buf;
00146     static SingletonPtr<PlatformMutex>  _mutex;
00147 };
00148 
00149 } /* namespace mbed */
00150 
00151 #endif  /* DEVICE_FLASH */
00152 
00153 #endif  /* MBED_FLASHIAP_H */