mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L1/flash_api.c@174:b96e65c34a4d, 2017-10-02 (annotated)
- Committer:
- AnnaBridge
- Date:
- Mon Oct 02 15:33:19 2017 +0100
- Revision:
- 174:b96e65c34a4d
- Parent:
- 170:19eb464bc2be
- Child:
- 175: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 |
---|---|---|---|
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 | /* Unlock the Flash to enable the flash control register access *************/ |
Kojto | 170:19eb464bc2be | 30 | HAL_FLASH_Unlock(); |
Kojto | 170:19eb464bc2be | 31 | return 0; |
Kojto | 170:19eb464bc2be | 32 | } |
Kojto | 170:19eb464bc2be | 33 | |
Kojto | 170:19eb464bc2be | 34 | int32_t flash_free(flash_t *obj) |
Kojto | 170:19eb464bc2be | 35 | { |
Kojto | 170:19eb464bc2be | 36 | /* Lock the Flash to disable the flash control register access (recommended |
Kojto | 170:19eb464bc2be | 37 | * to protect the FLASH memory against possible unwanted operation) *********/ |
Kojto | 170:19eb464bc2be | 38 | HAL_FLASH_Lock(); |
Kojto | 170:19eb464bc2be | 39 | return 0; |
Kojto | 170:19eb464bc2be | 40 | } |
Kojto | 170:19eb464bc2be | 41 | |
Kojto | 170:19eb464bc2be | 42 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
Kojto | 170:19eb464bc2be | 43 | { |
Kojto | 170:19eb464bc2be | 44 | uint32_t FirstPage = 0; |
Kojto | 170:19eb464bc2be | 45 | uint32_t PAGEError = 0; |
Kojto | 170:19eb464bc2be | 46 | FLASH_EraseInitTypeDef EraseInitStruct; |
Kojto | 170:19eb464bc2be | 47 | |
Kojto | 170:19eb464bc2be | 48 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 49 | |
Kojto | 170:19eb464bc2be | 50 | return -1; |
Kojto | 170:19eb464bc2be | 51 | } |
Kojto | 170:19eb464bc2be | 52 | |
Kojto | 170:19eb464bc2be | 53 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR | FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR); |
Kojto | 170:19eb464bc2be | 54 | /* MBED HAL erases 1 sector at a time */ |
Kojto | 170:19eb464bc2be | 55 | /* Fill EraseInit structure*/ |
Kojto | 170:19eb464bc2be | 56 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
Kojto | 170:19eb464bc2be | 57 | EraseInitStruct.PageAddress = address; |
Kojto | 170:19eb464bc2be | 58 | EraseInitStruct.NbPages = NUM_PAGES_IN_SECTOR; |
Kojto | 170:19eb464bc2be | 59 | |
Kojto | 170:19eb464bc2be | 60 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
Kojto | 170:19eb464bc2be | 61 | you have to make sure that these data are rewritten before they are accessed during code |
Kojto | 170:19eb464bc2be | 62 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
Kojto | 170:19eb464bc2be | 63 | DCRST and ICRST bits in the FLASH_CR register. */ |
Kojto | 170:19eb464bc2be | 64 | |
Kojto | 170:19eb464bc2be | 65 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { |
Kojto | 170:19eb464bc2be | 66 | return -1; |
Kojto | 170:19eb464bc2be | 67 | } else { |
Kojto | 170:19eb464bc2be | 68 | return 0; |
Kojto | 170:19eb464bc2be | 69 | } |
Kojto | 170:19eb464bc2be | 70 | } |
Kojto | 170:19eb464bc2be | 71 | |
Kojto | 170:19eb464bc2be | 72 | int32_t flash_program_page(flash_t *obj, uint32_t address, |
Kojto | 170:19eb464bc2be | 73 | const uint8_t *data, uint32_t size) |
Kojto | 170:19eb464bc2be | 74 | { |
Kojto | 170:19eb464bc2be | 75 | uint32_t StartAddress = 0; |
Kojto | 170:19eb464bc2be | 76 | |
Kojto | 170:19eb464bc2be | 77 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 78 | return -1; |
Kojto | 170:19eb464bc2be | 79 | } |
Kojto | 170:19eb464bc2be | 80 | |
Kojto | 170:19eb464bc2be | 81 | if ((size % 4) != 0) { |
Kojto | 170:19eb464bc2be | 82 | /* L1 flash devices can only be programmed 32bits/4 bytes at a time */ |
Kojto | 170:19eb464bc2be | 83 | return -1; |
Kojto | 170:19eb464bc2be | 84 | } |
Kojto | 170:19eb464bc2be | 85 | |
Kojto | 170:19eb464bc2be | 86 | /* Program the user Flash area word by word */ |
Kojto | 170:19eb464bc2be | 87 | StartAddress = address; |
Kojto | 170:19eb464bc2be | 88 | |
Kojto | 170:19eb464bc2be | 89 | /* HW needs an aligned address to program flash, which data |
Kojto | 170:19eb464bc2be | 90 | * parameters doesn't ensure */ |
Kojto | 170:19eb464bc2be | 91 | if ((uint32_t) data % 4 != 0) { |
Kojto | 170:19eb464bc2be | 92 | volatile uint32_t data32; |
Kojto | 170:19eb464bc2be | 93 | while (address < (StartAddress + size)) { |
Kojto | 170:19eb464bc2be | 94 | for (uint8_t i =0; i < 4; i++) { |
Kojto | 170:19eb464bc2be | 95 | *(((uint8_t *) &data32) + i) = *(data + i); |
Kojto | 170:19eb464bc2be | 96 | } |
Kojto | 170:19eb464bc2be | 97 | |
Kojto | 170:19eb464bc2be | 98 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) { |
Kojto | 170:19eb464bc2be | 99 | address = address + 4; |
Kojto | 170:19eb464bc2be | 100 | data = data + 4; |
Kojto | 170:19eb464bc2be | 101 | } else { |
Kojto | 170:19eb464bc2be | 102 | return -1; |
Kojto | 170:19eb464bc2be | 103 | } |
Kojto | 170:19eb464bc2be | 104 | } |
Kojto | 170:19eb464bc2be | 105 | } else { /* case where data is aligned, so let's avoid any copy */ |
Kojto | 170:19eb464bc2be | 106 | while (address < (StartAddress + size)) { |
Kojto | 170:19eb464bc2be | 107 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) { |
Kojto | 170:19eb464bc2be | 108 | address = address + 4; |
Kojto | 170:19eb464bc2be | 109 | data = data + 4; |
Kojto | 170:19eb464bc2be | 110 | } else { |
Kojto | 170:19eb464bc2be | 111 | return -1; |
Kojto | 170:19eb464bc2be | 112 | } |
Kojto | 170:19eb464bc2be | 113 | } |
Kojto | 170:19eb464bc2be | 114 | } |
Kojto | 170:19eb464bc2be | 115 | |
Kojto | 170:19eb464bc2be | 116 | return 0; |
Kojto | 170:19eb464bc2be | 117 | } |
Kojto | 170:19eb464bc2be | 118 | |
Kojto | 170:19eb464bc2be | 119 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
Kojto | 170:19eb464bc2be | 120 | { |
Kojto | 170:19eb464bc2be | 121 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
Kojto | 170:19eb464bc2be | 122 | return MBED_FLASH_INVALID_SIZE; |
Kojto | 170:19eb464bc2be | 123 | } else { |
Kojto | 170:19eb464bc2be | 124 | return (NUM_PAGES_IN_SECTOR * FLASH_PAGE_SIZE); |
Kojto | 170:19eb464bc2be | 125 | } |
Kojto | 170:19eb464bc2be | 126 | } |
Kojto | 170:19eb464bc2be | 127 | |
Kojto | 170:19eb464bc2be | 128 | uint32_t flash_get_page_size(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 129 | { |
AnnaBridge | 174:b96e65c34a4d | 130 | /* Page size is the minimum programable size, which 4 bytes */ |
AnnaBridge | 174:b96e65c34a4d | 131 | return 4; |
Kojto | 170:19eb464bc2be | 132 | } |
Kojto | 170:19eb464bc2be | 133 | |
Kojto | 170:19eb464bc2be | 134 | uint32_t flash_get_start_address(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 135 | { |
Kojto | 170:19eb464bc2be | 136 | return FLASH_BASE; |
Kojto | 170:19eb464bc2be | 137 | } |
Kojto | 170:19eb464bc2be | 138 | |
Kojto | 170:19eb464bc2be | 139 | uint32_t flash_get_size(const flash_t *obj) |
Kojto | 170:19eb464bc2be | 140 | { |
Kojto | 170:19eb464bc2be | 141 | return FLASH_SIZE; |
Kojto | 170:19eb464bc2be | 142 | } |
Kojto | 170:19eb464bc2be | 143 | |
Kojto | 170:19eb464bc2be | 144 | #endif |