mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
mbed-dev library. Release version 162

Who changed what in which revision?

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