Mbed OS Reference
Loading...
Searching...
No Matches
FlashIAP.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2017 ARM Limited
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef MBED_FLASHIAP_H
25#define MBED_FLASHIAP_H
26
27#if DEVICE_FLASH || defined(DOXYGEN_ONLY)
28
29#include "flash_api.h"
30#include "platform/SingletonPtr.h"
31#include "rtos/Mutex.h"
32#include "platform/NonCopyable.h"
33#include <algorithm>
34
35// Export ROM end address, if not defined by HAL layer
36#ifndef FLASHIAP_APP_ROM_END_ADDR
37#if defined(TOOLCHAIN_GCC_ARM)
38extern uint32_t __etext;
39extern uint32_t __data_start__;
40extern uint32_t __data_end__;
41#define FLASHIAP_APP_ROM_END_ADDR (((uint32_t) &__etext) + ((uint32_t) &__data_end__) - ((uint32_t) &__data_start__))
42#elif defined(TOOLCHAIN_ARM)
43extern uint32_t Load$$LR$$LR_IROM1$$Limit[];
44#define FLASHIAP_APP_ROM_END_ADDR ((uint32_t)Load$$LR$$LR_IROM1$$Limit)
45#elif defined(TOOLCHAIN_IAR)
46#pragma section=".rodata"
47#pragma section=".text"
48#pragma section=".init_array"
49#define FLASHIAP_APP_ROM_END_ADDR std::max(std::max((uint32_t) __section_end(".rodata"), (uint32_t) __section_end(".text")), \
50 (uint32_t) __section_end(".init_array"))
51#endif
52#endif
53
54namespace mbed {
55
56/** \addtogroup drivers-public-api */
57/** @{*/
58
59/**
60 * \defgroup drivers_FlashIAP FlashIAP class
61 * @{
62 */
63
64/** Flash IAP driver. It invokes flash HAL functions.
65 *
66 * @note Synchronization level: Thread safe
67 */
68class FlashIAP : private NonCopyable<FlashIAP> {
69public:
70 constexpr FlashIAP() : _flash(), _page_buf(nullptr)
71 {
72
73 }
74
75 /** Initialize a flash IAP device
76 *
77 * Should be called once per lifetime of the object.
78 * @return 0 on success or a negative error code on failure
79 */
80 int init();
81
82 /** Deinitialize a flash IAP device
83 *
84 * @return 0 on success or a negative error code on failure
85 */
86 int deinit();
87
88 /** Read data from a flash device.
89 *
90 * This method invokes memcpy - reads number of bytes from the address
91 *
92 * @param buffer Buffer to write to
93 * @param addr Flash address to begin reading from
94 * @param size Size to read in bytes
95 * @return 0 on success, negative error code on failure
96 */
97 int read(void *buffer, uint32_t addr, uint32_t size);
98
99 /** Program data to pages
100 *
101 * The sectors must have been erased prior to being programmed
102 *
103 * @param buffer Buffer of data to be written
104 * @param addr Address of a page to begin writing to
105 * @param size Size to write in bytes, must be a multiple of program size
106 * @return 0 on success, negative error code on failure
107 */
108 int program(const void *buffer, uint32_t addr, uint32_t size);
109
110 /** Erase sectors
111 *
112 * The state of an erased sector is undefined until it has been programmed
113 *
114 * @param addr Address of a sector to begin erasing, must be a multiple of the sector size
115 * @param size Size to erase in bytes, must be a multiple of the sector size
116 * @return 0 on success, negative error code on failure
117 */
118 int erase(uint32_t addr, uint32_t size);
119
120 /** Get the sector size at the defined address
121 *
122 * Sector size might differ at address ranges.
123 * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
124 *
125 * @param addr Address of or inside the sector to query
126 * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
127 */
128 uint32_t get_sector_size(uint32_t addr) const;
129
130 /** Get the flash start address
131 *
132 * \note This is the start address of the entire flash region, not
133 * the first address after the end of the program in flash.
134 *
135 * @return Flash start address
136 */
137 uint32_t get_flash_start() const;
138
139 /** Get the flash size
140 *
141 * @return Flash size
142 */
143 uint32_t get_flash_size() const;
144
145 /** Get the program page size
146 *
147 * The page size defines the writable page size
148 * @return Size of a program page in bytes
149 */
150 uint32_t get_page_size() const;
151
152 /** Get the flash erase value
153 *
154 * Get the value we read after erase operation
155 * @return flash erase value
156 */
157 uint8_t get_erase_value() const;
158
159#if !defined(DOXYGEN_ONLY)
160private:
161
162 /* Check if address and size are aligned to a sector
163 *
164 * @param addr Address of block to check for alignment
165 * @param size Size of block to check for alignment
166 * @return true if the block is sector aligned, false otherwise
167 */
168 bool is_aligned_to_sector(uint32_t addr, uint32_t size);
169
170 flash_t _flash;
171 uint8_t *_page_buf;
172 static SingletonPtr<rtos::Mutex> _mutex;
173#endif
174};
175
176/** @}*/
177/** @}*/
178
179} /* namespace mbed */
180
181#endif /* DEVICE_FLASH */
182
183#endif /* MBED_FLASHIAP_H */
Flash IAP driver.
Definition: FlashIAP.h:68
int init()
Initialize a flash IAP device.
uint32_t get_flash_size() const
Get the flash size.
uint32_t get_flash_start() const
Get the flash start address.
uint32_t get_sector_size(uint32_t addr) const
Get the sector size at the defined address.
uint32_t get_page_size() const
Get the program page size.
int deinit()
Deinitialize a flash IAP device.
int read(void *buffer, uint32_t addr, uint32_t size)
Read data from a flash device.
int program(const void *buffer, uint32_t addr, uint32_t size)
Program data to pages.
uint8_t get_erase_value() const
Get the flash erase value.
int erase(uint32_t addr, uint32_t size)
Erase sectors.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
Utility class for creating and using a singleton.
Definition: SingletonPtr.h:88
Target flash configuration For targets not supporting TrustZone, its flash_set_target_config must def...
Definition: flash_data.h:58