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