82 lines
1.3 KiB
C
82 lines
1.3 KiB
C
|
/******************************************************************************
|
||
|
*
|
||
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
||
|
* Please, refer LICENSE file for legal information
|
||
|
*
|
||
|
******************************************************************************/
|
||
|
|
||
|
/**
|
||
|
* @file log.c
|
||
|
* @author Ilja Kartašov <ik@lowenware.com>
|
||
|
* @brief
|
||
|
*
|
||
|
* @see https://lowenware.com/
|
||
|
*/
|
||
|
|
||
|
#include <device/uart/uart.h>
|
||
|
#include "log.h"
|
||
|
|
||
|
void
|
||
|
k_log_init(void)
|
||
|
{
|
||
|
uart_init();
|
||
|
}
|
||
|
|
||
|
Ki32
|
||
|
k_logs(const Ki8 *string)
|
||
|
{
|
||
|
Ki8 c;
|
||
|
const Ki8 *p = string;
|
||
|
while((c = *p) != 0) {
|
||
|
if (uart_write(c))
|
||
|
return -1;
|
||
|
p++;
|
||
|
}
|
||
|
return (Ki32)(p - string);
|
||
|
}
|
||
|
|
||
|
Ki32
|
||
|
k_logi(Ki64 value, Ku8 base)
|
||
|
{
|
||
|
Ki32 result;
|
||
|
|
||
|
if (value < 0) {
|
||
|
if (uart_write('-'))
|
||
|
return -1;
|
||
|
value = -value;
|
||
|
result = 1;
|
||
|
} else {
|
||
|
result = 0;
|
||
|
}
|
||
|
result += k_logu(value, base);
|
||
|
return (result) ? result : -1;
|
||
|
}
|
||
|
|
||
|
Ki32
|
||
|
k_logu(Ku64 value, Ku8 base)
|
||
|
{
|
||
|
Ki32 i = 0, result;
|
||
|
Ku8 buffer[64];
|
||
|
|
||
|
if (!value) {
|
||
|
return (uart_write('0')) ? -1 : 1;
|
||
|
}
|
||
|
|
||
|
do {
|
||
|
if (!(i < (sizeof(buffer) / sizeof(buffer[0]))))
|
||
|
return -1;
|
||
|
|
||
|
Ku64 r = value % base;
|
||
|
buffer[i++] = (r < 0x0A) ? '0' + r : 'A' + r - 0x0A;
|
||
|
value /= base;
|
||
|
} while (value);
|
||
|
|
||
|
result = i;
|
||
|
|
||
|
while (i--) {
|
||
|
if (uart_write(buffer[i]))
|
||
|
return -1;
|
||
|
}
|
||
|
return result;
|
||
|
}
|