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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//
// Copyright 2013 Ettus Research LLC
//
// 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 "x300_impl.hpp"
#include <uhd/types/wb_iface.hpp>
#include "x300_regs.hpp"
#include <uhd/utils/msg.hpp>
#include <uhd/types/serial.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp>
using namespace uhd;
struct x300_uart_iface : uart_iface
{
x300_uart_iface(wb_iface::sptr iface):
rxoffset(0), txoffset(0), txword32(0), rxpool(0), txpool(0), poolsize(0)
{
_iface = iface;
rxoffset = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_RX_INDEX));
txoffset = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_TX_INDEX));
rxpool = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_RX_ADDR));
txpool = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_TX_ADDR));
poolsize = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_WORDS32));
_rxcache.resize(poolsize);
_last_device_rxoffset = rxoffset;
//this->write_uart("HELLO UART\n");
//this->read_uart(0.1);
}
void putchar(const char ch)
{
txoffset = (txoffset + 1) % (poolsize*4);
const int shift = ((txoffset%4) * 8);
if (shift == 0) txword32 = 0;
txword32 |= boost::uint32_t(ch) << shift;
_iface->poke32(SR_ADDR(txpool, txoffset/4), txword32);
_iface->poke32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_TX_INDEX), txoffset);
}
void write_uart(const std::string &buff)
{
boost::mutex::scoped_lock(_write_mutex);
BOOST_FOREACH(const char ch, buff)
{
if (ch == '\n') this->putchar('\r');
this->putchar(ch);
}
}
int getchar(void)
{
if (rxoffset == _last_device_rxoffset)
return -1;
rxoffset++;
return static_cast<int>(_rxcache[(rxoffset/4) % poolsize] >> ((rxoffset%4)*8) & 0xFF);
}
void update_cache(void)
{
boost::uint32_t device_rxoffset = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_RX_INDEX));
boost::uint32_t delta = device_rxoffset - rxoffset;
while (delta)
{
if (delta >= poolsize*4)
{
// all the data is new - reload the entire cache
for (boost::uint32_t i = 0; i < poolsize; i++)
_rxcache[i] = _iface->peek32(SR_ADDR(rxpool, i));
// set rxoffset to the end of the first string
rxoffset = device_rxoffset - (poolsize*4) + 1;
while (static_cast<char>((_rxcache[(rxoffset/4) % poolsize] >> ((rxoffset%4)*8) & 0xFF)) != '\n')
++rxoffset;
// clear the partial string in the buffer;
_rxbuff.clear();
}
else if (rxoffset == _last_device_rxoffset)
{
// new data was added - refresh the portion of the cache that was updated
for (boost::uint32_t i = ((_last_device_rxoffset+1)/4) % poolsize; i != (((device_rxoffset)/4)+1) % poolsize; i = (i+1) % poolsize)
{
_rxcache[i] = _iface->peek32(SR_ADDR(rxpool, i));
}
} else {
// there is new data, but we aren't done with what we have - check back later
break;
}
_last_device_rxoffset = device_rxoffset;
// check again to see if anything changed while we were updating the cache
device_rxoffset = _iface->peek32(SR_ADDR(X300_FW_SHMEM_BASE, X300_FW_SHMEM_UART_RX_INDEX));
delta = device_rxoffset - rxoffset;
}
}
std::string read_uart(double timeout)
{
boost::mutex::scoped_lock(_read_mutex);
const boost::system_time exit_time = boost::get_system_time() + boost::posix_time::microseconds(long(timeout*1e6));
std::string buff;
while (true)
{
// Update cache
this->update_cache();
// Get available characters
for (int ch = this->getchar(); ch != -1; ch = this->getchar())
{
// skip carriage returns
if (ch == '\r')
continue;
// avoid returning empty strings
if (ch == '\n' and _rxbuff.empty())
continue;
// store character to buffer
_rxbuff += std::string(1, (char)ch);
// newline found - return string
if (ch == '\n')
{
buff = _rxbuff;
_rxbuff.clear();
return buff;
}
}
// no more characters - check time
if (boost::get_system_time() > exit_time)
break;
}
return buff;
}
wb_iface::sptr _iface;
boost::uint32_t rxoffset, txoffset, txword32, rxpool, txpool, poolsize;
boost::uint32_t _last_device_rxoffset;
std::vector<boost::uint32_t> _rxcache;
std::string _rxbuff;
boost::mutex _read_mutex;
boost::mutex _write_mutex;
};
uart_iface::sptr x300_make_uart_iface(wb_iface::sptr iface)
{
return uart_iface::sptr(new x300_uart_iface(iface));
}
|