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