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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
//
// Copyright 2017 Ettus Research (National Instruments)
//
// 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 "adi/common.h"
#include <mpm/ad937x/adi_ctrl.hpp>
#include <uhd/exception.hpp>
#include <iostream>
#include <chrono>
#include <thread>
ad9371_spiSettings_t::ad9371_spiSettings_t(uhd::spi_iface::sptr uhd_iface) :
spi_iface(uhd_iface)
{
spi_settings.chipSelectIndex = 0; // set later
spi_settings.writeBitPolarity = 1; // unused
spi_settings.longInstructionWord = 1;
spi_settings.MSBFirst = 1;
spi_settings.CPHA = 0;
spi_settings.CPOL = 0;
spi_settings.enSpiStreaming = 0; // unused
spi_settings.autoIncAddrUp = 0; // unused
spi_settings.fourWireMode = 1; // unused
spi_settings.spiClkFreq_Hz = 250000000; // currently unused
}
uhd::spi_config_t::edge_t _get_edge(const spiSettings_t & sps)
{
return (sps.CPOL ^ sps.CPHA) ? uhd::spi_config_t::EDGE_FALL : uhd::spi_config_t::EDGE_RISE;
}
// TODO: change // not implemented to meaningful errors
// close hardware pointers
commonErr_t CMB_closeHardware(void)
{
// not implemented
return COMMONERR_FAILED;
}
// GPIO function
commonErr_t CMB_setGPIO(uint32_t GPIO)
{
// not implemented
return COMMONERR_FAILED;
}
// hardware reset function
commonErr_t CMB_hardReset(uint8_t spiChipSelectIndex)
{
// TODO: implement
return COMMONERR_OK;
}
//
// SPI read/write functions
//
// allows the platform HAL to work with devices with various SPI settings
commonErr_t CMB_setSPIOptions(spiSettings_t *spiSettings)
{
// not implemented
return COMMONERR_OK;
}
// value of 0 deasserts all chip selects
commonErr_t CMB_setSPIChannel(uint16_t chipSelectIndex)
{
// not implemented
return COMMONERR_OK;
}
// single SPI byte write function
commonErr_t CMB_SPIWriteByte(spiSettings_t *spiSettings, uint16_t addr, uint8_t data)
{
// TODO: crash and burn for these errors?
if (spiSettings == nullptr || spiSettings->MSBFirst == 0) return COMMONERR_FAILED;
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
uhd::spi_config_t config(_get_edge(*spiSettings));
uint32_t data_word = (0) | (addr << 8) | (data);
try {
mpm_spi->spi_iface->write_spi(spiSettings->chipSelectIndex, config, data_word, 24);
return COMMONERR_OK;
} catch (const std::exception &e) {
// ... error handling ...
}
return COMMONERR_FAILED;
}
commonErr_t CMB_SPIWriteBytes(spiSettings_t *spiSettings, uint16_t *addr, uint8_t *data, uint32_t count)
{
// TODO: crash and burn for these errors?
if (spiSettings == nullptr ||
addr == nullptr ||
data == nullptr ||
spiSettings->MSBFirst == 0)
{
return COMMONERR_FAILED;
}
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
uhd::spi_config_t config(_get_edge(*spiSettings));
try {
for (size_t i = 0; i < count; ++i)
{
uint32_t data_word = (0) | (addr[i] << 8) | (data[i]);
mpm_spi->spi_iface->write_spi(spiSettings->chipSelectIndex, config, data_word, 24);
}
return COMMONERR_OK;
} catch (const std::exception &e) {
// ... error handling ...
}
return COMMONERR_FAILED;
}
// single SPI byte read function
commonErr_t CMB_SPIReadByte (spiSettings_t *spiSettings, uint16_t addr, uint8_t *readdata)
{
if (spiSettings == nullptr ||
readdata == nullptr ||
spiSettings->MSBFirst == 0)
{
return COMMONERR_FAILED;
}
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
uhd::spi_config_t config(_get_edge(*spiSettings));
uint32_t data_word = (0) | (addr << 8);
try {
*readdata = static_cast<uint8_t>(
mpm_spi->spi_iface->read_spi(spiSettings->chipSelectIndex, config, data_word, 24));
return COMMONERR_OK;
} catch (const std::exception &e) {
// ... error handling ...
}
return COMMONERR_FAILED;
}
// write a field in a single register
commonErr_t CMB_SPIWriteField(
spiSettings_t *spiSettings,
uint16_t addr, uint8_t field_val,
uint8_t mask, uint8_t start_bit
) {
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
uhd::spi_config_t config(_get_edge(*spiSettings));
uint32_t read_word = (0) | (addr << 8);
try {
uint32_t current_value = mpm_spi->spi_iface->read_spi(spiSettings->chipSelectIndex, config, read_word, 24);
uint8_t new_value = static_cast<uint8_t>((current_value & ~mask) | (field_val << start_bit));
uint32_t write_word = (0) | (addr << 8) | new_value;
mpm_spi->spi_iface->write_spi(spiSettings->chipSelectIndex, config, write_word, 24);
return COMMONERR_OK;
} catch (const std::exception &e) {
// ... error handling ...
}
return COMMONERR_FAILED;
}
// read a field in a single register
commonErr_t CMB_SPIReadField(
spiSettings_t *spiSettings,
uint16_t addr, uint8_t *field_val,
uint8_t mask, uint8_t start_bit
) {
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
uhd::spi_config_t config(_get_edge(*spiSettings));
uint32_t read_word = (0) | (addr << 8);
try {
uint32_t value = mpm_spi->spi_iface->read_spi(spiSettings->chipSelectIndex, config, read_word, 24);
*field_val = static_cast<uint8_t>((value & mask) >> start_bit);
return COMMONERR_OK;
} catch (const std::exception &e) {
/* ... error handling ... */
}
return COMMONERR_FAILED;
}
// platform timer functions
commonErr_t CMB_wait_ms(uint32_t time_ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(time_ms));
return COMMONERR_OK;
}
commonErr_t CMB_wait_us(uint32_t time_us)
{
std::this_thread::sleep_for(std::chrono::microseconds(time_us));
return COMMONERR_OK;
}
commonErr_t CMB_setTimeout_ms(spiSettings_t *spiSettings, uint32_t timeOut_ms)
{
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
mpm_spi->timeout_start = std::chrono::steady_clock::now();
mpm_spi->timeout_duration = std::chrono::milliseconds(timeOut_ms);
return COMMONERR_OK;
}
commonErr_t CMB_setTimeout_us(spiSettings_t *spiSettings, uint32_t timeOut_us)
{
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
mpm_spi->timeout_start = std::chrono::steady_clock::now();
mpm_spi->timeout_duration = std::chrono::microseconds(timeOut_us);
return COMMONERR_OK;
}
commonErr_t CMB_hasTimeoutExpired(spiSettings_t *spiSettings)
{
ad9371_spiSettings_t *mpm_spi = ad9371_spiSettings_t::make(spiSettings);
auto current_time = std::chrono::steady_clock::now();
if ((std::chrono::steady_clock::now() - mpm_spi->timeout_start) > mpm_spi->timeout_duration)
{
return COMMONERR_FAILED;
}
else {
return COMMONERR_OK;
}
}
// platform logging functions
commonErr_t CMB_openLog(const char *filename)
{
// not implemented
return COMMONERR_FAILED;
}
commonErr_t CMB_closeLog(void)
{
// not implemented
return COMMONERR_FAILED;
}
commonErr_t CMB_writeToLog(ADI_LOGLEVEL level, uint8_t deviceIndex, uint32_t errorCode, const char *comment)
{
std::cout << level << " " << errorCode << " " << comment << std::endl;
return COMMONERR_OK;
}
commonErr_t CMB_flushLog(void)
{
// not implemented
return COMMONERR_FAILED;
}
/* platform FPGA AXI register read/write functions */
commonErr_t CMB_regRead(uint32_t offset, uint32_t *data)
{
// not implemented
return COMMONERR_FAILED;
}
commonErr_t CMB_regWrite(uint32_t offset, uint32_t data)
{
// not implemented
return COMMONERR_FAILED;
}
/* platform DDR3 memory read/write functions */
commonErr_t CMB_memRead(uint32_t offset, uint32_t *data, uint32_t len)
{
// not implemented
return COMMONERR_FAILED;
}
commonErr_t CMB_memWrite(uint32_t offset, uint32_t *data, uint32_t len)
{
// not implemented
return COMMONERR_FAILED;
}
|