Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers util.c Source File

util.c

Go to the documentation of this file.
00001 /**
00002  * @file    util.c
00003  * @brief   Implementation of util.h
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include <string.h>
00023 
00024 #include "util.h"
00025 #include "settings.h"
00026 #include "cortex_m.h"
00027 
00028 //remove dependency from vfs_manager
00029 __attribute__((weak)) void vfs_mngr_fs_remount(void) {}
00030 
00031 uint32_t util_write_hex8(char *str, uint8_t value)
00032 {
00033     static const char nybble_chars[] = "0123456789abcdef";
00034     *(str + 0) = nybble_chars[(value >> 4) & 0x0F ];
00035     *(str + 1) = nybble_chars[(value >> 0) & 0x0F ];
00036     return 2;
00037 }
00038 
00039 uint32_t util_write_hex16(char *str, uint16_t value)
00040 {
00041     uint32_t pos = 0;
00042     pos += util_write_hex8(str + pos, (value >> 8) & 0xFF);
00043     pos += util_write_hex8(str + pos, (value >> 0) & 0xFF);
00044     return pos;
00045 }
00046 
00047 uint32_t util_write_hex32(char *str, uint32_t value)
00048 {
00049     uint32_t pos = 0;
00050     pos += util_write_hex8(str + pos, (value >> 0x18) & 0xFF);
00051     pos += util_write_hex8(str + pos, (value >> 0x10) & 0xFF);
00052     pos += util_write_hex8(str + pos, (value >> 0x08) & 0xFF);
00053     pos += util_write_hex8(str + pos, (value >> 0x00) & 0xFF);
00054     return pos;
00055 }
00056 
00057 uint32_t util_write_uint32(char *str, uint32_t value)
00058 {
00059     uint32_t temp_val;
00060     uint64_t digits;
00061     uint32_t i;
00062     // Count the number of digits
00063     digits = 0;
00064     temp_val = value;
00065 
00066     while (temp_val > 0) {
00067         temp_val /= 10;
00068         digits += 1;
00069     }
00070 
00071     if (digits <= 0) {
00072         digits = 1;
00073     }
00074 
00075     // Write the number
00076     for (i = 0; i < digits; i++) {
00077         str[digits - i - 1] = '0' + (value % 10);
00078         value /= 10;
00079     }
00080 
00081     return digits;
00082 }
00083 
00084 uint32_t util_write_uint32_zp(char *str, uint32_t value, uint16_t total_size)
00085 {
00086     uint32_t size;
00087     // Get the size of value
00088     size = util_write_uint32(str, value);
00089 
00090     if (size >= total_size) {
00091         return size;
00092     }
00093 
00094     // Zero fill
00095     memset(str, '0', total_size);
00096     // Write value
00097     util_write_uint32(str + (total_size - size), value);
00098     return total_size;
00099 }
00100 
00101 uint32_t util_write_string(char *str, const char *data)
00102 {
00103     uint32_t pos = 0;
00104 
00105     while (0 != data[pos]) {
00106         str[pos] = data[pos];
00107         pos++;
00108     }
00109 
00110     return pos;
00111 }
00112 
00113 uint32_t util_div_round_up(uint32_t dividen, uint32_t divisor)
00114 {
00115     return (dividen + divisor - 1) / divisor;
00116 }
00117 
00118 uint32_t util_div_round_down(uint32_t dividen, uint32_t divisor)
00119 {
00120     return dividen / divisor;
00121 }
00122 
00123 uint32_t util_div_round(uint32_t dividen, uint32_t divisor)
00124 {
00125     return (dividen + divisor / 2) / divisor;
00126 }
00127 
00128 void _util_assert(bool expression, const char *filename, uint16_t line)
00129 {
00130     bool assert_set;
00131     cortex_int_state_t int_state;
00132 
00133     if (expression) {
00134         return;
00135     }
00136 
00137     int_state = cortex_int_get_and_disable();
00138     // Only write the assert if there is not already one
00139     assert_set = config_ram_get_assert(0, 0, 0, 0);
00140 
00141     if (!assert_set) {
00142         config_ram_set_assert(filename, line);
00143     }
00144 
00145     cortex_int_restore(int_state);
00146 
00147     // Start a remount if this is the first assert
00148     // Do not call vfs_mngr_fs_remount from an ISR!
00149     if (!assert_set && !cortex_in_isr()) {
00150         vfs_mngr_fs_remount();
00151     }
00152 }
00153 
00154 void util_assert_clear()
00155 {
00156     cortex_int_state_t int_state;
00157     int_state = cortex_int_get_and_disable();
00158     config_ram_clear_assert();
00159     cortex_int_restore(int_state);
00160 }