Rtos API example

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