mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Oct 11 12:45:49 2017 +0100
Revision:
175:af195413fb11
Parent:
174:b96e65c34a4d
Child:
177:d650f5d4c87a
This updates the lib to the mbed lib v 153

Who changed what in which revision?

UserRevisionLine numberNew 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 /* L0 targets embed 32 pages sectors */
AnnaBridge 167:e84263d55307 25 #define NUM_PAGES_IN_SECTOR 32
AnnaBridge 167:e84263d55307 26
AnnaBridge 167:e84263d55307 27 int32_t flash_init(flash_t *obj)
AnnaBridge 167:e84263d55307 28 {
AnnaBridge 167:e84263d55307 29 return 0;
AnnaBridge 167:e84263d55307 30 }
AnnaBridge 167:e84263d55307 31
AnnaBridge 167:e84263d55307 32 int32_t flash_free(flash_t *obj)
AnnaBridge 167:e84263d55307 33 {
AnnaBridge 167:e84263d55307 34 return 0;
AnnaBridge 167:e84263d55307 35 }
AnnaBridge 167:e84263d55307 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
AnnaBridge 167:e84263d55307 58 int32_t flash_erase_sector(flash_t *obj, uint32_t address)
AnnaBridge 167:e84263d55307 59 {
AnnaBridge 167:e84263d55307 60 uint32_t FirstPage = 0;
AnnaBridge 167:e84263d55307 61 uint32_t PAGEError = 0;
AnnaBridge 167:e84263d55307 62 FLASH_EraseInitTypeDef EraseInitStruct;
AnnaBridge 175:af195413fb11 63 int32_t status = 0;
AnnaBridge 167:e84263d55307 64
AnnaBridge 167:e84263d55307 65 if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
AnnaBridge 167:e84263d55307 66
AnnaBridge 167:e84263d55307 67 return -1;
AnnaBridge 167:e84263d55307 68 }
AnnaBridge 167:e84263d55307 69
AnnaBridge 175:af195413fb11 70 if (flash_unlock() != HAL_OK) {
AnnaBridge 175:af195413fb11 71 return -1;
AnnaBridge 175:af195413fb11 72 }
AnnaBridge 175:af195413fb11 73
AnnaBridge 167:e84263d55307 74 /* Clear OPTVERR bit set on virgin samples */
AnnaBridge 167:e84263d55307 75 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
AnnaBridge 167:e84263d55307 76
AnnaBridge 167:e84263d55307 77 /* MBED HAL erases 1 sector at a time */
AnnaBridge 167:e84263d55307 78 /* Fill EraseInit structure*/
AnnaBridge 167:e84263d55307 79 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
AnnaBridge 167:e84263d55307 80 EraseInitStruct.PageAddress = address;
AnnaBridge 167:e84263d55307 81 EraseInitStruct.NbPages = NUM_PAGES_IN_SECTOR;
AnnaBridge 167:e84263d55307 82
AnnaBridge 167:e84263d55307 83 /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
AnnaBridge 167:e84263d55307 84 you have to make sure that these data are rewritten before they are accessed during code
AnnaBridge 167:e84263d55307 85 execution. If this cannot be done safely, it is recommended to flush the caches by setting the
AnnaBridge 167:e84263d55307 86 DCRST and ICRST bits in the FLASH_CR register. */
AnnaBridge 167:e84263d55307 87
AnnaBridge 167:e84263d55307 88 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
AnnaBridge 175:af195413fb11 89 status = -1;
AnnaBridge 167:e84263d55307 90 }
AnnaBridge 175:af195413fb11 91
AnnaBridge 175:af195413fb11 92 flash_lock();
AnnaBridge 175:af195413fb11 93
AnnaBridge 175:af195413fb11 94 return status;
AnnaBridge 175:af195413fb11 95
AnnaBridge 167:e84263d55307 96 }
AnnaBridge 167:e84263d55307 97
AnnaBridge 167:e84263d55307 98 int32_t flash_program_page(flash_t *obj, uint32_t address,
AnnaBridge 167:e84263d55307 99 const uint8_t *data, uint32_t size)
AnnaBridge 167:e84263d55307 100 {
AnnaBridge 167:e84263d55307 101 uint32_t StartAddress = 0;
AnnaBridge 175:af195413fb11 102 int32_t status = 0;
AnnaBridge 167:e84263d55307 103
AnnaBridge 167:e84263d55307 104 if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
AnnaBridge 167:e84263d55307 105 return -1;
AnnaBridge 167:e84263d55307 106 }
AnnaBridge 167:e84263d55307 107
AnnaBridge 167:e84263d55307 108 if ((size % 4) != 0) {
AnnaBridge 167:e84263d55307 109 /* L4 flash devices can only be programmed 64bits/8 bytes at a time */
AnnaBridge 167:e84263d55307 110 return -1;
AnnaBridge 167:e84263d55307 111 }
AnnaBridge 167:e84263d55307 112
AnnaBridge 175:af195413fb11 113 if (flash_unlock() != HAL_OK) {
AnnaBridge 175:af195413fb11 114 return -1;
AnnaBridge 175:af195413fb11 115 }
AnnaBridge 175:af195413fb11 116
AnnaBridge 167:e84263d55307 117 /* Program the user Flash area word by word */
AnnaBridge 167:e84263d55307 118 StartAddress = address;
AnnaBridge 167:e84263d55307 119
AnnaBridge 167:e84263d55307 120 /* HW needs an aligned address to program flash, which data
AnnaBridge 167:e84263d55307 121 * parameters doesn't ensure */
AnnaBridge 167:e84263d55307 122 if ((uint32_t) data % 4 != 0) {
AnnaBridge 167:e84263d55307 123 volatile uint32_t data32;
AnnaBridge 175:af195413fb11 124 while ((address < (StartAddress + size)) && (status == 0)) {
AnnaBridge 167:e84263d55307 125 for (uint8_t i =0; i < 4; i++) {
AnnaBridge 167:e84263d55307 126 *(((uint8_t *) &data32) + i) = *(data + i);
AnnaBridge 167:e84263d55307 127 }
AnnaBridge 167:e84263d55307 128
AnnaBridge 167:e84263d55307 129 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) {
AnnaBridge 167:e84263d55307 130 address = address + 4;
AnnaBridge 167:e84263d55307 131 data = data + 4;
AnnaBridge 167:e84263d55307 132 } else {
AnnaBridge 175:af195413fb11 133 status = -1;
AnnaBridge 167:e84263d55307 134 }
AnnaBridge 167:e84263d55307 135 }
AnnaBridge 167:e84263d55307 136 } else { /* case where data is aligned, so let's avoid any copy */
AnnaBridge 175:af195413fb11 137 while ((address < (StartAddress + size)) && (status == 0)) {
AnnaBridge 167:e84263d55307 138 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) {
AnnaBridge 167:e84263d55307 139 address = address + 4;
AnnaBridge 167:e84263d55307 140 data = data + 4;
AnnaBridge 167:e84263d55307 141 } else {
AnnaBridge 175:af195413fb11 142 status = -1;
AnnaBridge 167:e84263d55307 143 }
AnnaBridge 167:e84263d55307 144 }
AnnaBridge 167:e84263d55307 145 }
AnnaBridge 167:e84263d55307 146
AnnaBridge 175:af195413fb11 147 flash_lock();
AnnaBridge 175:af195413fb11 148
AnnaBridge 175:af195413fb11 149 return status;
AnnaBridge 167:e84263d55307 150 }
AnnaBridge 167:e84263d55307 151
AnnaBridge 167:e84263d55307 152 uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) {
AnnaBridge 167:e84263d55307 153 if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
AnnaBridge 167:e84263d55307 154 return MBED_FLASH_INVALID_SIZE;
AnnaBridge 167:e84263d55307 155 } else {
AnnaBridge 168:9672193075cf 156 /* sector composed of N pages */
AnnaBridge 167:e84263d55307 157 return (NUM_PAGES_IN_SECTOR * FLASH_PAGE_SIZE);
AnnaBridge 167:e84263d55307 158 }
AnnaBridge 167:e84263d55307 159 }
AnnaBridge 167:e84263d55307 160
AnnaBridge 167:e84263d55307 161 uint32_t flash_get_page_size(const flash_t *obj) {
AnnaBridge 174:b96e65c34a4d 162 /* Page size is the minimum programable size, which 4 bytes */
AnnaBridge 174:b96e65c34a4d 163 return 4;
AnnaBridge 167:e84263d55307 164 }
AnnaBridge 167:e84263d55307 165
AnnaBridge 167:e84263d55307 166 uint32_t flash_get_start_address(const flash_t *obj) {
AnnaBridge 167:e84263d55307 167 return FLASH_BASE;
AnnaBridge 167:e84263d55307 168 }
AnnaBridge 167:e84263d55307 169
AnnaBridge 167:e84263d55307 170 uint32_t flash_get_size(const flash_t *obj) {
AnnaBridge 167:e84263d55307 171 return FLASH_SIZE;
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 #endif