-
Notifications
You must be signed in to change notification settings - Fork 2
/
ser_print.c
120 lines (101 loc) · 1.85 KB
/
ser_print.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
// ser_print.c
// 2018-06-29 Markku-Juhani O. Saarinen <[email protected]>
// (c) 2018 Copyright Teserakt AG
// bits and pieces originally from various public domain sources
#include <stdio.h>
#include <avr/io.h>
#include "ser_print.h"
#ifndef F_CPU
#warning "F_CPU is not defined, set to 16MHz per default."
#define F_CPU 16000000
#endif
//#define BAUD 57600
#define BAUD 38400
#include <util/setbaud.h>
#ifndef UCSRB
# ifndef UDRE
# define UDRE UDRE0
# define RXEN RXEN0
# define TXEN TXEN0
# endif
# ifdef UCSR0A /* ATmega128 */
# define UCSRA UCSR0A
# define UCSRB UCSR0B
# define UBRRL UBRR0L
# define UBRRH UBRR0H
# define UDR UDR0
# else /* ATmega8 */
# define UCSRA USR
# define UCSRB UCR
# endif
#endif
#ifndef UBRR
# define UBRR UBRRL
#endif
static char ser_initialized = 0;
void ser_init(void)
{
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
/* Enable */
UCSRB = (1 << RXEN) | (1 << TXEN);
}
void ser_write(unsigned char c)
{
if (!ser_initialized) {
ser_init();
ser_initialized = 1;
}
while (!(UCSRA & (1 << UDRE))) {};
UDR = c;
}
void ser_print(const char *s)
{
while (*s != 0) {
ser_write(*s);
s++;
}
}
void ser_dec64(uint64_t x)
{
char buf[21];
int i;
if (x == 0) {
ser_print("0");
} else {
i = 20;
buf[i] = 0;
while (x > 0 && i > 0) {
buf[--i] = (char) ((x % 10) + '0');
x = x / 10;
}
ser_print(&buf[i]);
}
}
void ser_hex8(uint8_t x)
{
char y;
y = x >> 4;
if (y < 10)
y += '0';
else
y += 'A' - 10;
ser_write(y);
y = x & 0xF;
if (y < 10)
y += '0';
else
y += 'A' - 10;
ser_write(y);
}
void ser_hex16(uint16_t x)
{
ser_hex8(x >> 8);
ser_hex8(x & 0xFF);
}
void ser_end()
{
ser_write(4);
while (1)
{;}
}