Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c@160:d5399cc887bb, 2017-03-14 (annotated)
- Committer:
- <>
- Date:
- Tue Mar 14 16:40:56 2017 +0000
- Revision:
- 160:d5399cc887bb
This updates the lib to the mbed lib v138
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| <> | 160:d5399cc887bb | 1 | /* mbed Microcontroller Library |
| <> | 160:d5399cc887bb | 2 | * Copyright (c) 2017 ARM Limited |
| <> | 160:d5399cc887bb | 3 | * |
| <> | 160:d5399cc887bb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| <> | 160:d5399cc887bb | 5 | * you may not use this file except in compliance with the License. |
| <> | 160:d5399cc887bb | 6 | * You may obtain a copy of the License at |
| <> | 160:d5399cc887bb | 7 | * |
| <> | 160:d5399cc887bb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| <> | 160:d5399cc887bb | 9 | * |
| <> | 160:d5399cc887bb | 10 | * Unless required by applicable law or agreed to in writing, software |
| <> | 160:d5399cc887bb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| <> | 160:d5399cc887bb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| <> | 160:d5399cc887bb | 13 | * See the License for the specific language governing permissions and |
| <> | 160:d5399cc887bb | 14 | * limitations under the License. |
| <> | 160:d5399cc887bb | 15 | */ |
| <> | 160:d5399cc887bb | 16 | |
| <> | 160:d5399cc887bb | 17 | #include "flash_api.h" |
| <> | 160:d5399cc887bb | 18 | #include "flash_data.h" |
| <> | 160:d5399cc887bb | 19 | #include "mbed_critical.h" |
| <> | 160:d5399cc887bb | 20 | |
| <> | 160:d5399cc887bb | 21 | #define MBED_FLASH_ALGO_ERASE 1UL |
| <> | 160:d5399cc887bb | 22 | #define MBED_FLASH_ALGO_PROGRAM 2UL |
| <> | 160:d5399cc887bb | 23 | |
| <> | 160:d5399cc887bb | 24 | extern uint32_t SystemCoreClock; |
| <> | 160:d5399cc887bb | 25 | |
| <> | 160:d5399cc887bb | 26 | /* |
| <> | 160:d5399cc887bb | 27 | This binary blob (thumb code) sets r9 (static base) as the code we are jumping to |
| <> | 160:d5399cc887bb | 28 | is PIC (position independent code). |
| <> | 160:d5399cc887bb | 29 | |
| <> | 160:d5399cc887bb | 30 | These are the instructions (r0 is a pointer to arg_t): |
| <> | 160:d5399cc887bb | 31 | push {r5, lr, r4} |
| <> | 160:d5399cc887bb | 32 | mov r5, r9 |
| <> | 160:d5399cc887bb | 33 | push {r5} |
| <> | 160:d5399cc887bb | 34 | ldr r5, [r0, #20] |
| <> | 160:d5399cc887bb | 35 | ldr r3, [r0, #16] |
| <> | 160:d5399cc887bb | 36 | mov r9, r3 |
| <> | 160:d5399cc887bb | 37 | ldr r3, [r0, #12] |
| <> | 160:d5399cc887bb | 38 | ldr r2, [r0, #8] |
| <> | 160:d5399cc887bb | 39 | ldr r1, [r0, #4] |
| <> | 160:d5399cc887bb | 40 | ldr r0, [r0, #0] |
| <> | 160:d5399cc887bb | 41 | blx r5 |
| <> | 160:d5399cc887bb | 42 | pop {r5} |
| <> | 160:d5399cc887bb | 43 | mov r9, r5 |
| <> | 160:d5399cc887bb | 44 | pop {r4-r5, pc} |
| <> | 160:d5399cc887bb | 45 | bx r14 |
| <> | 160:d5399cc887bb | 46 | */ |
| <> | 160:d5399cc887bb | 47 | static uint32_t jump_to_flash_algo[] = { |
| <> | 160:d5399cc887bb | 48 | 0x464DB530, |
| <> | 160:d5399cc887bb | 49 | 0x6945B420, |
| <> | 160:d5399cc887bb | 50 | 0x46996903, |
| <> | 160:d5399cc887bb | 51 | 0x688268C3, |
| <> | 160:d5399cc887bb | 52 | 0x68006841, |
| <> | 160:d5399cc887bb | 53 | 0xBC2047A8, |
| <> | 160:d5399cc887bb | 54 | 0xBD3046A9 |
| <> | 160:d5399cc887bb | 55 | }; |
| <> | 160:d5399cc887bb | 56 | |
| <> | 160:d5399cc887bb | 57 | // should be called within critical section |
| <> | 160:d5399cc887bb | 58 | static int32_t flash_algo_init(flash_t *obj, uint32_t address, uint32_t function) |
| <> | 160:d5399cc887bb | 59 | { |
| <> | 160:d5399cc887bb | 60 | args_t arguments = { |
| <> | 160:d5399cc887bb | 61 | .r0 = address, |
| <> | 160:d5399cc887bb | 62 | .r1 = SystemCoreClock, |
| <> | 160:d5399cc887bb | 63 | .r2 = function, |
| <> | 160:d5399cc887bb | 64 | .r3 = 0, |
| <> | 160:d5399cc887bb | 65 | .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base, |
| <> | 160:d5399cc887bb | 66 | .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->init |
| <> | 160:d5399cc887bb | 67 | }; |
| <> | 160:d5399cc887bb | 68 | return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments); |
| <> | 160:d5399cc887bb | 69 | } |
| <> | 160:d5399cc887bb | 70 | |
| <> | 160:d5399cc887bb | 71 | // should be called within critical section |
| <> | 160:d5399cc887bb | 72 | static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t function) |
| <> | 160:d5399cc887bb | 73 | { |
| <> | 160:d5399cc887bb | 74 | args_t arguments = { |
| <> | 160:d5399cc887bb | 75 | .r0 = address, |
| <> | 160:d5399cc887bb | 76 | .r1 = SystemCoreClock, |
| <> | 160:d5399cc887bb | 77 | .r2 = function, |
| <> | 160:d5399cc887bb | 78 | .r3 = 0, |
| <> | 160:d5399cc887bb | 79 | .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base, |
| <> | 160:d5399cc887bb | 80 | .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->uninit |
| <> | 160:d5399cc887bb | 81 | }; |
| <> | 160:d5399cc887bb | 82 | return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments); |
| <> | 160:d5399cc887bb | 83 | } |
| <> | 160:d5399cc887bb | 84 | |
| <> | 160:d5399cc887bb | 85 | |
| <> | 160:d5399cc887bb | 86 | int32_t flash_init(flash_t *obj) |
| <> | 160:d5399cc887bb | 87 | { |
| <> | 160:d5399cc887bb | 88 | flash_set_target_config(obj); |
| <> | 160:d5399cc887bb | 89 | return 0; |
| <> | 160:d5399cc887bb | 90 | } |
| <> | 160:d5399cc887bb | 91 | |
| <> | 160:d5399cc887bb | 92 | int32_t flash_free(flash_t *obj) |
| <> | 160:d5399cc887bb | 93 | { |
| <> | 160:d5399cc887bb | 94 | return 0; |
| <> | 160:d5399cc887bb | 95 | } |
| <> | 160:d5399cc887bb | 96 | |
| <> | 160:d5399cc887bb | 97 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
| <> | 160:d5399cc887bb | 98 | { |
| <> | 160:d5399cc887bb | 99 | core_util_critical_section_enter(); |
| <> | 160:d5399cc887bb | 100 | flash_algo_init(obj, address, MBED_FLASH_ALGO_ERASE); |
| <> | 160:d5399cc887bb | 101 | |
| <> | 160:d5399cc887bb | 102 | args_t arguments = { |
| <> | 160:d5399cc887bb | 103 | .r0 = address, |
| <> | 160:d5399cc887bb | 104 | .r1 = 0, |
| <> | 160:d5399cc887bb | 105 | .r2 = 0, |
| <> | 160:d5399cc887bb | 106 | .r3 = 0, |
| <> | 160:d5399cc887bb | 107 | .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base, |
| <> | 160:d5399cc887bb | 108 | .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->erase_sector |
| <> | 160:d5399cc887bb | 109 | }; |
| <> | 160:d5399cc887bb | 110 | int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments); |
| <> | 160:d5399cc887bb | 111 | |
| <> | 160:d5399cc887bb | 112 | flash_algo_uninit(obj, address, MBED_FLASH_ALGO_ERASE); |
| <> | 160:d5399cc887bb | 113 | core_util_critical_section_exit(); |
| <> | 160:d5399cc887bb | 114 | return ret ? -1 : 0; |
| <> | 160:d5399cc887bb | 115 | } |
| <> | 160:d5399cc887bb | 116 | |
| <> | 160:d5399cc887bb | 117 | |
| <> | 160:d5399cc887bb | 118 | int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
| <> | 160:d5399cc887bb | 119 | { |
| <> | 160:d5399cc887bb | 120 | core_util_critical_section_enter(); |
| <> | 160:d5399cc887bb | 121 | flash_algo_init(obj, address, MBED_FLASH_ALGO_PROGRAM); |
| <> | 160:d5399cc887bb | 122 | |
| <> | 160:d5399cc887bb | 123 | args_t arguments = { |
| <> | 160:d5399cc887bb | 124 | .r0 = address, |
| <> | 160:d5399cc887bb | 125 | .r1 = size, |
| <> | 160:d5399cc887bb | 126 | .r2 = (uint32_t)data, |
| <> | 160:d5399cc887bb | 127 | .r3 = 0, |
| <> | 160:d5399cc887bb | 128 | .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base, |
| <> | 160:d5399cc887bb | 129 | .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->program_page |
| <> | 160:d5399cc887bb | 130 | }; |
| <> | 160:d5399cc887bb | 131 | int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments); |
| <> | 160:d5399cc887bb | 132 | |
| <> | 160:d5399cc887bb | 133 | flash_algo_uninit(obj, address, MBED_FLASH_ALGO_PROGRAM); |
| <> | 160:d5399cc887bb | 134 | core_util_critical_section_exit(); |
| <> | 160:d5399cc887bb | 135 | return ret ? -1 : 0; |
| <> | 160:d5399cc887bb | 136 | } |
| <> | 160:d5399cc887bb | 137 | |
| <> | 160:d5399cc887bb | 138 | |
| <> | 160:d5399cc887bb | 139 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
| <> | 160:d5399cc887bb | 140 | { |
| <> | 160:d5399cc887bb | 141 | const sector_info_t *sectors = obj->target_config->sectors; |
| <> | 160:d5399cc887bb | 142 | |
| <> | 160:d5399cc887bb | 143 | if (address >= obj->target_config->flash_start + obj->target_config->flash_size) { |
| <> | 160:d5399cc887bb | 144 | return MBED_FLASH_INVALID_SIZE; |
| <> | 160:d5399cc887bb | 145 | } |
| <> | 160:d5399cc887bb | 146 | |
| <> | 160:d5399cc887bb | 147 | int sector_index = obj->target_config->sector_info_count - 1; |
| <> | 160:d5399cc887bb | 148 | for (; sector_index >= 0; sector_index--) { |
| <> | 160:d5399cc887bb | 149 | if (address >= sectors[sector_index].start) { |
| <> | 160:d5399cc887bb | 150 | return sectors[sector_index].size; |
| <> | 160:d5399cc887bb | 151 | } |
| <> | 160:d5399cc887bb | 152 | } |
| <> | 160:d5399cc887bb | 153 | return MBED_FLASH_INVALID_SIZE; |
| <> | 160:d5399cc887bb | 154 | } |
| <> | 160:d5399cc887bb | 155 | |
| <> | 160:d5399cc887bb | 156 | uint32_t flash_get_page_size(const flash_t *obj) |
| <> | 160:d5399cc887bb | 157 | { |
| <> | 160:d5399cc887bb | 158 | return obj->target_config->page_size; |
| <> | 160:d5399cc887bb | 159 | } |
| <> | 160:d5399cc887bb | 160 | |
| <> | 160:d5399cc887bb | 161 | uint32_t flash_get_start_address(const flash_t *obj) |
| <> | 160:d5399cc887bb | 162 | { |
| <> | 160:d5399cc887bb | 163 | return obj->target_config->flash_start; |
| <> | 160:d5399cc887bb | 164 | } |
| <> | 160:d5399cc887bb | 165 | |
| <> | 160:d5399cc887bb | 166 | uint32_t flash_get_size(const flash_t *obj) |
| <> | 160:d5399cc887bb | 167 | { |
| <> | 160:d5399cc887bb | 168 | return obj->target_config->flash_size; |
| <> | 160:d5399cc887bb | 169 | } |
