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