/
harleyjg.c
431 lines (376 loc) · 10.9 KB
/
harleyjg.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define F_CPU 12000000
#include <util/delay.h>
/*-------------------------------------------------------------------------*/
#define INACT_TIMEOUT 20 /* s */
#define TIMER_PRESCALER 64
/* timing formulas */
#define MS(x) ((x) * (F_CPU / 1000) / (TIMER_PRESCALER))
#define US(x) (MS(x) / 1000)
#define SECS_TO_OVF(x) ((x) * F_CPU / (TIMER_PRESCALER * 65536))
#define VPW_SHORT_PULSE_MIN 35 /* us */
#define VPW_LONG_PULSE_MIN 97 /* us */
#define VPW_SOF_MIN 164 /* us */
#define VPW_EOF_MIN 240 /* us */
/*-------------------------------------------------------------------------*/
#define OUTSIZE 96
#define MIDSIZE 80
#define JBSIZE 24
static unsigned char jmsgbuf[JBSIZE], midbuf[MIDSIZE], outbuf[OUTSIZE], outhead, outtail, midlen, jmsglen;
#define UPUTC(x) { outbuf[outhead++] = (x); if (outhead >= OUTSIZE) outhead = 0; }
#define UGETC(x) { (x) = outbuf[outtail++]; if (outtail >= OUTSIZE) outtail = 0; }
// time in TOVFs to make the UART RX to TX transparent (dump other traffic).
static unsigned transptime;
// add buffered data from J1850 or PPS or whereever to output buffer
static void sendmid()
{
if( transptime )
return;
unsigned char *mp = midbuf;
while (midlen) {
UPUTC(*mp++);
midlen--;
}
}
/*-------------------------------------------------------------------------*/
static unsigned lasttcnt, hightime, marktime, marklow;
#define checkhitime(t) { if( t < lasttcnt )hightime++; lasttcnt = t;}
/*-------------------------------------------------------------------------*/
static unsigned char innmea; // middle of NMEA, wait for end to add interleaved
// Received character from GPS - put to transmit buffer, but also handle other cases
ISR(USART_RX_vect)
{
unsigned char c = UDR;
if (!c || c == 0xa0)
transptime = SECS_TO_OVF(10);
if (c == '$' && !transptime)
innmea = 1;
UPUTC(c);
if (midlen && (!c || c == '\n')) {
innmea = 0;
// append any queued lines
if (transptime)
midlen = 0; // dump during config
else
sendmid();
}
// enable send
UCSRB |= _BV(UDRIE);
}
// Send next character from transmit buffer
ISR(USART_UDRE_vect)
{
// disable interrupt when buffer empty
if (outhead == outtail) {
UCSRB &= ~_BV(UDRIE);
return;
}
UGETC(UDR);
}
/*-------------------------------------------------------------------------*/
static unsigned char tsout[3];
static void timestamp(unsigned lowtime) {
unsigned long mark = marktime;
mark <<= 16;
mark |= marklow;
checkhitime(lowtime);
unsigned long now = hightime;
now <<= 16;
now |= lowtime;
now -= mark;
if( (now & 0xffff0000UL) == 0xffff0000UL )
now &= 0xffffUL;
if( now > (F_CPU/TIMER_PRESCALER) ) {
now -= (F_CPU/TIMER_PRESCALER);
if( now > 8*(F_CPU/TIMER_PRESCALER) ) { // reset mark
marklow = TCNT1;
marktime = hightime;
now = 0;
}
else {
mark += (F_CPU/TIMER_PRESCALER); // move mark 1 second
marklow = mark;
marktime = mark >> 16;
}
}
while( now >= (F_CPU/TIMER_PRESCALER) )
now -= (F_CPU/TIMER_PRESCALER);
unsigned char digit = '0';
while( now >= (F_CPU/TIMER_PRESCALER/10) )
digit++, now -= (F_CPU/TIMER_PRESCALER/10);
tsout[0] = digit;
digit = '0';
while( now >= (F_CPU/TIMER_PRESCALER/100) )
digit++, now -= (F_CPU/TIMER_PRESCALER/100);
tsout[1] = digit;
digit = '0';
while( now > (F_CPU/TIMER_PRESCALER/1000) && digit < '9')
digit++, now -= (F_CPU/TIMER_PRESCALER/1000);
tsout[2] = digit;
}
// PPS on the UTC second mark
ISR(INT1_vect)
{
if( transptime )
return;
// PORTB |= _BV(PB2);
unsigned t = TCNT1;
timestamp(t);
midbuf[midlen++] = tsout[0];
midbuf[midlen++] = tsout[1];
midbuf[midlen++] = tsout[2];
midbuf[midlen++] = '=';
marktime = hightime;
marklow = t;
// if( TIFR & _BV(TOV1) ) // timer overflowed while in this interrupt
// marktime++;
midbuf[midlen++] = '\r';
midbuf[midlen++] = '\n';
if (!innmea) {
sendmid();
UCSRB |= _BV(UDRIE);
}
// PORTB &= ~_BV(PB2);
}
/*-------------------------------------------------------------------------*/
// set up UART port and configuration, out of reset and deep sleep
static void uart_init()
{
outhead = outtail = midlen = innmea = 0;
/* turn on bluetooth */
DDRB |= _BV(PB1);
PORTB |= _BV(PB1);
DDRB |= _BV(PB2);
PORTB &= ~_BV(PB2);
/* setup serial port */
#define BAUD 115200
#include <util/setbaud.h>
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
#if USE_2X
UCSRA |= (1 << U2X);
#else
UCSRA &= ~(1 << U2X);
#endif
UCSRC = _BV(UCSZ0) | _BV(UCSZ1); /* 8N1 */
UCSRB = _BV(RXEN) | _BV(TXEN); /* enable rx/tx */
}
// send string from flash to local buffer and push it out
static void uart_puts_P(const char *p)
{
char c = pgm_read_byte(p);
while (c) {
midbuf[midlen++] = c;
c = pgm_read_byte(++p);
}
sendmid();
UCSRB |= _BV(UDRIE);
}
/*-------------------------------------------------------------------------*/
static volatile unsigned int lastedge; /* = 0 */
static unsigned char polarity; /* = 0 */
static unsigned char jbitaccum, jbitcnt;
// General setup of timing and J1850 - for out of reset and out of deep sleep
void receiver_init(void)
{
lastedge = 0;
polarity = 0;
jmsglen = 0;
jbitaccum = 0;
jbitcnt = 0;
hightime = 0;
marktime = 0;
marklow = 0;
/* j1850 input - PD6 as input without pullup */
DDRD &= ~_BV(PD6);
PORTD &= ~_BV(PD6);
GTCCR = _BV(PSR10); /* reset prescaler */
TCNT1 = 0; /* reset counter value */
/* activate noise canceller, */
/* trigger on rising edge, clk/8 */
// lower 3 bits is div, off,1,8,64,256,1024,extfall,extris ; CS12,11,10
TCCR1B = _BV(ICES1) | _BV(ICNC1) | _BV(CS11) | _BV(CS10);
/* clear and enable Overflow and Input Capture interrupt */
TIFR |= _BV(TOV1) | _BV(ICF1) | _BV(OCF1B);
TIMSK |= _BV(TOIE1) | _BV(ICIE1) | _BV(OCIE1B);
OCR1B = 32768;
MCUCR |= 0xC;
GIMSK |= 0x80;
}
/*-------------------------------------------------------------------------*/
// Special timing requires assembly to disable brownout detect.
#define sleep_bod_disable() \
do { \
uint8_t tempreg; \
__asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
"ori %[tempreg], %[bods_bodse]" "\n\t" \
"out %[mcucr], %[tempreg]" "\n\t" \
"andi %[tempreg], %[not_bodse]" "\n\t" \
"out %[mcucr], %[tempreg]" \
: [tempreg] "=&d" (tempreg) \
: [mcucr] "I" _SFR_IO_ADDR(BODCR), \
[bods_bodse] "i" (_BV(BPDS) | _BV(BPDSE)), \
[not_bodse] "i" (~_BV(BPDSE))); \
} while (0)
// Wake-up on J1850 edge after sleep
ISR(PCINT_D_vect)
{
#define PCIE2 4
GIMSK &= ~_BV(PCIE2);
}
// Shut things down, deep-sleep until j1850 interrupt, then restart everything
void deepsleep(void)
{
/* disable timers */
TIMSK = 0;
UCSRB &= ~_BV(RXCIE); // disable receives
outhead = outtail = midlen = 0;
uart_puts_P(PSTR("\r\n=Sleep\r\n"));
sei();
while (UCSRB & _BV(UDRIE));
cli();
_delay_ms(20);
/* turn off bluetooth */
PORTB &= ~_BV(PB1);
// DDRB &= ~_BV(PB1);
/* disable UART */
UCSRB = 0;
_delay_ms(1);
/* set RX/TX as inputs in HiZ to prevent leakage */
DDRD &= ~(_BV(PD0) | _BV(PD1));
PORTD &= ~(_BV(PD0) | _BV(PD1));
/* clear and enable bit change interrupt for wakeup */
GIMSK = _BV(PCIE2);
PCMSK2 = _BV(PCINT17);
#define PCIF2 4
EIFR = _BV(PCIF2);
/* power down with BOD disabled, INT will awake */
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
receiver_init();
/* setup uart */
uart_init();
_delay_ms(1);
uart_puts_P(PSTR("=WakeUp\r\n"));
UCSRB |= _BV(RXCIE);
}
/*-------------------------------------------------------------------------*/
ISR(TIMER1_COMPB_vect)
{
checkhitime(TCNT1);
}
volatile unsigned inactime; /* = 0 */
// overflow - used as a coarse counter for long timeouts
ISR(TIMER1_OVF_vect)
{
// hightime++;
checkhitime(TCNT1);
// downcount transparency counter if active
if (transptime) {
transptime--;
return;
}
// downcount inactivity timer
if (inactime > SECS_TO_OVF(INACT_TIMEOUT)) {
if (PIND & _BV(PD4))
return; // don't powerdown while BT active
inactime = 0;
deepsleep();
}
else
++inactime;
}
// J1850 EOD/EOF after 300 or so microseconds.
ISR(TIMER1_COMPA_vect)
{
jbitcnt = jbitaccum = 0;
/* disable compare match interrupt */
TIMSK &= ~_BV(OCIE1A);
lastedge = TCNT1;
/* timeout - J1850 EOD/EOF */
jmsgbuf[jmsglen++] = '\r';
jmsgbuf[jmsglen++] = '\n';
if( midlen + jmsglen < MIDSIZE ){
memcpy(&midbuf[midlen], jmsgbuf, jmsglen);
midlen += jmsglen;
}
jmsglen = 0;
if (!innmea) {
sendmid();
UCSRB |= _BV(UDRIE);
}
}
// Process edge on J1850
ISR(TIMER1_CAPT_vect)
{
unsigned int now, width;
/* toggle interrupt on rising/falling edge */
TCCR1B ^= _BV(ICES1);
now = ICR1;
width = now - lastedge;
lastedge = now;
polarity ^= 1;
/* bad width or outside, will happen at EOD - ignore */
if (width >= US(VPW_EOF_MIN) || width < US(VPW_SHORT_PULSE_MIN))
return;
OCR1A = now + US(VPW_EOF_MIN); /* timeout - EOD */
TIFR |= _BV(OCF1A); /* clear compare match interrupt */
TIMSK |= _BV(OCIE1A); /* enable compare match interrupt */
inactime = 0;
/* doesn't quite do IFRs - normalization bit, crc? */
if (!polarity && width >= US(VPW_SOF_MIN)) {
#if 1
jmsglen = 4;
timestamp(now);
jmsgbuf[0] = tsout[0];
jmsgbuf[1] = tsout[1];
jmsgbuf[2] = tsout[2];
jmsgbuf[3] = 'J';
#else
jmsglen = 1;
jmsgbuf[0] = 'J';
#endif
jbitcnt = jbitaccum = 0;
return;
}
jbitaccum <<= 1;
if ((width < US(VPW_LONG_PULSE_MIN)) ^ polarity)
jbitaccum++;
if (++jbitcnt < 4)
return;
if( jmsglen < 22 )
jmsgbuf[jmsglen++] = jbitaccum > 9 ? 'A' - 10 + jbitaccum : '0' + jbitaccum;
jbitcnt = jbitaccum = 0;
}
/*-------------------------------------------------------------------------*/
int main(void)
{
/* power savings */
PRR = _BV(PRUSI); /* shut down USI */
DIDR = _BV(AIN0D) | _BV(AIN1D); /* disable digital input on analog */
ACSR = _BV(ACD); /* disable analog comparator */
/* setup uart */
uart_init();
receiver_init();
sei();
uart_puts_P(PSTR("=Harley J1850-GPS\r\n"));
UCSRB |= _BV(RXCIE);
for (;;) {
/* idle sleep, INT from J1850 or serial will awake */
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
}