forked from Schrolli91/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
114 lines (102 loc) · 3.51 KB
/
uart.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
/*
* uart.c -- uart decoder and packet dump
*
* Copyright (C) 2007
*
* 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 2 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* ---------------------------------------------------------------------- */
#include "multimon.h"
#include <string.h>
/* ---------------------------------------------------------------------- */
static void disp_packet(struct demod_state *s, unsigned char *bp, unsigned int len)
{
unsigned char i,j;
(void) s; // Suppress the warning.
if (!bp)
return;
if (!len) {
verbprintf(0, "\n");
return;
}
j = 0;
while (len) {
i = *bp++;
if ((i >= 32) && (i < 128))
verbprintf(0, "%c",i);
else if (i == 13) {
if (j)
verbprintf(0, "\n");
j = 0;
} else
verbprintf(0, "[0x%02X]",i);
if (i >= 32)
j = 1;
len--;
}
if (j)
verbprintf(0, "\n");
}
/* ---------------------------------------------------------------------- */
void uart_init(struct demod_state *s)
{
memset(&s->l2.uart, 0, sizeof(s->l2.uart));
s->l2.uart.rxptr = s->l2.uart.rxbuf;
}
/* ---------------------------------------------------------------------- */
void uart_rxbit(struct demod_state *s, int bit)
{
s->l2.uart.rxbitstream <<= 1;
s->l2.uart.rxbitstream |= !!bit;
if (!s->l2.uart.rxstate) {
switch (s->l2.uart.rxbitstream & 0x03) {
case 0x02: /* start bit */
s->l2.uart.rxstate = 1;
s->l2.uart.rxbitbuf = 0x100;
break;
case 0x00: /* no start bit */
case 0x03: /* consecutive stop bits*/
if ((s->l2.uart.rxptr - s->l2.uart.rxbuf) >= 1)
disp_packet(s, s->l2.uart.rxbuf, s->l2.uart.rxptr - s->l2.uart.rxbuf);
s->l2.uart.rxptr = s->l2.uart.rxbuf;
break;
}
return;
}
if (s->l2.uart.rxbitstream & 1)
s->l2.uart.rxbitbuf |= 0x200;
// verbprintf(7, "b=%c", '0'+(s->l2.uart.rxbitstream & 1));
if (s->l2.uart.rxbitbuf & 1) {
if (s->l2.uart.rxptr >= s->l2.uart.rxbuf+sizeof(s->l2.uart.rxbuf)) {
s->l2.uart.rxstate = 0;
disp_packet(s, s->l2.uart.rxbuf, s->l2.uart.rxptr - s->l2.uart.rxbuf);
verbprintf(1, "Error: packet size too large\n");
return;
}
if ( !(s->l2.uart.rxbitstream & 1) ) {
s->l2.uart.rxstate = 0;
verbprintf(1, "Error: stop bit is 0. Bad framing\n");
return;
}
*s->l2.uart.rxptr++ = s->l2.uart.rxbitbuf >> 1;
// verbprintf(6, "B=%02X ", (s->l2.uart.rxbitbuf >> 1) & 0xff);
// verbprintf(5, "%c", (s->l2.uart.rxbitbuf >> 1) & 0xff);
s->l2.uart.rxbitbuf = 0x100;
s->l2.uart.rxstate = 0;
return;
}
s->l2.uart.rxbitbuf >>= 1;
}
/* ---------------------------------------------------------------------- */