mbed library sources. Supersedes mbed-src.

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