Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / hal_uart.c
1 /* -*- c -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "hal_uart.h"
20 #include "hal_io.h"
21 #include "memory_map.h"
22
23 // First pass, no interrupts
24
25 // Replaced with divisors.py which generates best divisor
26 //#define CALC_DIVISOR(rate) (WISHBONE_CLK_RATE / ((rate) * 16))
27
28 #define NSPEEDS 6
29 #define MAX_WB_DIV 4
30
31 static const uint16_t
32 divisor_table[MAX_WB_DIV+1][NSPEEDS] = {
33   {   2,   2,   2,   2,  2,  2},   // 0: can't happen
34   { 651, 326, 163, 109, 54, 27 },  // 1: 100 MHz
35   { 326, 163,  81,  54, 27, 14 },  // 2:  50 MHz
36   { 217, 109,  54,  36, 18,  9 },  // 3:  33.3333 MHz
37   { 163,  81,  41,  27, 14,  7 },  // 4:  25 MHz
38 };
39
40 #define u uart_regs
41
42 void
43 hal_uart_init(void)
44 {
45   u->clkdiv = 217;  // 230400 bps
46 }
47
48 void
49 hal_uart_putc(int ch)
50 {
51   if (ch == '\n')               // FIXME for now map \n -> \r\n
52     hal_uart_putc('\r');
53
54   while (u->txlevel == 0)        // wait for fifo to have space
55     ;
56
57   u->txchar = ch;
58 }
59
60 void
61 hal_uart_putc_nowait(int ch)
62 {
63   if (ch == '\n')               // FIXME for now map \n -> \r\n
64     hal_uart_putc('\r');
65
66   if(u->txlevel)   // If fifo has space
67     u->txchar = ch;
68 }
69
70 int
71 hal_uart_getc(void)
72 {
73   while ((u->rxlevel) == 0)  // wait for data to be ready
74     ;
75
76   return u->rxchar;
77 }