81c315c8b84b1443932e654849392faa0d124cde
[aversive.git] / projects / microb2010 / tests / beacon_tsop / uart_proto.c
1 /*  
2  *  Copyright Droids Corporation (2010)
3  *  Olivier Matz <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: main.c,v 1.8 2009-05-02 10:08:09 zer0 Exp $
20  *
21  */
22
23 #include <aversive.h>
24 #include <aversive/wait.h>
25
26 #include <pid.h>
27 #include <pwm_ng.h>
28 #include <parse.h>
29 #include <rdline.h>
30 #include <uart.h>
31
32 #include "cmdline.h"
33 #include "main.h"
34 #include "board2006.h"
35 //#include "board2010.h"
36
37 #define UART_NUM 0
38
39 #if UART_NUM == 0
40
41 #define UCSRxA UCSR0A
42 #define UCSRxB UCSR0B
43 #define UCSRxC UCSR0C
44 #define RXCx RXC0
45 #define UDRx UDR0
46 #define UDREx UDRE0
47 #define U2Xx U2X0
48 #define RXENx RXEN0
49 #define TXENx TXEN0
50 #define UCSZx0 UCSZ00
51 #define UCSZx1 UCSZ01
52 #define UBRRxL UBRR0L
53 #define UBRRxH UBRR0H
54
55 #elif  UART_NUM == 1
56
57 #define UCSRxA UCSR1A
58 #define UCSRxB UCSR1B
59 #define UCSRxC UCSR1C
60 #define RXCx RXC1
61 #define UDRx UDR1
62 #define UDREx UDRE1
63 #define U2Xx U2X1
64 #define RXENx RXEN1
65 #define TXENx TXEN1
66 #define UCSZx0 UCSZ10
67 #define UCSZx1 UCSZ11
68 #define UBRRxL UBRR1L
69 #define UBRRxH UBRR1H
70
71 #endif
72
73 void uart_proto_init(void)
74 {
75         UCSRxA = _BV(U2Xx);
76         UCSRxB = _BV(RXENx) | _BV(TXENx);
77         UCSRxC = _BV(UCSZx1) | _BV(UCSZx0); /* 8 bits no parity 1 stop */
78         UBRRxL = 34; /* 57600 at 16 Mhz */
79         UBRRxH = 0;
80 }
81
82 static void uart_proto_send(char c) 
83
84         while ( !( UCSRxA & (1<<UDREx)) ) ;
85         UDRx = c; 
86 }
87
88
89 /* transmit an integer between 0 and 16384 */
90 static void xmit_int14(uint16_t x)
91 {
92         uint8_t c;
93
94         c = x & 0x7f;
95         c |= 0x80;
96         uart_proto_send(c);
97
98         x >>= 7;
99         c = x & 0x7f;
100         c |= 0x80;
101         uart_proto_send(c);
102 }
103
104 void xmit_opp(uint16_t d, uint16_t a)
105 {
106         uart_proto_send(0);
107         xmit_int14(d);
108         xmit_int14(a);
109 }
110
111 void xmit_static(uint16_t x, uint16_t y, uint16_t a)
112 {
113         uart_proto_send(1);
114         xmit_int14(x);
115         xmit_int14(y);
116         xmit_int14(a);
117 }