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_MAX32672_FLC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_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      Enable flash interrupts
126  * @param      flags   Interrupts to enable
127  * @return     #E_NO_ERROR If function is successful.
128  */
129 int MXC_FLC_EnableInt(uint32_t flags);
130 
131 /**
132  * @brief      Disable flash interrupts
133  * @param      flags   Interrupts to disable
134  * @return     #E_NO_ERROR If function is successful.
135  */
136 int MXC_FLC_DisableInt(uint32_t flags);
137 
138 /**
139  * @brief      Retrieve flash interrupt flags
140  * @return     Interrupt flags registers
141  */
142 int MXC_FLC_GetFlags(void);
143 
144 /**
145  * @brief      Clear flash interrupt flags
146  * @note       Provide the bit position to clear, even if the flag is write-0-to-clear
147  * @param      flags Flag bit(s) to clear
148  * @return     #E_NO_ERROR If function is successful.
149  */
150 int MXC_FLC_ClearFlags(uint32_t flags);
151 
152 /**
153  * @brief      Unlock info block
154  *
155  * @param[in]  address  The address in the info block needing written to
156  *
157  * @return     #E_NO_ERROR If function is successful.
158  */
159 int MXC_FLC_UnlockInfoBlock(uint32_t address);
160 
161 /**
162  * @brief      Lock info block
163  *
164  * @param[in]  address  The address in the info block that was written to
165  * @return     #E_NO_ERROR If function is successful.
166  */
167 int MXC_FLC_LockInfoBlock(uint32_t address);
168 
169 /**
170  * @brief       Blocks write operations to the flash page associated with the 'address' argument
171  * @note        Flash pages cannot be unblocked except for on POR and external resets
172  *
173  * @param       address     Absolute address located anywhere in the flash page to be locked (does not need to be word-aligned)
174  *
175  * @return      #E_NO_ERROR If function is successful.
176  */
177 int MXC_FLC_BlockPageWrite(uint32_t address);
178 
179 /**
180  * @brief       Blocks read operations from the flash page associated with the 'address' argument
181  * @note        Flash pages cannot be unblocked except for on POR and external resets
182  *
183  * @param       address     Absolute address located anywhere in the flash page to be locked (does not need to be word-aligned)
184  *
185  * @return      #E_NO_ERROR If function is successful.
186  */
187 int MXC_FLC_BlockPageRead(uint32_t address);
188 
189 /**@} end of group flc */
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_FLC_H_
196