-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.c
328 lines (272 loc) · 8.24 KB
/
serial.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (c) 2011 Steve Chamberlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this hardware, software, and associated documentation
files (the "Product"), to deal in the Product without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Product, and to permit persons to whom the Product is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Product.
THE PRODUCT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE PRODUCT OR THE USE OR OTHER DEALINGS IN THE PRODUCT.
*/
/*
* serial.c
*
* bit-bang 8N1 serial interface using the MOSI and MISO pins
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "serial.h"
#include "speaker.h"
#include "sampling.h"
#include "clock.h"
#include "hikea.h"
#define SERIAL_IN PB4
#define SERIAL_OUT PB3
#define CMD_VERSION '1'
#define CMD_GETGRAPHS '2'
#define CMD_GETSNAPSHOTS '3'
// determine how many clock cycles in one 26 microsecond bit time at 38400 bps
#ifdef LOGGER_CLASSIC
// 1000000 MHz / 38400 bps = 26 cycles per bit. Select a timer compare value of 25 to get a 26 cycle period.
// #define BIT_TIME 25
/* Experimentally, a value of 25 always seems to be about 3-4% too slow, even with a calibrated oscillator.
Why? An off by one error somehow? Using 24 gets much better results in my tests. Is this just a side-effect of
my particular ATmega chip and battery voltage, and it will be too fast for someone else's Logger?
*/
#define BIT_TIME 24
#endif
#ifdef LOGGER_MINI
// 8000000 MHz / 38400 bps = 208 cycles per bit. Select a timer compare value of 207 to get a 208 cycle period.
#define BIT_TIME 207
#endif
uint8_t checksum;
void SerialInit()
{
// enable the internal pull-up for serial in
PORTB |= (1<<SERIAL_IN);
}
void SerialBegin()
{
// turn off the speaker beeps, so we can use the timers without interference
SpeakerOff();
// configure the timer for use with serial communication
// setup timer 1 for note length of approximately 100 ms
PRR &= ~(1<<PRTIM1); // turn on the timer hardware
TCCR1A = 0; // clear timer on compare match (CTC) OCR0A mode, (WGM13 and WGM12 in TCCR1B)
TCCR1B = (1<<CS10) | (1<<WGM12) ; // prescale clock/1 (no prescale)
// set the timer compare value.
// AVR datasheet section 16.3: To do a 16-bit write, the high byte must be written before the low byte.
OCR1AH = 0;
OCR1AL = BIT_TIME;
TIFR1 = (1 << OCF1A); // clear the timer 1 compare A match interrupt flag
PORTB |= (1<<SERIAL_OUT);
}
void SerialEnd()
{
PRR |= (1<<PRTIM1);
}
void SerialDoCommand()
{
SerialBegin();
uint8_t commandPrefix[] = { 'C', 'M', 'D' };
uint8_t index = 0;
uint8_t readCount = 0;
// wait for command prefix "CMD"
while (readCount < 5)
{
uint8_t c = SerialReceiveByte();
if (c == commandPrefix[index])
{
index++;
if (index == 3)
{
uint8_t cmd = SerialReceiveByte();
if (cmd != 0)
{
SerialDispatchCommand(cmd);
}
break;
}
}
else if (c == commandPrefix[0])
{
index = 1;
}
else
{
index = 0;
}
readCount++;
}
SerialEnd();
}
void SerialDispatchCommand(uint8_t cmd)
{
// send "LOG" prefix
SerialSendByte('L');
SerialSendByte('O');
SerialSendByte('G');
// send the result of the command
checksum = 0;
switch (cmd)
{
case CMD_VERSION: // version
for (PGM_P p = versionStr; pgm_read_byte(p) != 0; p++)
{
SerialSendByte(pgm_read_byte(p));
}
SerialSendByte(0);
break;
case CMD_GETGRAPHS:
SerialSendGraphs();
break;
case CMD_GETSNAPSHOTS:
SerialSendSnapshots();
break;
default:
// unrecognized command- do nothing
break;
}
// send the checksum
SerialSendByte(checksum);
}
void SerialSendGraphs()
{
// graphs version number
SerialSendByte(1);
// number of graphs
SerialSendByte(NUM_TIME_SCALES);
// samples per graph
SerialSendByte(SAMPLES_PER_GRAPH);
// "now" time reference for the graphs:
// graph g is series of samples from the "now" time back to now - SAMPLES_PER_GRAPH * minutesPerSample[g]
SerialSendByte(clock_second);
SerialSendByte(clock_minute);
SerialSendByte(clock_hour);
SerialSendByte(clock_day);
SerialSendByte(clock_month);
SerialSendByte(clock_year); // year - 2000
// for each graph g, send minutesPerSample[g] followed by the sample data
for (uint8_t g=0; g<NUM_TIME_SCALES; g++)
{
SerialSendByte(minutesPerSample[g] >> 8); // hi byte - TODO: should be big or little endian?
SerialSendByte(minutesPerSample[g] & 0xFF); // low byte
uint8_t index = GetTimescaleNextSampleIndex(g);
for (uint8_t s=0; s<SAMPLES_PER_GRAPH; s++)
{
Sample* pSample = GetSample(g, index);
index++;
if (index == SAMPLES_PER_GRAPH)
{
index = 0;
}
for (uint8_t i=0; i<sizeof(Sample); i++)
{
SerialSendByte(*((uint8_t*)pSample + i));
}
}
}
}
void SerialSendSnapshots()
{
// snapshot version number
SerialSendByte(1);
// number of snapshots
uint8_t numSnapshots = GetNumSnapshots();
SerialSendByte(numSnapshots);
int index = GetNewestSnapshotIndex() + 1 - numSnapshots;
if (index < 0)
{
index += GetMaxSnapshots();
}
for (uint8_t s=0; s<numSnapshots; s++)
{
Snapshot* pSnapshot = GetSnapshot(index);
index++;
if (index == GetMaxSnapshots())
{
index = 0;
}
for (uint8_t i=0; i<sizeof(Snapshot); i++)
{
SerialSendByte(*((uint8_t*)pSnapshot + i));
}
}
}
void SerialSendByte(uint8_t c)
{
checksum ^= c;
// mark
PORTB |= (1<<SERIAL_OUT);
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer - might be just a fractional timer period
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer
// start bit
PORTB &= ~(1<<SERIAL_OUT);
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer
// shift out the data byte, LSB first
for (uint8_t i=0; i<8; i++)
{
if ((c & 0x1) != 0)
PORTB |= (1<<SERIAL_OUT);
else
PORTB &= ~(1<<SERIAL_OUT);
c = c >> 1;
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer
}
// no parity
// stop bit
PORTB |= (1<<SERIAL_OUT);
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer
}
uint8_t SerialReceiveByte()
{
uint8_t c = 0;
uint16_t try = 0;
// wait for a mark followed by a start bit, up to 19200 bit times (half a second)
while (bit_is_clear(PINB, SERIAL_IN))
{
// timer compare match flag is set?
if (bit_is_set(TIFR1, OCF1A))
{
TIFR1 = (1 << OCF1A); // clear the compare match flag
try++;
if (try == 19200)
return 0;
}
}
while (bit_is_set(PINB, SERIAL_IN))
{
// timer compare match flag is set?
if (bit_is_set(TIFR1, OCF1A))
{
TIFR1 = (1 << OCF1A); // clear the compare match flag
try++;
if (try == 19200)
return 0;
}
}
// delay half a bit time, minus a few clock cycles to allow for the time needed to respond to the timer
__builtin_avr_delay_cycles(((BIT_TIME+1)>>1)-8);
// reset the timer
TCNT1H = 0;
TCNT1L = 0;
// shift in the data byte, LSB first
for (uint8_t i=0; i<8; i++)
{
TIFR1 = (1 << OCF1A); // clear the compare match flag
loop_until_bit_is_set(TIFR1, OCF1A); // wait for the timer
if (bit_is_set(PINB, SERIAL_IN))
c = (0x80 | (c >> 1));
else
c = (c >> 1);
}
return c;
}