mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L1/flash_api.c@181:96ed750bd169, 2018-01-17 (annotated)
- Committer:
- Anna Bridge
- Date:
- Wed Jan 17 15:23:54 2018 +0000
- Revision:
- 181:96ed750bd169
- Parent:
- 177:d650f5d4c87a
mbed-dev libray. Release version 158
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 170:19eb464bc2be | 1 | /* mbed Microcontroller Library |
Kojto | 170:19eb464bc2be | 2 | * Copyright (c) 2017 ARM Limited |
Kojto | 170:19eb464bc2be | 3 | * |
Kojto | 170:19eb464bc2be | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 170:19eb464bc2be | 5 | * you may not use this file except in compliance with the License. |
Kojto | 170:19eb464bc2be | 6 | * You may obtain a copy of the License at |
Kojto | 170:19eb464bc2be | 7 | * |
Kojto | 170:19eb464bc2be | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 170:19eb464bc2be | 9 | * |
Kojto | 170:19eb464bc2be | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 170:19eb464bc2be | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 170:19eb464bc2be | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 170:19eb464bc2be | 13 | * See the License for the specific language governing permissions and |
Kojto | 170:19eb464bc2be | 14 | * limitations under the License. |
Kojto | 170:19eb464bc2be | 15 | */ |
Kojto | 170:19eb464bc2be | 16 | |
Kojto | 170:19eb464bc2be | 17 | #include "flash_api.h" |
Kojto | 170:19eb464bc2be | 18 | #include "mbed_critical.h" |
Kojto | 170:19eb464bc2be | 19 | |
Kojto | 170:19eb464bc2be | 20 | #if DEVICE_FLASH |
Kojto | 170:19eb464bc2be | 21 | #include "mbed_assert.h" |
Kojto | 170:19eb464bc2be | 22 | #include "cmsis.h" |
Kojto | 170:19eb464bc2be | 23 | |
Kojto | 170:19eb464bc2be | 24 | /* L1 targets embed 16 pages sectors */ |
Kojto | 170:19eb464bc2be | 25 | #define NUM_PAGES_IN_SECTOR 16 |
Kojto | 170:19eb464bc2be | 26 | |
Kojto | 170:19eb464bc2be | 27 | int32_t flash_init(flash_t *obj) |
Kojto | 170:19eb464bc2be | 28 | { |
Kojto | 170:19eb464bc2be | 29 | return 0; |
Kojto | 170:19eb464bc2be | 30 | } |
Kojto | 170:19eb464bc2be | 31 | |
Kojto | 170:19eb464bc2be | 32 | int32_t flash_free(flash_t *obj) |
Kojto | 170:19eb464bc2be | 33 | { |
Kojto | 170:19eb464bc2be | 34 | return 0; |
Kojto | 170:19eb464bc2be | 35 | } |
Kojto | 170:19eb464bc2be | 36 | |
AnnaBridge | 175:af195413fb11 | 37 | static int32_t flash_unlock(void) |
AnnaBridge | 175:af195413fb11 | 38 | { |
AnnaBridge | 175:af195413fb11 | 39 | /* Allow Access to Flash control registers and user Falsh */ |
AnnaBridge | 175:af195413fb11 | 40 | if (HAL_FLASH_Unlock()) { |
AnnaBridge | 175:af195413fb11 | 41 | return -1; |
AnnaBridge | 175:af195413fb11 | 42 | } else { |
AnnaBridge | 175:af195413fb11 | 43 | return 0; |
AnnaBridge | 175:af195413fb11 | 44 | } |
AnnaBridge | 175:af195413fb11 | 45 | } |
AnnaBridge | 175:af195413fb11 | 46 | |
AnnaBridge | 175:af195413fb11 | 47 | static int32_t flash_lock(void) |
AnnaBridge | 175:af195413fb11 | 48 | { |
AnnaBridge | 175:af195413fb11 | 49 | /* Disable the Flash option control register access (recommended to protect |
AnnaBridge | 175:af195413fb11 | 50 | the option Bytes against possible unwanted operations) */ |
AnnaBridge | 175:af195413fb11 | 51 | if (HAL_FLASH_Lock()) { |
AnnaBridge | 175:af195413fb11 | 52 | return -1; |
AnnaBridge | 175:af195413fb11 | 53 | } else { |
AnnaBridge | 175:af195413fb11 | 54 | return 0; |
AnnaBridge | 175:af195413fb11 | 55 | } |
AnnaBridge | 175:af195413fb11 | 56 | } |
AnnaBridge | 175:af195413fb11 | 57 | |
Kojto | 170:19eb464bc2be | 58 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
Kojto | 170:19eb464bc2be | 59 | { |
Kojto | 170:19eb464bc2be | 60 | uint32_t PAGEError = 0; |
Kojto | 170:19eb464bc2be | 61 | FLASH_EraseInitTypeDef EraseInitStruct; |
AnnaBridge | 175:af195413fb11 | 62 | int32_t status = 0; |
Kojto | 170:19eb464bc2be | 63 | |
Kojto | 170:19eb464bc2be | 64 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 65 | |
Kojto | 170:19eb464bc2be | 66 | return -1; |
Kojto | 170:19eb464bc2be | 67 | } |
Kojto | 170:19eb464bc2be | 68 | |
AnnaBridge | 175:af195413fb11 | 69 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 70 | return -1; |
AnnaBridge | 175:af195413fb11 | 71 | } |
AnnaBridge | 175:af195413fb11 | 72 | |
Kojto | 170:19eb464bc2be | 73 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR | FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR); |
Kojto | 170:19eb464bc2be | 74 | /* MBED HAL erases 1 sector at a time */ |
Kojto | 170:19eb464bc2be | 75 | /* Fill EraseInit structure*/ |
Kojto | 170:19eb464bc2be | 76 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
Kojto | 170:19eb464bc2be | 77 | EraseInitStruct.PageAddress = address; |
Kojto | 170:19eb464bc2be | 78 | EraseInitStruct.NbPages = NUM_PAGES_IN_SECTOR; |
Kojto | 170:19eb464bc2be | 79 | |
Kojto | 170:19eb464bc2be | 80 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
Kojto | 170:19eb464bc2be | 81 | you have to make sure that these data are rewritten before they are accessed during code |
Kojto | 170:19eb464bc2be | 82 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
Kojto | 170:19eb464bc2be | 83 | DCRST and ICRST bits in the FLASH_CR register. */ |
Kojto | 170:19eb464bc2be | 84 | |
Kojto | 170:19eb464bc2be | 85 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 86 | status = -1; |
Kojto | 170:19eb464bc2be | 87 | } |
AnnaBridge | 175:af195413fb11 | 88 | |
AnnaBridge | 175:af195413fb11 | 89 | flash_lock(); |
AnnaBridge | 175:af195413fb11 | 90 | |
AnnaBridge | 175:af195413fb11 | 91 | return status; |
AnnaBridge | 175:af195413fb11 | 92 | |
Kojto | 170:19eb464bc2be | 93 | } |
Kojto | 170:19eb464bc2be | 94 | |
Kojto | 170:19eb464bc2be | 95 | int32_t flash_program_page(flash_t *obj, uint32_t address, |
Kojto | 170:19eb464bc2be | 96 | const uint8_t *data, uint32_t size) |
Kojto | 170:19eb464bc2be | 97 | { |
Kojto | 170:19eb464bc2be | 98 | uint32_t StartAddress = 0; |
AnnaBridge | 175:af195413fb11 | 99 | int32_t status = 0; |
Kojto | 170:19eb464bc2be | 100 | |
Kojto | 170:19eb464bc2be | 101 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 102 | return -1; |
Kojto | 170:19eb464bc2be | 103 | } |
Kojto | 170:19eb464bc2be | 104 | |
Kojto | 170:19eb464bc2be | 105 | if ((size % 4) != 0) { |
Kojto | 170:19eb464bc2be | 106 | /* L1 flash devices can only be programmed 32bits/4 bytes at a time */ |
Kojto | 170:19eb464bc2be | 107 | return -1; |
Kojto | 170:19eb464bc2be | 108 | } |
Kojto | 170:19eb464bc2be | 109 | |
AnnaBridge | 175:af195413fb11 | 110 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 111 | return -1; |
AnnaBridge | 175:af195413fb11 | 112 | } |
AnnaBridge | 175:af195413fb11 | 113 | |
Kojto | 170:19eb464bc2be | 114 | /* Program the user Flash area word by word */ |
Kojto | 170:19eb464bc2be | 115 | StartAddress = address; |
Kojto | 170:19eb464bc2be | 116 | |
Kojto | 170:19eb464bc2be | 117 | /* HW needs an aligned address to program flash, which data |
Kojto | 170:19eb464bc2be | 118 | * parameters doesn't ensure */ |
Kojto | 170:19eb464bc2be | 119 | if ((uint32_t) data % 4 != 0) { |
Kojto | 170:19eb464bc2be | 120 | volatile uint32_t data32; |
AnnaBridge | 175:af195413fb11 | 121 | while (address < (StartAddress + size) && (status == 0)) { |
Kojto | 170:19eb464bc2be | 122 | for (uint8_t i =0; i < 4; i++) { |
Kojto | 170:19eb464bc2be | 123 | *(((uint8_t *) &data32) + i) = *(data + i); |
Kojto | 170:19eb464bc2be | 124 | } |
Kojto | 170:19eb464bc2be | 125 | |
Kojto | 170:19eb464bc2be | 126 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) { |
Kojto | 170:19eb464bc2be | 127 | address = address + 4; |
Kojto | 170:19eb464bc2be | 128 | data = data + 4; |
Kojto | 170:19eb464bc2be | 129 | } else { |
AnnaBridge | 175:af195413fb11 | 130 | status = -1; |
Kojto | 170:19eb464bc2be | 131 | } |
Kojto | 170:19eb464bc2be | 132 | } |
Kojto | 170:19eb464bc2be | 133 | } else { /* case where data is aligned, so let's avoid any copy */ |
AnnaBridge | 175:af195413fb11 | 134 | while ((address < (StartAddress + size)) && (status == 0)) { |
Kojto | 170:19eb464bc2be | 135 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) { |
Kojto | 170:19eb464bc2be | 136 | address = address + 4; |
Kojto | 170:19eb464bc2be | 137 | data = data + 4; |
Kojto | 170:19eb464bc2be | 138 | } else { |
AnnaBridge | 175:af195413fb11 | 139 | status = -1; |
Kojto | 170:19eb464bc2be | 140 | } |
Kojto | 170:19eb464bc2be | 141 | } |
Kojto | 170:19eb464bc2be | 142 | } |
Kojto | 170:19eb464bc2be | 143 | |
AnnaBridge | 175:af195413fb11 | 144 | flash_lock(); |
AnnaBridge | 175:af195413fb11 | 145 | |
AnnaBridge | 175:af195413fb11 | 146 | return status; |
Kojto | 170:19eb464bc2be | 147 | } |
Kojto | 170:19eb464bc2be | 148 | |
Kojto | 170:19eb464bc2be | 149 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
Kojto | 170:19eb464bc2be | 150 | { |
Kojto | 170:19eb464bc2be | 151 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 152 | return MBED_FLASH_INVALID_SIZE; |
Kojto | 170:19eb464bc2be | 153 | } else { |
Kojto | 170:19eb464bc2be | 154 | return (NUM_PAGES_IN_SECTOR * FLASH_PAGE_SIZE); |
Kojto | 170:19eb464bc2be | 155 | } |
Kojto | 170:19eb464bc2be | 156 | } |
Kojto | 170:19eb464bc2be | 157 | |
Kojto | 170:19eb464bc2be | 158 | uint32_t flash_get_page_size(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 159 | { |
AnnaBridge | 174:b96e65c34a4d | 160 | /* Page size is the minimum programable size, which 4 bytes */ |
AnnaBridge | 174:b96e65c34a4d | 161 | return 4; |
Kojto | 170:19eb464bc2be | 162 | } |
Kojto | 170:19eb464bc2be | 163 | |
Kojto | 170:19eb464bc2be | 164 | uint32_t flash_get_start_address(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 165 | { |
Kojto | 170:19eb464bc2be | 166 | return FLASH_BASE; |
Kojto | 170:19eb464bc2be | 167 | } |
Kojto | 170:19eb464bc2be | 168 | |
Kojto | 170:19eb464bc2be | 169 | uint32_t flash_get_size(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 170 | { |
Kojto | 170:19eb464bc2be | 171 | return FLASH_SIZE; |
Kojto | 170:19eb464bc2be | 172 | } |
Kojto | 170:19eb464bc2be | 173 | |
Kojto | 170:19eb464bc2be | 174 | #endif |