• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <common/bk_include.h>
18 #include <driver/flash_types.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * @brief     Init the flash driver
26  *
27  * This API init the resoure common:
28  *   - Init flash driver control memory
29  *
30  * @attention 1. This API should be called before any other flash APIs.
31  *
32  * @return
33  *    - BK_OK: succeed
34  *    - others: other errors.
35  */
36 bk_err_t bk_flash_driver_init(void);
37 
38 /**
39  * @brief     Deinit the flash driver
40  *
41  * This API free all resource related to flash and disable flash.
42  *
43  * @return
44  *    - BK_OK: succeed
45  *    - others: other errors.
46  */
47 bk_err_t bk_flash_driver_deinit(void);
48 
49 /**
50  * @brief     Set flash line mode
51  *
52  * @param line_mode flash line mode
53  *
54  * @return
55  *    - BK_OK: succeed
56  *    - others: other errors.
57  */
58 bk_err_t bk_flash_set_line_mode(flash_line_mode_t line_mode);
59 
60 /**
61  * @brief     Get flash line mode
62  *
63  * @return flash line mode
64  */
65 flash_line_mode_t bk_flash_get_line_mode(void);
66 
67 /**
68  * @brief     Get flash id
69  *
70  * @return flash line mode
71  */
72 uint32_t bk_flash_get_id(void);
73 
74 /**
75  * @brief     Set flash clock dpll
76  *
77  * @return
78  *    - BK_OK: succeed
79  *    - others: other errors.
80  */
81 bk_err_t bk_flash_set_clk_dpll(void);
82 
83 /**
84  * @brief     Set flash clock dco
85  *
86  * @return
87  *    - BK_OK: succeed
88  *    - others: other errors.
89  */
90 bk_err_t bk_flash_set_clk_dco(void);
91 
92 /**
93  * @brief     Enable flash write
94  *
95  * @return
96  *    - BK_OK: succeed
97  *    - others: other errors.
98  */
99 bk_err_t bk_flash_write_enable(void);
100 
101 /**
102  * @brief     Disable flash write
103  *
104  * @return
105  *    - BK_OK: succeed
106  *    - others: other errors.
107  */
108 bk_err_t bk_flash_write_disable(void);
109 
110 /**
111  * @brief     Set flash protect type
112  *
113  * @param type flash protect type
114  *
115  * @return
116  *    - BK_OK: succeed
117  *    - others: other errors.
118  */
119 bk_err_t bk_flash_set_protect_type(flash_protect_type_t type);
120 
121 /**
122  * @brief     Get flash protect type
123  *
124  * @return the flash protect type
125  */
126 flash_protect_type_t bk_flash_get_protect_type(void);
127 
128 /**
129  * @brief     Get flash status register value
130  *
131  * @return the flash status register value
132  */
133 uint16_t bk_flash_read_status_reg(void);
134 
135 /**
136  * @brief     Set flash status register value
137  *
138  * @param status_reg_data the flash status register data
139  *
140  * @return
141  *    - BK_OK: succeed
142  *    - others: other errors.
143  */
144 bk_err_t bk_flash_write_status_reg(uint16_t status_reg_data);
145 
146 /**
147  * @brief     Erase a sector of flash, use flash command 0x20
148  *
149  * @param address flash address
150  *
151  * @return
152  *    - BK_OK: succeed
153  *    - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
154  *    - others: other errors.
155  */
156 bk_err_t bk_flash_erase_sector(uint32_t address);
157 
158 /**
159  * @brief     Write data to flash
160  *
161  * @param address address to write
162  * @param user_buf the pointer to data which is to write
163  * @param size size to write
164  *
165  * @return
166  *    - BK_OK: succeed
167  *    - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
168  *    - others: other errors.
169  */
170 bk_err_t bk_flash_write_bytes(uint32_t address, const uint8_t *user_buf, uint32_t size);
171 
172 /**
173  * @brief     Read data from flash
174  *
175  * @param address address to read
176  * @param user_buf the buffer to read the data
177  * @param size size to read
178  *
179  * @return
180  *    - BK_OK: succeed
181  *    - BK_ERR_FLASH_ADDR_OUT_OF_RANGE: flash address is out of range
182  *    - others: other errors.
183  */
184 bk_err_t bk_flash_read_bytes(uint32_t address, uint8_t *user_buf, uint32_t size);
185 
186 /**
187  * @brief   Get flash  init  flag
188  *
189  * @return the flash  init  flag
190  */
191 bool bk_flash_is_driver_inited(void);
192 
193 /**
194  * @brief     Get flash total size
195  *
196  * @return the flash total size
197  */
198 uint32_t bk_flash_get_current_total_size(void);
199 
200 /**
201  * @brief     Register flash power save suspend callback
202  *
203  * @param ps_suspend_cb power save suspend callback
204  *
205  * @return
206  *    - BK_OK: succeed
207  *    - others: other errors.
208  */
209 bk_err_t bk_flash_register_ps_suspend_callback(flash_ps_callback_t ps_suspend_cb);
210 
211 /**
212  * @brief     Register flash power save resume callback
213  *
214  * @param ps_resume_cb power save resume callback
215  *
216  * @return
217  *    - BK_OK: succeed
218  *    - others: other errors.
219  */
220 bk_err_t bk_flash_register_ps_resume_callback(flash_ps_callback_t ps_resume_cb);
221 
222 /**
223  * @brief  Modify flash speed type
224  *
225  * @param flash_speed_type the high/low type of flash clock
226  * @param modules the app module to control the flash spped
227  *
228  * @return
229  *    - BK_OK: succeed
230  *    - others: other errors.
231  */
232 
233 bk_err_t bk_flash_clk_switch(uint32_t flash_speed_type, uint32_t modules);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 
240