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_MAX78000_FLC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78000_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  The address of the page in flash to erase.  If the address falls in the middle of a page, the entire page will be erased.
79  * @return     #E_NO_ERROR If function is successful.
80  */
81 int MXC_FLC_PageErase(uint32_t address);
82 
83 /**
84  * @brief      Read bytes from a flash address.
85  * @details This function is essentially a wrapper around memcpy.  'buffer' can be a
86  * pointer to any type, and the data bytes read from this function will attempt
87  * to be typecast to match 'buffer'.
88  *
89  * @param[in]  address  The address to read from
90  * @param      buffer   The buffer to read the data into
91  * @param[in]  len      The number of bytes to read from 'address' into 'buffer'.
92  *
93  */
94 void MXC_FLC_Read(int address, void *buffer, int len);
95 
96 /**
97  * @brief      Writes data to flash.
98  * @note       This function must be executed from RAM.
99  * @param      address  Address in flash to start writing from.
100  * @param      length   Number of bytes to be written.
101  * @param      buffer     Pointer to data to be written to flash.
102  * @return     #E_NO_ERROR If function is successful.
103  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
104  */
105 int MXC_FLC_Write(uint32_t address, uint32_t length, uint32_t *buffer);
106 
107 /**
108  * @brief      Writes 32 bits of data to flash.
109  * @note       This function must be executed from RAM.
110  * @param      address  Address in flash to start writing from.
111  * @param      data     Pointer to data to be written to flash.
112  * @return     #E_NO_ERROR If function is successful.
113  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
114  */
115 int MXC_FLC_Write32(uint32_t address, uint32_t data);
116 
117 /**
118  * @brief      Writes 128 bits of data to flash.
119  * @note       This function must be executed from RAM.
120  * @param      address  Address in flash to start writing from.
121  * @param      data     Pointer to data to be written to flash.
122  * @return     #E_NO_ERROR If function is successful.
123  * @note       make sure to disable ICC with ICC_Disable(); before Running this function
124  */
125 int MXC_FLC_Write128(uint32_t address, uint32_t *data);
126 
127 /**
128  * @brief      Enable flash interrupts
129  * @param      flags   Interrupts to enable
130  * @return     #E_NO_ERROR If function is successful.
131  */
132 int MXC_FLC_EnableInt(uint32_t flags);
133 
134 /**
135  * @brief      Disable flash interrupts
136  * @param      flags   Interrupts to disable
137  * @return     #E_NO_ERROR If function is successful.
138  */
139 int MXC_FLC_DisableInt(uint32_t flags);
140 
141 /**
142  * @brief      Retrieve flash interrupt flags
143  * @return     Interrupt flags registers
144  */
145 int MXC_FLC_GetFlags(void);
146 
147 /**
148  * @brief      Clear flash interrupt flags
149  * @note       Provide the bit position to clear, even if the flag is write-0-to-clear
150  * @param      flags Flag bit(s) to clear
151  * @return     #E_NO_ERROR If function is successful.
152  */
153 int MXC_FLC_ClearFlags(uint32_t flags);
154 
155 /**
156  * @brief      Unlock info block
157  *
158  * @param[in]  address  The address in the info block needing written to
159  *
160  * @return     #E_NO_ERROR If function is successful.
161  */
162 int MXC_FLC_UnlockInfoBlock(uint32_t address);
163 
164 /**
165  * @brief      Lock info block
166  *
167  * @param[in]  address  The address in the info block that was written to
168  * @return     #E_NO_ERROR If function is successful.
169  */
170 int MXC_FLC_LockInfoBlock(uint32_t address);
171 
172 /**
173  * @brief       Blocks write operations to the flash page associated with the 'address' argument
174  * @note        Flash pages cannot be unblocked except for on POR and external resets
175  *
176  * @param       address     Absolute address located anywhere in the flash page to be locked (does not need to be word-aligned)
177  *
178  * @return      #E_NO_ERROR If function is successful.
179  */
180 int MXC_FLC_BlockPageWrite(uint32_t address);
181 
182 /**
183  * @brief       Blocks read operations from the flash page associated with the 'address' argument
184  * @note        Flash pages cannot be unblocked except for on POR and external resets
185  *
186  * @param       address     Absolute address located anywhere in the flash page to be locked (does not need to be word-aligned)
187  *
188  * @return      #E_NO_ERROR If function is successful.
189  */
190 int MXC_FLC_BlockPageRead(uint32_t address);
191 
192 /**@} end of group flc */
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78000_FLC_H_
199