Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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