summaryrefslogtreecommitdiff
path: root/drivers/ppmsum.cpp
blob: 24d62ac63248843026103eef3dbf35cce9bcff0e (plain)
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
#include "ppmsum.h"

#include <rcc/rcc.h>

PPMSum* PPMSum::self = 0;

template<>
void interrupt<Interrupt::TIM2>() {
	PPMSum::self->irq();
}

void PPMSum::irq() {
	int16_t sr = TIM2.SR;
	TIM2.SR = 0;
	
	if(sr & 0x01) {
		// Timeout.
		
		synced = false;
		
	} else if(sr & 0x02) {
		// Period.
		
		if(TIM2.CCR1 > 5000) {
			synced = true;
			index = 0;
		} else {
			index++;
			index &= 0xf;
		}
		
	} else if(sr & 0x04) {
		// Pulsewidth.
		
		channels[index] = TIM2.CCR2;
	}
}

void PPMSum::enable() {
	RCC.enable(RCC.TIM2);
	TIM2.PSC = 84 - 1;
	TIM2.CCMR1 = 0x0201;
	TIM2.SMCR = 0x54;
	TIM2.CCER = 0x13;
	TIM2.DIER = 0x07;
	
	Interrupt::enable(Interrupt::TIM2);
	
	TIM2.CR1 = 0x05;
}