mbed
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L4/flash_api.c@175:b96e65c34a4d, 2017-10-02 (annotated)
- Committer:
- AnnaBridge
- Date:
- Mon Oct 02 15:33:19 2017 +0100
- Revision:
- 175:b96e65c34a4d
- Parent:
- 167:e84263d55307
- Child:
- 176:af195413fb11
This updates the lib to the mbed lib v 152
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 167:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 167:e84263d55307 | 2 | * Copyright (c) 2017 ARM Limited |
AnnaBridge | 167:e84263d55307 | 3 | * |
AnnaBridge | 167:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 167:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 167:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 167:e84263d55307 | 7 | * |
AnnaBridge | 167:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 167:e84263d55307 | 9 | * |
AnnaBridge | 167:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 167:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 167:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 167:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 167:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 167:e84263d55307 | 15 | */ |
AnnaBridge | 167:e84263d55307 | 16 | |
AnnaBridge | 167:e84263d55307 | 17 | #include "flash_api.h" |
AnnaBridge | 167:e84263d55307 | 18 | #include "mbed_critical.h" |
AnnaBridge | 167:e84263d55307 | 19 | |
AnnaBridge | 167:e84263d55307 | 20 | #if DEVICE_FLASH |
AnnaBridge | 167:e84263d55307 | 21 | #include "mbed_assert.h" |
AnnaBridge | 167:e84263d55307 | 22 | #include "cmsis.h" |
AnnaBridge | 167:e84263d55307 | 23 | |
AnnaBridge | 167:e84263d55307 | 24 | /** |
AnnaBridge | 167:e84263d55307 | 25 | * @brief Gets the page of a given address |
AnnaBridge | 167:e84263d55307 | 26 | * @param Addr: Address of the FLASH Memory |
AnnaBridge | 167:e84263d55307 | 27 | * @retval The page of a given address |
AnnaBridge | 167:e84263d55307 | 28 | */ |
AnnaBridge | 167:e84263d55307 | 29 | static uint32_t GetPage(uint32_t Addr) |
AnnaBridge | 167:e84263d55307 | 30 | { |
AnnaBridge | 167:e84263d55307 | 31 | uint32_t page = 0; |
AnnaBridge | 167:e84263d55307 | 32 | |
AnnaBridge | 167:e84263d55307 | 33 | if (Addr < (FLASH_BASE + FLASH_BANK_SIZE)) { |
AnnaBridge | 167:e84263d55307 | 34 | /* Bank 1 */ |
AnnaBridge | 167:e84263d55307 | 35 | page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE; |
AnnaBridge | 167:e84263d55307 | 36 | } else { |
AnnaBridge | 167:e84263d55307 | 37 | /* Bank 2 */ |
AnnaBridge | 167:e84263d55307 | 38 | page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE; |
AnnaBridge | 167:e84263d55307 | 39 | } |
AnnaBridge | 167:e84263d55307 | 40 | |
AnnaBridge | 167:e84263d55307 | 41 | return page; |
AnnaBridge | 167:e84263d55307 | 42 | } |
AnnaBridge | 167:e84263d55307 | 43 | |
AnnaBridge | 167:e84263d55307 | 44 | /** |
AnnaBridge | 167:e84263d55307 | 45 | * @brief Gets the bank of a given address |
AnnaBridge | 167:e84263d55307 | 46 | * @param Addr: Address of the FLASH Memory |
AnnaBridge | 167:e84263d55307 | 47 | * @retval The bank of a given address |
AnnaBridge | 167:e84263d55307 | 48 | */ |
AnnaBridge | 167:e84263d55307 | 49 | static uint32_t GetBank(uint32_t Addr) |
AnnaBridge | 167:e84263d55307 | 50 | { |
AnnaBridge | 167:e84263d55307 | 51 | uint32_t bank = 0; |
AnnaBridge | 167:e84263d55307 | 52 | #if defined(SYSCFG_MEMRMP_FB_MODE) |
AnnaBridge | 167:e84263d55307 | 53 | if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0) { |
AnnaBridge | 167:e84263d55307 | 54 | /* No Bank swap */ |
AnnaBridge | 167:e84263d55307 | 55 | if (Addr < (FLASH_BASE + FLASH_BANK_SIZE)) { |
AnnaBridge | 167:e84263d55307 | 56 | bank = FLASH_BANK_1; |
AnnaBridge | 167:e84263d55307 | 57 | } else { |
AnnaBridge | 167:e84263d55307 | 58 | bank = FLASH_BANK_2; |
AnnaBridge | 167:e84263d55307 | 59 | } |
AnnaBridge | 167:e84263d55307 | 60 | } else { |
AnnaBridge | 167:e84263d55307 | 61 | /* Bank swap */ |
AnnaBridge | 167:e84263d55307 | 62 | if (Addr < (FLASH_BASE + FLASH_BANK_SIZE)) { |
AnnaBridge | 167:e84263d55307 | 63 | bank = FLASH_BANK_2; |
AnnaBridge | 167:e84263d55307 | 64 | } else { |
AnnaBridge | 167:e84263d55307 | 65 | bank = FLASH_BANK_1; |
AnnaBridge | 167:e84263d55307 | 66 | } |
AnnaBridge | 167:e84263d55307 | 67 | } |
AnnaBridge | 167:e84263d55307 | 68 | #else |
AnnaBridge | 167:e84263d55307 | 69 | /* Device like L432KC */ |
AnnaBridge | 167:e84263d55307 | 70 | bank = FLASH_BANK_1; |
AnnaBridge | 167:e84263d55307 | 71 | #endif |
AnnaBridge | 167:e84263d55307 | 72 | |
AnnaBridge | 167:e84263d55307 | 73 | return bank; |
AnnaBridge | 167:e84263d55307 | 74 | } |
AnnaBridge | 167:e84263d55307 | 75 | |
AnnaBridge | 167:e84263d55307 | 76 | /** Initialize the flash peripheral and the flash_t object |
AnnaBridge | 167:e84263d55307 | 77 | * |
AnnaBridge | 167:e84263d55307 | 78 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 79 | * @return 0 for success, -1 for error |
AnnaBridge | 167:e84263d55307 | 80 | */ |
AnnaBridge | 167:e84263d55307 | 81 | int32_t flash_init(flash_t *obj) |
AnnaBridge | 167:e84263d55307 | 82 | { |
AnnaBridge | 167:e84263d55307 | 83 | /* Unlock the Flash to enable the flash control register access *************/ |
AnnaBridge | 167:e84263d55307 | 84 | HAL_FLASH_Unlock(); |
AnnaBridge | 167:e84263d55307 | 85 | return 0; |
AnnaBridge | 167:e84263d55307 | 86 | } |
AnnaBridge | 167:e84263d55307 | 87 | |
AnnaBridge | 167:e84263d55307 | 88 | /** Uninitialize the flash peripheral and the flash_t object |
AnnaBridge | 167:e84263d55307 | 89 | * |
AnnaBridge | 167:e84263d55307 | 90 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 91 | * @return 0 for success, -1 for error |
AnnaBridge | 167:e84263d55307 | 92 | */ |
AnnaBridge | 167:e84263d55307 | 93 | int32_t flash_free(flash_t *obj) |
AnnaBridge | 167:e84263d55307 | 94 | { |
AnnaBridge | 167:e84263d55307 | 95 | /* Lock the Flash to disable the flash control register access (recommended |
AnnaBridge | 167:e84263d55307 | 96 | * to protect the FLASH memory against possible unwanted operation) *********/ |
AnnaBridge | 167:e84263d55307 | 97 | HAL_FLASH_Lock(); |
AnnaBridge | 167:e84263d55307 | 98 | return 0; |
AnnaBridge | 167:e84263d55307 | 99 | } |
AnnaBridge | 167:e84263d55307 | 100 | |
AnnaBridge | 167:e84263d55307 | 101 | /** Erase one sector starting at defined address |
AnnaBridge | 167:e84263d55307 | 102 | * |
AnnaBridge | 167:e84263d55307 | 103 | * The address should be at sector boundary. This function does not do any check for address alignments |
AnnaBridge | 167:e84263d55307 | 104 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 105 | * @param address The sector starting address |
AnnaBridge | 167:e84263d55307 | 106 | * @return 0 for success, -1 for error |
AnnaBridge | 167:e84263d55307 | 107 | */ |
AnnaBridge | 167:e84263d55307 | 108 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
AnnaBridge | 167:e84263d55307 | 109 | { |
AnnaBridge | 167:e84263d55307 | 110 | uint32_t FirstPage = 0, BankNumber = 0; |
AnnaBridge | 167:e84263d55307 | 111 | uint32_t PAGEError = 0; |
AnnaBridge | 167:e84263d55307 | 112 | FLASH_EraseInitTypeDef EraseInitStruct; |
AnnaBridge | 167:e84263d55307 | 113 | |
AnnaBridge | 167:e84263d55307 | 114 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 167:e84263d55307 | 115 | |
AnnaBridge | 167:e84263d55307 | 116 | return -1; |
AnnaBridge | 167:e84263d55307 | 117 | } |
AnnaBridge | 167:e84263d55307 | 118 | |
AnnaBridge | 167:e84263d55307 | 119 | /* Clear OPTVERR bit set on virgin samples */ |
AnnaBridge | 167:e84263d55307 | 120 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); |
AnnaBridge | 167:e84263d55307 | 121 | /* Get the 1st page to erase */ |
AnnaBridge | 167:e84263d55307 | 122 | FirstPage = GetPage(address); |
AnnaBridge | 167:e84263d55307 | 123 | /* MBED HAL erases 1 page / sector at a time */ |
AnnaBridge | 167:e84263d55307 | 124 | /* Get the bank */ |
AnnaBridge | 167:e84263d55307 | 125 | BankNumber = GetBank(address); |
AnnaBridge | 167:e84263d55307 | 126 | /* Fill EraseInit structure*/ |
AnnaBridge | 167:e84263d55307 | 127 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
AnnaBridge | 167:e84263d55307 | 128 | EraseInitStruct.Banks = BankNumber; |
AnnaBridge | 167:e84263d55307 | 129 | EraseInitStruct.Page = FirstPage; |
AnnaBridge | 167:e84263d55307 | 130 | EraseInitStruct.NbPages = 1; |
AnnaBridge | 167:e84263d55307 | 131 | |
AnnaBridge | 167:e84263d55307 | 132 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
AnnaBridge | 167:e84263d55307 | 133 | you have to make sure that these data are rewritten before they are accessed during code |
AnnaBridge | 167:e84263d55307 | 134 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
AnnaBridge | 167:e84263d55307 | 135 | DCRST and ICRST bits in the FLASH_CR register. */ |
AnnaBridge | 167:e84263d55307 | 136 | |
AnnaBridge | 167:e84263d55307 | 137 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { |
AnnaBridge | 167:e84263d55307 | 138 | return -1; |
AnnaBridge | 167:e84263d55307 | 139 | } else { |
AnnaBridge | 167:e84263d55307 | 140 | return 0; |
AnnaBridge | 167:e84263d55307 | 141 | } |
AnnaBridge | 167:e84263d55307 | 142 | } |
AnnaBridge | 167:e84263d55307 | 143 | |
AnnaBridge | 167:e84263d55307 | 144 | /** Program one page starting at defined address |
AnnaBridge | 167:e84263d55307 | 145 | * |
AnnaBridge | 167:e84263d55307 | 146 | * The page should be at page boundary, should not cross multiple sectors. |
AnnaBridge | 167:e84263d55307 | 147 | * This function does not do any check for address alignments or if size |
AnnaBridge | 167:e84263d55307 | 148 | * is aligned to a page size. |
AnnaBridge | 167:e84263d55307 | 149 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 150 | * @param address The sector starting address |
AnnaBridge | 167:e84263d55307 | 151 | * @param data The data buffer to be programmed |
AnnaBridge | 167:e84263d55307 | 152 | * @param size The number of bytes to program |
AnnaBridge | 167:e84263d55307 | 153 | * @return 0 for success, -1 for error |
AnnaBridge | 167:e84263d55307 | 154 | */ |
AnnaBridge | 167:e84263d55307 | 155 | int32_t flash_program_page(flash_t *obj, uint32_t address, |
AnnaBridge | 167:e84263d55307 | 156 | const uint8_t *data, uint32_t size) |
AnnaBridge | 167:e84263d55307 | 157 | { |
AnnaBridge | 167:e84263d55307 | 158 | uint32_t StartAddress = 0; |
AnnaBridge | 167:e84263d55307 | 159 | |
AnnaBridge | 167:e84263d55307 | 160 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 167:e84263d55307 | 161 | return -1; |
AnnaBridge | 167:e84263d55307 | 162 | } |
AnnaBridge | 167:e84263d55307 | 163 | |
AnnaBridge | 167:e84263d55307 | 164 | if ((size % 8) != 0) { |
AnnaBridge | 167:e84263d55307 | 165 | /* L4 flash devices can only be programmed 64bits/8 bytes at a time */ |
AnnaBridge | 167:e84263d55307 | 166 | return -1; |
AnnaBridge | 167:e84263d55307 | 167 | } |
AnnaBridge | 167:e84263d55307 | 168 | |
AnnaBridge | 167:e84263d55307 | 169 | /* Program the user Flash area word by word */ |
AnnaBridge | 167:e84263d55307 | 170 | StartAddress = address; |
AnnaBridge | 167:e84263d55307 | 171 | |
AnnaBridge | 167:e84263d55307 | 172 | /* HW needs an aligned address to program flash, which data |
AnnaBridge | 167:e84263d55307 | 173 | * parameters doesn't ensure */ |
AnnaBridge | 167:e84263d55307 | 174 | if ((uint32_t) data % 4 != 0) { |
AnnaBridge | 167:e84263d55307 | 175 | volatile uint64_t data64; |
AnnaBridge | 167:e84263d55307 | 176 | while (address < (StartAddress + size)) { |
AnnaBridge | 167:e84263d55307 | 177 | for (uint8_t i =0; i < 8; i++) { |
AnnaBridge | 167:e84263d55307 | 178 | *(((uint8_t *) &data64) + i) = *(data + i); |
AnnaBridge | 167:e84263d55307 | 179 | } |
AnnaBridge | 167:e84263d55307 | 180 | |
AnnaBridge | 167:e84263d55307 | 181 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64) == HAL_OK) { |
AnnaBridge | 167:e84263d55307 | 182 | address = address + 8; |
AnnaBridge | 167:e84263d55307 | 183 | data = data + 8; |
AnnaBridge | 167:e84263d55307 | 184 | } else { |
AnnaBridge | 167:e84263d55307 | 185 | return -1; |
AnnaBridge | 167:e84263d55307 | 186 | } |
AnnaBridge | 167:e84263d55307 | 187 | } |
AnnaBridge | 167:e84263d55307 | 188 | } else { /* case where data is aligned, so let's avoid any copy */ |
AnnaBridge | 167:e84263d55307 | 189 | while (address < (StartAddress + size)) { |
AnnaBridge | 167:e84263d55307 | 190 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, *((uint64_t*) data)) == HAL_OK) { |
AnnaBridge | 167:e84263d55307 | 191 | address = address + 8; |
AnnaBridge | 167:e84263d55307 | 192 | data = data + 8; |
AnnaBridge | 167:e84263d55307 | 193 | } else { |
AnnaBridge | 167:e84263d55307 | 194 | return -1; |
AnnaBridge | 167:e84263d55307 | 195 | } |
AnnaBridge | 167:e84263d55307 | 196 | } |
AnnaBridge | 167:e84263d55307 | 197 | } |
AnnaBridge | 167:e84263d55307 | 198 | |
AnnaBridge | 167:e84263d55307 | 199 | return 0; |
AnnaBridge | 167:e84263d55307 | 200 | } |
AnnaBridge | 167:e84263d55307 | 201 | |
AnnaBridge | 167:e84263d55307 | 202 | /** Get sector size |
AnnaBridge | 167:e84263d55307 | 203 | * |
AnnaBridge | 167:e84263d55307 | 204 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 205 | * @param address The sector starting address |
AnnaBridge | 167:e84263d55307 | 206 | * @return The size of a sector |
AnnaBridge | 167:e84263d55307 | 207 | */ |
AnnaBridge | 167:e84263d55307 | 208 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) { |
AnnaBridge | 167:e84263d55307 | 209 | /* considering 1 sector = 1 page */ |
AnnaBridge | 167:e84263d55307 | 210 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 167:e84263d55307 | 211 | return MBED_FLASH_INVALID_SIZE; |
AnnaBridge | 167:e84263d55307 | 212 | } else { |
AnnaBridge | 167:e84263d55307 | 213 | return FLASH_PAGE_SIZE; |
AnnaBridge | 167:e84263d55307 | 214 | } |
AnnaBridge | 167:e84263d55307 | 215 | } |
AnnaBridge | 167:e84263d55307 | 216 | |
AnnaBridge | 167:e84263d55307 | 217 | /** Get page size |
AnnaBridge | 167:e84263d55307 | 218 | * |
AnnaBridge | 167:e84263d55307 | 219 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 220 | * @param address The page starting address |
AnnaBridge | 167:e84263d55307 | 221 | * @return The size of a page |
AnnaBridge | 167:e84263d55307 | 222 | */ |
AnnaBridge | 167:e84263d55307 | 223 | uint32_t flash_get_page_size(const flash_t *obj) { |
AnnaBridge | 175:b96e65c34a4d | 224 | /* Page size is the minimum programable size, which 8 bytes */ |
AnnaBridge | 175:b96e65c34a4d | 225 | return 8; |
AnnaBridge | 167:e84263d55307 | 226 | } |
AnnaBridge | 167:e84263d55307 | 227 | |
AnnaBridge | 167:e84263d55307 | 228 | /** Get start address for the flash region |
AnnaBridge | 167:e84263d55307 | 229 | * |
AnnaBridge | 167:e84263d55307 | 230 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 231 | * @return The start address for the flash region |
AnnaBridge | 167:e84263d55307 | 232 | */ |
AnnaBridge | 167:e84263d55307 | 233 | uint32_t flash_get_start_address(const flash_t *obj) { |
AnnaBridge | 167:e84263d55307 | 234 | return FLASH_BASE; |
AnnaBridge | 167:e84263d55307 | 235 | } |
AnnaBridge | 167:e84263d55307 | 236 | |
AnnaBridge | 167:e84263d55307 | 237 | /** Get the flash region size |
AnnaBridge | 167:e84263d55307 | 238 | * |
AnnaBridge | 167:e84263d55307 | 239 | * @param obj The flash object |
AnnaBridge | 167:e84263d55307 | 240 | * @return The flash region size |
AnnaBridge | 167:e84263d55307 | 241 | */ |
AnnaBridge | 167:e84263d55307 | 242 | uint32_t flash_get_size(const flash_t *obj) { |
AnnaBridge | 167:e84263d55307 | 243 | return FLASH_SIZE; |
AnnaBridge | 167:e84263d55307 | 244 | } |
AnnaBridge | 167:e84263d55307 | 245 | |
AnnaBridge | 167:e84263d55307 | 246 | #endif |