1 /**
2  * @file       flc.h
3  * @brief      Flash Controller driver.
4  * @details    This driver can be used to operate on the embedded flash memory.
5  */
6 
7 /******************************************************************************
8  *
9  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
10  * Analog Devices, Inc.),
11  * Copyright (C) 2023-2024 Analog Devices, Inc.
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *     http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  ******************************************************************************/
26 
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_FLC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_FLC_H_
29 
30 /* **** Includes **** */
31 #include "flc_regs.h"
32 #include "mxc_sys.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup flc Flash Controller  (FLC)
40  * @ingroup periphlibs
41  * @{
42  */
43 
44 /***** Definitions *****/
45 
46 /// Bit mask that can be used to find the starting address of a page in flash
47 #define MXC_FLASH_PAGE_MASK ~(MXC_FLASH_PAGE_SIZE - 1)
48 
49 /// Calculate the address of a page in flash from the page number
50 #define MXC_FLASH_PAGE_ADDR(page) (MXC_FLASH_MEM_BASE + ((uint32_t)page * MXC_FLASH_PAGE_SIZE))
51 
52 /***** Function Prototypes *****/
53 
54 /**
55  * @brief      Initializes the Flash Controller for erase/write operations
56  * @return     #E_NO_ERROR if successful.
57  */
58 int MXC_FLC_Init(void);
59 
60 /**
61  * @brief      Checks if Flash Controller is busy.
62  * @details    Reading or executing from flash is not possible if flash is busy
63  *             with an erase or write operation.
64  * @return     If non-zero, flash operation is in progress
65  */
66 int MXC_FLC_Busy(void);
67 
68 /**
69  * @brief      Erases the entire flash array.
70  * @note       This function must be executed from RAM.
71  * @return     #E_NO_ERROR If function is successful.
72  */
73 int MXC_FLC_MassErase(void);
74 
75 /**
76  * @brief      Erases the page of flash at the specified address.
77  * @note       This function must be executed from RAM.
78  * @param      address  Any address within the page to erase.
79  * @return     #E_NO_ERROR If function is successful.
80  */
81 int MXC_FLC_PageErase(uint32_t address);
82 
83 /**
84  * @brief      Read Data out of Flash from an address
85  *
86  * @param[in]  address  The address to read from
87  * @param      buffer   The buffer to read the data into
88  * @param[in]  len      The length of the buffer
89  *
90  */
91 void MXC_FLC_Read(int address, void *buffer, int len);
92 
93 /**
94  * @brief      Writes data to flash.
95  * @note       This function must be executed from RAM.
96  * @param      address  Address in flash to start writing from.
97  * @param      length   Number of bytes to be written.
98  * @param      buffer     Pointer to data to be written to flash.
99  * @return     #E_NO_ERROR If function is successful.
100  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
101  */
102 int MXC_FLC_Write(uint32_t address, uint32_t length, uint32_t *buffer);
103 
104 /**
105  * @brief      Writes 32 bits of data to flash.
106  * @note       This function must be executed from RAM.
107  * @param      address  Address in flash to start writing from.
108  * @param      data     Pointer to data to be written to flash.
109  * @return     #E_NO_ERROR If function is successful.
110  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
111  */
112 int MXC_FLC_Write32(uint32_t address, uint32_t data);
113 
114 /**
115  * @brief      Writes 128 bits of data to flash.
116  * @note       This function must be executed from RAM.
117  * @param      address  Address in flash to start writing from.
118  * @param      data     Pointer to data to be written to flash.
119  * @return     #E_NO_ERROR If function is successful.
120  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
121  */
122 int MXC_FLC_Write128(uint32_t address, uint32_t *data);
123 
124 /**
125  * @brief      Set FLC Instance used for Interrupts functions.
126  * @note       Default is set to MXC_FLC0 if you don't run this.
127  * @param      flc   Pointer to FLC instance.
128  */
129 void MXC_FLC_SetFLCInt(mxc_flc_regs_t *flc);
130 
131 /**
132  * @brief      Receive the currently set FLC instances used for Interrupts.
133  * @return     Pointer to set FLC instance.
134  */
135 mxc_flc_regs_t *MXC_FLC_GetFLCInt(void);
136 
137 /**
138  * @brief      Enable flash interrupts
139  * @note       Run MXC_SetFLCInt to select FLC Instance.
140  * @param      flags   Interrupts to enable
141  * @return     #E_NO_ERROR If function is successful.
142  */
143 int MXC_FLC_EnableInt(uint32_t flags);
144 
145 /**
146  * @brief      Disable flash interrupts
147  * @note       Run MXC_SetFLCInt to select FLC Instance.
148  * @param      flags   Interrupts to disable
149  * @return     #E_NO_ERROR If function is successful.
150  */
151 int MXC_FLC_DisableInt(uint32_t flags);
152 
153 /**
154  * @brief      Retrieve flash interrupt flags
155  * @note       Run MXC_SetFLCInt to select FLC Instance.
156  * @return     Interrupt flags registers
157  */
158 int MXC_FLC_GetFlags(void);
159 
160 /**
161  * @brief      Clear flash interrupt flags
162  * @note       Provide the bit position to clear, even if the flag is write-0-to-clear
163  * @note       Run MXC_SetFLCInt to select FLC Instance.
164  * @param      flags Flag bit(s) to clear
165  * @return     #E_NO_ERROR If function is successful.
166  */
167 int MXC_FLC_ClearFlags(uint32_t flags);
168 
169 /**
170  * @brief      Unlock info block
171  *
172  * @param[in]  address  The address in the info block needing written to
173  *
174  * @return     #E_NO_ERROR If function is successful.
175  */
176 int MXC_FLC_UnlockInfoBlock(uint32_t address);
177 
178 /**
179  * @brief      Lock info block
180  *
181  * @param[in]  address  The address in the info block that was written to
182  * @return     #E_NO_ERROR If function is successful.
183  */
184 int MXC_FLC_LockInfoBlock(uint32_t address);
185 
186 /**@} end of group flc */
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_FLC_H_
193