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