1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_flash.h
3  * @version $Rev:: 8260         $
4  * @date    $Date:: 2024-11-05 #$
5  * @brief   The header file of the FLASH library.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Define to prevent recursive inclusion -------------------------------------------------------------------*/
29 #ifndef __HT32F5XXXX_FLASH_H
30 #define __HT32F5XXXX_FLASH_H
31 
32 #ifdef __cplusplus
33  extern "C" {
34 #endif
35 
36 /* Includes ------------------------------------------------------------------------------------------------*/
37 #include "ht32.h"
38 
39 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
40   * @{
41   */
42 
43 /** @addtogroup FLASH
44   * @{
45   */
46 
47 
48 /* Exported types ------------------------------------------------------------------------------------------*/
49 /** @defgroup FLASH_Exported_Types FLASH exported types
50   * @{
51   */
52 
53 /**
54  * @brief  Enumeration of FLASH return status.
55  */
56 typedef enum
57 {
58   FLASH_COMPLETE              = 0,
59   FLASH_ERR_ADDR_OUT_OF_RANGE,
60   FLASH_ERR_WRITE_PROTECTED,
61   FLASH_TIME_OUT
62 } FLASH_State;
63 
64 /**
65  * @brief  Enumeration of FLASH boot mode.
66  */
67 typedef enum
68 {
69   FLASH_BOOT_LOADER           = 1,
70   FLASH_BOOT_MAIN             = 2
71 } FLASH_Vector;
72 
73 typedef struct
74 {
75   u32 WriteProtect[4];
76   u32 MainSecurity;
77   u32 OptionProtect;
78 } FLASH_OptionByte;
79 
80 /**
81   * @}
82   */
83 
84 /* Exported constants --------------------------------------------------------------------------------------*/
85 /** @defgroup FLASH_Exported_Constants FLASH exported constants
86   * @{
87   */
88 
89 /* Flash Information                                                                                        */
90 #define FLASH_PAGE_SIZE               (LIBCFG_FLASH_PAGESIZE)   /* Flash page size                          */
91 
92 #if (LIBCFG_FMC_PREFETCH)
93 
94 /* Flash Wait State                                                                                         */
95 #define FLASH_WAITSTATE_0             (0x00000001)   /* FLASH zero wait state                               */
96 #define FLASH_WAITSTATE_1             (0x00000002)   /* FLASH one wait state                                */
97 #if (LIBCFG_FMC_WAIT_STATE_2)
98 #define FLASH_WAITSTATE_2             (0x00000003)   /* FLASH two wait state                                */
99 #endif
100 #if (LIBCFG_FMC_WAIT_STATE_3)
101 #define FLASH_WAITSTATE_3             (0x00000004)   /* FLASH three wait state                              */
102 #endif
103 
104 #if (LIBCFG_FMC_WAIT_STATE_3)
105 #define FLASH_WAITSTATE_MAX           (FLASH_WAITSTATE_3)
106 #elif (LIBCFG_FMC_WAIT_STATE_2)
107 #define FLASH_WAITSTATE_MAX           (FLASH_WAITSTATE_2)
108 #else
109 #define FLASH_WAITSTATE_MAX           (FLASH_WAITSTATE_1)
110 #endif
111 
112 #endif
113 
114 /* FLASH OISR Flags                                                                                         */
115 #define FLASH_FLAG_ORFF               (0x00000001)   /* Operation Finished Flag                             */
116 #define FLASH_FLAG_ITADF              (0x00000002)   /* Invalid Target Address Flag                         */
117 #define FLASH_FLAG_OBEF               (0x00000004)   /* Option Byte Check Sum Error Flag                    */
118 #define FLASH_FLAG_IOCMF              (0x00000008)   /* Invalid Operation Command Flag                      */
119 #define FLASH_FLAG_OREF               (0x00000010)   /* Operation Error Flag                                */
120 #define FLASH_FLAG_RORFF              (0x00010000)   /* Raw Operation Finished Flag                         */
121 #define FLASH_FLAG_PPEF               (0x00020000)   /* Page Erase/Program Protected Error Flag             */
122 
123 /* FLASH OIER                                                                                               */
124 #define FLASH_INT_ORFIEN              (0x00000001)   /* Flash Operation Finished Interrupt Enable           */
125 #define FLASH_INT_ITADIEN             (0x00000002)   /* Invalid Target Address Interrupt Enable             */
126 #define FLASH_INT_OBEIEN              (0x00000004)   /* Option Byte Checksum Error Interrupt Enable         */
127 #define FLASH_INT_IOCMIEN             (0x00000008)   /* Invalid Operation Command Interrupt Enable          */
128 #define FLASH_INT_OREIEN              (0x00000010)   /* Operation Error Interrupt Enable                    */
129 #define FLASH_INT_ALL                 (0x0000001F)   /* Flash all Interrupt Enable                          */
130 
131 /* Option Bytes Address                                                                                     */
132 #define OPTION_BYTE_BASE              (0x1FF00000)   /* Option Byte Base Address                            */
133 #define OB_PP0                        (0x1FF00000)   /* Option Byte: Write Protection 0                     */
134 #define OB_PP1                        (0x1FF00004)   /* Option Byte: Write Protection 1                     */
135 #define OB_PP2                        (0x1FF00008)   /* Option Byte: Write Protection 2                     */
136 #define OB_PP3                        (0x1FF0000C)   /* Option Byte: Write Protection 3                     */
137 #define OB_CP                         (0x1FF00010)   /* Option Byte: Security Protection                    */
138 #define OB_CHECKSUM                   (0x1FF00020)   /* Option Byte: Checksum                               */
139 
140 /* Flash Write Protection Page Mask                                                                         */
141 #if (LIBCFG_FLASH_2PAGE_PER_WPBIT)
142   #define FLASH_WP_PAGE_SET(OP, PAGE)     (OP.WriteProtect[PAGE / 64] |= 1 << ((PAGE % 64) / 2))
143   #define FLASH_WP_PAGE_CLEAR(OP, PAGE)   (OP.WriteProtect[PAGE / 64] &= ~(1 << ((PAGE % 64) / 2)))
144   #define FLASH_IS_WP_PAGE(OP, PAGE)      (OP.WriteProtect[PAGE / 64] & (1 << ((PAGE % 64) / 2)))
145 #else
146   #define FLASH_WP_PAGE_SET(OP, PAGE)     (OP.WriteProtect[PAGE / 32] |= 1 << (PAGE % 32))
147   #define FLASH_WP_PAGE_CLEAR(OP, PAGE)   (OP.WriteProtect[PAGE / 32] &= ~(1 << (PAGE % 32)))
148   #define FLASH_IS_WP_PAGE(OP, PAGE)      (OP.WriteProtect[PAGE / 32] & (1 << (PAGE % 32)))
149 #endif
150 #define FLASH_WP_ALLPAGE_SET(OP)        {u32 i; for (i = 0; i < 4; i++) { OP.WriteProtect[i] = 0xFFFFFFFF; } }
151 
152 /**
153   * @}
154   */
155 
156 /* Exported functions --------------------------------------------------------------------------------------*/
157 /** @defgroup FLASH_Exported_Functions FLASH exported functions
158   * @{
159   */
160 #if (LIBCFG_FMC_PREFETCH)
161 void FLASH_SetWaitState(u32 FLASH_WAITSTATE_n);
162 void FLASH_PrefetchBufferCmd(ControlStatus NewState);
163 #endif
164 #if (LIBCFG_FMC_BRANCHCACHE)
165 void FLASH_BranchCacheCmd(ControlStatus NewState);
166 #endif
167 void FLASH_SetRemappingMode(FLASH_Vector FLASH_BOOT_x);
168 FLASH_State FLASH_ErasePage(u32 PageAddress);
169 FLASH_State FLASH_EraseOptionByte(void);
170 FLASH_State FLASH_MassErase(void);
171 FLASH_State FLASH_ProgramWordData(u32 Address, u32 Data);
172 FLASH_State FLASH_ProgramOptionByte(FLASH_OptionByte *Option);
173 void FLASH_GetOptionByteStatus(FLASH_OptionByte *Option);
174 void FLASH_IntConfig(u32 FLASH_INT, ControlStatus Cmd);
175 FlagStatus FLASH_GetIntStatus(u32 FLASH_FLAG_x);
176 void FLASH_ClearIntFlag(u32 FLASH_FLAG_x);
177 FLASH_State FLASH_WaitForOperationEnd(void);
178 /**
179   * @}
180   */
181 
182 
183 /**
184   * @}
185   */
186 
187 /**
188   * @}
189   */
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif
196