forked from bootc/PolyController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
106 lines (82 loc) · 2.21 KB
/
serial.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
/*
* This file is part of the PolyController firmware source code.
* Copyright (C) 2011 Chris Boot.
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <contiki.h>
#include <dev/serial-line.h>
#include <stdio.h>
#include <avr/io.h>
#include <init.h>
#include "serial.h"
#include "drivers/uart.h"
PROCESS(serial_process, "Serial");
INIT_PROCESS(serial_process);
static int serial_putc(char c, FILE *stream) {
if (c == '\n') {
uart_putc('\r');
}
uart_putc(c);
return 0;
}
static FILE uart_stream =
FDEV_SETUP_STREAM(serial_putc, NULL, _FDEV_SETUP_WRITE);
void serial_init(void) {
#define BAUD CONFIG_UART0_BAUD
#include <util/setbaud.h>
uart_init(
(UBRRH_VALUE << 8) |
(UBRRL_VALUE << 0) |
(USE_2X ? 0x8000 : 0));
#if CONFIG_UART1_BAUD
#undef BAUD
#define BAUD CONFIG_UART1_BAUD
#include <util/setbaud.h>
uart1_init(
(UBRRH_VALUE << 8) |
(UBRRL_VALUE << 0) |
(USE_2X ? 0x8000 : 0));
#endif
stdout = &uart_stream;
}
static void pollhandler(void) {
process_poll(&serial_process);
while (1) {
// Read a character
uint16_t c = uart_getc();
// Buffer is empty?
if (c & UART_NO_DATA) {
break;
}
uart_putc(c);
// Send it into contiki
serial_line_input_byte(c);
// Handle '\r' and add an extra '\n'
if ((c & 0xff) == '\r') {
uart_putc('\n');
serial_line_input_byte('\n');
}
};
}
PROCESS_THREAD(serial_process, ev, data) {
PROCESS_POLLHANDLER(pollhandler());
PROCESS_BEGIN();
serial_line_init();
process_poll(&serial_process);
PROCESS_WAIT_UNTIL(ev == PROCESS_EVENT_EXIT);
PROCESS_END();
}