summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 7af112253581c883a62acdc1a829480fc42ea201 (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
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
#include "stm32.h"
#include "interrupt.h"

volatile unsigned int cnt;

void i2c_write_reg(uint8_t addr, uint8_t reg, uint8_t data) {
	I2C2.CR1 |= 0x100;
	while(!(I2C2.SR1 & 0x01));
	
	I2C2.DR = (addr << 1) | 0;
	while (!(I2C2.SR1 & 0x02));
	I2C2.SR2;
	
	I2C2.DR = reg;
	while (!(I2C2.SR1 & 0x80));
	
	I2C2.DR = data;
	while (!(I2C2.SR1 & 0x04));
	
	I2C2.CR1 |= 0x200;
}

void i2c_read_reg(uint8_t addr, uint8_t reg, uint8_t len, uint8_t* buf) {
	I2C2.CR1 |= 0x100;
	while(!(I2C2.SR1 & 0x01));
	
	I2C2.DR = (addr << 1) | 0;
	while (!(I2C2.SR1 & 0x02));
	I2C2.SR2;
	
	I2C2.DR = reg;
	while (!(I2C2.SR1 & 0x80));
	
	I2C2.CR1 |= 0x100;
	while(!(I2C2.SR1 & 0x01));
	
	I2C2.DR = (addr << 1) | 1;
	while (!(I2C2.SR1 & 0x02));
	I2C2.SR2;
	
	I2C2.CR1 |= 0x400; // Set ACK.
	
	while(len) {
		if(len == 3) {
			while (!(I2C2.SR1 & 0x04)); // Wait for BTF
			
			I2C2.CR1 &= ~0x400; // Clear ACK.
			*buf++ = I2C2.DR;
			len--;
			
			I2C2.CR1 |= 0x200; // Set STOP.
			*buf++ = I2C2.DR;
			len--;
			
		} else {
			while (!(I2C2.SR1 & 0x40)); // Wait for RXNE
			
			*buf++ = I2C2.DR;
			len--;
		}
	}
}

template<>
void interrupt<Interrupt::USART1>() {
	USART1.DR;
	GPIOA.ODR ^= 1 << 5;
}

void usart_send(uint8_t data) {
	while(!(USART1.SR & 0x80)); // Wait for TXE.
	
	USART1.DR = data;
}

void xbee_send(int len, const uint8_t* buf) {
	// Start and length.
	usart_send(0x7e);
	usart_send(((len + 14) >> 8) & 0xff);
	usart_send((len + 14) & 0xff);
	
	// Frame type and ID.
	usart_send(0x10);
	usart_send(0x01);
	
	// Destination address.
	usart_send(0x00);
	usart_send(0x13);
	usart_send(0xa2);
	usart_send(0x00);
	usart_send(0x40);
	usart_send(0x6f);
	usart_send(0x19);
	usart_send(0xf1);
	
	usart_send(0x00);
	usart_send(0x00);
	usart_send(0x00);
	usart_send(0x00);
	
	uint8_t chsum = 0x80;
	
	// Payload
	for(int i = 0; i < len; i++) {
		usart_send(buf[i]);
		chsum -= buf[i];
	}
	
	usart_send(chsum);
}

class PPMSum {
	friend void interrupt<Interrupt::TIM4>();
	
	private:
		static PPMSum* self;
		
		uint8_t index;
		uint16_t channels[16];
		
		void irq() {
			int16_t sr = TIM4.SR;
			TIM4.SR = 0;
			
			if(sr & 0x06) {
				GPIOA.ODR = 1 << 5;
			} else {
				GPIOA.ODR = 0;
			}
			
			
			if(sr & 0x01) {
				// Timeout.
				
				// TODO: Indicate failsafe.
				
			} else if(sr & 0x02) {
				// Period.
				
				if(TIM4.CCR1 > 5000) {
					index = 0;
				} else {
					index++;
				}
				
			} else if(sr & 0x04) {
				// Pulsewidth.
				
				channels[index] = TIM4.CCR2;
			}
		}
	
	public:
		PPMSum() {
			self = this;
		}
		
		void start() {
			RCC.enable(RCC.TIM4);
			TIM4.PSC = 36;
			TIM4.CCMR1 = 0x0201;
			TIM4.SMCR = 0x54;
			TIM4.CCER = 0x31;
			TIM4.DIER = 0x07;
			
			Interrupt::enable(Interrupt::TIM4);
			
			TIM4.CR1 = 0x05;
		}
};

PPMSum* PPMSum::self = 0;

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

int main() {
	RCC.enable(RCC.AFIO);
	RCC.enable(RCC.IOPA);
	RCC.enable(RCC.IOPB);
	
	GPIOA.CRL = 0x44344444;
	GPIOA.CRH = 0x444444b4;
	GPIOA.ODR = 1 << 5;
	
	GPIOB.CRH = 0x4444ff44;
	
	cnt = 0;
	while(cnt++ < (1 << 20));
	
	// 100 kHz.
	RCC.enable(RCC.I2C2);
	while(cnt++ < (1 << 20));
	
	I2C2.CR1 = 0x8000;
	I2C2.CR1 = 0;
	
	I2C2.CR2 = 36;
	I2C2.TRISE = 37;
	I2C2.CCR = 180;
	I2C2.CR1 = 1;
	
	i2c_write_reg(0x68, 0x3e, 0x03);
	i2c_write_reg(0x68, 0x16, 0x18 | 0x02);
	
	uint8_t buf[6];
	
	RCC.enable(RCC.USART1);
	USART1.BRR = 7500; // 9600 baud
	USART1.CR1 = 0x202c;
	
	Interrupt::enable(Interrupt::USART1);
	
	PPMSum ppmsum;
	ppmsum.start();
	
	while(1) {
		cnt++;
		if(cnt & (1 << 20)) {
			//GPIOA.ODR = 1 << 5;
		} else {
			//GPIOA.ODR = 0;
		}
		
		if(!(cnt & ((1 << 20) - 1))) {
			i2c_read_reg(0x68, 0x1d, 6, buf);
			//xbee_send(6, buf);
		}
	}
}