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