beacon, prepare integration
[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
35 #define UART_NUM 0
36
37 #if UART_NUM == 0
38
39 #define UCSRxA UCSR0A
40 #define UCSRxB UCSR0B
41 #define UCSRxC UCSR0C
42 #define RXCx RXC0
43 #define UDRx UDR0
44 #define UDREx UDRE0
45 #define U2Xx U2X0
46 #define RXENx RXEN0
47 #define TXENx TXEN0
48 #define UCSZx0 UCSZ00
49 #define UCSZx1 UCSZ01
50 #define UBRRxL UBRR0L
51 #define UBRRxH UBRR0H
52
53 #elif  UART_NUM == 1
54
55 #define UCSRxA UCSR1A
56 #define UCSRxB UCSR1B
57 #define UCSRxC UCSR1C
58 #define RXCx RXC1
59 #define UDRx UDR1
60 #define UDREx UDRE1
61 #define U2Xx U2X1
62 #define RXENx RXEN1
63 #define TXENx TXEN1
64 #define UCSZx0 UCSZ10
65 #define UCSZx1 UCSZ11
66 #define UBRRxL UBRR1L
67 #define UBRRxH UBRR1H
68
69 #endif
70
71 void uart_proto_init(void)
72 {
73         UCSRxA = _BV(U2Xx);
74         UCSRxB = _BV(RXENx) | _BV(TXENx);
75         UCSRxC = _BV(UCSZx1) | _BV(UCSZx0); /* 8 bits no parity 1 stop */
76         UBRRxL = 34; /* 57600 at 16 Mhz */
77         UBRRxH = 0;
78 }
79
80 static void uart_proto_send(char c)
81 {
82         while ( !( UCSRxA & (1<<UDREx)) ) ;
83         UDRx = c;
84 }
85
86 int16_t uart_proto_recv(void)
87 {
88         if ( !(UCSRxA & (1<<RXCx)) )
89                 return -1;
90         return UDRx;
91 }
92
93 /* transmit an integer between 0 and 16384 */
94 static void xmit_int14(uint16_t x)
95 {
96         uint8_t c;
97
98         c = x & 0x7f;
99         c |= 0x80;
100         uart_proto_send(c);
101
102         x >>= 7;
103         c = x & 0x7f;
104         c |= 0x80;
105         uart_proto_send(c);
106 }
107
108 void xmit_opp(uint16_t d, uint16_t a)
109 {
110         uart_proto_send(0);
111         xmit_int14(d);
112         xmit_int14(a);
113 }
114
115 void xmit_static(uint16_t x, uint16_t y, uint16_t a)
116 {
117         uart_proto_send(1);
118         xmit_int14(x);
119         xmit_int14(y);
120         xmit_int14(a);
121 }