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 */
53 
54 /** Flash IAP driver. It invokes flash HAL functions.
55  *
56  * @note Synchronization level: Thread safe
57  * @ingroup drivers
58  */
59 class FlashIAP : private NonCopyable<FlashIAP> {
60 public:
61  FlashIAP();
62  ~FlashIAP();
63 
64  /** Initialize a flash IAP device
65  *
66  * Should be called once per lifetime of the object.
67  * @return 0 on success or a negative error code on failure
68  */
69  int init();
70 
71  /** Deinitialize a flash IAP device
72  *
73  * @return 0 on success or a negative error code on failure
74  */
75  int deinit();
76 
77  /** Read data from a flash device.
78  *
79  * This method invokes memcpy - reads number of bytes from the address
80  *
81  * @param buffer Buffer to write to
82  * @param addr Flash address to begin reading from
83  * @param size Size to read in bytes
84  * @return 0 on success, negative error code on failure
85  */
86  int read(void *buffer, uint32_t addr, uint32_t size);
87 
88  /** Program data to pages
89  *
90  * The sectors must have been erased prior to being programmed
91  *
92  * @param buffer Buffer of data to be written
93  * @param addr Address of a page to begin writing to
94  * @param size Size to write in bytes, must be a multiple of program size
95  * @return 0 on success, negative error code on failure
96  */
97  int program(const void *buffer, uint32_t addr, uint32_t size);
98 
99  /** Erase sectors
100  *
101  * The state of an erased sector is undefined until it has been programmed
102  *
103  * @param addr Address of a sector to begin erasing, must be a multiple of the sector size
104  * @param size Size to erase in bytes, must be a multiple of the sector size
105  * @return 0 on success, negative error code on failure
106  */
107  int erase(uint32_t addr, uint32_t size);
108 
109  /** Get the sector size at the defined address
110  *
111  * Sector size might differ at address ranges.
112  * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
113  *
114  * @param addr Address of or inside the sector to query
115  * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
116  */
117  uint32_t get_sector_size(uint32_t addr) const;
118 
119  /** Get the flash start address
120  *
121  * @return Flash start address
122  */
123  uint32_t get_flash_start() const;
124 
125  /** Get the flash size
126  *
127  * @return Flash size
128  */
129  uint32_t get_flash_size() const;
130 
131  /** Get the program page size
132  *
133  * The page size defines the writable page size
134  * @return Size of a program page in bytes
135  */
136  uint32_t get_page_size() const;
137 
138  /** Get the flash erase value
139  *
140  * Get the value we read after erase operation
141  * @return flash erase value
142  */
143  uint8_t get_erase_value() const;
144 
145 #if !defined(DOXYGEN_ONLY)
146 private:
147 
148  /* Check if address and size are aligned to a sector
149  *
150  * @param addr Address of block to check for alignment
151  * @param size Size of block to check for alignment
152  * @return true if the block is sector aligned, false otherwise
153  */
154  bool is_aligned_to_sector(uint32_t addr, uint32_t size);
155 
156  flash_t _flash;
157  uint8_t *_page_buf;
158  static SingletonPtr<PlatformMutex> _mutex;
159 #endif
160 };
161 
162 } /* namespace mbed */
163 
164 #endif /* DEVICE_FLASH */
165 
166 #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:168
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:59
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: AnalogIn.h:28
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.