-
Notifications
You must be signed in to change notification settings - Fork 31
/
SerialComm.h
156 lines (128 loc) · 3.01 KB
/
SerialComm.h
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
/*
The JTAG Whisperer: An Arduino library for JTAG.
By Mike Tsao <http://github.com/sowbug>.
Copyright © 2012 Mike Tsao. Use, modification, and distribution are
subject to the BSD-style license as described in the accompanying
LICENSE file.
See README for complete attributions.
*/
#ifndef INCLUDE_JTAG_WHISPERER_SERIAL_COMM_H
#define INCLUDE_JTAG_WHISPERER_SERIAL_COMM_H
#include <Arduino.h>
#define DEBUG 0
class SerialComm {
public:
SerialComm()
: read_ptr_(buffer_),
write_ptr_(buffer_),
stream_sum_(0),
stream_count_(0) {
Serial.begin(115200);
Ready("XSVF");
}
virtual ~SerialComm() {
Important("Checksum %lx/%lx.", stream_sum_, stream_count_);
Quit("Exiting!");
}
uint8_t GetNextByte() {
uint8_t c = get();
stream_sum_ += c;
++stream_count_;
return c;
}
void Important(const char* format, ...) {
va_list args;
va_start(args, format);
char tmp[128];
vsnprintf(tmp, 128, format, args);
va_end(args);
Serial.print("! ");
Serial.println(tmp);
}
void Ready(const char* message) {
Serial.print("\nR");
Serial.println(message);
}
void Quit(const char* message) {
Serial.print("\nQ ");
Serial.println(message);
}
#if DEBUG == 1
void Debug(const char* format, ...) {
va_list args;
va_start(args, format);
char tmp[128];
vsnprintf(tmp, 128, format, args);
va_end(args);
Serial.print("D ");
Serial.println(tmp);
}
void DebugBytes(const char* s, const uint8_t* p, uint8_t n) {
Serial.print("D ");
Serial.print(s);
print_bytes(p, n);
Serial.println();
}
#else
void Debug(const char* format, ...) {}
void DebugBytes(const char* s, const uint8_t* p, uint8_t n) {}
#endif
private:
void p(const char *fmt, ... ) {
char tmp[128];
va_list args;
va_start(args, fmt);
vsnprintf(tmp, 128, fmt, args);
va_end(args);
Serial.print(tmp);
}
void print_bytes(const uint8_t* pb, uint8_t count) {
while (count--) {
p("%02x", *pb++);
}
}
uint8_t get() {
if (!available()) {
fill();
}
uint8_t next_byte = *read_ptr_++;
if (read_ptr_ == buffer_ + BUFFER_SIZE) {
read_ptr_ -= BUFFER_SIZE;
}
return next_byte;
}
bool available() {
return read_ptr_ != write_ptr_;
}
void fill() {
load();
if (!available()) {
Ready("SEND");
int delay_time = 5;
do {
delay(delay_time);
load();
delay_time = delay_time + delay_time;
} while (!available());
}
}
void load() {
while (Serial.available() > 0) {
uint8_t c = Serial.read();
*write_ptr_++ = c;
if (write_ptr_ == buffer_ + BUFFER_SIZE) {
write_ptr_ -= BUFFER_SIZE;
}
if (!available()) {
Important("Overran serial buffer");
}
}
}
enum { BUFFER_SIZE = 128 };
uint8_t buffer_[BUFFER_SIZE];
uint8_t* read_ptr_;
uint8_t* write_ptr_;
uint32_t stream_sum_;
uint32_t stream_count_;
};
#endif // INCLUDE_JTAG_WHISPERER_SERIAL_COMM_H