-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.c
84 lines (78 loc) · 1.97 KB
/
helper.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
/*
* This file is part of the m032HidBootloader distribution (https://github.com/nimo-labs/m032HidBootloader).
* Copyright (c) 2021 Nimolabs Ltd. www.nimo.uk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Helper functions */
#include "nimolib.h"
#if HELPER == 1
#include <uart.h>
void printStr(char *str)
{
while(*str)
uartTx(DEBUG_UART, *str++);
}
void printHex(uint32_t val, uint8_t prefix)
{
uint8_t hex[] = "0123456789ABCDEF";
if(prefix)
printStr("0x");
uartTx(DEBUG_UART, hex[(val>>28) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>24) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>20) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>16) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>12) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>8) & 0xf]);
uartTx(DEBUG_UART, hex[(val>>4) & 0xf]);
uartTx(DEBUG_UART, hex[(val) & 0xf]);
}
void printDec(uint8_t val)
{
uint8_t reg[3];
if(val > 99)
{
reg[0] = (val / 100) + 0x30;
val -= ((val /100)*100);
}
else
{
reg[0] = 0;
}
if(val > 9)
{
reg[1] = (val / 10) + 0x30;
val -= ((val /10)*10);
}
else
{
reg[1] = 0;
}
if(val > 0)
{
reg[2] = val + 0x30;
val -= 10;
}
else
{
reg[2] = 0;
}
for(uint8_t i=0; i < 3; i++)
{
if(reg[i] > 0)
uartTx(DEBUG_UART, reg[i]);
else if(2 == i)
uartTx(DEBUG_UART, 0x30);
}
}
#endif