refactor: use standard integer types; remove <algorithm> dependency
- Replaced unsigned char, int and long long with uint8_t int32_t and int64_t for platform independence - Removed <algorithm> dependency, implemented custom alternatives
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
// ReSharper disable CppCompileTimeConstantCanBeReplacedWithBooleanConstant
|
||||
// ReSharper disable CppUseRangeAlgorithm
|
||||
// ReSharper disable CppUseStructuredBinding
|
||||
// ReSharper disable CppTooWideScopeInitStatement
|
||||
#include <cstdint>
|
||||
#include "cx9261adriver.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -299,7 +296,7 @@ void configBBPLL(const CX9261AConfiguration& config)
|
||||
bbloDiv *= 2;
|
||||
}
|
||||
Register r602;
|
||||
r602.replace(1, 0, static_cast<unsigned char>(std::log2(bbloDiv)));
|
||||
r602.replace(1, 0, static_cast<std::uint8_t>(std::log2(bbloDiv)));
|
||||
r602[5] = fclkDiv == 5;
|
||||
// ref div
|
||||
Register r601{ 0x0F };
|
||||
@@ -307,14 +304,14 @@ void configBBPLL(const CX9261AConfiguration& config)
|
||||
writeSpi(0x601, r601);
|
||||
// nf
|
||||
const auto nf = bblo * clock.refDiv / (clock.ref * (clock.refDoubler ? 2 : 1));
|
||||
int n = floor(nf);
|
||||
int n = floor32(nf);
|
||||
const auto f = nf - n;
|
||||
r602[6] = clock.bbUseIntDiv;
|
||||
writeSpi(0x602, r602);
|
||||
// 整数模式
|
||||
if (clock.bbUseIntDiv)
|
||||
n = round(2.0 * n / bbloDiv + 1);
|
||||
const int fBin = round(std::pow(2, 25) * f);
|
||||
n = round32(2.0 * n / bbloDiv + 1);
|
||||
const int fBin = round32(std::pow(2, 25) * f);
|
||||
Register r60D{ 0x0C };
|
||||
r60D[0] = f >= 0.5;
|
||||
r60D[4] = clock.bbUseIntDiv;
|
||||
@@ -364,7 +361,7 @@ void configBBPLL(const CX9261AConfiguration& config)
|
||||
writeSpi(0x747, r70F);
|
||||
auto c = 8600 * adjAdc / adcClock;
|
||||
c = (c - 3000) / 80;
|
||||
const Register r70D = round(c);
|
||||
const Register r70D = round32(c);
|
||||
writeSpi(0x70D, r70D);
|
||||
writeSpi(0x729, r70D);
|
||||
writeSpi(0x745, r70D);
|
||||
@@ -387,7 +384,7 @@ bool afcBBPll()
|
||||
LOGGING("BBPLL AFC");
|
||||
writeSpi(0x600, 0x00);
|
||||
writeSpi(0x600, 0x11);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
return readSpi(0x615) & 0b00100000;
|
||||
}
|
||||
|
||||
@@ -457,13 +454,13 @@ std::vector<int> configPllImpl(const double lo, const double ref)
|
||||
}();
|
||||
ret[1] = outputDiv;
|
||||
const auto nf = static_cast<std::int64_t>(std::round(vco / ref * 1e9)) / 1.0e9;
|
||||
const auto n = floor(nf);
|
||||
const auto n = floor32(nf);
|
||||
const auto f = nf - n;
|
||||
const Register nHigh = n >= 256;
|
||||
ret[2] = nHigh;
|
||||
const Register nLow = n & 0xFF;
|
||||
ret[3] = nLow;
|
||||
const int fint = round(std::pow(2, 24) * f);
|
||||
const int fint = round32(std::pow(2, 24) * f);
|
||||
ret[4] = fint >> 16 & 0xFF;
|
||||
ret[5] = fint >> 8 & 0xFF;
|
||||
ret[6] = fint & 0xFF;
|
||||
@@ -498,7 +495,7 @@ bool afcPll1()
|
||||
writeSpi(0x61D, r61D);
|
||||
writeSpi(0x617, 0x00);
|
||||
writeSpi(0x617, 0x13);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
writeSpi(0x61D, saved);
|
||||
const auto lock = readSpi(0x635);
|
||||
return 0x10 & lock;
|
||||
@@ -530,7 +527,7 @@ bool afcPll2()
|
||||
writeSpi(0x63F, r63F);
|
||||
writeSpi(0x639, 0x00);
|
||||
writeSpi(0x639, 0x13);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
writeSpi(0x63F, saved);
|
||||
const auto lock = readSpi(0x657);
|
||||
return 0x10 & lock;
|
||||
@@ -562,7 +559,7 @@ bool afcPll3()
|
||||
writeSpi(0x661, r661);
|
||||
writeSpi(0x65B, 0x00);
|
||||
writeSpi(0x65B, 0x13);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
writeSpi(0x661, saved);
|
||||
const auto lock = readSpi(0x679);
|
||||
return 0x10 & lock;
|
||||
@@ -594,7 +591,7 @@ bool afcPll4()
|
||||
writeSpi(0x683, r683);
|
||||
writeSpi(0x67D, 0x00);
|
||||
writeSpi(0x67D, 0x13);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
writeSpi(0x683, saved);
|
||||
const auto lock = readSpi(0x69B);
|
||||
return 0x10 & lock;
|
||||
@@ -626,7 +623,7 @@ bool afcPll5()
|
||||
writeSpi(0x6A5, r6A5);
|
||||
writeSpi(0x69F, 0x00);
|
||||
writeSpi(0x69F, 0x13);
|
||||
pause(delay);
|
||||
pause(afcDelay);
|
||||
writeSpi(0x6A5, saved);
|
||||
const auto lock = readSpi(0x6BD);
|
||||
return 0x10 & lock;
|
||||
@@ -1124,19 +1121,19 @@ CalibPredis readRxCalibPredis(const RxChannel chan)
|
||||
LOGGING(std::string("读取") + toString(chan) + "校准值");
|
||||
writeSpi(0x000, chan == RxChannel::Rx1 ? 0x12 : chan == RxChannel::Rx2 ? 0x22 : 0x32);
|
||||
writeSpi(0x121, 0x00);
|
||||
const CalibPredis predis{ .bHigh = readSpi(0x13D),
|
||||
.bLow = readSpi(0x13E),
|
||||
.cHigh = readSpi(0x13F),
|
||||
.cLow = readSpi(0x140),
|
||||
.dciHigh = readSpi(0x141),
|
||||
.dciLow = readSpi(0x142),
|
||||
.dcqHigh = readSpi(0x143),
|
||||
.dcqLow = readSpi(0x144) };
|
||||
CalibPredis predis;
|
||||
predis.bLow = readSpi(0x13E);
|
||||
predis.cHigh = readSpi(0x13F);
|
||||
predis.cLow = readSpi(0x140);
|
||||
predis.dciHigh = readSpi(0x141);
|
||||
predis.dciLow = readSpi(0x142);
|
||||
predis.dcqHigh = readSpi(0x143);
|
||||
predis.dcqLow = readSpi(0x144);
|
||||
writeSpi(0x000, 02);
|
||||
return predis;
|
||||
}
|
||||
|
||||
void saveRxCalibPredis(const unsigned char index, const RxChannel chan, const LoSource pll)
|
||||
void saveRxCalibPredis(const std::uint8_t index, const RxChannel chan, const LoSource pll)
|
||||
{
|
||||
LOGGING(std::string("保存") + toString(chan) + " " + toString(pll) + " index: " + std::to_string(index)
|
||||
+ " 校准值");
|
||||
@@ -1147,7 +1144,7 @@ void saveRxCalibPredis(const unsigned char index, const RxChannel chan, const Lo
|
||||
writeSpi(0x0A0, 0x01);
|
||||
}
|
||||
|
||||
void loadRxCalibPredis(const unsigned char index, const RxChannel chan, const LoSource pll)
|
||||
void loadRxCalibPredis(const std::uint8_t index, const RxChannel chan, const LoSource pll)
|
||||
{
|
||||
LOGGING("加载: " + toString(chan) + " " + toString(pll) + " " + std::to_string(index) + " 校准值")
|
||||
writeSpi(0x0A0, 0x00);
|
||||
@@ -1161,7 +1158,7 @@ void configRxMultiPointsQecImpl(const CX9261AConfiguration& config, const std::v
|
||||
const RxChannel chan)
|
||||
{
|
||||
auto mutConfig = config;
|
||||
for (auto i = 0; i < points.size(); ++i) {
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
LOGGING("频点: " + std::to_string(points[i]));
|
||||
const auto source = chan == RxChannel::Rx1 ? config.pllSource.rx1Source
|
||||
: chan == RxChannel::Rx2 ? config.pllSource.rx2Source
|
||||
@@ -1344,7 +1341,7 @@ void txBisectionDcCalib(const double f0, const double fs)
|
||||
void configTxMultiPointsQecImpl(const CX9261AConfiguration& config, const std::vector<double>& points)
|
||||
{
|
||||
LOGGING("Tx多点校准");
|
||||
for (auto i = 0; i < points.size(); ++i) {
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
LOGGING("频点: " + std::to_string(points[i]));
|
||||
configTxChannelFrequency(points[i], TxChannel::Tx1);
|
||||
configTxChannelFrequency(points[i], TxChannel::Tx2);
|
||||
@@ -1379,7 +1376,7 @@ void configTxMultiPointsQecImpl(const CX9261AConfiguration& config, const std::v
|
||||
void configTxMultiPointsQec(const CX9261AConfiguration& config, const std::vector<double>& points)
|
||||
{
|
||||
auto mutConfig = config;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
for (size_t i = 0; i < 5; ++i) {
|
||||
const auto& calibPll = mutConfig.calib.tx1CalibPll;
|
||||
if (calibPll & (0b1 << i)) {
|
||||
mutConfig.pllSource.tx1Source = static_cast<LoSource>(calibPll & (0b1 << i));
|
||||
@@ -1409,7 +1406,7 @@ void txCalib(const CX9261AConfiguration& config)
|
||||
}
|
||||
}
|
||||
|
||||
void saveTxCalibPredis(const unsigned char index, const TxChannel chan, const LoSource pll)
|
||||
void saveTxCalibPredis(const std::uint8_t index, const TxChannel chan, const LoSource pll)
|
||||
{
|
||||
LOGGING(std::string("保存") + toString(chan) + " " + "pll: " + toString(pll) + " "
|
||||
+ " index: " + std::to_string(index) + " 校准值");
|
||||
@@ -1420,7 +1417,7 @@ void saveTxCalibPredis(const unsigned char index, const TxChannel chan, const Lo
|
||||
writeSpi(0x0A0, 0x01);
|
||||
}
|
||||
|
||||
void loadTxCalibPredis(const unsigned char index, const TxChannel chan, const LoSource pll)
|
||||
void loadTxCalibPredis(const std::uint8_t index, const TxChannel chan, const LoSource pll)
|
||||
{
|
||||
LOGGING("加载: " + toString(chan) + toString(pll) + " " + " " + std::to_string(index) + " 校准值")
|
||||
writeSpi(0x0A0, 0x00);
|
||||
@@ -1470,17 +1467,11 @@ void configQecCalib(const CX9261AConfiguration& config)
|
||||
configPll5(config.pllFreq.pll5Freq, config.clock);
|
||||
afcPll5();
|
||||
}
|
||||
auto loadRxDefault = [&config](const RxChannel chan, const LoSource source, const double freq) {
|
||||
auto points = config.calib.rxMultiPoints;
|
||||
std::sort(points.begin(), points.end());
|
||||
const auto location = std::lower_bound(points.begin(), points.end(), freq);
|
||||
if (location != points.end()) {
|
||||
const auto fundFreq = *location;
|
||||
const auto it =
|
||||
std::find(config.calib.rxMultiPoints.begin(), config.calib.rxMultiPoints.end(), fundFreq);
|
||||
if (it != config.calib.rxMultiPoints.end()) {
|
||||
loadRxCalibPredis(it - config.calib.rxMultiPoints.begin(), chan, source);
|
||||
}
|
||||
const auto& points = config.calib.rxMultiPoints;
|
||||
auto loadRxDefault = [&points](const RxChannel chan, const LoSource source, const double freq) {
|
||||
const auto idx = search_first_ge(points, freq);
|
||||
if (idx != points.size()) {
|
||||
loadRxCalibPredis(idx, chan, source);
|
||||
}
|
||||
};
|
||||
if (config.pllSource.rx1Source != LoSource::Off) {
|
||||
@@ -1528,17 +1519,11 @@ void configQecCalib(const CX9261AConfiguration& config)
|
||||
configPll4(config.pllFreq.pll4Freq, config.clock);
|
||||
afcPll4();
|
||||
}
|
||||
auto loadTxDefault = [&config](const TxChannel chan, const LoSource source, const double freq) {
|
||||
auto points = config.calib.txMultiPoints;
|
||||
std::sort(points.begin(), points.end());
|
||||
const auto location = std::lower_bound(points.begin(), points.end(), freq);
|
||||
if (location != points.end()) {
|
||||
const auto fundFreq = *location;
|
||||
const auto it =
|
||||
std::find(config.calib.txMultiPoints.begin(), config.calib.txMultiPoints.end(), fundFreq);
|
||||
if (it != config.calib.txMultiPoints.end()) {
|
||||
loadTxCalibPredis(it - config.calib.txMultiPoints.begin(), chan, source);
|
||||
}
|
||||
const auto& points = config.calib.txMultiPoints;
|
||||
auto loadTxDefault = [&points](const TxChannel chan, const LoSource source, const double freq) {
|
||||
const auto idx = search_first_ge(points, freq);
|
||||
if (idx != points.size()) {
|
||||
loadTxCalibPredis(idx, chan, source);
|
||||
}
|
||||
};
|
||||
if (config.pllSource.tx1Source != LoSource::Off) {
|
||||
@@ -1800,12 +1785,12 @@ std::pair<int, int> configBandwidthImpl(const double bandwidth)
|
||||
auto c = 255;
|
||||
auto r = 0;
|
||||
for (; c > 0; --c) {
|
||||
r = floor(c * x + 128 * x - 1);
|
||||
r = floor32(c * x + 128 * x - 1);
|
||||
if (r <= 31 && r >= 0)
|
||||
break;
|
||||
}
|
||||
r = clamp(r, 0, 31);
|
||||
c = round((r + 1) / x) - 128;
|
||||
c = round32((r + 1) / x) - 128;
|
||||
return { r, c };
|
||||
}
|
||||
|
||||
@@ -1839,7 +1824,7 @@ void configRxChannel(const RxConfig& config, const RxChannel chan)
|
||||
{
|
||||
LOGGING("配置通道" + toString(chan));
|
||||
configRxTIA(config.tia, chan);
|
||||
int tiaCSel = round(1036.4364 * (config.tia + 1) / config.bandwidth - 40);
|
||||
int tiaCSel = round32(1036.4364 * (config.tia + 1) / config.bandwidth - 40);
|
||||
tiaCSel = clamp(tiaCSel, 0, 8191);
|
||||
const Register c1 = tiaCSel >> 8;
|
||||
const Register c2 = tiaCSel & 0xFF;
|
||||
@@ -1904,8 +1889,8 @@ void configRxFilters(const Filters& filters)
|
||||
void configRxFIRCoe(const std::vector<double>& coe)
|
||||
{
|
||||
LOGGING("配置Rx FIR系数");
|
||||
for (auto i = 0; i < coe.size(); ++i) {
|
||||
const auto value = round(coe[i] * std::pow(2, 15)) + (coe[i] < 0 ? 65536 : 0);
|
||||
for (size_t i = 0; i < coe.size(); ++i) {
|
||||
const auto value = round32(coe[i] * std::pow(2, 15)) + (coe[i] < 0 ? 65536 : 0);
|
||||
writeSpi(0x401, i);
|
||||
writeSpi(0x402, value >> 8 & 0xFF);
|
||||
writeSpi(0x403, value & 0xFF);
|
||||
@@ -1923,12 +1908,12 @@ std::pair<int, int> configTxBandwidthImpl(const double bandwidth)
|
||||
auto c = 127;
|
||||
auto r = 0;
|
||||
for (; c > 0; --c) {
|
||||
r = floor(c * x + 128 * x - 1);
|
||||
r = floor32(c * x + 128 * x - 1);
|
||||
if (r <= 31 && r >= 0)
|
||||
break;
|
||||
}
|
||||
r = clamp(r, 0, 31);
|
||||
c = round((r + 1) / x) - 128;
|
||||
c = round32((r + 1) / x) - 128;
|
||||
return { r, c };
|
||||
}
|
||||
|
||||
@@ -2044,8 +2029,8 @@ void configTxFilters(const Filters& filters)
|
||||
void configTxFIRCoe(const std::vector<double>& coe)
|
||||
{
|
||||
LOGGING("配置Tx FIR系数");
|
||||
for (auto i = 0; i < coe.size(); ++i) {
|
||||
const auto value = round(coe[i] * std::pow(2, 15)) + (coe[i] < 0 ? 65536 : 0);
|
||||
for (size_t i = 0; i < coe.size(); ++i) {
|
||||
const auto value = round32(coe[i] * std::pow(2, 15)) + (coe[i] < 0 ? 65536 : 0);
|
||||
writeSpi(0x501, i);
|
||||
writeSpi(0x502, value >> 8 & 0xFF);
|
||||
writeSpi(0x503, value & 0xFF);
|
||||
@@ -2062,39 +2047,40 @@ CalibPredis readTxCalibPredis(const TxChannel chan)
|
||||
LOGGING("读取校准系数: " + toString(chan));
|
||||
writeSpi(0x000, chan == TxChannel::Tx1 ? 0x12 : 0x22);
|
||||
writeSpi(0x121, 0x00);
|
||||
const CalibPredis predis{ .bHigh = readSpi(0x145),
|
||||
.bLow = readSpi(0x146),
|
||||
.cHigh = readSpi(0x147),
|
||||
.cLow = readSpi(0x148),
|
||||
.dciHigh = readSpi(0x149),
|
||||
.dciLow = readSpi(0x14A),
|
||||
.dcqHigh = readSpi(0x14B),
|
||||
.dcqLow = readSpi(0x14C) };
|
||||
CalibPredis predis;
|
||||
predis.bHigh = readSpi(0x145);
|
||||
predis.bLow = readSpi(0x146);
|
||||
predis.cHigh = readSpi(0x147);
|
||||
predis.cLow = readSpi(0x148);
|
||||
predis.dciHigh = readSpi(0x149);
|
||||
predis.dciLow = readSpi(0x14A);
|
||||
predis.dcqHigh = readSpi(0x14B);
|
||||
predis.dcqLow = readSpi(0x14C);
|
||||
writeSpi(0x000, 02);
|
||||
return predis;
|
||||
}
|
||||
|
||||
void configFPGA(const Interface& interface)
|
||||
{
|
||||
setPin(33, 1);
|
||||
setPin(39, 1);
|
||||
setPin(40, 1);
|
||||
setPin(33, true);
|
||||
setPin(39, true);
|
||||
setPin(40, true);
|
||||
|
||||
setPin(32, 1);
|
||||
setPin(32, true);
|
||||
setPin(7, interface.ioMode == IOMode::CMOS ? 0 : 1);
|
||||
setPin(34, 1);
|
||||
setPin(34, 0);
|
||||
setPin(34, true);
|
||||
setPin(34, false);
|
||||
|
||||
setPin(38, interface.portMode == PortMode::Single);
|
||||
setPin(37, interface.trMode == TrMode::R2T2);
|
||||
setPin(36, interface.workMode != WorkMode::FDD);
|
||||
setPin(35, interface.workMode == WorkMode::Rx);
|
||||
|
||||
setPin(34, 1);
|
||||
setPin(34, 0);
|
||||
setPin(34, true);
|
||||
setPin(34, false);
|
||||
|
||||
setPin(43, 0);
|
||||
setPin(43, 1);
|
||||
setPin(43, false);
|
||||
setPin(43, true);
|
||||
}
|
||||
|
||||
void rxFFTCalib(const double lo, const double fs, const int firLength, const CX9261AConfiguration& config,
|
||||
@@ -2117,13 +2103,13 @@ void rxFFTCalib(const double lo, const double fs, const int firLength, const CX9
|
||||
writeSpi(0x77E, chan == RxChannel::Rx1 ? 0x50 : chan == RxChannel::Rx2 ? 0x0A : 0x03);
|
||||
writeSpi(0x09B, 0x01); // TXPLL4
|
||||
writeSpi(0x09D, firLength);
|
||||
writeSpi(0x096, (round(lo) >> 8) & 0xFF);
|
||||
writeSpi(0x097, round(lo) & 0xFF);
|
||||
writeSpi(0x096, (round32(lo) >> 8) & 0xFF);
|
||||
writeSpi(0x097, round32(lo) & 0xFF);
|
||||
const double ref = config.clock.ref * (config.clock.refDoubler ? 2 : 1);
|
||||
writeSpi(0x098, (round(ref * 10) >> 8) & 0xFF);
|
||||
writeSpi(0x099, round(ref * 10) & 0xFF);
|
||||
writeSpi(0x09E, (round(fs * 100) >> 8) & 0xFF);
|
||||
writeSpi(0x09F, round(fs * 100) & 0xFF);
|
||||
writeSpi(0x098, (round32(ref * 10) >> 8) & 0xFF);
|
||||
writeSpi(0x099, round32(ref * 10) & 0xFF);
|
||||
writeSpi(0x09E, (round32(fs * 100) >> 8) & 0xFF);
|
||||
writeSpi(0x09F, round32(fs * 100) & 0xFF);
|
||||
writeSpi(0x09C, static_cast<int>(chan));
|
||||
pause(qecDelay);
|
||||
while (true) {
|
||||
@@ -2164,8 +2150,8 @@ void txTSTPhaseDelay(const Clock& clock, const double lo, const double fs, const
|
||||
writeSpi(0x18C, 0x00);
|
||||
writeSpi(chan == TxChannel::Tx1 ? 0x763 : 0x76D, 0x14);
|
||||
writeSpi(0x095, 0x00);
|
||||
writeSpi(0x096, (round(lo) >> 8) & 0xFF);
|
||||
writeSpi(0x097, round(lo) & 0xFF);
|
||||
writeSpi(0x096, (round32(lo) >> 8) & 0xFF);
|
||||
writeSpi(0x097, round32(lo) & 0xFF);
|
||||
writeSpi(0x09C, chan == TxChannel::Tx1 ? 6 : 7);
|
||||
pause(qecDelay);
|
||||
while (true) {
|
||||
|
||||
Reference in New Issue
Block a user