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
|
/* -*- c -*- */
/*
* Copyright 2007,2008 Free Software Foundation, Inc.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "memory_map.h"
#include "hal_uart.h"
#include "hal_io.h"
#include "mdelay.h"
//just to save you from going insane, note that firmware/FPGA UARTs [0-2] correspond to serial ports [1-3].
//so in software, we refer to UART_DEBUG as UART0, but it transmits on pin TXD<1>. see the UART assignments in hal_uart.h.
#define NSPEEDS 6
#define MAX_WB_DIV 4
//if you're going to recalculate the divisors, it's just uart_clock_rate / baud_rate.
//uart_clock_rate is 50MHz for USRP2.
static const uint16_t
divisor_table[NSPEEDS] = {
5208, // 9600
2604, // 19200
1302, // 38400
868, // 57600
434, // 115200
217 // 230400
};
static char uart_mode[4] = {
[UART_DEBUG] = UART_MODE_ONLCR,
[UART_EXP] = UART_MODE_ONLCR,
[UART_GPS] = UART_MODE_ONLCR
};
static char uart_speeds[4] = {
[UART_DEBUG] = US_230400,
[UART_EXP] = US_230400,
[UART_GPS] = US_115200
};
void
hal_uart_set_mode(hal_uart_name_t uart, int mode)
{
uart_mode[uart] = mode;
}
void hal_uart_set_speed(hal_uart_name_t uart, hal_uart_speed_t speed)
{
uart_regs[uart].clkdiv = divisor_table[speed];
}
void
hal_uart_init(void)
{
for(int i = 0; i < 3; i++) {
hal_uart_set_mode(i, uart_mode[i]);
hal_uart_set_speed(i, uart_speeds[i]);
}
}
void
hal_uart_putc(hal_uart_name_t u, int ch)
{
if ((ch == '\n') && (uart_mode[u] == UART_MODE_ONLCR)) //map \n->\r\n if necessary
hal_uart_putc(u, '\r');
while (uart_regs[u].txlevel == 0) // wait for fifo to have space
;
uart_regs[u].txchar = ch;
}
void
hal_uart_putc_nowait(hal_uart_name_t u, int ch)
{
if ((ch == '\n') && (uart_mode[u] == UART_MODE_ONLCR)) //map \n->\r\n if necessary
hal_uart_putc(u, '\r');
if(uart_regs[u].txlevel) // If fifo has space
uart_regs[u].txchar = ch;
}
int
hal_uart_getc(hal_uart_name_t u)
{
while ((uart_regs[u].rxlevel) == 0) // wait for data to be ready
;
return uart_regs[u].rxchar;
}
int
hal_uart_getc_timeout(hal_uart_name_t u)
{
int timeout = 0;
while (((uart_regs[u].rxlevel) == 0) && (timeout++ < HAL_UART_TIMEOUT_MS))
mdelay(1);
return (uart_regs[u].rxlevel == 0) ? 0 : uart_regs[u].rxchar;
}
int hal_uart_rx_flush(hal_uart_name_t u)
{
char x;
while(uart_regs[u].rxlevel) x = uart_regs[u].rxchar;
return x;
}
|