-
Notifications
You must be signed in to change notification settings - Fork 20
/
freqmeter.c
356 lines (284 loc) · 8.71 KB
/
freqmeter.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/usb/usbd.h>
#include "usbcdc.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define PACKET_SIZE 64
#define BUFFER_SIZE 256
#define DISP_DELAY 100
/* NOTE: For systems that has SYSCLK != 72MHz, modify mco_val, mco_name and filters_name in addition to clock setup. */
static volatile uint32_t systick_ms = 0;
static volatile uint32_t freq = 0; /* 32bit = approx. 4.3G ticks per second. */
static volatile uint32_t freq_scratch = 0; /* scratch pad. */
static volatile bool hold = false;
static uint32_t mco_val[] = {
RCC_CFGR_MCO_NOCLK,
//RCC_CFGR_MCO_SYSCLK, /* Will not be able to output. */
RCC_CFGR_MCO_HSI,
RCC_CFGR_MCO_HSE,
RCC_CFGR_MCO_PLL_DIV2,
//RCC_CFGR_MCO_PLL2CLK, /* No signal. */
//RCC_CFGR_MCO_PLL3CLK_DIV2, /* No signal. */
//RCC_CFGR_MCO_XT1, /* No signal. */
//RCC_CFGR_MCO_PLL3, /* No signal. */
};
static char *mco_name[] = {
" OFF",
//"72 MHz ", /* Will not be able to output. */
" 8 MHz RC",
" 8 MHz ",
"36 MHz ",
//"PLL2 ", /* No signal. */
//"PLL3 DIV2", /* No signal. */
//"XT1 ", /* No signal. */
//"PLL3 ", /* No signal. */
};
static int mco_current = 0; /* Default to off. */
static enum tim_ic_filter filters_val[] = {
TIM_IC_OFF,
TIM_IC_CK_INT_N_2,
TIM_IC_CK_INT_N_4,
TIM_IC_CK_INT_N_8,
TIM_IC_DTF_DIV_2_N_6,
TIM_IC_DTF_DIV_2_N_8,
TIM_IC_DTF_DIV_4_N_6,
TIM_IC_DTF_DIV_4_N_8,
TIM_IC_DTF_DIV_8_N_6,
TIM_IC_DTF_DIV_8_N_8,
TIM_IC_DTF_DIV_16_N_5,
TIM_IC_DTF_DIV_16_N_6,
TIM_IC_DTF_DIV_16_N_8,
TIM_IC_DTF_DIV_32_N_5,
TIM_IC_DTF_DIV_32_N_6,
TIM_IC_DTF_DIV_32_N_8,
};
static char *filters_name[] = {
" OFF",
"36.000 MHz",
"18.000 MHz",
" 9.000 MHz",
" 6.000 MHz",
" 4.500 MHz",
" 3.000 MHz",
" 2.250 MHz",
" 1.500 MHz",
" 1.125 MHz",
"900.00 kHz",
"750.00 kHz",
"562.50 kHz",
"450.00 kHz",
"375.00 kHz",
"281.25 kHz",
};
static int filter_current = 0; /* Default to no filter. */
static enum tim_ic_psc prescalers_val[] = {
TIM_IC_PSC_OFF,
TIM_IC_PSC_2,
TIM_IC_PSC_4,
TIM_IC_PSC_8,
};
static char *prescalers_name[] = {
"OFF",
" 2",
" 4",
" 8",
};
static int prescaler_current = 0; /* Default to no prescaler. */
static char buffer[BUFFER_SIZE];
void systick_ms_setup(void) {
/* 72MHz clock, interrupt for every 72,000 CLKs (1ms). */
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_set_reload(72000 - 1);
systick_interrupt_enable();
systick_counter_enable();
}
void timer_setup(void) {
/* NOTE: Digital input pins have Schmitt filter. */
rcc_periph_clock_enable(RCC_TIM2);
rcc_periph_reset_pulse(RST_TIM2);
/* Disable inputs. */
timer_ic_disable(TIM2, TIM_IC1);
timer_ic_disable(TIM2, TIM_IC2);
timer_ic_disable(TIM2, TIM_IC3);
timer_ic_disable(TIM2, TIM_IC4);
/* Disable outputs. */
timer_disable_oc_output(TIM2, TIM_OC1);
timer_disable_oc_output(TIM2, TIM_OC2);
timer_disable_oc_output(TIM2, TIM_OC3);
timer_disable_oc_output(TIM2, TIM_OC4);
/* Timer mode: no divider, edge, count up */
timer_disable_preload(TIM2);
timer_continuous_mode(TIM2);
timer_set_period(TIM2, 65535);
timer_slave_set_mode(TIM2, TIM_SMCR_SMS_ECM1);
timer_slave_set_filter(TIM2, filters_val[filter_current]);
timer_slave_set_polarity(TIM2, TIM_ET_RISING);
timer_slave_set_prescaler(TIM2, prescalers_val[prescaler_current]);
timer_slave_set_trigger(TIM2, TIM_SMCR_TS_ETRF);
timer_update_on_overflow(TIM2);
nvic_enable_irq(NVIC_TIM2_IRQ);
timer_enable_counter(TIM2);
timer_enable_irq(TIM2, TIM_DIER_CC1IE);
}
void mco_setup(void) {
/* Outputs 36MHz clock on PA8, for calibration. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO8);
rcc_set_mco(mco_val[mco_current]); /* This merely sets RCC_CFGR. */
}
void usbcdc_printf(const char *fmt, ...) {
int len;
uint16_t written = 0;
va_list args;
va_start(args, fmt);
/* TODO: The following line costs approx. 20KB. Find an alternative if necessary. */
len = vsnprintf(
buffer,
BUFFER_SIZE,
fmt,
args
);
while (written < len) {
if ((len - written) > PACKET_SIZE) {
written += usbcdc_write(buffer + written, PACKET_SIZE);
} else {
written += usbcdc_write(buffer + written, len - written);
}
}
}
void usbcdc_clear_screen(void) {
usbcdc_printf("\033c\r");
}
void poll_command(void) {
char cmd = usbcdc_getc();
switch (cmd) {
case '\0': {
/* No input available. */
return;
}
case 'o':
case 'O': {
/* Switch MCO. */
mco_current ++;
if (mco_current >= ARRAY_SIZE(mco_val)) {
mco_current = 0;
}
rcc_set_mco(mco_val[mco_current]);
return;
}
case 'h':
case 'H': {
/* Toggle hold. */
hold = !hold;
return;
}
case 'f':
case 'F': {
/* Configure digital filter. */
filter_current ++;
if (filter_current >= ARRAY_SIZE(filters_val)) {
filter_current = 0;
}
timer_slave_set_filter(TIM2, filters_val[filter_current]);
return;
}
case 'p':
case 'P': {
/* Configure prescaler. */
prescaler_current ++;
if (prescaler_current >= ARRAY_SIZE(prescalers_val)) {
prescaler_current = 0;
}
timer_slave_set_prescaler(TIM2, prescalers_val[prescaler_current]);
return;
}
case '\n':
case '\r': {
/* Remote echo for newline -- for convenient data recording. */
usbcdc_write("\r\n", 1); /* This works since buffer is not modified. */
return;
}
default: {
/* Invalid command. */
return;
}
}
}
int main(void) {
rcc_clock_setup_in_hse_8mhz_out_72mhz();
rcc_periph_clock_enable(RCC_GPIOA); /* For MCO. */
rcc_periph_clock_enable(RCC_GPIOB); /* For LED, USB pull-up and TIM2. */
rcc_periph_clock_enable(RCC_AFIO); /* For MCO. */
/* Setup PB1 for the LED. */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
gpio_set(GPIOB, GPIO1);
/* Pull PA1 down to GND, which is adjascent to timer imput and can be used as an convenient return path. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
gpio_clear(GPIOA, GPIO1);
/* Setup PB9 to pull up the D+ high. The circuit is active low. */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO9);
gpio_clear(GPIOB, GPIO9);
usbcdc_init();
gpio_clear(GPIOB, GPIO1);
timer_setup();
systick_ms_setup();
mco_setup();
/* Wait 500ms for USB setup to complete before trying to send anything. */
/* Takes ~ 130ms on my machine */
// TODO: a better way?
while (systick_ms < 500);
/* The loop. */
uint32_t last_ms = 0;
while (freq == 0);
/* The loop (for real). */
while (true) {
// TODO: whether to support dividers? Any meaningful use?
poll_command();
// TODO: currently missing 20 ticks out of 36,000,000 ticks (<0.6ppm error).
// However, before we use TCXO to supply clock to the MCU, fixing it will not improve precision.
usbcdc_clear_screen();
/* NOTE: Subtract one extra overflow (65536 ticks) occurred during counter reset. */
/* TODO: The following line costs approx. 20KB. Find an alternative if necessary. */
usbcdc_printf("%4lu.%06lu MHz %c [Hold: %s]\r\n\r\n",
(freq - 65536) / 1000000,
(freq - 65536) % 1000000,
gpio_get(GPIOB, GPIO1) ? '.' : ' ',
hold ? "ON " : "OFF"
);
usbcdc_printf("Clock output: %s\r\n", mco_name[mco_current]);
usbcdc_printf("Digital Filter: %s\r\n", filters_name[filter_current]);
usbcdc_printf("Pre-scaler: %s\r\n", prescalers_name[prescaler_current]);
while (systick_ms < (last_ms + DISP_DELAY));
last_ms = systick_ms;
}
return 0;
}
/* Interrupts */
void tim2_isr(void) {
if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {
freq_scratch += 65536; /* TIM2 is 16-bit and overflows every 65536 events. */
timer_clear_flag(TIM2, TIM_SR_CC1IF); /* Clear interrupt flag. */
}
}
void sys_tick_handler(void) {
systick_ms ++;
if (systick_ms % 1000 == 0) {
/* Scratch pad to finalized result */
if (!hold) {
freq = freq_scratch + timer_get_counter(TIM2);
if (prescaler_current)
freq *= (1 << prescaler_current);
}
/* Reset the counter. This will generate one extra overflow for next measurement. */
/* In case of nothing got counted, manually generate a reset to keep consistency. */
timer_set_counter(TIM2, 1);
timer_set_counter(TIM2, 0);
freq_scratch = 0;
gpio_toggle(GPIOB, GPIO1);
}
}