Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file settings.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief Implementation of settings.h
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * Copyright 2019, Cypress Semiconductor Corporation
Pawel Zarembski 0:01f31e923fe2 8 * or a subsidiary of Cypress Semiconductor Corporation.
Pawel Zarembski 0:01f31e923fe2 9 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 10 *
Pawel Zarembski 0:01f31e923fe2 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 12 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 13 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 16 *
Pawel Zarembski 0:01f31e923fe2 17 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 20 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 21 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 22 */
Pawel Zarembski 0:01f31e923fe2 23
Pawel Zarembski 0:01f31e923fe2 24 #include <string.h>
Pawel Zarembski 0:01f31e923fe2 25
Pawel Zarembski 0:01f31e923fe2 26 #include "settings.h"
Pawel Zarembski 0:01f31e923fe2 27 #include "target_config.h"
Pawel Zarembski 0:01f31e923fe2 28 #include "compiler.h"
Pawel Zarembski 0:01f31e923fe2 29 #include "cortex_m.h"
Pawel Zarembski 0:01f31e923fe2 30 #include "daplink.h"
Pawel Zarembski 0:01f31e923fe2 31
Pawel Zarembski 0:01f31e923fe2 32 // 'kvld' in hex - key valid
Pawel Zarembski 0:01f31e923fe2 33 #define CFG_KEY 0x6b766c64
Pawel Zarembski 0:01f31e923fe2 34 #define SECTOR_BUFFER_SIZE 16
Pawel Zarembski 0:01f31e923fe2 35
Pawel Zarembski 0:01f31e923fe2 36 // For generating hexdumps on faults
Pawel Zarembski 0:01f31e923fe2 37 #define ALLOWED_HEXDUMP 16
Pawel Zarembski 0:01f31e923fe2 38
Pawel Zarembski 0:01f31e923fe2 39 // WARNING - THIS STRUCTURE RESIDES IN RAM STORAGE!
Pawel Zarembski 0:01f31e923fe2 40 // Be careful with changes:
Pawel Zarembski 0:01f31e923fe2 41 // -Only add new members to end end of this structure
Pawel Zarembski 0:01f31e923fe2 42 // -Do not change the order of members in this structure
Pawel Zarembski 0:01f31e923fe2 43 // -Structure must remain packed so no padding bytes are added
Pawel Zarembski 0:01f31e923fe2 44 typedef struct __attribute__((__packed__)) cfg_ram {
Pawel Zarembski 0:01f31e923fe2 45 uint32_t key; // Magic key to indicate a valid record
Pawel Zarembski 0:01f31e923fe2 46 uint16_t size; // Offset of the last member from the start
Pawel Zarembski 0:01f31e923fe2 47
Pawel Zarembski 0:01f31e923fe2 48 // Configurable values
Pawel Zarembski 0:01f31e923fe2 49 uint8_t hold_in_bl;
Pawel Zarembski 0:01f31e923fe2 50 char assert_file_name[64 + 1];
Pawel Zarembski 0:01f31e923fe2 51 uint16_t assert_line;
Pawel Zarembski 0:01f31e923fe2 52 uint8_t assert_source;
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 // Additional debug information on faults
Pawel Zarembski 0:01f31e923fe2 55 uint8_t valid_dumps;
Pawel Zarembski 0:01f31e923fe2 56 uint32_t hexdump[ALLOWED_HEXDUMP]; //Alignments checked
Pawel Zarembski 0:01f31e923fe2 57
Pawel Zarembski 0:01f31e923fe2 58 // Disable msd support
Pawel Zarembski 0:01f31e923fe2 59 uint8_t disable_msd;
Pawel Zarembski 0:01f31e923fe2 60
Pawel Zarembski 0:01f31e923fe2 61 //Add new entries from here
Pawel Zarembski 0:01f31e923fe2 62 uint8_t page_erase_enable;
Pawel Zarembski 0:01f31e923fe2 63 } cfg_ram_t;
Pawel Zarembski 0:01f31e923fe2 64
Pawel Zarembski 0:01f31e923fe2 65 // Configuration RAM
Pawel Zarembski 0:01f31e923fe2 66 static cfg_ram_t config_ram __attribute__((section("cfgram"), zero_init));
Pawel Zarembski 0:01f31e923fe2 67 // Ram copy of RAM config
Pawel Zarembski 0:01f31e923fe2 68 static cfg_ram_t config_ram_copy;
Pawel Zarembski 0:01f31e923fe2 69
Pawel Zarembski 0:01f31e923fe2 70 void config_init()
Pawel Zarembski 0:01f31e923fe2 71 {
Pawel Zarembski 0:01f31e923fe2 72 uint32_t new_size;
Pawel Zarembski 0:01f31e923fe2 73 // Initialize RAM copy
Pawel Zarembski 0:01f31e923fe2 74 memset(&config_ram_copy, 0, sizeof(config_ram_copy));
Pawel Zarembski 0:01f31e923fe2 75 // Read settings from RAM if the key is valid
Pawel Zarembski 0:01f31e923fe2 76 new_size = sizeof(config_ram);
Pawel Zarembski 0:01f31e923fe2 77
Pawel Zarembski 0:01f31e923fe2 78 if (CFG_KEY == config_ram.key) {
Pawel Zarembski 0:01f31e923fe2 79 uint32_t size = MIN(config_ram.size, sizeof(config_ram));
Pawel Zarembski 0:01f31e923fe2 80 new_size = MAX(config_ram.size, sizeof(config_ram));
Pawel Zarembski 0:01f31e923fe2 81 memcpy(&config_ram_copy, (void *)&config_ram, size);
Pawel Zarembski 0:01f31e923fe2 82 config_ram_copy.assert_file_name[sizeof(config_ram_copy.assert_file_name) - 1] = 0;
Pawel Zarembski 0:01f31e923fe2 83 }
Pawel Zarembski 0:01f31e923fe2 84
Pawel Zarembski 0:01f31e923fe2 85 // Initialize RAM
Pawel Zarembski 0:01f31e923fe2 86 memset((void *)&config_ram, 0, sizeof(config_ram));
Pawel Zarembski 0:01f31e923fe2 87 config_ram.key = CFG_KEY;
Pawel Zarembski 0:01f31e923fe2 88 config_ram.size = new_size;
Pawel Zarembski 0:01f31e923fe2 89 // Copy assert info back over (must be explicitly cleared)
Pawel Zarembski 0:01f31e923fe2 90 memcpy(config_ram.assert_file_name,
Pawel Zarembski 0:01f31e923fe2 91 config_ram_copy.assert_file_name,
Pawel Zarembski 0:01f31e923fe2 92 sizeof(config_ram_copy.assert_file_name));
Pawel Zarembski 0:01f31e923fe2 93 config_ram.assert_line = config_ram_copy.assert_line;
Pawel Zarembski 0:01f31e923fe2 94 config_ram.assert_source = config_ram_copy.assert_source;
Pawel Zarembski 0:01f31e923fe2 95 config_ram.valid_dumps = config_ram_copy.valid_dumps;
Pawel Zarembski 0:01f31e923fe2 96 memcpy(config_ram.hexdump, config_ram_copy.hexdump, sizeof(config_ram_copy.hexdump[0]) * config_ram_copy.valid_dumps);
Pawel Zarembski 0:01f31e923fe2 97 config_ram.disable_msd = config_ram_copy.disable_msd;
Pawel Zarembski 0:01f31e923fe2 98 config_ram.page_erase_enable = config_ram_copy.page_erase_enable;
Pawel Zarembski 0:01f31e923fe2 99 config_rom_init();
Pawel Zarembski 0:01f31e923fe2 100 }
Pawel Zarembski 0:01f31e923fe2 101
Pawel Zarembski 0:01f31e923fe2 102 void config_ram_set_hold_in_bl(bool hold)
Pawel Zarembski 0:01f31e923fe2 103 {
Pawel Zarembski 0:01f31e923fe2 104 config_ram.hold_in_bl = hold;
Pawel Zarembski 0:01f31e923fe2 105 }
Pawel Zarembski 0:01f31e923fe2 106
Pawel Zarembski 0:01f31e923fe2 107 void config_ram_set_assert(const char *file, uint16_t line)
Pawel Zarembski 0:01f31e923fe2 108 {
Pawel Zarembski 0:01f31e923fe2 109 // Initialize
Pawel Zarembski 0:01f31e923fe2 110 uint32_t file_name_size = strlen(file) + 1;
Pawel Zarembski 0:01f31e923fe2 111 const char *start;
Pawel Zarembski 0:01f31e923fe2 112 uint32_t assert_buf_size = sizeof(config_ram.assert_file_name);
Pawel Zarembski 0:01f31e923fe2 113 uint32_t copy_size;
Pawel Zarembski 0:01f31e923fe2 114 memset(config_ram.assert_file_name, 0, sizeof(config_ram.assert_file_name));
Pawel Zarembski 0:01f31e923fe2 115
Pawel Zarembski 0:01f31e923fe2 116 // Determine size to copy
Pawel Zarembski 0:01f31e923fe2 117 if (file_name_size <= assert_buf_size) {
Pawel Zarembski 0:01f31e923fe2 118 start = file;
Pawel Zarembski 0:01f31e923fe2 119 copy_size = file_name_size;
Pawel Zarembski 0:01f31e923fe2 120 } else {
Pawel Zarembski 0:01f31e923fe2 121 start = &file[file_name_size - assert_buf_size];
Pawel Zarembski 0:01f31e923fe2 122 copy_size = assert_buf_size;
Pawel Zarembski 0:01f31e923fe2 123 }
Pawel Zarembski 0:01f31e923fe2 124
Pawel Zarembski 0:01f31e923fe2 125 // Write to ram
Pawel Zarembski 0:01f31e923fe2 126 memcpy(config_ram.assert_file_name, start, copy_size);
Pawel Zarembski 0:01f31e923fe2 127 config_ram.assert_line = line;
Pawel Zarembski 0:01f31e923fe2 128
Pawel Zarembski 0:01f31e923fe2 129 if (daplink_is_bootloader()) {
Pawel Zarembski 0:01f31e923fe2 130 config_ram.assert_source = ASSERT_SOURCE_BL;
Pawel Zarembski 0:01f31e923fe2 131 } else if (daplink_is_interface()) {
Pawel Zarembski 0:01f31e923fe2 132 config_ram.assert_source = ASSERT_SOURCE_APP;
Pawel Zarembski 0:01f31e923fe2 133 } else {
Pawel Zarembski 0:01f31e923fe2 134 config_ram.assert_source = ASSERT_SOURCE_NONE;
Pawel Zarembski 0:01f31e923fe2 135 }
Pawel Zarembski 0:01f31e923fe2 136 }
Pawel Zarembski 0:01f31e923fe2 137
Pawel Zarembski 0:01f31e923fe2 138 void config_ram_clear_assert()
Pawel Zarembski 0:01f31e923fe2 139 {
Pawel Zarembski 0:01f31e923fe2 140 memset(config_ram.assert_file_name, 0, sizeof(config_ram.assert_file_name));
Pawel Zarembski 0:01f31e923fe2 141 config_ram.assert_line = 0;
Pawel Zarembski 0:01f31e923fe2 142 config_ram.valid_dumps = 0;
Pawel Zarembski 0:01f31e923fe2 143 }
Pawel Zarembski 0:01f31e923fe2 144
Pawel Zarembski 0:01f31e923fe2 145 bool config_ram_get_hold_in_bl()
Pawel Zarembski 0:01f31e923fe2 146 {
Pawel Zarembski 0:01f31e923fe2 147 return config_ram.hold_in_bl;
Pawel Zarembski 0:01f31e923fe2 148 }
Pawel Zarembski 0:01f31e923fe2 149
Pawel Zarembski 0:01f31e923fe2 150 bool config_ram_get_initial_hold_in_bl()
Pawel Zarembski 0:01f31e923fe2 151 {
Pawel Zarembski 0:01f31e923fe2 152 return config_ram_copy.hold_in_bl;
Pawel Zarembski 0:01f31e923fe2 153 }
Pawel Zarembski 0:01f31e923fe2 154
Pawel Zarembski 0:01f31e923fe2 155 bool config_ram_get_assert(char *buf, uint16_t buf_size, uint16_t *line, assert_source_t *source)
Pawel Zarembski 0:01f31e923fe2 156 {
Pawel Zarembski 0:01f31e923fe2 157 // Initialize
Pawel Zarembski 0:01f31e923fe2 158 const char *start;
Pawel Zarembski 0:01f31e923fe2 159 uint32_t copy_size;
Pawel Zarembski 0:01f31e923fe2 160 uint32_t assert_size = strlen(config_ram.assert_file_name) + 1;
Pawel Zarembski 0:01f31e923fe2 161
Pawel Zarembski 0:01f31e923fe2 162 if (0 != buf) {
Pawel Zarembski 0:01f31e923fe2 163 memset(buf, 0, buf_size);
Pawel Zarembski 0:01f31e923fe2 164 }
Pawel Zarembski 0:01f31e923fe2 165
Pawel Zarembski 0:01f31e923fe2 166 if (0 != line) {
Pawel Zarembski 0:01f31e923fe2 167 *line = 0;
Pawel Zarembski 0:01f31e923fe2 168 }
Pawel Zarembski 0:01f31e923fe2 169
Pawel Zarembski 0:01f31e923fe2 170 if (0 != source) {
Pawel Zarembski 0:01f31e923fe2 171 *source = ASSERT_SOURCE_NONE;
Pawel Zarembski 0:01f31e923fe2 172 }
Pawel Zarembski 0:01f31e923fe2 173
Pawel Zarembski 0:01f31e923fe2 174 // If the string is empty then there is no assert
Pawel Zarembski 0:01f31e923fe2 175 if (0 == config_ram.assert_file_name[0]) {
Pawel Zarembski 0:01f31e923fe2 176 return false;
Pawel Zarembski 0:01f31e923fe2 177 }
Pawel Zarembski 0:01f31e923fe2 178
Pawel Zarembski 0:01f31e923fe2 179 // Determine size to copy
Pawel Zarembski 0:01f31e923fe2 180 if (assert_size <= buf_size) {
Pawel Zarembski 0:01f31e923fe2 181 start = config_ram.assert_file_name;
Pawel Zarembski 0:01f31e923fe2 182 copy_size = assert_size;
Pawel Zarembski 0:01f31e923fe2 183 } else {
Pawel Zarembski 0:01f31e923fe2 184 start = &config_ram.assert_file_name[assert_size - buf_size];
Pawel Zarembski 0:01f31e923fe2 185 copy_size = buf_size;
Pawel Zarembski 0:01f31e923fe2 186 }
Pawel Zarembski 0:01f31e923fe2 187
Pawel Zarembski 0:01f31e923fe2 188 // Copy data over
Pawel Zarembski 0:01f31e923fe2 189 if (0 != buf) {
Pawel Zarembski 0:01f31e923fe2 190 *line = config_ram.assert_line;
Pawel Zarembski 0:01f31e923fe2 191 }
Pawel Zarembski 0:01f31e923fe2 192
Pawel Zarembski 0:01f31e923fe2 193 if (0 != line) {
Pawel Zarembski 0:01f31e923fe2 194 memcpy(buf, start, copy_size);
Pawel Zarembski 0:01f31e923fe2 195 }
Pawel Zarembski 0:01f31e923fe2 196
Pawel Zarembski 0:01f31e923fe2 197 if (0 != source) {
Pawel Zarembski 0:01f31e923fe2 198 *source = (assert_source_t)config_ram.assert_source;
Pawel Zarembski 0:01f31e923fe2 199 }
Pawel Zarembski 0:01f31e923fe2 200
Pawel Zarembski 0:01f31e923fe2 201 return true;
Pawel Zarembski 0:01f31e923fe2 202 }
Pawel Zarembski 0:01f31e923fe2 203
Pawel Zarembski 0:01f31e923fe2 204 uint8_t config_ram_add_hexdump(uint32_t hexdump)
Pawel Zarembski 0:01f31e923fe2 205 {
Pawel Zarembski 0:01f31e923fe2 206 if (config_ram.valid_dumps >= ALLOWED_HEXDUMP) {
Pawel Zarembski 0:01f31e923fe2 207 return 0;
Pawel Zarembski 0:01f31e923fe2 208 }
Pawel Zarembski 0:01f31e923fe2 209
Pawel Zarembski 0:01f31e923fe2 210 //alignment is maintained here
Pawel Zarembski 0:01f31e923fe2 211 config_ram.hexdump[config_ram.valid_dumps++] = hexdump;
Pawel Zarembski 0:01f31e923fe2 212 return config_ram.valid_dumps;
Pawel Zarembski 0:01f31e923fe2 213 }
Pawel Zarembski 0:01f31e923fe2 214
Pawel Zarembski 0:01f31e923fe2 215 uint8_t config_ram_get_hexdumps(uint32_t **hexdumps)
Pawel Zarembski 0:01f31e923fe2 216 {
Pawel Zarembski 0:01f31e923fe2 217 if (config_ram.valid_dumps == 0) {
Pawel Zarembski 0:01f31e923fe2 218 return 0;
Pawel Zarembski 0:01f31e923fe2 219 }
Pawel Zarembski 0:01f31e923fe2 220
Pawel Zarembski 0:01f31e923fe2 221 //prevent memcopy check alignment
Pawel Zarembski 0:01f31e923fe2 222 *hexdumps = config_ram.hexdump;
Pawel Zarembski 0:01f31e923fe2 223 return config_ram.valid_dumps;
Pawel Zarembski 0:01f31e923fe2 224 }
Pawel Zarembski 0:01f31e923fe2 225
Pawel Zarembski 0:01f31e923fe2 226 void config_ram_set_disable_msd(bool disable_msd)
Pawel Zarembski 0:01f31e923fe2 227 {
Pawel Zarembski 0:01f31e923fe2 228 config_ram.disable_msd = disable_msd;
Pawel Zarembski 0:01f31e923fe2 229 }
Pawel Zarembski 0:01f31e923fe2 230
Pawel Zarembski 0:01f31e923fe2 231 uint8_t config_ram_get_disable_msd(void)
Pawel Zarembski 0:01f31e923fe2 232 {
Pawel Zarembski 0:01f31e923fe2 233 return config_ram.disable_msd;
Pawel Zarembski 0:01f31e923fe2 234 }
Pawel Zarembski 0:01f31e923fe2 235
Pawel Zarembski 0:01f31e923fe2 236 void config_ram_set_page_erase(bool page_erase_enable)
Pawel Zarembski 0:01f31e923fe2 237 {
Pawel Zarembski 0:01f31e923fe2 238 config_ram.page_erase_enable = page_erase_enable;
Pawel Zarembski 0:01f31e923fe2 239 }
Pawel Zarembski 0:01f31e923fe2 240
Pawel Zarembski 0:01f31e923fe2 241 bool config_ram_get_page_erase(void)
Pawel Zarembski 0:01f31e923fe2 242 {
Pawel Zarembski 0:01f31e923fe2 243 return config_ram.page_erase_enable;
Pawel Zarembski 0:01f31e923fe2 244 }