Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/daplink/util.c@0:01f31e923fe2, 2020-04-07 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file util.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief Implementation of util.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 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include <string.h> |
Pawel Zarembski |
0:01f31e923fe2 | 23 | |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "util.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "settings.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "cortex_m.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | |
Pawel Zarembski |
0:01f31e923fe2 | 28 | //remove dependency from vfs_manager |
Pawel Zarembski |
0:01f31e923fe2 | 29 | __attribute__((weak)) void vfs_mngr_fs_remount(void) {} |
Pawel Zarembski |
0:01f31e923fe2 | 30 | |
Pawel Zarembski |
0:01f31e923fe2 | 31 | uint32_t util_write_hex8(char *str, uint8_t value) |
Pawel Zarembski |
0:01f31e923fe2 | 32 | { |
Pawel Zarembski |
0:01f31e923fe2 | 33 | static const char nybble_chars[] = "0123456789abcdef"; |
Pawel Zarembski |
0:01f31e923fe2 | 34 | *(str + 0) = nybble_chars[(value >> 4) & 0x0F ]; |
Pawel Zarembski |
0:01f31e923fe2 | 35 | *(str + 1) = nybble_chars[(value >> 0) & 0x0F ]; |
Pawel Zarembski |
0:01f31e923fe2 | 36 | return 2; |
Pawel Zarembski |
0:01f31e923fe2 | 37 | } |
Pawel Zarembski |
0:01f31e923fe2 | 38 | |
Pawel Zarembski |
0:01f31e923fe2 | 39 | uint32_t util_write_hex16(char *str, uint16_t value) |
Pawel Zarembski |
0:01f31e923fe2 | 40 | { |
Pawel Zarembski |
0:01f31e923fe2 | 41 | uint32_t pos = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 42 | pos += util_write_hex8(str + pos, (value >> 8) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 43 | pos += util_write_hex8(str + pos, (value >> 0) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 44 | return pos; |
Pawel Zarembski |
0:01f31e923fe2 | 45 | } |
Pawel Zarembski |
0:01f31e923fe2 | 46 | |
Pawel Zarembski |
0:01f31e923fe2 | 47 | uint32_t util_write_hex32(char *str, uint32_t value) |
Pawel Zarembski |
0:01f31e923fe2 | 48 | { |
Pawel Zarembski |
0:01f31e923fe2 | 49 | uint32_t pos = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 50 | pos += util_write_hex8(str + pos, (value >> 0x18) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 51 | pos += util_write_hex8(str + pos, (value >> 0x10) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 52 | pos += util_write_hex8(str + pos, (value >> 0x08) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 53 | pos += util_write_hex8(str + pos, (value >> 0x00) & 0xFF); |
Pawel Zarembski |
0:01f31e923fe2 | 54 | return pos; |
Pawel Zarembski |
0:01f31e923fe2 | 55 | } |
Pawel Zarembski |
0:01f31e923fe2 | 56 | |
Pawel Zarembski |
0:01f31e923fe2 | 57 | uint32_t util_write_uint32(char *str, uint32_t value) |
Pawel Zarembski |
0:01f31e923fe2 | 58 | { |
Pawel Zarembski |
0:01f31e923fe2 | 59 | uint32_t temp_val; |
Pawel Zarembski |
0:01f31e923fe2 | 60 | uint64_t digits; |
Pawel Zarembski |
0:01f31e923fe2 | 61 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | // Count the number of digits |
Pawel Zarembski |
0:01f31e923fe2 | 63 | digits = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 64 | temp_val = value; |
Pawel Zarembski |
0:01f31e923fe2 | 65 | |
Pawel Zarembski |
0:01f31e923fe2 | 66 | while (temp_val > 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 67 | temp_val /= 10; |
Pawel Zarembski |
0:01f31e923fe2 | 68 | digits += 1; |
Pawel Zarembski |
0:01f31e923fe2 | 69 | } |
Pawel Zarembski |
0:01f31e923fe2 | 70 | |
Pawel Zarembski |
0:01f31e923fe2 | 71 | if (digits <= 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 72 | digits = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 73 | } |
Pawel Zarembski |
0:01f31e923fe2 | 74 | |
Pawel Zarembski |
0:01f31e923fe2 | 75 | // Write the number |
Pawel Zarembski |
0:01f31e923fe2 | 76 | for (i = 0; i < digits; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 77 | str[digits - i - 1] = '0' + (value % 10); |
Pawel Zarembski |
0:01f31e923fe2 | 78 | value /= 10; |
Pawel Zarembski |
0:01f31e923fe2 | 79 | } |
Pawel Zarembski |
0:01f31e923fe2 | 80 | |
Pawel Zarembski |
0:01f31e923fe2 | 81 | return digits; |
Pawel Zarembski |
0:01f31e923fe2 | 82 | } |
Pawel Zarembski |
0:01f31e923fe2 | 83 | |
Pawel Zarembski |
0:01f31e923fe2 | 84 | uint32_t util_write_uint32_zp(char *str, uint32_t value, uint16_t total_size) |
Pawel Zarembski |
0:01f31e923fe2 | 85 | { |
Pawel Zarembski |
0:01f31e923fe2 | 86 | uint32_t size; |
Pawel Zarembski |
0:01f31e923fe2 | 87 | // Get the size of value |
Pawel Zarembski |
0:01f31e923fe2 | 88 | size = util_write_uint32(str, value); |
Pawel Zarembski |
0:01f31e923fe2 | 89 | |
Pawel Zarembski |
0:01f31e923fe2 | 90 | if (size >= total_size) { |
Pawel Zarembski |
0:01f31e923fe2 | 91 | return size; |
Pawel Zarembski |
0:01f31e923fe2 | 92 | } |
Pawel Zarembski |
0:01f31e923fe2 | 93 | |
Pawel Zarembski |
0:01f31e923fe2 | 94 | // Zero fill |
Pawel Zarembski |
0:01f31e923fe2 | 95 | memset(str, '0', total_size); |
Pawel Zarembski |
0:01f31e923fe2 | 96 | // Write value |
Pawel Zarembski |
0:01f31e923fe2 | 97 | util_write_uint32(str + (total_size - size), value); |
Pawel Zarembski |
0:01f31e923fe2 | 98 | return total_size; |
Pawel Zarembski |
0:01f31e923fe2 | 99 | } |
Pawel Zarembski |
0:01f31e923fe2 | 100 | |
Pawel Zarembski |
0:01f31e923fe2 | 101 | uint32_t util_write_string(char *str, const char *data) |
Pawel Zarembski |
0:01f31e923fe2 | 102 | { |
Pawel Zarembski |
0:01f31e923fe2 | 103 | uint32_t pos = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 104 | |
Pawel Zarembski |
0:01f31e923fe2 | 105 | while (0 != data[pos]) { |
Pawel Zarembski |
0:01f31e923fe2 | 106 | str[pos] = data[pos]; |
Pawel Zarembski |
0:01f31e923fe2 | 107 | pos++; |
Pawel Zarembski |
0:01f31e923fe2 | 108 | } |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | return pos; |
Pawel Zarembski |
0:01f31e923fe2 | 111 | } |
Pawel Zarembski |
0:01f31e923fe2 | 112 | |
Pawel Zarembski |
0:01f31e923fe2 | 113 | uint32_t util_div_round_up(uint32_t dividen, uint32_t divisor) |
Pawel Zarembski |
0:01f31e923fe2 | 114 | { |
Pawel Zarembski |
0:01f31e923fe2 | 115 | return (dividen + divisor - 1) / divisor; |
Pawel Zarembski |
0:01f31e923fe2 | 116 | } |
Pawel Zarembski |
0:01f31e923fe2 | 117 | |
Pawel Zarembski |
0:01f31e923fe2 | 118 | uint32_t util_div_round_down(uint32_t dividen, uint32_t divisor) |
Pawel Zarembski |
0:01f31e923fe2 | 119 | { |
Pawel Zarembski |
0:01f31e923fe2 | 120 | return dividen / divisor; |
Pawel Zarembski |
0:01f31e923fe2 | 121 | } |
Pawel Zarembski |
0:01f31e923fe2 | 122 | |
Pawel Zarembski |
0:01f31e923fe2 | 123 | uint32_t util_div_round(uint32_t dividen, uint32_t divisor) |
Pawel Zarembski |
0:01f31e923fe2 | 124 | { |
Pawel Zarembski |
0:01f31e923fe2 | 125 | return (dividen + divisor / 2) / divisor; |
Pawel Zarembski |
0:01f31e923fe2 | 126 | } |
Pawel Zarembski |
0:01f31e923fe2 | 127 | |
Pawel Zarembski |
0:01f31e923fe2 | 128 | void _util_assert(bool expression, const char *filename, uint16_t line) |
Pawel Zarembski |
0:01f31e923fe2 | 129 | { |
Pawel Zarembski |
0:01f31e923fe2 | 130 | bool assert_set; |
Pawel Zarembski |
0:01f31e923fe2 | 131 | cortex_int_state_t int_state; |
Pawel Zarembski |
0:01f31e923fe2 | 132 | |
Pawel Zarembski |
0:01f31e923fe2 | 133 | if (expression) { |
Pawel Zarembski |
0:01f31e923fe2 | 134 | return; |
Pawel Zarembski |
0:01f31e923fe2 | 135 | } |
Pawel Zarembski |
0:01f31e923fe2 | 136 | |
Pawel Zarembski |
0:01f31e923fe2 | 137 | int_state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 138 | // Only write the assert if there is not already one |
Pawel Zarembski |
0:01f31e923fe2 | 139 | assert_set = config_ram_get_assert(0, 0, 0, 0); |
Pawel Zarembski |
0:01f31e923fe2 | 140 | |
Pawel Zarembski |
0:01f31e923fe2 | 141 | if (!assert_set) { |
Pawel Zarembski |
0:01f31e923fe2 | 142 | config_ram_set_assert(filename, line); |
Pawel Zarembski |
0:01f31e923fe2 | 143 | } |
Pawel Zarembski |
0:01f31e923fe2 | 144 | |
Pawel Zarembski |
0:01f31e923fe2 | 145 | cortex_int_restore(int_state); |
Pawel Zarembski |
0:01f31e923fe2 | 146 | |
Pawel Zarembski |
0:01f31e923fe2 | 147 | // Start a remount if this is the first assert |
Pawel Zarembski |
0:01f31e923fe2 | 148 | // Do not call vfs_mngr_fs_remount from an ISR! |
Pawel Zarembski |
0:01f31e923fe2 | 149 | if (!assert_set && !cortex_in_isr()) { |
Pawel Zarembski |
0:01f31e923fe2 | 150 | vfs_mngr_fs_remount(); |
Pawel Zarembski |
0:01f31e923fe2 | 151 | } |
Pawel Zarembski |
0:01f31e923fe2 | 152 | } |
Pawel Zarembski |
0:01f31e923fe2 | 153 | |
Pawel Zarembski |
0:01f31e923fe2 | 154 | void util_assert_clear() |
Pawel Zarembski |
0:01f31e923fe2 | 155 | { |
Pawel Zarembski |
0:01f31e923fe2 | 156 | cortex_int_state_t int_state; |
Pawel Zarembski |
0:01f31e923fe2 | 157 | int_state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 158 | config_ram_clear_assert(); |
Pawel Zarembski |
0:01f31e923fe2 | 159 | cortex_int_restore(int_state); |
Pawel Zarembski |
0:01f31e923fe2 | 160 | } |