-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk66TPM.ino
108 lines (93 loc) · 2.23 KB
/
k66TPM.ino
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
// T3.6 TPM tests pin 16 PDB0 TPM1_CH0
// default config: 16 MHz crystal clock,
// PPS to pin 23
#define PRREG(x) Serial.print(#x" 0x"); Serial.println(x,HEX)
volatile uint16_t ticks, ding;
void ppsisr() {
ticks = TPM1_CNT;
ding = 1;
}
void pps() {
analogWriteFrequency(16, 400); // MOD 40000
attachInterrupt(23, ppsisr, RISING); // GPS PPS
while (1) {
if (ding) {
static uint16_t prev, delta; // 16-bit counter, count mod 40000
int32_t d;
ding = 0;
// Serial.print(ticks); Serial.print(" "); Serial.println(prev);
if (ticks < prev) delta = 40000 + ticks - prev; // rollover
else delta = ticks - prev;
Serial.print(delta); Serial.print(" ticks ");
if (delta < 40000) d = -(40000 - delta);
else d = delta;
Serial.print(.0625 * d); Serial.println(" ppm");
prev = ticks;
}
}
}
void tpm1_isr() {
TPM1_SC |= 1 << 7; // clear FTM_SC_TOF first
ticks++;
}
void doticks() {
analogWriteFrequency(16, 400); // MOD 40000
NVIC_ENABLE_IRQ(IRQ_TPM1);
TPM1_SC |= 1 << 6; // FTM_SC_TOIE
analogWrite(16, 128);
PRREG(TPM1_SC);
PRREG(TPM1_MOD);
PRREG(TPM1_C0V);
while (1) {
delay(1000);
Serial.println(ticks);
Serial.print("TPM1_CNT "); Serial.println(TPM1_CNT);
}
}
void dflt() {
analogWrite(16, 128);
PRREG(SIM_SCGC2);
PRREG(SIM_SOPT2);
PRREG(SIM_SOPT9);
PRREG(TPM1_SC);
PRREG(TPM1_MOD);
PRREG(TPM1_C0V);
PRREG(TPM1_C0SC);
}
void tpm2() {
// pin 29 TPM2_CH0 default 16mhz/32768
SIM_SCGC2 |= SIM_SCGC2_TPM2;
SIM_SOPT2 |= SIM_SOPT2_TPMSRC(2);
CORE_PIN29_CONFIG = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
TPM2_CNT = 0;
TPM2_MOD = 32767;
TPM2_C0V = 0x4000;
TPM2_C0SC = 0x28;
TPM2_C1SC = 0x28;
TPM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
PRREG(SIM_SCGC2);
PRREG(SIM_SOPT2);
PRREG(SIM_SOPT9);
PRREG(TPM2_SC);
PRREG(TPM2_MOD);
PRREG(TPM2_C0V);
PRREG(TPM2_C0SC);
}
void pwm8() {
analogWriteFrequency(16, 8000000); // 16 and 17 max 8mhz uses TPM T3.6 only
analogWrite(16, 128);
PRREG(TPM1_SC);
PRREG(TPM1_MOD);
PRREG(TPM1_C0V);
}
void setup() {
Serial.begin(9600);
while (!Serial);
//dflt(); // default
//pwm8(); // 8mhz PWM
//doticks();
//pps();
tpm2();
}
void loop() {
}