• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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 
16 #ifndef _DUET_FLASH_H_
17 #define _DUET_FLASH_H_
18 #include <stdint.h>
19 
20 #define LOW_LEVEL_FLASH_RW_SUPPORT 1
21 
22 #define PAR_OPT_READ_POS  (0)
23 #define PAR_OPT_WRITE_POS (1)
24 
25 #define PAR_OPT_READ_MASK  (0x1u << PAR_OPT_READ_POS)
26 #define PAR_OPT_WRITE_MASK (0x1u << PAR_OPT_WRITE_POS)
27 
28 #define PAR_OPT_READ_DIS  (0x0u << PAR_OPT_READ_POS)
29 #define PAR_OPT_READ_EN   (0x1u << PAR_OPT_READ_POS)
30 #define PAR_OPT_WRITE_DIS (0x0u << PAR_OPT_WRITE_POS)
31 #define PAR_OPT_WRITE_EN  (0x1u << PAR_OPT_WRITE_POS)
32 
33 typedef enum {
34     PARTITION_ERROR = -1,
35     PARTITION_BOOTLOADER,
36     PARTITION_APPLICATION,
37     PARTITION_ATE,
38     // PARTITION_token_TEMP,
39     PARTITION_OTA_TEMP,
40     PARTITION_RF_FIRMWARE,
41     PARTITION_PARAMETER_1,
42     PARTITION_PARAMETER_2,
43     PARTITION_PARAMETER_3,
44     PARTITION_PARAMETER_4,
45     PARTITION_BT_FIRMWARE,
46     PARTITION_SPIFFS,
47     PARTITION_CUSTOM_1,
48     PARTITION_CUSTOM_2,
49     PARTITION_RECOVERY,
50 #ifdef MS_CONFIG_OTA_SUPPORT
51     PARTITION_OTA_MCU,
52     PARTITION_OTA_PARA,
53     PARTITION_OTA_HEAD_PARA,
54 #endif
55     PARTITION_PARAMETER_5,
56     PARTITION_MAX,
57     PARTITION_NONE,
58 } duet_partition_t;
59 
60 typedef enum {
61     FLASH_EMBEDDED,
62     FLASH_SPI,
63     FLASH_QSPI,
64     FLASH_MAX,
65     FLASH_NONE,
66 } duet_flash_t;
67 
68 typedef struct {
69     duet_flash_t partition_owner;
70     const char *partition_description;
71     uint32_t    partition_start_addr;
72     uint32_t    partition_length;
73     uint32_t    partition_options;
74 } duet_logic_partition_t;
75 
76 /**
77  * lega flash init
78  *
79  * @note this function must be called before flash erase/write operation
80  *       also, this function must not be interrupted, it should be called
81  *       with interrupt disabled
82  * @param[in]  none
83  *
84  * @return     HAL_logi_partition struct
85  */
86 int32_t duet_flash_init(void);
87 
88 /**
89  * Get the infomation of the specified flash area
90  *
91  * @param[in]  in_partition  The target flash logical partition
92  *
93  * @return     HAL_logi_partition struct
94  */
95 duet_logic_partition_t *duet_flash_get_info(duet_partition_t in_partition);
96 
97 /**
98  * Erase an area on a Flash logical partition
99  *
100  * @note  Erase on an address will erase all data on a sector that the
101  *        address is belonged to, this function does not save data that
102  *        beyond the address area but in the affected sector, the data
103  *        will be lost.
104  *
105  * @param[in]  in_partition  The target flash logical partition which should be erased
106  * @param[in]  off_set       Start address of the erased flash area
107  * @param[in]  size          Size of the erased flash area
108  *
109  * @return  0 : On success, EIO : If an error occurred with any step
110  */
111 int32_t duet_flash_erase(duet_partition_t in_partition, uint32_t off_set, uint32_t size);
112 
113 /**
114  * Write data to an area on a flash logical partition without erase
115  *
116  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
117  * @param[in]  off_set         Point to the start address that the data is written to, and
118  *                             point to the last unwritten address after this function is
119  *                             returned, so you can call this function serval times without
120  *                             update this start address.
121  * @param[in]  inBuffer        point to the data buffer that will be written to flash
122  * @param[in]  inBufferLength  The length of the buffer
123  *
124  * @return  0 : On success, EIO : If an error occurred with any step
125  */
126 int32_t duet_flash_write(duet_partition_t in_partition, uint32_t *off_set,
127                          const void *in_buf, uint32_t in_buf_len);
128 
129 /**
130  * Write data to an area on a flash logical partition with erase first
131  *
132  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
133  * @param[in]  off_set         Point to the start address that the data is written to, and
134  *                             point to the last unwritten address after this function is
135  *                             returned, so you can call this function serval times without
136  *                             update this start address.
137  * @param[in]  inBuffer        point to the data buffer that will be written to flash
138  * @param[in]  inBufferLength  The length of the buffer
139  *
140  * @return  0 : On success, EIO : If an error occurred with any step
141  */
142 int32_t duet_flash_erase_write(duet_partition_t in_partition, uint32_t *off_set,
143                                const void *in_buf, uint32_t in_buf_len);
144 
145 /**
146  * Read data from an area on a Flash to data buffer in RAM
147  *
148  * @param[in]  in_partition    The target flash logical partition which should be read
149  * @param[in]  off_set         Point to the start address that the data is read, and
150  *                             point to the last unread address after this function is
151  *                             returned, so you can call this function serval times without
152  *                             update this start address.
153  * @param[in]  outBuffer       Point to the data buffer that stores the data read from flash
154  * @param[in]  inBufferLength  The length of the buffer
155  *
156  * @return  0 : On success, EIO : If an error occurred with any step
157  */
158 int32_t duet_flash_read(duet_partition_t in_partition, uint32_t *off_set,
159                         void *out_buf, uint32_t in_buf_len);
160 
161 /**  Erase internal flash
162  *
163  * This will erase the flash for the specified start address
164  * and size
165  *
166  * \param addr Start address in flash (*MUST* 4 KB alignment)
167  * \param len Number of bytes to be erased (*MUST* 4 KB alignment, erase must in one partition)
168  * \return 0 on success
169  */
170 int32_t duet_flash_erase_common(uint32_t addr, uint32_t len);
171 
172 /**  Write data to internal flash
173  *
174  * This will write specified number of bytes to flash.
175  * Note that flash don't need to be erased before writing.
176  *
177  * \param addr Write address in flash
178  * \param buff Pointer to the data to be written
179  * \param len Length of data to be written, make sure write only in one partition
180  *
181  * \return 0 on success
182  */
183 int32_t duet_flash_write_common(uint32_t addr, const void *in_buff, uint32_t len);
184 /**
185  * Set security options on a logical partition
186  *
187  * @param[in]  partition  The target flash logical partition
188  * @param[in]  offset     Point to the start address that the data is read, and
189  *                        point to the last unread address after this function is
190  *                        returned, so you can call this function serval times without
191  *                        update this start address.
192  * @param[in]  size       Size of enabled flash area
193  *
194  * @return  0 : On success, EIO : If an error occurred with any step
195  */
196 int32_t duet_flash_enable_secure(duet_partition_t partition, uint32_t off_set, uint32_t size);
197 
198 /**
199  * Disable security options on a logical partition
200  *
201  * @param[in]  partition  The target flash logical partition
202  * @param[in]  offset     Point to the start address that the data is read, and
203  *                        point to the last unread address after this function is
204  *                        returned, so you can call this function serval times without
205  *                        update this start address.
206  * @param[in]  size       Size of disabled flash area
207  *
208  * @return  0 : On success, EIO : If an error occurred with any step
209  */
210 int32_t duet_flash_dis_secure(duet_partition_t partition, uint32_t off_set, uint32_t size);
211 #ifdef LOW_LEVEL_FLASH_RW_SUPPORT
212 #define FLASH_MAC_ADDR_TOKEN_NAME "wifi_mac_addr_default_value"
213 #define FLASH_MAC_ADDR_TOKEN      (0xACBDEFFE)
214 #define FLASH_MAC_ADDR_TOKEN_LEN  (4)
215 #define MAC_ADDR_LEN              (6)
216 typedef struct {
217     uint8_t   mac[MAC_ADDR_LEN];
218     uint8_t   resv[2];
219     uint32_t  token;
220 } flash_mac_addr_t;
221 /* *
222  * * Wifi mac addr should use efuse value
223  * * If efuse have no mac addr, use this value
224  * * The mac addr saved in flash, use kv to store. if no kv, user should rewrite the read/write function
225  * * Default set 0x8c59dc + radom value
226  */
227 int32_t duet_flash_get_wifi_mac(flash_mac_addr_t *addr);
228 int32_t duet_flash_set_wifi_mac(flash_mac_addr_t *addr);
229 #endif
230 #endif // _LEGA_FLASH_H_
231