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_MAX32660_FLC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_FLC_H_
29 
30 /* **** Includes **** */
31 #include "flc_regs.h"
32 #include "mxc_sys.h"
33 #include "mxc_errors.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @defgroup flc Flash Controller  (FLC)
41  * @ingroup periphlibs
42  * @{
43  */
44 
45 /***** Definitions *****/
46 
47 /// Bit mask that can be used to find the starting address of a page in flash
48 #define MXC_FLASH_PAGE_MASK ~(MXC_FLASH_PAGE_SIZE - 1)
49 
50 /// Calculate the address of a page in flash from the page number
51 #define MXC_FLASH_PAGE_ADDR(page) (MXC_FLASH_MEM_BASE + ((uint32_t)page * MXC_FLASH_PAGE_SIZE))
52 
53 /***** Function Prototypes *****/
54 
55 /**
56  * @brief      Initializes the Flash Controller for erase/write operations
57  * @return     #E_NO_ERROR if successful.
58  */
59 int MXC_FLC_Init(void);
60 
61 /**
62  * @brief      Checks if Flash Controller is busy.
63  * @details    Reading or executing from flash is not possible if flash is busy
64  *             with an erase or write operation.
65  * @return     If non-zero, flash operation is in progress
66  */
67 int MXC_FLC_Busy(void);
68 
69 /**
70  * @brief      Erases the entire flash array.
71  * @note       This function must be executed from RAM.
72  * @return     #E_NO_ERROR If function is successful.
73  */
74 int MXC_FLC_MassErase(void);
75 
76 /**
77  * @brief      Erases the page of flash at the specified address.
78  * @note       This function must be executed from RAM.
79  * @param      address  Any address within the page to erase.
80  * @return     #E_NO_ERROR If function is successful.
81  */
82 int MXC_FLC_PageErase(uint32_t address);
83 
84 /**
85  * @brief      Read Data out of Flash from an address
86  *
87  * @param[in]  address  The address to read from
88  * @param      buffer   The buffer to read the data into
89  * @param[in]  len      The length of the buffer
90  *
91  */
92 void MXC_FLC_Read(int address, void *buffer, int len);
93 
94 /**
95  * @brief      Writes data to flash.
96  * @note       This function must be executed from RAM.
97  * @param      address  Address in flash to start writing from.
98  * @param      length   Number of bytes to be written.
99  * @param      buffer     Pointer to data to be written to flash.
100  * @return     #E_NO_ERROR If function is successful.
101  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
102  */
103 int MXC_FLC_Write(uint32_t address, uint32_t length, uint32_t *buffer);
104 
105 /**
106  * @brief      Writes 32 bits of data to flash.
107  * @note       This function must be executed from RAM.
108  * @param      address  Address in flash to start writing from.
109  * @param      data     Pointer to data to be written to flash.
110  * @return     #E_NO_ERROR If function is successful.
111  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
112  */
113 int MXC_FLC_Write32(uint32_t address, uint32_t data);
114 
115 /**
116  * @brief      Writes 128 bits of data to flash.
117  * @note       This function must be executed from RAM.
118  * @param      address  Address in flash to start writing from.
119  * @param      data     Pointer to data to be written to flash.
120  * @return     #E_NO_ERROR If function is successful.
121  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
122  */
123 int MXC_FLC_Write128(uint32_t address, uint32_t *data);
124 
125 /**
126  * @brief      Enable flash interrupts
127  * @param      flags   Interrupts to enable
128  * @return     #E_NO_ERROR If function is successful.
129  */
130 int MXC_FLC_EnableInt(uint32_t flags);
131 
132 /**
133  * @brief      Disable flash interrupts
134  * @param      flags   Interrupts to disable
135  * @return     #E_NO_ERROR If function is successful.
136  */
137 int MXC_FLC_DisableInt(uint32_t flags);
138 
139 /**
140  * @brief      Retrieve flash interrupt flags
141  * @return     Interrupt flags registers
142  */
143 int MXC_FLC_GetFlags(void);
144 
145 /**
146  * @brief      Clear flash interrupt flags
147  * @note       Provide the bit position to clear, even if the flag is write-0-to-clear
148  * @param      flags Flag bit(s) to clear
149  * @return     #E_NO_ERROR If function is successful.
150  */
151 int MXC_FLC_ClearFlags(uint32_t flags);
152 
153 /**
154  * @brief      Unlock info block
155  *
156  * @param[in]  address  The address in the info block needing written to
157  *
158  * @return     #E_NO_ERROR If function is successful.
159  */
160 int MXC_FLC_UnlockInfoBlock(uint32_t address);
161 
162 /**
163  * @brief      Lock info block
164  *
165  * @param[in]  address  The address in the info block that was written to
166  * @return     #E_NO_ERROR If function is successful.
167  */
168 int MXC_FLC_LockInfoBlock(uint32_t address);
169 
170 /**@} end of group flc */
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_FLC_H_
177