This repository has been archived by the owner on Jul 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.c
242 lines (221 loc) · 4.07 KB
/
comm.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
#include "comm.h"
#include <delay.h>
#include "hardware.h"
#pragma pack(1)
typedef struct
{
uint8_t cmd;
uint16_t data;
} TPacket;
#pragma pack()
#define TYPE_REQUEST_MASTER 'm'
#define TYPE_COLOR_DATA 'c'
#define TYPE_KEY_DATA 'k'
#define WAITING_FOR_PREAMBLE 0
#define WAITING_FOR_SYNC 1
#define WAITING_FOR_DATA 2
#define WAITING_FOR_CHK 3
// vars
//public
uint8_t commIsPaired = 0;
uint8_t commIsLocked = 0;
uint16_t mykey;
//private
uint8_t master = 0;
uint32_t nextBroadcastTime;
uint16_t commonColor;
uint32_t stateChangeTime, lastPing, lastSend;
//recv
int recvState = 0;
uint8_t mychk;
int dataIdx = 0;
TPacket recvPacket;
// funcs
void comm_sendPacket (TPacket* p);
void comm_processPacket ();
void comm_setupTx ();
void comm_setupSense ();
// public
void commInit ()
{
comm_setupTx ();
IO_ALT_OPEN_DRAIN(UART_RX);
IO_ALT_SET(UART_TX, 1);
IO_ALT_SET(UART_RX, 1);
IO_PULLUP(UART_RX);
USART1->BRR = USART_BRR(115200);
USART1->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
USART1->CR3 = USART_CR3_OVRDIS;
ENABLE_INTERRUPT(USART1_IRQn);
nextBroadcastTime = ticks + randminmax (5, 20) * 10;
recvState = WAITING_FOR_PREAMBLE;
}
void commProcess ()
{
if (commIsPaired)
{
comm_setupTx ();
if (ticks - lastPing >= 2000)
{
commIsPaired = 0;
}
else if (ticks - lastSend >= 10)
{
TPacket p;
if (master)
{
p.cmd = TYPE_COLOR_DATA;
p.data = commonColor;
}
else
{
p.cmd = TYPE_KEY_DATA;
p.data = mykey;
}
comm_sendPacket (&p);
lastSend = ticks;
}
}
else
{
if (ticks >= nextBroadcastTime)
{
comm_setupTx ();
TPacket p;
p.cmd = TYPE_REQUEST_MASTER;
p.data = 0xabcd;
USART1->CR1 &= ~USART_CR1_RE;
comm_sendPacket (&p);
USART1->CR1 |= USART_CR1_RE;
nextBroadcastTime = ticks + randminmax (5, 20) * 10;
}
else
{
if (commIsLocked)
{
comm_setupSense ();
if (IO_IS_LOW(UART_TX))
{
commIsLocked = 0;
int i;
for (i = 0; i < 6; i++)
{
TIM3->CCR1 = TIM3->CCR2 = TIM3->CCR4 = 255;
_delay_ms (50);
TIM3->CCR1 = TIM3->CCR2 = TIM3->CCR4 = 0;
_delay_ms (50);
}
}
}
}
}
if (recvState != WAITING_FOR_PREAMBLE)
if (ticks - stateChangeTime >= 200)
recvState = WAITING_FOR_PREAMBLE;
}
// private
void comm_setupTx ()
{
IO_ALT_OPEN_DRAIN(UART_TX);
}
void comm_setupSense ()
{
IO_INPUT(UART_TX);
}
static inline void comm_sendByte (uint8_t b)
{
USART1->TDR = b;
while (!(USART1->ISR & USART_ISR_TC));
}
void comm_sendPacket (TPacket* p)
{
int i;
uint8_t chk = 0xcc;
uint8_t *data = (uint8_t*)p;
comm_sendByte (0xaa);
comm_sendByte (0xab);
for (i = 0; i < sizeof (TPacket); i++)
{
chk ^= data[i];
comm_sendByte (data[i]);
}
comm_sendByte (chk);
}
void comm_processPacket ()
{
TPacket *p = &recvPacket;
switch (p->cmd)
{
case TYPE_REQUEST_MASTER:
if (p->data == 0xabcd)
{
commIsPaired = 1;
master = 0;
}
break;
case TYPE_COLOR_DATA:
if (commIsPaired && !master)
{
showColor (p->data);
commIsLocked = 1;
}
break;
case TYPE_KEY_DATA:
{
commIsPaired = 1;
master = 1;
uint16_t remoteKey = p->data;
commonColor = mykey + remoteKey;
showColor (commonColor);
commIsLocked = 1;
}
break;
}
lastPing = ticks;
}
void USART1_Handler ()
{
if (USART1->ISR & USART_ISR_ORE)
{
USART1->ICR = USART_ICR_ORECF;
}
if (USART1->ISR & USART_ISR_RXNE)
{
uint8_t d = USART1->RDR;
uint8_t *packetData = (uint8_t*)&recvPacket;
switch (recvState)
{
case WAITING_FOR_PREAMBLE:
if (d == 0xaa)
{
recvState = WAITING_FOR_SYNC;
stateChangeTime = ticks;
}
break;
case WAITING_FOR_SYNC:
if (d == 0xab)
{
recvState = WAITING_FOR_DATA;
stateChangeTime = ticks;
dataIdx = 0;
mychk = 0xcc;
}
break;
case WAITING_FOR_DATA:
packetData[dataIdx] = d;
dataIdx++;
mychk ^= d;
if (dataIdx == sizeof (TPacket))
{
recvState = WAITING_FOR_CHK;
stateChangeTime = ticks;
}
break;
case WAITING_FOR_CHK:
if (mychk == d)
comm_processPacket ();
recvState = WAITING_FOR_PREAMBLE;
break;
}
}
}