-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.c
73 lines (60 loc) · 1.69 KB
/
main.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
#include <xc.h>
#include <stdint.h>
#include "clock.h"
#include "can.h"
#include "frontend.h"
#include "elm327slcan.h"
#include "rbuf.h"
extern void init(void);
#define RSIZE 128
#define SSIZE 248
char rBuf[RSIZE];
char sBuf[SSIZE];
rbuf_t rRing = { rBuf, 0, 0, RSIZE };
rbuf_t sRing = { sBuf, 0, 0, SSIZE };
uint8_t state = STATE_CONFIG;
void __interrupt(high_priority) myIsr(void) {
uint8_t data;
if (UxTXIE && UxTXIF) {
if (rbuf_pop_isr((rbuf_t *)&sRing, &data))
UxTXREG = data;
else
cmdStop();
}
if (UxRCIF && UxRCIE) {
if (!rbuf_push_isr((rbuf_t *)&rRing, UxRCREG))
SET_TX_OVFL();
}
}
int main(void) {
uint8_t ch, num, txtbuf[32];
char line[LINE_MAXLEN];
uint8_t linepos = 0;
canmsg_t canmsg_buffer;
init();
while (1) {
if (state != STATE_CONFIG) {
while (can_receive_message(&canmsg_buffer)) {
num = canmsg2ascii(&canmsg_buffer, &txtbuf);
if (num < rbuf_free_items((rbuf_t *)&sRing)) {
for (ch = 0; ch < num; ch++) {
rbuf_push((rbuf_t *)&sRing, txtbuf[ch]);
}
cmdSend();
}
else SET_RX_OVFL();
}
}
while (rbuf_pop((rbuf_t *)&rRing, &ch)) {
if (ch == CR) {
line[linepos] = 0;
parseLine(line);
linepos = 0;
} else if (ch != LR) {
line[linepos] = ch;
if (linepos < LINE_MAXLEN - 1) linepos++;
}
}
}
return 0;
}