-
Notifications
You must be signed in to change notification settings - Fork 31
/
flash.h
284 lines (254 loc) · 6.95 KB
/
flash.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
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
// Copyright 2020-2021 Beken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <common/bk_include.h>
#include <driver/flash_types.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Init the flash driver
*
* This API init the resoure common:
* - Init flash driver control memory
*
* @attention 1. This API should be called before any other flash APIs.
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_driver_init(void);
/**
* @brief Deinit the flash driver
*
* This API free all resource related to flash and disable flash.
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_driver_deinit(void);
/**
* @brief Set flash line mode
*
* @param line_mode flash line mode
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_set_line_mode(flash_line_mode_t line_mode);
/**
* @brief Get flash line mode
*
* @return flash line mode
*/
flash_line_mode_t bk_flash_get_line_mode(void);
/**
* @brief Get flash id
*
* @return flash line mode
*/
uint32_t bk_flash_get_id(void);
/**
* @brief Set flash clock dpll
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_set_clk_dpll(void);
/**
* @brief Set flash clock dco
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_set_clk_dco(void);
/**
* @brief Enable flash write
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_write_enable(void);
/**
* @brief Disable flash write
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_write_disable(void);
/**
* @brief Set flash protect type
*
* @param type flash protect type
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_set_protect_type(flash_protect_type_t type);
/**
* @brief Get flash protect type
*
* @return the flash protect type
*/
flash_protect_type_t bk_flash_get_protect_type(void);
/**
* @brief Get flash status register value
*
* @return the flash status register value
*/
uint16_t bk_flash_read_status_reg(void);
/**
* @brief Set flash status register value
*
* @param status_reg_data the flash status register data
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_write_status_reg(uint16_t status_reg_data);
/**
* @brief Erase a sector of flash, use flash command 0x20
*
* @param address flash address
*
* @return
* - BK_OK: succeed
* - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
* - others: other errors.
*/
__attribute__((section(".itcm_sec_code"))) bk_err_t bk_flash_erase_sector(uint32_t address);
/**
* @brief Write data to flash
*
* @param address address to write
* @param user_buf the pointer to data which is to write
* @param size size to write
*
* @return
* - BK_OK: succeed
* - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
* - others: other errors.
*/
bk_err_t bk_flash_write_bytes(uint32_t address, const uint8_t *user_buf, uint32_t size);
/**
* @brief Read data from flash
*
* @param address address to read
* @param user_buf the buffer to read the data
* @param size size to read
*
* @return
* - BK_OK: succeed
* - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
* - others: other errors.
*/
bk_err_t bk_flash_read_bytes(uint32_t address, uint8_t *user_buf, uint32_t size);
/**
* @brief Read data from flas
*
* @param address address to read
* @param user_buf the buffer to read the data, intend to psram addr must aline by 4 Bytes
* @param size size to read, length uint by byte
*
* @return
* - BK_OK: succeed
* - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
* - others: other errors.
*/
bk_err_t bk_flash_read_word(uint32_t address, uint32_t *user_buf, uint32_t size);
/**
* @brief Get flash init flag
*
* @return the flash init flag
*/
bool bk_flash_is_driver_inited(void);
/**
* @brief Get flash total size
*
* @return the flash total size
*/
uint32_t bk_flash_get_current_total_size(void);
/**
* @brief Register flash power save suspend callback
*
* @param ps_suspend_cb power save suspend callback
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_register_ps_suspend_callback(flash_ps_callback_t ps_suspend_cb);
/**
* @brief Register flash power save resume callback
*
* @param ps_resume_cb power save resume callback
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_register_ps_resume_callback(flash_ps_callback_t ps_resume_cb);
/**
* @brief Modify flash speed type
*
* @param flash_speed_type the high/low type of flash clock
* @param modules the app module to control the flash spped
*
* @return
* - BK_OK: succeed
* - others: other errors.
*/
bk_err_t bk_flash_clk_switch(uint32_t flash_speed_type, uint32_t modules);
/**
* @brief register a callback to be called when flash is busy waiting.
* WARNING/NOTES:
* 1. the wait_cb code is better in ITCM to avoid running in flash.
* or the wait_cb will be blocked until flash is idle.
* 2.The wait_cb will be called forever until flash is idle, so the implemetion of
* wait_cb should garante the programm is safe.
* @param wait_cb:If flash is writing/erasing, it will block all of other applications.
* But maybe the application can't be blocked when flash is writing/erasing.
* So the application should register this wait_cb to flash.
* When flash is writing/erasing, it will call this wait_cb
*
* @return
* - BK_OK: succeed
* - others: registered too many(>4) wait_cb to flash.
*/
bk_err_t bk_flash_register_wait_cb(flash_wait_callback_t wait_cb);
/**
* @brief unregister the wait_cb from flash waiting.
*
* @param wait_cb:If flash is writing/erasing, it will block all of other applications.
* But maybe the application can't be blocked when flash is writing/erasing.
* So the application should register this wait_cb to flash.
* When flash is writing/erasing, it will call this wait_cb
*
* @return
* - BK_OK: succeed
* - others: The wait_cb isn't registered to flash.
*/
bk_err_t bk_flash_unregister_wait_cb(flash_wait_callback_t wait_cb);
#ifdef __cplusplus
}
#endif