1 /**
2   *********************************************************************************
3   *
4   * @file    ald_flash.h
5   * @brief   Header file of FLASH driver
6   *
7   * @version V1.0
8   * @date    17 Jun 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          17 Jun 2019     AE Team         The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #ifndef __ALD_FLASH_H__
34 #define __ALD_FLASH_H__
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include "utils.h"
41 
42 /** @addtogroup ES32FXXX_ALD
43   * @{
44   */
45 
46 /** @addtogroup FLASH
47   * @{
48   */
49 
50 /**
51   * @defgroup FLASH_Private_Macros FLASH Private Macros
52   * @{
53   */
54 #define FLASH_REG_UNLOCK()				\
55 do {							\
56 	if (op_cmd == OP_FLASH)	 {			\
57 		WRITE_REG(MSC->FLASHKEY, 0x8ACE0246);	\
58 		WRITE_REG(MSC->FLASHKEY, 0x9BDF1357);	\
59 	}						\
60 	else {						\
61 		WRITE_REG(MSC->INFOKEY, 0x7153BFD9);	\
62 		WRITE_REG(MSC->INFOKEY, 0x0642CEA8);	\
63 	}						\
64 } while (0)
65 #define FLASH_REQ()			(SET_BIT(MSC->FLASHCR, MSC_FLASHCR_FLASHREQ_MSK))
66 #define FLASH_REQ_FIN()			(CLEAR_BIT(MSC->FLASHCR, MSC_FLASHCR_FLASHREQ_MSK))
67 #define FLASH_IAP_ENABLE()		(SET_BIT(MSC->FLASHCR, MSC_FLASHCR_IAPEN_MSK))
68 #define FLASH_IAP_DISABLE()		(CLEAR_BIT(MSC->FLASHCR, MSC_FLASHCR_IAPEN_MSK))
69 #define FLASH_BASE_ADDR			0x00000000
70 #define	FLASH_PAGE_SIZE			1024UL
71 #define FLASH_WORD_SIZE			8UL
72 #define FLASH_TOTAL_SIZE		512UL
73 #define FLASH_PAGE_MASK			(FLASH_PAGE_SIZE - 1)
74 #define FLASH_WORD_MASK			(FLASH_WORD_SIZE - 1)
75 #define IS_FLASH_ADDRESS(ADDR)		((ADDR) < (FLASH_BASE_ADDR + FLASH_PAGE_SIZE * FLASH_TOTAL_SIZE))
76 #define IS_4BYTES_ALIGN(ADDR)		(((uint32_t)(ADDR) & 0x3) == 0 ? 1 : 0)
77 #define FLASH_PAGE_ADDR(ADDR)		((ADDR) & (~FLASH_PAGE_MASK))
78 #define FLASH_PAGEEND_ADDR(ADDR)	((ADDR) | FLASH_PAGE_MASK)
79 #define FLASH_WORD_ADDR(ADDR)		((ADDR) & (~FLASH_WORD_MASK))
80 #define FLASH_WORDEND_ADDR(ADDR)	((ADDR) | FLASH_WORD_MASK)
81 #define INFO_PAGE_SIZE			1024UL
82 #define INFO_PAGE_MASK			(INFO_PAGE_SIZE - 1)
83 #define INFO_PAGE_ADDR(ADDR)		((ADDR) & (~INFO_PAGE_MASK))
84 
85 #ifdef USE_FLASH_FIFO
86 	#define	FLASH_FIFO 1
87 #else
88 	#define	FLASH_FIFO 0
89 #endif
90 /**
91   * @}
92   */
93 
94 /** @defgroup FLASH_Private_Types FLASH Private Types
95   * @{
96   */
97 typedef enum {
98 	FLASH_CMD_AE          = 0x000051AEU,	/**< Program area erase all */
99 	FLASH_CMD_PE          = 0x00005EA1U,	/**< Page erase  */
100 	FLASH_CMD_WP          = 0x00005DA2U,	/**< Word program */
101 	FLASH_CMD_WP_FAST     = 0x00005CA3U, 	/**< Flash quickly program */
102 	FLASH_CMD_DATAPE      = 0x00005BA4U,	/**< Data flash page page erase */
103 	FLASH_CMD_DATAWP      = 0x00005AA5U,	/**< Data flash word program */
104 	FLASH_CMD_DATAWP_FAST = 0x000059A6U,	/**< Data flash quickly program */
105 	FLASH_CMD_NP_AE       = 0x000050AFU,	/**< No-private area erase all */
106 } flash_cmd_type;
107 
108 typedef enum {
109 	OP_FLASH = 0U,	/**< Operate Pragram area */
110 	OP_INFO  = 1U,	/**< Operate info area */
111 } op_cmd_type;
112 
113 /**
114   * @}
115   */
116 /** @addtogroup Flash_Private_Functions
117   * @{
118   */
119 ald_status_t flash_page_erase(uint32_t addr);
120 ald_status_t flash_word_program(uint32_t addr, uint32_t *data, uint32_t len, uint32_t fifo);
121 /**
122   * @}
123   */
124 
125 /** @addtogroup Flash_Public_Functions
126   * @{
127   */
128 ald_status_t ald_flash_read(uint32_t *ram_addr, uint32_t addr, uint16_t len);
129 ald_status_t ald_flash_write(uint32_t addr, uint8_t *buf, uint16_t len);
130 ald_status_t ald_flash_erase(uint32_t addr, uint16_t len);
131 /**
132   * @}
133   */
134 /**
135   * @}
136   */
137 
138 /**
139   * @}
140   */
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* __ALD_FLASH_H__ */
147