Mistake on this page?
Report an issue in GitHub or email us
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 "platform/PlatformMutex.h"
32 #include "platform/NonCopyable.h"
33 #include <algorithm>
34 
35 // Export ROM end address
36 #if defined(TOOLCHAIN_GCC_ARM)
37 extern uint32_t __etext;
38 extern uint32_t __data_start__;
39 extern uint32_t __data_end__;
40 #define FLASHIAP_APP_ROM_END_ADDR (((uint32_t) &__etext) + ((uint32_t) &__data_end__) - ((uint32_t) &__data_start__))
41 #elif defined(TOOLCHAIN_ARM)
42 extern uint32_t Load$$LR$$LR_IROM1$$Limit[];
43 #define FLASHIAP_APP_ROM_END_ADDR ((uint32_t)Load$$LR$$LR_IROM1$$Limit)
44 #elif defined(TOOLCHAIN_IAR)
45 #pragma section=".rodata"
46 #pragma section=".text"
47 #pragma section=".init_array"
48 #define FLASHIAP_APP_ROM_END_ADDR std::max(std::max((uint32_t) __section_end(".rodata"), (uint32_t) __section_end(".text")), \
49  (uint32_t) __section_end(".init_array"))
50 #endif
51 
52 namespace mbed {
53 
54 /** \addtogroup drivers-public-api */
55 /** @{*/
56 
57 /**
58  * \defgroup drivers_FlashIAP FlashIAP class
59  * @{
60  */
61 
62 /** Flash IAP driver. It invokes flash HAL functions.
63  *
64  * @note Synchronization level: Thread safe
65  */
66 class FlashIAP : private NonCopyable<FlashIAP> {
67 public:
68  constexpr FlashIAP() : _flash(), _page_buf(nullptr)
69  {
70 
71  }
72 
73  /** Initialize a flash IAP device
74  *
75  * Should be called once per lifetime of the object.
76  * @return 0 on success or a negative error code on failure
77  */
78  int init();
79 
80  /** Deinitialize a flash IAP device
81  *
82  * @return 0 on success or a negative error code on failure
83  */
84  int deinit();
85 
86  /** Read data from a flash device.
87  *
88  * This method invokes memcpy - reads number of bytes from the address
89  *
90  * @param buffer Buffer to write to
91  * @param addr Flash address to begin reading from
92  * @param size Size to read in bytes
93  * @return 0 on success, negative error code on failure
94  */
95  int read(void *buffer, uint32_t addr, uint32_t size);
96 
97  /** Program data to pages
98  *
99  * The sectors must have been erased prior to being programmed
100  *
101  * @param buffer Buffer of data to be written
102  * @param addr Address of a page to begin writing to
103  * @param size Size to write in bytes, must be a multiple of program size
104  * @return 0 on success, negative error code on failure
105  */
106  int program(const void *buffer, uint32_t addr, uint32_t size);
107 
108  /** Erase sectors
109  *
110  * The state of an erased sector is undefined until it has been programmed
111  *
112  * @param addr Address of a sector to begin erasing, must be a multiple of the sector size
113  * @param size Size to erase in bytes, must be a multiple of the sector size
114  * @return 0 on success, negative error code on failure
115  */
116  int erase(uint32_t addr, uint32_t size);
117 
118  /** Get the sector size at the defined address
119  *
120  * Sector size might differ at address ranges.
121  * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
122  *
123  * @param addr Address of or inside the sector to query
124  * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
125  */
126  uint32_t get_sector_size(uint32_t addr) const;
127 
128  /** Get the flash start address
129  *
130  * @return Flash start address
131  */
132  uint32_t get_flash_start() const;
133 
134  /** Get the flash size
135  *
136  * @return Flash size
137  */
138  uint32_t get_flash_size() const;
139 
140  /** Get the program page size
141  *
142  * The page size defines the writable page size
143  * @return Size of a program page in bytes
144  */
145  uint32_t get_page_size() const;
146 
147  /** Get the flash erase value
148  *
149  * Get the value we read after erase operation
150  * @return flash erase value
151  */
152  uint8_t get_erase_value() const;
153 
154 #if !defined(DOXYGEN_ONLY)
155 private:
156 
157  /* Check if address and size are aligned to a sector
158  *
159  * @param addr Address of block to check for alignment
160  * @param size Size of block to check for alignment
161  * @return true if the block is sector aligned, false otherwise
162  */
163  bool is_aligned_to_sector(uint32_t addr, uint32_t size);
164 
165  flash_t _flash;
166  uint8_t *_page_buf;
167  static SingletonPtr<PlatformMutex> _mutex;
168 #endif
169 };
170 
171 /** @}*/
172 /** @}*/
173 
174 } /* namespace mbed */
175 
176 #endif /* DEVICE_FLASH */
177 
178 #endif /* MBED_FLASHIAP_H */
int read(void *buffer, uint32_t addr, uint32_t size)
Read data from a flash device.
uint32_t get_page_size() const
Get the program page size.
int deinit()
Deinitialize a flash IAP device.
int program(const void *buffer, uint32_t addr, uint32_t size)
Program data to pages.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
uint32_t get_sector_size(uint32_t addr) const
Get the sector size at the defined address.
uint32_t get_flash_size() const
Get the flash size.
Flash IAP driver.
Definition: FlashIAP.h:66
Target flash configuration For targets not supporting TrustZone, its flash_set_target_config must def...
Definition: flash_data.h:58
int init()
Initialize a flash IAP device.
Definition: ATHandler.h:46
uint32_t get_flash_start() const
Get the flash start address.
uint8_t get_erase_value() const
Get the flash erase value.
int erase(uint32_t addr, uint32_t size)
Erase sectors.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.