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