|
| 1 | +/************************************************************************* |
| 2 | + * Copyright (c) 2026 - 2026 Yichao Yu <yyc1992@gmail.com> * |
| 3 | + * * |
| 4 | + * This library is free software; you can redistribute it and/or * |
| 5 | + * modify it under the terms of the GNU Lesser General Public * |
| 6 | + * License as published by the Free Software Foundation; either * |
| 7 | + * version 3.0 of the License, or (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This library is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 12 | + * Lesser General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU Lesser General Public * |
| 15 | + * License along with this library. If not, * |
| 16 | + * see <http://www.gnu.org/licenses/>. * |
| 17 | + *************************************************************************/ |
| 18 | + |
| 19 | +#ifndef __NACS_SEQ_ZYNQ_UTILS_H__ |
| 20 | +#define __NACS_SEQ_ZYNQ_UTILS_H__ |
| 21 | + |
| 22 | +#include "../../nacs-utils/number.h" |
| 23 | + |
| 24 | +namespace NaCs::Seq::Zynq { |
| 25 | + |
| 26 | +static inline uint32_t dds_freq_to_mu(double freq) |
| 27 | +{ |
| 28 | + constexpr double factor = 1.0 * (1 << 16) * (1 << 16) / 3.5e9; |
| 29 | + if (freq <= 0) |
| 30 | + return 0; |
| 31 | + // The instruction only have 31 bits so it actually won't encode exactly 1.75 GHz. |
| 32 | + if (freq > 1.7499999987776392e9) // last float that would round to 2^31 - 2 |
| 33 | + return 0x7fffffff; |
| 34 | + return round<int32_t>(freq * factor); |
| 35 | +} |
| 36 | + |
| 37 | +static constexpr inline double dds_freq_from_mu(uint32_t freq) |
| 38 | +{ |
| 39 | + constexpr double factor = 3.5e9 / (1 << 16) / (1 << 16); |
| 40 | + return double(freq) * factor; |
| 41 | +} |
| 42 | + |
| 43 | +static inline uint16_t dds_amp_to_mu(double amp) |
| 44 | +{ |
| 45 | + constexpr double factor = 4095; |
| 46 | + if (amp <= 0) |
| 47 | + return 0; |
| 48 | + if (amp >= 1) |
| 49 | + return 4095; |
| 50 | + return (uint16_t)round<int32_t>(amp * factor); |
| 51 | +} |
| 52 | + |
| 53 | +static constexpr inline double dds_amp_from_mu(uint32_t amp) |
| 54 | +{ |
| 55 | + return double(amp) / 4095; |
| 56 | +} |
| 57 | + |
| 58 | +static inline uint32_t dds_phase_to_mu(double phase) |
| 59 | +{ |
| 60 | + // phase in [0, 1] |
| 61 | + return round<int32_t>(phase * (1 << 16)) & ((1 << 16) - 1); |
| 62 | +} |
| 63 | + |
| 64 | +static constexpr inline double dds_phase_from_mu(uint32_t phase) |
| 65 | +{ |
| 66 | + return double(phase) / double(1 << 16); |
| 67 | +} |
| 68 | + |
| 69 | +static inline uint32_t dac_to_mu(double V) |
| 70 | +{ |
| 71 | + constexpr double factor = 65535 / 20.0; |
| 72 | + constexpr double offset = 10.0; |
| 73 | + if (V <= -10) |
| 74 | + return 0; |
| 75 | + if (V >= 10) |
| 76 | + return 0xffff; |
| 77 | + V += offset; |
| 78 | + return round<int32_t>(V * factor); |
| 79 | +} |
| 80 | + |
| 81 | +static constexpr inline double dac_from_mu(uint32_t V) |
| 82 | +{ |
| 83 | + return V * (20.0 / 65535) - 10.0; |
| 84 | +} |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +#endif |
0 commit comments