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_MAX32520_FLC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_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      Write encrytped flash
126  * @note       This function must be executed from RAM.
127  * @param      address  Address in flash to start writing from.
128  * @param      length   Number of bytes to be written.
129  * @param      buffer     Pointer to data to be written to flash.
130  *
131  * @return     #E_NO_ERROR If function is successful.
132  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
133  */
134 int MXC_FLC_Write_Encrypted(uint32_t address, uint32_t length, uint32_t *buffer);
135 
136 /**
137  * @brief      Enable flash interrupts
138  * @param      flags   Interrupts to enable
139  * @return     #E_NO_ERROR If function is successful.
140  */
141 int MXC_FLC_EnableInt(uint32_t flags);
142 
143 /**
144  * @brief      Disable flash interrupts
145  * @param      flags   Interrupts to disable
146  * @return     #E_NO_ERROR If function is successful.
147  */
148 int MXC_FLC_DisableInt(uint32_t flags);
149 
150 /**
151  * @brief      Retrieve flash interrupt flags
152  * @return     Interrupt flags registers
153  */
154 int MXC_FLC_GetFlags(void);
155 
156 /**
157  * @brief      Clear flash interrupt flags
158  * @note       Provide the bit position to clear, even if the flag is write-0-to-clear
159  * @param      flags Flag bit(s) to clear
160  * @return     #E_NO_ERROR If function is successful.
161  */
162 int MXC_FLC_ClearFlags(uint32_t flags);
163 
164 /**
165  * @brief      Unlock info block
166  *
167  * @param[in]  address  The address in the info block needing written to
168  *
169  * @return     #E_NO_ERROR If function is successful.
170  */
171 int MXC_FLC_UnlockInfoBlock(uint32_t address);
172 
173 /**
174  * @brief      Lock info block
175  *
176  * @param[in]  address  The address in the info block that was written to
177  * @return     #E_NO_ERROR If function is successful.
178  */
179 int MXC_FLC_LockInfoBlock(uint32_t address);
180 
181 /**@} end of group flc */
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_FLC_H_
188