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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
//*****************************************************************************
//
// am_hal_flash.h
//! @file
//!
//! @brief Functions for performing Flash operations.
//!
//! @addtogroup flash2 Flash
//! @ingroup apollo2hal
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// Copyright (c) 2017, Ambiq Micro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// This is part of revision 1.2.11 of the AmbiqSuite Development Package.
//
//*****************************************************************************
#ifndef AM_HAL_FLASH_H
#define AM_HAL_FLASH_H
#include <stdint.h>
#include <stdbool.h>
//*****************************************************************************
//
// Flash Program keys.
//
//*****************************************************************************
#define AM_HAL_FLASH_PROGRAM_KEY 0x12344321
#define AM_HAL_FLASH_RECOVERY_KEY 0xA35C9B6D
#define AM_HAL_FLASH_INFO_KEY 0x12344321
#define AM_HAL_FLASH_OTP_KEY (AM_HAL_FLASH_INFO_KEY)
//*****************************************************************************
//
// Some helpful flash values and macros.
//
//*****************************************************************************
#define AM_HAL_FLASH_ADDR 0x00000000
#define AM_HAL_FLASH_PAGE_SIZE ( 8 * 1024 )
#define AM_HAL_FLASH_INFO_SIZE AM_HAL_FLASH_PAGE_SIZE
#define AM_HAL_FLASH_INSTANCE_SIZE ( 512 * 1024 )
#define AM_HAL_FLASH_INSTANCE_PAGES ( AM_HAL_FLASH_INSTANCE_SIZE / AM_HAL_FLASH_PAGE_SIZE )
#define AM_HAL_FLASH_TOTAL_SIZE ( AM_HAL_FLASH_INSTANCE_SIZE * 2 )
#define AM_HAL_FLASH_LARGEST_VALID_ADDR ( AM_HAL_FLASH_ADDR + AM_HAL_FLASH_TOTAL_SIZE - 1 )
//
// Convert an absolute flash address to a instance
//
#define AM_HAL_FLASH_ADDR2INST(addr) ( ( addr >> 19 ) & 1 )
//
// Convert an absolute flash address to a page number relative to the instance
//
#define AM_HAL_FLASH_ADDR2PAGE(addr) ( ( addr >> 13 ) & 0x3F )
//
// Convert an absolute flash address to an absolute page number
//
#define AM_HAL_FLASH_ADDR2ABSPAGE(addr) ( addr >> 13 )
//*****************************************************************************
//
// Given an integer number of microseconds, convert to a value representing the
// number of am_hal_flash_delay() cycles that will provide that amount of delay.
// This macro is designed to take into account some of the call overhead.
//
// e.g. To provide a 2us delay:
// am_hal_flash_delay( FLASH_CYCLES_US(2) );
//
// IMPORTANT - Apollo2 is spec'ed for only 48MHz operation, so this macro
// assumes that.
//
//*****************************************************************************
#define FLASH_CYCLES_US(n) ((n * (AM_HAL_CLKGEN_FREQ_MAX_MHZ / 3)) - 4)
//
// Backward compatibility
//
#define am_hal_flash_program_otp am_hal_flash_program_info
#define am_hal_flash_program_otp_sram am_hal_flash_program_info_sram
//*****************************************************************************
//
// Structure of function pointers to helper functions for invoking various
// flash operations. The functions we are pointing to here are in the Apollo 2
// integrated BOOTROM.
//
//*****************************************************************************
typedef struct am_hal_flash_helper_struct
{
//
// The basics.
//
int (*flash_mass_erase)(uint32_t, uint32_t);
int (*flash_page_erase)(uint32_t, uint32_t, uint32_t);
int (*flash_program_main)(uint32_t, uint32_t *,
uint32_t*, uint32_t);
int (*flash_program_info)(uint32_t, uint32_t,
uint32_t*, uint32_t, uint32_t);
//
// Non-blocking variants, but be careful these are not interrupt safe so
// mask interrupts while these very long operations proceed.
//
int (*flash_mass_erase_nb)(uint32_t, uint32_t);
int (*flash_page_erase_nb)(uint32_t, uint32_t, uint32_t);
bool (*flash_nb_operation_complete)(void);
//
// Essentially these are recovery options.
//
int (*flash_erase_info)(uint32_t, uint32_t);
int (*flash_erase_main_plus_info)(uint32_t, uint32_t);
int (*flash_erase_main_plus_info_both_instances)(uint32_t);
void (*flash_recovery)(uint32_t);
//
// Useful utilities.
//
uint32_t (*flash_util_read_word)(uint32_t*);
void (*flash_util_write_word)(uint32_t*, uint32_t);
void (*delay_cycles)(uint32_t);
//
// The following functions pointers will generally never be called from
// user programs. They are here primarily to document these entry points
// which are usable from a debugger or debugger script.
//
void (*flash_program_main_sram)(void);
void (*flash_program_info_sram)(void);
void (*flash_erase_main_pages_sram)(void);
void (*flash_mass_erase_sram)(void);
void (*flash_erase_info_sram)(void);
void (*flash_erase_main_plus_info_sram)(void);
} g_am_hal_flash_t;
extern g_am_hal_flash_t g_am_hal_flash;
//*****************************************************************************
//
// Define some FLASH INFO SPACE values and macros.
//
//*****************************************************************************
#define AM_HAL_FLASH_INFO_ADDR 0x50020000
#define AM_HAL_FLASH_INFO_SECURITY_O 0x10
#define AM_HAL_FLASH_INFO_WRITPROT_O 0x20
#define AM_HAL_FLASH_INFO_COPYPROT_O 0x30
#define AM_HAL_FLASH_INFO_SECURITY_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_SECURITY_O)
#define AM_HAL_FLASH_INFO_WRITPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_WRITPROT_O)
#define AM_HAL_FLASH_INFO_COPYPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_COPYPROT_O)
//
// Define the customer info signature data (at AM_HAL_FLASH_INFO_ADDR).
// These bits must exist in the customer info space in order for many of the
// security and protection functions to work.
//
#define AM_HAL_FLASH_INFO_SIGNATURE0 0x48EAAD88
#define AM_HAL_FLASH_INFO_SIGNATURE1 0xC9705737
#define AM_HAL_FLASH_INFO_SIGNATURE2 0x0A6B8458
#define AM_HAL_FLASH_INFO_SIGNATURE3 0xE41A9D74
//
// Define the customer security bits (at AM_HAL_FLASH_INFO_SECURITY_ADDR)
//
#define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S 0
#define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S 1
#define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S 2
#define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S 3
#define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S 4
#define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S 8
#define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S 9
#define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S))
#define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S))
#define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S))
#define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S))
#define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M ((uint32_t)(0xF << AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S))
#define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S))
#define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
#define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
#define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP ((uint32_t)(0x0 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
//
// Protection chunk macros
// AM_HAL_FLASH_INFO_CHUNK2ADDR: Convert a chunk number to an address
// AM_HAL_FLASH_INFO_CHUNK2INST: Convert a chunk number to an instance number
// AM_HAL_FLASH_INFO_ADDR2CHUNK: Convert an address to a chunk number
//
#define AM_HAL_FLASH_INFO_CHUNKSIZE (16*1024)
#define AM_HAL_FLASH_INFO_CHUNK2ADDR(n) (AM_HAL_FLASH_ADDR + (n << 14))
#define AM_HAL_FLASH_INFO_CHUNK2INST(n) ((n >> 5) & 1
#define AM_HAL_FLASH_INFO_ADDR2CHUNK(n) ((n) >> 14)
#ifdef __cplusplus
extern "C"
{
#endif
//*****************************************************************************
//
// Function prototypes for the helper functions
//
//*****************************************************************************
extern int am_hal_flash_mass_erase(uint32_t ui32Value, uint32_t ui32FlashInst);
extern int am_hal_flash_page_erase(uint32_t ui32Value, uint32_t ui32FlashInst,
uint32_t ui32PageNum);
extern int am_hal_flash_program_main(uint32_t value, uint32_t *pSrc,
uint32_t *pDst, uint32_t NumberOfWords);
extern int am_hal_flash_program_info(uint32_t ui32Value, uint32_t ui32InfoInst,
uint32_t *pui32Src, uint32_t ui32Offset,
uint32_t ui32NumWords);
//
// Recovery type functions for Customer INFO space.
//
extern int am_hal_flash_erase_info(uint32_t ui32ProgramKey,
uint32_t ui32Instance);
extern int am_hal_flash_erase_main_plus_info(uint32_t ui32ProgramKey,
uint32_t ui32Instance);
extern int am_hal_flash_erase_main_plus_info_both_instances(
uint32_t ui32ProgramKey);
extern void am_hal_flash_recovery(uint32_t ui32RecoveryKey);
//
// BOOTROM resident reader, writer and delay utility functions.
//
extern uint32_t am_hal_flash_load_ui32(uint32_t ui32Address);
extern void am_hal_flash_store_ui32(uint32_t ui32Address, uint32_t ui32Data);
extern void am_hal_flash_delay(uint32_t ui32Iterations);
extern uint32_t am_hal_flash_delay_status_change(uint32_t ui32Iterations,
uint32_t ui32Address,
uint32_t ui32Mask,
uint32_t ui32Value);
//
// These functions update security/protection bits in the customer INFO blOCK.
//
extern bool am_hal_flash_customer_info_signature_check(void);
extern bool am_hal_flash_info_signature_set(void);
extern int32_t am_hal_flash_info_erase_disable(void);
extern bool am_hal_flash_info_erase_disable_check(void);
extern int32_t am_hal_flash_info_program_disable(uint32_t ui32Mask);
extern uint32_t am_hal_flash_info_program_disable_get(void);
extern int32_t am_hal_flash_wipe_flash_enable(void);
extern bool am_hal_flash_wipe_flash_enable_check(void);
extern int32_t am_hal_flash_wipe_sram_enable(void);
extern bool am_hal_flash_wipe_sram_enable_check(void);
extern int32_t am_hal_flash_swo_disable(void);
extern bool am_hal_flash_swo_disable_check(void);
extern int32_t am_hal_flash_debugger_disable(void);
extern bool am_hal_flash_debugger_disable_check(void);
extern int32_t am_hal_flash_copy_protect_set(uint32_t *pui32StartAddress,
uint32_t *pui32StopAddress);
extern bool am_hal_flash_copy_protect_check(uint32_t *pui32StartAddress,
uint32_t *pui32StopAddress);
extern int32_t am_hal_flash_write_protect_set(uint32_t *pui32StartAddress,
uint32_t *pui32StopAddress);
extern bool am_hal_flash_write_protect_check(uint32_t *pui32StartAddress,
uint32_t *pui32StopAddress);
#ifdef __cplusplus
}
#endif
#endif // AM_HAL_FLASH_H
//*****************************************************************************
//
// End Doxygen group.
//! @}
//
//*****************************************************************************
|