mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32625/mxc/flc.c@151:02e0a0aed4ec, 2016-11-08 (annotated)
- Committer:
- <>
- Date:
- Tue Nov 08 17:45:16 2016 +0000
- Revision:
- 151:02e0a0aed4ec
This updates the lib to the mbed lib v129
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 151:02e0a0aed4ec | 1 | /** |
<> | 151:02e0a0aed4ec | 2 | * @file flc.c |
<> | 151:02e0a0aed4ec | 3 | * @brief This file contains the function implementations for the Flash |
<> | 151:02e0a0aed4ec | 4 | * Controller peripheral module. |
<> | 151:02e0a0aed4ec | 5 | */ |
<> | 151:02e0a0aed4ec | 6 | |
<> | 151:02e0a0aed4ec | 7 | /* **************************************************************************** |
<> | 151:02e0a0aed4ec | 8 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 151:02e0a0aed4ec | 9 | * |
<> | 151:02e0a0aed4ec | 10 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 151:02e0a0aed4ec | 11 | * copy of this software and associated documentation files (the "Software"), |
<> | 151:02e0a0aed4ec | 12 | * to deal in the Software without restriction, including without limitation |
<> | 151:02e0a0aed4ec | 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 151:02e0a0aed4ec | 14 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 151:02e0a0aed4ec | 15 | * Software is furnished to do so, subject to the following conditions: |
<> | 151:02e0a0aed4ec | 16 | * |
<> | 151:02e0a0aed4ec | 17 | * The above copyright notice and this permission notice shall be included |
<> | 151:02e0a0aed4ec | 18 | * in all copies or substantial portions of the Software. |
<> | 151:02e0a0aed4ec | 19 | * |
<> | 151:02e0a0aed4ec | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 151:02e0a0aed4ec | 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 151:02e0a0aed4ec | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 151:02e0a0aed4ec | 23 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 151:02e0a0aed4ec | 24 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 151:02e0a0aed4ec | 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 151:02e0a0aed4ec | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 151:02e0a0aed4ec | 27 | * |
<> | 151:02e0a0aed4ec | 28 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 151:02e0a0aed4ec | 29 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 151:02e0a0aed4ec | 30 | * Products, Inc. Branding Policy. |
<> | 151:02e0a0aed4ec | 31 | * |
<> | 151:02e0a0aed4ec | 32 | * The mere transfer of this software does not imply any licenses |
<> | 151:02e0a0aed4ec | 33 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 151:02e0a0aed4ec | 34 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 151:02e0a0aed4ec | 35 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 151:02e0a0aed4ec | 36 | * ownership rights. |
<> | 151:02e0a0aed4ec | 37 | * |
<> | 151:02e0a0aed4ec | 38 | * $Date: 2016-08-02 13:46:43 -0500 (Tue, 02 Aug 2016) $ |
<> | 151:02e0a0aed4ec | 39 | * $Revision: 23898 $ |
<> | 151:02e0a0aed4ec | 40 | * |
<> | 151:02e0a0aed4ec | 41 | *************************************************************************** */ |
<> | 151:02e0a0aed4ec | 42 | |
<> | 151:02e0a0aed4ec | 43 | /* **** Includes **** */ |
<> | 151:02e0a0aed4ec | 44 | #include "mxc_config.h" |
<> | 151:02e0a0aed4ec | 45 | #include "flc.h" |
<> | 151:02e0a0aed4ec | 46 | |
<> | 151:02e0a0aed4ec | 47 | |
<> | 151:02e0a0aed4ec | 48 | /** |
<> | 151:02e0a0aed4ec | 49 | * @ingroup flc |
<> | 151:02e0a0aed4ec | 50 | * @{ |
<> | 151:02e0a0aed4ec | 51 | */ |
<> | 151:02e0a0aed4ec | 52 | |
<> | 151:02e0a0aed4ec | 53 | /* **** Definitions **** */ |
<> | 151:02e0a0aed4ec | 54 | |
<> | 151:02e0a0aed4ec | 55 | /* **** Globals **** */ |
<> | 151:02e0a0aed4ec | 56 | |
<> | 151:02e0a0aed4ec | 57 | /* **** Functions **** */ |
<> | 151:02e0a0aed4ec | 58 | |
<> | 151:02e0a0aed4ec | 59 | /* ************************************************************************* */ |
<> | 151:02e0a0aed4ec | 60 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 61 | #undef IAR_PRAGMAS //Make sure this is not defined for GCC |
<> | 151:02e0a0aed4ec | 62 | #endif |
<> | 151:02e0a0aed4ec | 63 | |
<> | 151:02e0a0aed4ec | 64 | #if IAR_PRAGMAS |
<> | 151:02e0a0aed4ec | 65 | // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM. |
<> | 151:02e0a0aed4ec | 66 | #pragma section=".flashprog" |
<> | 151:02e0a0aed4ec | 67 | #endif |
<> | 151:02e0a0aed4ec | 68 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 69 | __attribute__ ((section(".flashprog"))) |
<> | 151:02e0a0aed4ec | 70 | #endif |
<> | 151:02e0a0aed4ec | 71 | /** |
<> | 151:02e0a0aed4ec | 72 | * @brief Return the status of the busy state of the flash controller. |
<> | 151:02e0a0aed4ec | 73 | * |
<> | 151:02e0a0aed4ec | 74 | * @return 0 Flash Controller is idle. |
<> | 151:02e0a0aed4ec | 75 | * @return Non-zero indicates the flash controller is performing an |
<> | 151:02e0a0aed4ec | 76 | * erase or write request. |
<> | 151:02e0a0aed4ec | 77 | */ |
<> | 151:02e0a0aed4ec | 78 | __STATIC_INLINE int FLC_Busy(void) |
<> | 151:02e0a0aed4ec | 79 | { |
<> | 151:02e0a0aed4ec | 80 | return (MXC_FLC->ctrl & (MXC_F_FLC_CTRL_WRITE | MXC_F_FLC_CTRL_MASS_ERASE | MXC_F_FLC_CTRL_PAGE_ERASE)); |
<> | 151:02e0a0aed4ec | 81 | } |
<> | 151:02e0a0aed4ec | 82 | |
<> | 151:02e0a0aed4ec | 83 | /* ************************************************************************* */ |
<> | 151:02e0a0aed4ec | 84 | #if IAR_PRAGMAS |
<> | 151:02e0a0aed4ec | 85 | // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM. |
<> | 151:02e0a0aed4ec | 86 | #pragma section=".flashprog" |
<> | 151:02e0a0aed4ec | 87 | #endif |
<> | 151:02e0a0aed4ec | 88 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 89 | __attribute__ ((section(".flashprog"))) |
<> | 151:02e0a0aed4ec | 90 | #endif |
<> | 151:02e0a0aed4ec | 91 | int FLC_Init(void) |
<> | 151:02e0a0aed4ec | 92 | { |
<> | 151:02e0a0aed4ec | 93 | /* Check if the flash controller is busy */ |
<> | 151:02e0a0aed4ec | 94 | if (FLC_Busy()) { |
<> | 151:02e0a0aed4ec | 95 | return E_BUSY; |
<> | 151:02e0a0aed4ec | 96 | } |
<> | 151:02e0a0aed4ec | 97 | |
<> | 151:02e0a0aed4ec | 98 | /* Enable automatic calculation of the clock divider to generate a 1MHz clock from the APB clock */ |
<> | 151:02e0a0aed4ec | 99 | MXC_FLC->perform |= MXC_F_FLC_PERFORM_AUTO_CLKDIV; |
<> | 151:02e0a0aed4ec | 100 | |
<> | 151:02e0a0aed4ec | 101 | /* The flash controller will stall any reads while flash operations are in |
<> | 151:02e0a0aed4ec | 102 | * progress. Disable the legacy failure detection logic that would flag reads |
<> | 151:02e0a0aed4ec | 103 | * during flash operations as errors. |
<> | 151:02e0a0aed4ec | 104 | */ |
<> | 151:02e0a0aed4ec | 105 | MXC_FLC->perform |= MXC_F_FLC_PERFORM_EN_PREVENT_FAIL; |
<> | 151:02e0a0aed4ec | 106 | |
<> | 151:02e0a0aed4ec | 107 | return E_NO_ERROR; |
<> | 151:02e0a0aed4ec | 108 | } |
<> | 151:02e0a0aed4ec | 109 | |
<> | 151:02e0a0aed4ec | 110 | /* ************************************************************************* */ |
<> | 151:02e0a0aed4ec | 111 | #if IAR_PRAGMAS |
<> | 151:02e0a0aed4ec | 112 | // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM. |
<> | 151:02e0a0aed4ec | 113 | #pragma section=".flashprog" |
<> | 151:02e0a0aed4ec | 114 | #endif |
<> | 151:02e0a0aed4ec | 115 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 116 | __attribute__ ((section(".flashprog"))) |
<> | 151:02e0a0aed4ec | 117 | #endif |
<> | 151:02e0a0aed4ec | 118 | int FLC_PageErase(uint32_t address, uint8_t erase_code, uint8_t unlock_key) |
<> | 151:02e0a0aed4ec | 119 | { |
<> | 151:02e0a0aed4ec | 120 | /* Check if the flash controller is busy */ |
<> | 151:02e0a0aed4ec | 121 | if (FLC_Busy()) { |
<> | 151:02e0a0aed4ec | 122 | return E_BUSY; |
<> | 151:02e0a0aed4ec | 123 | } |
<> | 151:02e0a0aed4ec | 124 | |
<> | 151:02e0a0aed4ec | 125 | /* Clear stale errors. Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 126 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 127 | |
<> | 151:02e0a0aed4ec | 128 | /* Unlock flash */ |
<> | 151:02e0a0aed4ec | 129 | MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_FLSH_UNLOCK) | |
<> | 151:02e0a0aed4ec | 130 | ((unlock_key << MXC_F_FLC_CTRL_FLSH_UNLOCK_POS) & MXC_F_FLC_CTRL_FLSH_UNLOCK); |
<> | 151:02e0a0aed4ec | 131 | |
<> | 151:02e0a0aed4ec | 132 | /* Write the Erase Code */ |
<> | 151:02e0a0aed4ec | 133 | MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_ERASE_CODE) | |
<> | 151:02e0a0aed4ec | 134 | ((erase_code << MXC_F_FLC_CTRL_ERASE_CODE_POS) & MXC_F_FLC_CTRL_ERASE_CODE); |
<> | 151:02e0a0aed4ec | 135 | |
<> | 151:02e0a0aed4ec | 136 | /* Erase the request page */ |
<> | 151:02e0a0aed4ec | 137 | MXC_FLC->faddr = address; |
<> | 151:02e0a0aed4ec | 138 | MXC_FLC->ctrl |= MXC_F_FLC_CTRL_PAGE_ERASE; |
<> | 151:02e0a0aed4ec | 139 | |
<> | 151:02e0a0aed4ec | 140 | /* Wait until flash operation is complete */ |
<> | 151:02e0a0aed4ec | 141 | while (FLC_Busy()); |
<> | 151:02e0a0aed4ec | 142 | |
<> | 151:02e0a0aed4ec | 143 | /* Lock flash */ |
<> | 151:02e0a0aed4ec | 144 | MXC_FLC->ctrl &= ~(MXC_F_FLC_CTRL_FLSH_UNLOCK | MXC_F_FLC_CTRL_ERASE_CODE); |
<> | 151:02e0a0aed4ec | 145 | |
<> | 151:02e0a0aed4ec | 146 | /* Check for failures */ |
<> | 151:02e0a0aed4ec | 147 | if (MXC_FLC->intr & MXC_F_FLC_INTR_FAILED_IF) { |
<> | 151:02e0a0aed4ec | 148 | /* Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 149 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 150 | return E_UNKNOWN; |
<> | 151:02e0a0aed4ec | 151 | } |
<> | 151:02e0a0aed4ec | 152 | |
<> | 151:02e0a0aed4ec | 153 | return E_NO_ERROR; |
<> | 151:02e0a0aed4ec | 154 | } |
<> | 151:02e0a0aed4ec | 155 | |
<> | 151:02e0a0aed4ec | 156 | /* ************************************************************************* */ |
<> | 151:02e0a0aed4ec | 157 | #if IAR_PRAGMAS |
<> | 151:02e0a0aed4ec | 158 | // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM. |
<> | 151:02e0a0aed4ec | 159 | #pragma section=".flashprog" |
<> | 151:02e0a0aed4ec | 160 | #endif |
<> | 151:02e0a0aed4ec | 161 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 162 | __attribute__ ((section(".flashprog"))) |
<> | 151:02e0a0aed4ec | 163 | #endif |
<> | 151:02e0a0aed4ec | 164 | int FLC_Write(uint32_t address, const void *data, uint32_t length, uint8_t unlock_key) |
<> | 151:02e0a0aed4ec | 165 | { |
<> | 151:02e0a0aed4ec | 166 | uint32_t *ptr = (uint32_t*)data; |
<> | 151:02e0a0aed4ec | 167 | |
<> | 151:02e0a0aed4ec | 168 | /* Can only write in full word units */ |
<> | 151:02e0a0aed4ec | 169 | if ((address & 3) || (length & 3)) { |
<> | 151:02e0a0aed4ec | 170 | return E_BAD_PARAM; |
<> | 151:02e0a0aed4ec | 171 | } |
<> | 151:02e0a0aed4ec | 172 | |
<> | 151:02e0a0aed4ec | 173 | if (length == 0) { |
<> | 151:02e0a0aed4ec | 174 | /* Nothing to do */ |
<> | 151:02e0a0aed4ec | 175 | return E_NO_ERROR; |
<> | 151:02e0a0aed4ec | 176 | } |
<> | 151:02e0a0aed4ec | 177 | |
<> | 151:02e0a0aed4ec | 178 | /* Check if the flash controller is busy */ |
<> | 151:02e0a0aed4ec | 179 | if (FLC_Busy()) { |
<> | 151:02e0a0aed4ec | 180 | return E_BUSY; |
<> | 151:02e0a0aed4ec | 181 | } |
<> | 151:02e0a0aed4ec | 182 | |
<> | 151:02e0a0aed4ec | 183 | /* Clear stale errors. Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 184 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 185 | |
<> | 151:02e0a0aed4ec | 186 | /* Unlock flash */ |
<> | 151:02e0a0aed4ec | 187 | MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_FLSH_UNLOCK) | |
<> | 151:02e0a0aed4ec | 188 | ((unlock_key << MXC_F_FLC_CTRL_FLSH_UNLOCK_POS) & MXC_F_FLC_CTRL_FLSH_UNLOCK); |
<> | 151:02e0a0aed4ec | 189 | |
<> | 151:02e0a0aed4ec | 190 | /* Set the address to write and enable auto increment */ |
<> | 151:02e0a0aed4ec | 191 | MXC_FLC->faddr = address; |
<> | 151:02e0a0aed4ec | 192 | MXC_FLC->ctrl |= MXC_F_FLC_CTRL_AUTO_INCRE_MODE; |
<> | 151:02e0a0aed4ec | 193 | uint32_t write_cmd = MXC_FLC->ctrl | MXC_F_FLC_CTRL_WRITE; |
<> | 151:02e0a0aed4ec | 194 | |
<> | 151:02e0a0aed4ec | 195 | for (; length > 0; length -= 4) { |
<> | 151:02e0a0aed4ec | 196 | /* Perform the write */ |
<> | 151:02e0a0aed4ec | 197 | MXC_FLC->fdata = *ptr++; |
<> | 151:02e0a0aed4ec | 198 | MXC_FLC->ctrl = write_cmd; |
<> | 151:02e0a0aed4ec | 199 | while (FLC_Busy()); |
<> | 151:02e0a0aed4ec | 200 | } |
<> | 151:02e0a0aed4ec | 201 | |
<> | 151:02e0a0aed4ec | 202 | /* Lock flash */ |
<> | 151:02e0a0aed4ec | 203 | MXC_FLC->ctrl &= ~MXC_F_FLC_CTRL_FLSH_UNLOCK; |
<> | 151:02e0a0aed4ec | 204 | |
<> | 151:02e0a0aed4ec | 205 | /* Check for failures */ |
<> | 151:02e0a0aed4ec | 206 | if (MXC_FLC->intr & MXC_F_FLC_INTR_FAILED_IF) { |
<> | 151:02e0a0aed4ec | 207 | /* Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 208 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 209 | return E_UNKNOWN; |
<> | 151:02e0a0aed4ec | 210 | } |
<> | 151:02e0a0aed4ec | 211 | |
<> | 151:02e0a0aed4ec | 212 | return E_NO_ERROR; |
<> | 151:02e0a0aed4ec | 213 | } |
<> | 151:02e0a0aed4ec | 214 | |
<> | 151:02e0a0aed4ec | 215 | /* ************************************************************************* */ |
<> | 151:02e0a0aed4ec | 216 | #if IAR_PRAGMAS |
<> | 151:02e0a0aed4ec | 217 | // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM. |
<> | 151:02e0a0aed4ec | 218 | #pragma section=".flashprog" |
<> | 151:02e0a0aed4ec | 219 | #endif |
<> | 151:02e0a0aed4ec | 220 | #if defined ( __GNUC__ ) |
<> | 151:02e0a0aed4ec | 221 | __attribute__ ((section(".flashprog"))) |
<> | 151:02e0a0aed4ec | 222 | #endif |
<> | 151:02e0a0aed4ec | 223 | int FLC_MassErase(uint8_t erase_code, uint8_t unlock_key) |
<> | 151:02e0a0aed4ec | 224 | { |
<> | 151:02e0a0aed4ec | 225 | /* Check if the flash controller is busy */ |
<> | 151:02e0a0aed4ec | 226 | if (FLC_Busy()) { |
<> | 151:02e0a0aed4ec | 227 | return E_BUSY; |
<> | 151:02e0a0aed4ec | 228 | } |
<> | 151:02e0a0aed4ec | 229 | |
<> | 151:02e0a0aed4ec | 230 | /* Clear stale errors. Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 231 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 232 | |
<> | 151:02e0a0aed4ec | 233 | /* Unlock flash */ |
<> | 151:02e0a0aed4ec | 234 | MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_FLSH_UNLOCK) | |
<> | 151:02e0a0aed4ec | 235 | ((unlock_key << MXC_F_FLC_CTRL_FLSH_UNLOCK_POS) & MXC_F_FLC_CTRL_FLSH_UNLOCK); |
<> | 151:02e0a0aed4ec | 236 | |
<> | 151:02e0a0aed4ec | 237 | /* Write the Erase Code */ |
<> | 151:02e0a0aed4ec | 238 | MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_ERASE_CODE) | |
<> | 151:02e0a0aed4ec | 239 | ((erase_code << MXC_F_FLC_CTRL_ERASE_CODE_POS) & MXC_F_FLC_CTRL_ERASE_CODE); |
<> | 151:02e0a0aed4ec | 240 | |
<> | 151:02e0a0aed4ec | 241 | /* Start the mass erase */ |
<> | 151:02e0a0aed4ec | 242 | MXC_FLC->ctrl |= MXC_F_FLC_CTRL_MASS_ERASE; |
<> | 151:02e0a0aed4ec | 243 | |
<> | 151:02e0a0aed4ec | 244 | /* Wait until flash operation is complete */ |
<> | 151:02e0a0aed4ec | 245 | while (FLC_Busy()); |
<> | 151:02e0a0aed4ec | 246 | |
<> | 151:02e0a0aed4ec | 247 | /* Lock flash */ |
<> | 151:02e0a0aed4ec | 248 | MXC_FLC->ctrl &= ~(MXC_F_FLC_CTRL_FLSH_UNLOCK | MXC_F_FLC_CTRL_ERASE_CODE); |
<> | 151:02e0a0aed4ec | 249 | |
<> | 151:02e0a0aed4ec | 250 | /* Check for failures */ |
<> | 151:02e0a0aed4ec | 251 | if (MXC_FLC->intr & MXC_F_FLC_INTR_FAILED_IF) { |
<> | 151:02e0a0aed4ec | 252 | /* Interrupt flags can only be written to zero, so this is safe */ |
<> | 151:02e0a0aed4ec | 253 | MXC_FLC->intr &= ~MXC_F_FLC_INTR_FAILED_IF; |
<> | 151:02e0a0aed4ec | 254 | return E_UNKNOWN; |
<> | 151:02e0a0aed4ec | 255 | } |
<> | 151:02e0a0aed4ec | 256 | |
<> | 151:02e0a0aed4ec | 257 | return E_NO_ERROR; |
<> | 151:02e0a0aed4ec | 258 | } |
<> | 151:02e0a0aed4ec | 259 | |
<> | 151:02e0a0aed4ec | 260 | /**@} end of group flc */ |