/******************************************************************************* * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only * intended for use with Renesas products. No other uses are authorized. This * software is owned by Renesas Electronics Corporation and is protected under * all applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software * and to discontinue the availability of this software. By using this software, * you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * Copyright 2018-2023 Renesas Electronics Corporation All rights reserved. *******************************************************************************/ /******************************************************************************* * DESCRIPTION : log ******************************************************************************/ /****************************************************************************** * @file log.c * - Version : 0.04 * @brief Access protection setting driver. * . *****************************************************************************/ /****************************************************************************** * History : DD.MM.YYYY Version Description * : 03.07.2020 0.01 First Release * : 16.06.2022 0.02 Change log output * : 31.10.2022 0.03 License notation change. * : 04.04.2023 0.04 Removed sprintf. *****************************************************************************/ #include "stdarg.h" #include "stdint.h" #include "math.h" #include "log.h" #include "scif.h" #define NULL_CHAR '\0' /*********************************************************** * log_printf ***********************************************************/ #define DOUBLE_PRINT_RES (0.001) static uint32_t double_print(double num) { uint8_t num_buf[10]; uint32_t count = 0U; uint32_t i = 0U; uint32_t rem; double unum; double res = 1/DOUBLE_PRINT_RES; if (num < 0) { (void)console_putc((uint8_t)'-'); count = 1U; unum = -num; } else { unum = num; } unum /= (DOUBLE_PRINT_RES); while (1) { if (i == 0) { rem = fmin(9, round( fmod(unum, 10.0) ) ); //last digit is rounded to correctly handle hidden digits. (last digit is processed first) } else { rem = fmod(unum, 10.0); } num_buf[i] = (uint8_t)('0' + rem); i++; unum = unum / 10.0; res = res / 10.0; if ( (res <= 5.0) && (res > 0.5) ) { num_buf[i] = (uint8_t)('.'); i++; } if ( (unum < 1.0) && (res < 1.0) ) { break; } } while (i != 0U) { i--; (void)console_putc(num_buf[i]); count++; }; return count; } static uint32_t uint32_print(uint32_t num) { uint8_t num_buf[10]; uint32_t count = 0U; uint32_t i = 0U; uint32_t rem; uint32_t unum = num; while (1) { rem = unum % 10U; num_buf[i] = (uint8_t)('0' + rem); i++; unum = unum / 10U; if (unum < 1U) { break; } } while (i != 0U) { i--; (void)console_putc(num_buf[i]); count++; }; return count; } static uint32_t int32_print(int32_t num) { uint32_t unum; uint32_t count = 0U; if (num < 0) { (void)console_putc((uint8_t)'-'); count = 1U; unum = (uint32_t)-num; } else { unum = (uint32_t)num; } count += uint32_print(unum); return count; } static uint32_t uint32_hex_print(uint32_t num) { uint32_t i; uint32_t count = 0U; uint8_t c; for (i = 0U; i < 8U; i++) { /* 0-F */ c = (uint8_t)((num >> ((7U - i) * 4U)) & 0x0FU); if (c >= 0x0AU) { /* A-F */ c += (uint8_t)('a' - 0x0AU); } else { /* 0-9 */ c += (uint8_t)'0'; } (void)console_putc(c); count++; } return count; } static uint32_t uint8_hex_print(uint8_t num) { uint32_t i; uint32_t count = 0U; uint8_t c; for (i = 0U; i < 2U; i++) { /* 0-F */ c = ((num >> ((1U - i) * 4U)) & 0x0FU); if (c >= 0x0AU) { /* A-F */ c += (uint8_t)('a' - 0x0AU); } else { /* 0-9 */ c += (uint8_t)'0'; } (void)console_putc(c); count++; } return count; } static uint32_t str_print(const char *str) { uint32_t count = 0; while (*str != NULL_CHAR) { (void)console_putc((uint8_t)*str); str++; count++; } return count; } void log_printf(const char *fmt, ...) { va_list args; double dnum; int32_t num; uint32_t unum; char *str; uint32_t count = 0U; va_start(args, fmt); while (*fmt != NULL_CHAR) { if (*fmt == '%') { fmt++; switch (*fmt) { case 'i': /* No break */ case 'd': num = va_arg(args, int32_t); count += int32_print(num); break; case 's': str = va_arg(args, char *); count += str_print(str); break; case 'x': unum = va_arg(args, uint32_t); count += uint32_hex_print(unum); break; case 'b': unum = (uint8_t)va_arg(args, uint32_t); count += uint8_hex_print(unum); break; case 'u': unum = va_arg(args, uint32_t); count += uint32_print(unum); break; case 'f': dnum = va_arg(args, double); count += double_print(dnum); break; default: break; } } else { (void)console_putc((uint8_t)*fmt); count++; } fmt++; } va_end(args); }