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