1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < neo430_freq_gen.c - Frequency Generator helper functions > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
#include "neo430.h"
|
36 |
|
|
#include "neo430_freq_gen.h"
|
37 |
|
|
|
38 |
|
|
// Private function prototypes
|
39 |
|
|
static uint32_t neo430_freq_gen_nco_real_output(uint16_t tuning_word, uint16_t prsc_shift);
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
/* ------------------------------------------------------------
|
43 |
|
|
* INFO Enable programmable frequency output channel ch (0..2)
|
44 |
|
|
* ------------------------------------------------------------ */
|
45 |
|
|
void neo430_freq_gen_enable_ch(uint16_t ch) {
|
46 |
|
|
|
47 |
|
|
if (ch > 2) {
|
48 |
|
|
return;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
FREQ_GEN_CT |= (1 << (FREQ_GEN_CT_CH0_EN+ch));
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* ------------------------------------------------------------
|
56 |
|
|
* INFO Disable programmable frequency output channel ch (0..2)
|
57 |
|
|
* ------------------------------------------------------------ */
|
58 |
|
|
void neo430_freq_gen_disable_ch(uint16_t ch) {
|
59 |
|
|
|
60 |
|
|
if (ch > 2) {
|
61 |
|
|
return;
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
FREQ_GEN_CT &= ~(1 << (FREQ_GEN_CT_CH0_EN+ch));
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
/* ------------------------------------------------------------
|
69 |
|
|
* INFO Disable all programmable frequency outputs
|
70 |
|
|
* ------------------------------------------------------------ */
|
71 |
|
|
void neo430_freq_gen_disable(void) {
|
72 |
|
|
|
73 |
|
|
register uint16_t ct = FREQ_GEN_CT;
|
74 |
|
|
ct &= ~(1 << FREQ_GEN_CT_CH0_EN);
|
75 |
|
|
ct &= ~(1 << FREQ_GEN_CT_CH1_EN);
|
76 |
|
|
ct &= ~(1 << FREQ_GEN_CT_CH2_EN);
|
77 |
|
|
FREQ_GEN_CT = ct;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/* ------------------------------------------------------------
|
82 |
|
|
* INFO Disable all programmable frequency outputs and reset unit
|
83 |
|
|
* ------------------------------------------------------------ */
|
84 |
|
|
void neo430_freq_gen_reset(void) {
|
85 |
|
|
|
86 |
|
|
FREQ_GEN_CT = 0;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
/* ------------------------------------------------------------
|
91 |
|
|
* INFO Set frequency programmable frequency output
|
92 |
|
|
* INFO f_out = ((f_cpu / nco_prsc) * tuning_word[15:0]) / 2^17
|
93 |
|
|
* WARNING Imprecise due to rounding/truncation errors!
|
94 |
|
|
* PARAM ch channel to configure (0..2)
|
95 |
|
|
* PARAM frequency: output frequency in Hz (no fractions possible here)
|
96 |
|
|
* RETURN the actual output frequency
|
97 |
|
|
* ------------------------------------------------------------ */
|
98 |
|
|
uint32_t neo430_freq_gen_set_freq(uint16_t ch, uint32_t frequency) {
|
99 |
|
|
|
100 |
|
|
// tuning_word = (f_out * 2^17) / (f_cpu / nco_prsc)
|
101 |
|
|
|
102 |
|
|
uint32_t f_cpu = CLOCKSPEED_32bit;
|
103 |
|
|
|
104 |
|
|
int16_t i;
|
105 |
|
|
uint16_t prsc_shift = 12; // start with highest prescaler (4096 => 12)
|
106 |
|
|
|
107 |
|
|
if (frequency > (f_cpu/4)) {
|
108 |
|
|
return 0;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
uint64_t freq_tmp;
|
112 |
|
|
uint32_t freq_real;
|
113 |
|
|
uint32_t freq_diff;
|
114 |
|
|
|
115 |
|
|
uint32_t freq_diff_best = 0xffffffff; // max
|
116 |
|
|
uint16_t tuning_word_best = 0;
|
117 |
|
|
uint16_t prsc_best = 0;
|
118 |
|
|
uint32_t freq_real_best = 0;
|
119 |
|
|
|
120 |
|
|
// check all possible prescaler
|
121 |
|
|
for(i=7; i>=0; i--) {
|
122 |
|
|
|
123 |
|
|
freq_tmp = (uint64_t)frequency;
|
124 |
|
|
freq_tmp = freq_tmp << (17 + prsc_shift); // multiply via bit shifts
|
125 |
|
|
freq_tmp = freq_tmp / f_cpu;
|
126 |
|
|
|
127 |
|
|
uint16_t tuning_word = (uint16_t)(freq_tmp);
|
128 |
|
|
|
129 |
|
|
// add 1 to tuning word (for rounding issues)
|
130 |
|
|
freq_real = neo430_freq_gen_nco_real_output(tuning_word+1, prsc_shift);
|
131 |
|
|
|
132 |
|
|
freq_diff = freq_real - frequency;
|
133 |
|
|
if ((int32_t)freq_diff < 0) {
|
134 |
|
|
freq_diff = 0 - freq_diff;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
// best result yet?
|
138 |
|
|
if (freq_diff < freq_diff_best) {
|
139 |
|
|
tuning_word_best = tuning_word;
|
140 |
|
|
prsc_best = i;
|
141 |
|
|
freq_diff_best = freq_diff;
|
142 |
|
|
freq_real_best = freq_real;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
// compute next prescaler
|
146 |
|
|
if ((i == 5) || (i == 3)) {
|
147 |
|
|
prsc_shift = prsc_shift - 3;
|
148 |
|
|
}
|
149 |
|
|
else {
|
150 |
|
|
prsc_shift = prsc_shift - 1;
|
151 |
|
|
}
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
// set tuning word and prescaler
|
156 |
|
|
neo430_freq_gen_set(ch, tuning_word_best, prsc_best);
|
157 |
|
|
|
158 |
|
|
return freq_real_best;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
/* ------------------------------------------------------------
|
163 |
|
|
* INFO Compute actual NCO output frequency based on tuning word and prescaler
|
164 |
|
|
* RETURN the actual output frequency in Hz
|
165 |
|
|
* ------------------------------------------------------------ */
|
166 |
|
|
static uint32_t neo430_freq_gen_nco_real_output(uint16_t tuning_word, uint16_t prsc_shift) {
|
167 |
|
|
|
168 |
|
|
// f_out = ((f_cpu/nco_prsc) * tuning_word[15:0]) / 2^17
|
169 |
|
|
|
170 |
|
|
uint32_t f_cpu = CLOCKSPEED_32bit;
|
171 |
|
|
uint64_t f_out = (uint64_t)f_cpu;
|
172 |
|
|
f_out = f_out * tuning_word;
|
173 |
|
|
f_out = f_out >> (17 + prsc_shift); // divide by 2^17 * PRSC
|
174 |
|
|
|
175 |
|
|
return (uint32_t)f_out;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
/* ------------------------------------------------------------
|
180 |
|
|
* INFO Set HW configuration
|
181 |
|
|
* PARAM ch channel 0,1,2
|
182 |
|
|
* PARAM 16-bit tuning word
|
183 |
|
|
* PARAM 3-bit prescaler selector (0,...,7)
|
184 |
|
|
* ------------------------------------------------------------ */
|
185 |
|
|
void neo430_freq_gen_set(uint16_t ch, uint16_t tuning_word, uint16_t prsc) {
|
186 |
|
|
|
187 |
|
|
// set tuning word and prescaler
|
188 |
|
|
register uint16_t ctrl = FREQ_GEN_CT;
|
189 |
|
|
if (ch == 0) {
|
190 |
|
|
FREQ_GEN_TW_CH0 = tuning_word;
|
191 |
|
|
ctrl &= ~(0b111 << FREQ_GEN_CT_CH0_PRSC0); // clear old prescaler config
|
192 |
|
|
ctrl |= (prsc << FREQ_GEN_CT_CH0_PRSC0); // set new prescaler config
|
193 |
|
|
}
|
194 |
|
|
else if (ch == 1) {
|
195 |
|
|
FREQ_GEN_TW_CH1 = tuning_word;
|
196 |
|
|
ctrl &= ~(0b111 << FREQ_GEN_CT_CH1_PRSC0); // clear old prescaler config
|
197 |
|
|
ctrl |= (prsc << FREQ_GEN_CT_CH1_PRSC0); // set new prescaler config
|
198 |
|
|
}
|
199 |
|
|
else if (ch == 2) {
|
200 |
|
|
FREQ_GEN_TW_CH2 = tuning_word;
|
201 |
|
|
ctrl &= ~(0b111 << FREQ_GEN_CT_CH2_PRSC0); // clear old prescaler config
|
202 |
|
|
ctrl |= (prsc << FREQ_GEN_CT_CH2_PRSC0); // set new prescaler config
|
203 |
|
|
}
|
204 |
|
|
FREQ_GEN_CT = ctrl;
|
205 |
|
|
}
|