• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #pragma once
19 
20 #include "compiler.h"
21 #include "mspi.h"
22 
23 #define PAGE_SIZE 256
24 
25 /**
26  * @brief     flash command definition
27  */
28 typedef enum {
29     FLASH_WRITE_STATUS_CMD = 0x01,
30     FLASH_WRITE_CMD = 0x02,
31     FLASH_READ_CMD = 0x03,
32 
33     FLASH_WRITE_DISABLE_CMD = 0x04,
34     FLASH_READ_STATUS_CMD = 0x05,
35     FLASH_WRITE_ENABLE_CMD = 0x06,
36 
37     FLASH_CHIP_ERASE_CMD = 0x60,  // or 0xc7
38 
39     FLASH_PES_CMD = 0x75,
40     FLASH_PER_CMD = 0x7A,
41     FLASH_QUAD_PAGE_PROGRAM_CMD = 0x32,
42     FLASH_READ_DEVICE_ID_CMD = 0x90,
43 
44     FLASH_FAST_READ_CMD = 0x0B,
45     FLASH_X2READ_CMD = 0xBB,
46     FLASH_DREAD_CMD = 0x3B,
47     FLASH_X4READ_CMD = 0xEB,
48     FLASH_QREAD_CMD = 0x6B,
49 
50     FLASH_SECT_ERASE_CMD = 0x20,  // sector size = 4KBytes
51     FLASH_32KBLK_ERASE_CMD = 0x52,
52     FLASH_64KBLK_ERASE_CMD = 0xD8,
53     FLASH_GD_PUYA_READ_UID_CMD = 0x4B,  // Flash Type = GD/PUYA
54     FLASH_XTX_READ_UID_CMD = 0x5A,      // Flash Type = XTX
55     FLASH_PAGE_ERASE_CMD = 0x81,        // caution: only P25Q40L support this function
56 
57     FLASH_POWER_DOWN = 0xB9,
58     FLASH_POWER_DOWN_RELEASE = 0xAB,
59     FLASH_GET_JEDEC_ID = 0x9F,
60     FLASH_READ_STATUS_1_CMD = 0x35,
61 
62     FLASH_VOLATILE_SR_WRITE_CMD = 0x50,
63     FLASH_SET_BURST_WITH_WRAP_CMD = 0x77,
64     FLASH_ENABLE_SO_TO_OUTPUT_CMD = 0x70,
65     FLASH_READ_DEVICE_ID_DUAL_CME = 0x92,
66     RLASH_READ_DEVICE_ID_QUAD_CMD = 0x94,
67     FLASH_ERASE_SECURITY_REGISTERS_CMD = 0x44,
68     FLASH_PROGRAM_SECURITY_REGISTERS_CMD = 0x42,
69     FLASH_READ_SECURITY_REGISTERS_CMD = 0x48,
70     FLASH_ENABLE_RESET_CMD = 0x99,
71 
72     FLASH_ENABLE_RESET = 0x66,
73     FLASH_DISABLE_SO_TO_OUTPUT = 0x80,
74 } flash_command_e;
75 
76 /**
77  * @brief     flash type definition
78  */
79 typedef enum {
80     FLASH_TYPE_PUYA = 0,
81 } flash_type_e;
82 
83 /**
84  * @brief     	This function serves to erase a page(256 bytes).
85  * @param[in] 	addr	- the start address of the page needs to erase.
86  * @return    	none.
87  */
88 _attribute_text_sec_ void flash_erase_page(unsigned int addr);
89 
90 /**
91  * @brief 		This function serves to erase a sector.
92  * @param[in]   addr	- the start address of the sector needs to erase.
93  * @return 		none.
94  */
95 _attribute_text_sec_ void flash_erase_sector(unsigned long addr);
96 
97 /**
98  * @brief 		This function serves to erase a block(32k).
99  * @param[in]   addr	- the start address of the block needs to erase.
100  * @return 		none.
101  */
102 _attribute_text_sec_ void flash_erase_32kblock(unsigned int addr);
103 
104 /**
105  * @brief 		This function serves to erase a block(64k).
106  * @param[in]   addr	- the start address of the block needs to erase.
107  * @return 		none.
108  */
109 _attribute_text_sec_ void flash_erase_64kblock(unsigned int addr);
110 
111 /**
112  * @brief     	This function serves to erase a chip.
113  * @return    	none.
114  */
115 _attribute_text_sec_ void flash_erase_chip(void);
116 
117 /**
118  * @brief 		This function writes the buffer's content to a page.
119  * @param[in]   addr	- the start address of the page.
120  * @param[in]   len		- the length(in byte) of content needs to write into the page.
121  * @param[in]   buf		- the start address of the content needs to write into.
122  * @return 		none.
123  */
124 _attribute_text_sec_ void flash_write_page(unsigned long addr, unsigned long len, unsigned char *buf);
125 
126 /**
127  * @brief 		This function reads the content from a page to the buf.
128  * @param[in]   addr	- the start address of the page.
129  * @param[in]   len		- the length(in byte) of content needs to read out from the page.
130  * @param[out]  buf		- the start address of the buffer.
131  * @return 		none.
132  */
133 _attribute_text_sec_ void flash_read_page(unsigned long addr, unsigned long len, unsigned char *buf);
134 
135 /**
136  * @brief 		This function write the status of flash.
137  * @param[in]  	data	- the value of status.
138  * @return 		none.
139  */
140 _attribute_text_sec_ void flash_write_status(unsigned short data);
141 
142 /**
143  * @brief 		This function reads the status of flash.
144  * @return 		the value of status.
145  */
146 _attribute_text_sec_ unsigned short flash_read_status(void);
147 
148 /**
149  * @brief		Deep Power Down mode to put the device in the lowest consumption mode
150  * 				it can be used as an extra software protection mechanism,while the device
151  * 				is not in active use,since in the mode,  all write,Program and Erase commands
152  * 				are ignored,except the Release from Deep Power-Down and Read Device ID(RDI)
153  * 				command.This release the device from this mode
154  * @return 		none.
155  */
156 _attribute_text_sec_ void flash_deep_powerdown(void);
157 
158 /**
159  * @brief		The Release from Power-Down or High Performance Mode/Device ID command is a
160  * 				Multi-purpose command.it can be used to release the device from the power-Down
161  * 				State or High Performance Mode or obtain the devices electronic identification
162  * 				(ID)number.Release from Power-Down will take the time duration of tRES1 before
163  * 				the device will resume normal operation and other command are accepted.The CS#
164  * 				pin must remain high during the tRES1(8us) time duration.
165  * @return      none.
166  */
167 _attribute_text_sec_ void flash_release_deep_powerdown(void);
168 
169 /**
170  * @brief	  	This function serves to read MID of flash(MAC id). Before reading UID of flash,
171  * 				you must read MID of flash. and then you can look up the related table to select
172  * 				the idcmd and read UID of flash
173  * @param[in] 	buf		- store MID of flash
174  * @return    	none.
175  */
176 _attribute_text_sec_ void flash_read_mid(unsigned char *buf);
177 
178 /**
179  * @brief	  	This function serves to read UID of flash
180  * @param[in] 	idcmd	- different flash vendor have different read-uid command. E.g: GD/PUYA:0x4B; XTX: 0x5A
181  * @param[in] 	buf		- store UID of flash
182  * @return    	none.
183  */
184 _attribute_text_sec_ void flash_read_uid(unsigned char idcmd, unsigned char *buf);
185 
186 /**
187  * @brief		This function serves to read flash mid and uid,and check the correctness of mid and uid.
188  * @param[out]	flash_mid	- Flash Manufacturer ID
189  * @param[out]	flash_uid	- Flash Unique ID
190  * @return		0:error 1:ok
191  */
192 _attribute_text_sec_ int flash_read_mid_uid_with_check(unsigned int *flash_mid, unsigned char *flash_uid);
193 
194 /**
195  * @brief 		This function serves to set the protection area of the flash.
196  * @param[in]   type	- flash type include Puya.
197  * @param[in]   data	- refer to Driver API Doc.
198  * @return 		none.
199  */
200 _attribute_text_sec_ void flash_lock(flash_type_e type, unsigned short data);
201 
202 /**
203  * @brief 		This function serves to flash release protection.
204  * @param[in]   type	- flash type include Puya.
205  * @return 		none.
206  */
207 _attribute_text_sec_ void flash_unlock(flash_type_e type);
208 
209 /**
210  * @brief 		This function serves to set priority threshold.
211  *              When the interrupt priority > Threshold flash process will disturb by interrupt.
212  * @param[in]   preempt_en	- 1 can disturb by interrupt, 0 can disturb by interrupt.
213  * @param[in]	threshold	- priority Threshold.
214  * @return    	none.
215  */
216 _attribute_text_sec_ void flash_plic_preempt_config(unsigned char preempt_en, unsigned char threshold);
217 
218 /**
219  * @brief		This function serves to set flash write command.
220  *              This function interface is only used internally by flash,
221  * 				and is currently included in the H file for compatibility with other SDKs. When using this interface,
222  * 				please ensure that you understand the precautions of flash before using it.
223  * @param[in]	cmd	- set command.
224  * @return		none.
225  */
226 _attribute_ram_code_sec_noinline_ void flash_send_cmd(unsigned char cmd);
227