forked from ScribbleJ/sjfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.cpp
157 lines (126 loc) · 2.26 KB
/
Host.cpp
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
/*
(c) 2011 Christopher "ScribbleJ" Jansen
*/
#include "Host.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
#include "GcodeQueue.h"
#include "config.h"
Host::Host(unsigned long BAUD, int port_in)
: rxring(HOST_RECV_BUFSIZE, rxbuf), txring(HOST_SEND_BUFSIZE, txbuf)
{
input_ready = 0;
port = port_in;
#ifdef HIGHPORTS
#ifdef HAS_BT
if(port == 2)
Init2(BAUD);
else
#endif
#endif
Init0(BAUD);
}
void Host::Init0(unsigned long BAUD)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
PRR0 &= ~(MASK(PRUSART0));
if(BAUD > 38401)
{
UCSR0A = MASK(U2X0);
UBRR0 = (((F_CPU / 8) / BAUD) - 0.5);
}
else
{
UCSR0A = 0;
UBRR0 = (((F_CPU / 16) / BAUD) - 0.5);
}
UCSR0B = MASK(RXEN0) | MASK(TXEN0);
UCSR0C = MASK(UCSZ01) | MASK(UCSZ00);
UCSR0B |= MASK(RXCIE0) | MASK(UDRIE0);
}
}
void Host::Init2(unsigned long BAUD)
{
#ifdef HIGHPORTS
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
PRR1 &= ~MASK(PRUSART2);
if(BAUD > 38401)
{
UCSR2A = MASK(U2X2);
UBRR2 = (((F_CPU / 8) / BAUD) - 0.5);
}
else
{
UCSR2A = 0;
UBRR2 = (((F_CPU / 16) / BAUD) - 0.5);
}
UCSR2B = MASK(RXEN2) | MASK(TXEN2);
UCSR2C = MASK(UCSZ21) | MASK(UCSZ20);
UCSR2B |= MASK(RXCIE2) | MASK(UDRIE2);
}
#endif
}
void Host::scan_input()
{
if(input_ready == 0)
return;
if(GCODES.isFull())
return;
char buf[MAX_GCODE_FRAG_SIZE];
uint8_t len;
// HOST.labelnum("Input fragments ready:", input_ready);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
input_ready--;
}
for(len=0;len<MAX_GCODE_FRAG_SIZE;len++)
{
buf[len] = rxring.pop();
if(buf[len] <= 32)
break;
}
// HOST.labelnum("Fragment length:", len);
if(len == MAX_GCODE_FRAG_SIZE)
{
rxerror("Frag Over");
return;
}
#ifdef HAS_BT
#ifdef BT_DEBUG
uint8_t temp = buf[len];
buf[len] = 0;
int other = 0;
if(port == 0) other = 2;
Host::Instance(other).write("COM: ");
Host::Instance(other).write(buf);
Host::Instance(other).endl();
buf[len] = temp;
#endif
#endif
GCODES.parsebytes(buf, len, port);
}
/*** INTERRUPT HANDLERS ***/
ISR(USART0_RX_vect)
{
HOST.rx_interrupt_handler0();
}
ISR(USART0_UDRE_vect)
{
HOST.udre_interrupt_handler0();
}
#ifdef HIGHPORTS
ISR(USART2_RX_vect)
{
#ifdef HAS_BT
BT.rx_interrupt_handler2();
#endif
}
ISR(USART2_UDRE_vect)
{
#ifdef HAS_BT
BT.udre_interrupt_handler2();
#endif
}
#endif