-
Notifications
You must be signed in to change notification settings - Fork 20
/
console.c
156 lines (132 loc) · 3.17 KB
/
console.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright © 2008-2015, Matthias Urlichs <matthias@urlichs.de>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 (included; see the file LICENSE)
* for more details.
*/
#include "dev_config.h"
#ifdef N_CONSOLE
#include <stdlib.h>
#include "pgm.h"
#include <stdlib.h>
#include "console.h"
#include "debug.h"
/** Size of the circular transmit buffer, must be power of 2 */
#ifndef CONSOLE_BUFFER_SIZE
#define CONSOLE_BUFFER_SIZE 128
#endif
#define CONSOLE_BUFFER_MASK ((CONSOLE_BUFFER_SIZE)-1)
#if (CONSOLE_BUFFER_SIZE & CONSOLE_BUFFER_MASK)
#error TX buffer size is not a power of 2
#endif
/*
* module global variables
*/
#ifndef DBG_CONSOLE_SYNC
static volatile unsigned char console_buf[CONSOLE_BUFFER_SIZE];
static volatile unsigned char console_head;
static volatile unsigned char console_tail;
#endif
// initialized early, thus not called init_console()
void console_init(void)
{
console_head = 0;
console_tail = 0;
}
void console_putc(unsigned char data)
{
uint8_t sreg;
uint8_t head,head2;
sreg = SREG;
cli();
head = console_head;
head2 = (head+1) & CONSOLE_BUFFER_MASK;
if (head2 != console_tail) {
console_buf[head] = data;
console_head = head2;
} else {
/* Mark overrun by a null byte */
head = (head-1) & CONSOLE_BUFFER_MASK;
console_buf[head] = 0x00;
}
SREG = sreg;
}
uint8_t console_buf_len(void)
{
return (console_head-console_tail) & CONSOLE_BUFFER_MASK;
}
uint8_t console_buf_read(unsigned char *addr, uint8_t maxlen)
{
uint8_t head,tail,len = 0;
head = console_head;
tail = console_tail;
while(head != tail && len < maxlen) {
*addr++ = console_buf[tail];
len++;
tail = (tail+1) & CONSOLE_BUFFER_MASK;
}
return len;
}
void console_buf_done(uint8_t len)
{
console_tail = (console_tail + len) & CONSOLE_BUFFER_MASK;
}
void console_puts(const char *s)
{
while (*s)
console_putc(*s++);
}
void console_puts_p(const char *progmem_s)
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) )
console_putc(c);
}
#if 0 /* unused */
void console_puti(const int val)
{
char buffer[sizeof(int)*8+1];
console_puts(itoa(val, buffer, 10));
}
void console_putl(const long val)
{
char buffer[sizeof(long)*8+1];
console_puts( ltoa(val, buffer, 10));
}
#endif
void console_puthex_nibble(const unsigned char b)
{
unsigned char c = b & 0x0f;
if (c>9) c += 'A'-10;
else c += '0';
console_putc(c);
}
void console_puthex_byte_(const unsigned char b)
{
console_puthex_nibble(b>>4);
console_puthex_nibble(b);
}
void console_puthex_byte(const unsigned char b)
{
if(b & 0xF0)
console_puthex_nibble(b>>4);
console_puthex_nibble(b);
}
void console_puthex_word(const uint16_t b)
{
if (b&0xFF00) {
console_puthex_byte(b>>8);
console_puthex_byte_(b);
} else {
console_puthex_byte(b);
}
}
#endif // HAVE_CONSOLE