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
|
#include <inttypes.h>
#include <avr/io.h>
#include "uart.h"
#include "common.h"
#include "protocol.h"
void uart_parser(char *buf)
{
answer[0] = buf[0];
switch(buf[0])
{
case CMD_UART_INIT:
uart_init_usb((char)buf[2]);
break;
case CMD_UART_DEINIT:
uart_deinit_usb((char)buf[2]);
break;
case CMD_UART_BAUDRATE:
uart_baudrate_usb((char)buf[2], (char)buf[3], (char)buf[4], (char)buf[5], (char)buf[6]);
break;
case CMD_UART_STOPBITS:
uart_stopbits_usb((char)buf[2], (char)buf[3]);
break;
case CMD_UART_DATABITS:
uart_stopbits_usb((char)buf[2], (char)buf[3]);
break;
case CMD_UART_PARITY:
uart_parity_usb((char)buf[2], (char)buf[3]);
break;
case CMD_UART_SEND:
uart_send_usb((char)buf[2], (char)buf[3], &buf[4]);
break;
case CMD_UART_RECV:
uart_recv_usb((char)buf[2], (char)buf[3]);
break;
default:
answer[1] = RSP_UNKOWN_CMD;
answer[2] = 0;
CommandAnswer(3);
}
}
void uart_init_usb(char uartport)
{
answer[1] = (unsigned char)uart_init(uartport);
answer[2] = 0;
CommandAnswer(3);
}
char uart_init(char uartport)
{
switch(uartport)
{
case 0:
octopus.ports[41] = PIN_UART;
octopus.ports[42] = PIN_UART;
/* enable transmitter receiver */
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
break;
case 1:
octopus.ports[14] = PIN_UART;
octopus.ports[15] = PIN_UART;
/* enable transmitter receiver */
UCSR1B = (1 << RXEN1) | (1 << TXEN1);
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_deinit_usb(char uartport)
{
answer[1] = (unsigned char)uart_deinit(uartport);
answer[2] = 0;
CommandAnswer(3);
}
char uart_deinit(char uartport)
{
switch(uartport)
{
case 0:
octopus.ports[41] = PIN_NONE;
octopus.ports[42] = PIN_NONE;
UBRR0H = 0;
UBRR0L = 0;
UCSR0B = 0;
UCSR0C = 0;
break;
case 1:
octopus.ports[14] = PIN_NONE;
octopus.ports[15] = PIN_NONE;
UBRR1H = 0;
UBRR1L = 0;
UCSR1B = 0;
UCSR1C = 0;
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_baudrate_usb(char uartport, char ubrrll, char ubrrlh, char ubrrhl, char ubrrhh)
{
answer[1] = (unsigned char)uart_baudrate(uartport, ubrrll, ubrrlh, ubrrhl, ubrrhh);
answer[2] = 0;
CommandAnswer(3);
}
char uart_baudrate(char uartport, char ubrrll, char ubrrlh, char ubrrhl, char ubrrhh)
{
switch(uartport)
{
case 0:
UBRR0H = (ubrrhh << 8) | ubrrhl;
UBRR0L = (ubrrhl << 8) | ubrrll;
break;
case 1:
UBRR1H = (ubrrhh << 8) | ubrrhl;
UBRR1L = (ubrrhl << 8) | ubrrll;
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_stopbits_usb(char uartport, char stopbits)
{
answer[1] = (unsigned char)uart_stopbits(uartport, stopbits);
answer[2] = 0;
CommandAnswer(3);
}
char uart_stopbits(char uartport, char stopbits)
{
char usbs;
switch(stopbits)
{
case 1: usbs = 0; break;
case 2: usbs = 2; break;
default: usbs = 0; break;
}
switch(uartport)
{
case 0:
UCSR0C |= (usbs << USBS0);
break;
case 1:
UCSR1C |= (usbs << USBS1);
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_databits_usb(char uartport, char databits)
{
answer[1] = (unsigned char)uart_databits(uartport, databits);
answer[2] = 0;
CommandAnswer(3);
}
char uart_databits(char uartport, char databits)
{
char ucsz1,ucsz0;
switch(databits) {
case 5: ucsz1 = 0; ucsz0 = 0; break;
case 6: ucsz1 = 0; ucsz0 = 1; break;
case 7: ucsz1 = 1; ucsz0 = 0; break;
case 8: ucsz1 = 1; ucsz0 = 1; break;
default: ucsz1 = 1; ucsz0 = 1; break;
}
switch(uartport)
{
case 0:
UCSR0C |= (ucsz1 << UCSZ01) | (ucsz0 << UCSZ00);
break;
case 1:
UCSR1C |= (ucsz1 << UCSZ11) | (ucsz0 << UCSZ10);
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_parity_usb(char uartport, char parity)
{
answer[1] = (unsigned char)uart_databits(uartport, parity);
answer[2] = 0;
CommandAnswer(3);
}
char uart_parity(char uartport, char parity)
{
char upm1, upm0;
switch(parity)
{
case 'N': upm1 = 0; upm0 = 0; break;
case 'E': upm1 = 1; upm0 = 0; break;
case 'O': upm1 = 1; upm0 = 1; break;
default: upm1 = 0; upm0 = 0; break;
}
switch(uartport)
{
case 0:
UCSR0C |= (upm1 << UPM01) | (upm0 << UPM00);
break;
case 1:
UCSR1C |= (upm1 << UPM11) | (upm0 << UPM10);
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_send_usb(char uartport, char length, char *buf)
{
answer[1] = (unsigned char)uart_send(uartport, length, buf);
answer[2] = 0;
CommandAnswer(3);
}
char uart_send(char uartport, unsigned int length, char *buf)
{
if(uartport != 0 && uartport != 1)
return RSP_UNKOWN_PIN;
while(length--)
uart_putchar(uartport, *buf++);
return RSP_OK;
}
char uart_putchar(char uartport, char data)
{
switch(uartport)
{
case 0:
/* Wait for empty transmit buffer */
while (!(UCSR0A & (1 << UDRE0)));
/* Put data into buffer, sends the data */
UDR0 = data;
break;
case 1:
/* Wait for empty transmit buffer */
while (!(UCSR1A & (1 << UDRE1)));
/* Put data into buffer, sends the data */
UDR1 = data;
break;
default:
return RSP_UNKOWN_PIN;
}
return RSP_OK;
}
void uart_recv_usb(char uartport, int length)
{
char data[length];
int i;
answer[1] = (char)uart_recv(uartport, data, length);
for(i = 0; i < length; i++)
answer[2+i] = data[i];
answer[2+length] = 0;
CommandAnswer(3+length);
}
char uart_recv(char uartport, char * buf, int length)
{
if(uartport != 0 && uartport != 1)
return RSP_UNKOWN_PIN;
while(length-- > 0)
*buf++ = uart_getchar(uartport);
return RSP_OK;
}
char uart_getchar(char uartport)
{
char c;
switch(uartport)
{
case 0:
while (!(UCSR0A & (1 << RXC0)));
c = UDR0;
break;
case 1:
while (!(UCSR1A & (1 << RXC1)));
c = UDR1;
break;
default:
return RSP_UNKOWN_PIN;
}
return c;
}
|