-
Notifications
You must be signed in to change notification settings - Fork 17
/
debug.c
127 lines (105 loc) · 4.09 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Konstantin Belyalov. All rights reserved.
// Licensed under the MIT license.
#include "debug.h"
#include <stdio.h>
#include <string.h>
uint8_t _stm32_hal_debug_buffer[DEBUG_BUFFER_SIZE];
static uint8_t *uint_to_str(uint64_t value, uint8_t base, uint8_t *buf, uint8_t len)
{
uint8_t *current = buf + len - 1;
if (len < 2 || buf == NULL) {
return buf;
}
// Null terminated string
*current = 0;
// Special case for zero value
if (value == 0) {
current--;
*current = '0';
return current;
}
while (value > 0 && current != buf) {
uint8_t mod = value % base;
current--;
*current = mod > 9 ? 'W' + mod : '0' + mod;
value /= base;
}
return current;
}
uint8_t *debug_uint64_to_hexstring(uint64_t value, uint8_t *buf, uint8_t len)
{
return uint_to_str(value, 16, buf, len);
}
uint8_t *debug_uint64_to_string(uint64_t value, uint8_t *buf, uint8_t len)
{
return uint_to_str(value, 10, buf, len);
}
uint8_t *debug_int64_to_string(int64_t value, uint8_t *buf, uint8_t len)
{
if (value >= 0) {
return debug_uint64_to_string(value, buf, len);
}
if (len < 3) {
return buf;
}
// Reserve space for sign
buf++;
uint8_t *res = uint_to_str(0 - value, 10, buf, len - 1);
res--;
*res = '-';
return res;
}
void debug_print_str(UART_HandleTypeDef *uart, const char *msg)
{
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
}
void debug_print_strln(UART_HandleTypeDef *uart, const char *msg)
{
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)"\r\n", 2, DEBUG_UART_TIMEOUT);
}
void debug_print_uint64(UART_HandleTypeDef *uart, const char *msg, uint64_t val)
{
uint8_t *sval = debug_uint64_to_string(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
}
void debug_print_uint64ln(UART_HandleTypeDef *uart, const char *msg, uint64_t val)
{
uint8_t *sval = debug_uint64_to_string(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)"\r\n", 2, DEBUG_UART_TIMEOUT);
}
void debug_print_int64(UART_HandleTypeDef *uart, const char *msg, int64_t val)
{
uint8_t *sval = debug_int64_to_string(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
}
void debug_print_int64ln(UART_HandleTypeDef *uart, const char *msg, int64_t val)
{
uint8_t *sval = debug_int64_to_string(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)"\r\n", 2, DEBUG_UART_TIMEOUT);
}
void debug_print_hex64(UART_HandleTypeDef *uart, const char *msg, uint64_t val)
{
uint8_t *sval = debug_uint64_to_hexstring(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
}
void debug_print_hex64ln(UART_HandleTypeDef *uart, const char *msg, uint64_t val)
{
uint8_t *sval = debug_uint64_to_hexstring(val, _stm32_hal_debug_buffer, DEBUG_BUFFER_SIZE);
HAL_UART_Transmit(uart, (uint8_t*)msg, strlen(msg), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, sval, strlen((char*)sval), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)"\r\n", 2, DEBUG_UART_TIMEOUT);
}
void debug_print_strstrln(UART_HandleTypeDef *uart, const char *msg1, const char *msg2)
{
HAL_UART_Transmit(uart, (uint8_t*)msg1, strlen(msg1), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)msg2, strlen(msg2), DEBUG_UART_TIMEOUT);
HAL_UART_Transmit(uart, (uint8_t*)"\r\n", 2, DEBUG_UART_TIMEOUT);
}