summaryrefslogtreecommitdiff
path: root/main.cpp
blob: b0db636ca385f294a7d1f935f8be124da7b8912b (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
#include <rcc/rcc.h>
#include <interrupt/interrupt.h>
#include <os/thread.h>
#include <os/time.h>

#include <gpio/pin.h>

#include <usb/usb.h>
#include <i2c/i2c.h>

#include "lsm303dlm.h"
#include "l3gd20.h"

#include "ppmsum.h"

static Pin& led_status = PA4;
static Pin& led_error = PC4;

static Pin& usb_vbus = PB13;
static Pin& usb_dm = PB14;
static Pin& usb_dp = PB15;

static Pin& jtag_tdo = PC3;
static Pin& jtag_tms = PC13;
static Pin& jtag_tck = PC14;
static Pin& jtag_tdi = PC15;

static Pin& i2c_scl = PB10;
static Pin& i2c_sda = PB11;

static Pin& cs_pressure = PB4;
static Pin& cs_gyro = PB5;

USB_otg usb(OTG_HS);

bool i2c_read(uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
	uint8_t buf[wLength];
	I2C2.read_reg(wValue, wIndex, wLength, buf);
	
	usb.write(0, (uint32_t*)buf, wLength);
	return true;
}

bool jtag_tick(bool tdi, bool tms) {
	bool tdo = jtag_tdo.get();
	jtag_tdi.set(tdi);
	jtag_tms.set(tms);
	jtag_tck.on();
	for(uint32_t i = 0; i < 1000; i++) {
		asm volatile("nop");
	}
	jtag_tck.off();
	for(uint32_t i = 0; i < 1000; i++) {
		asm volatile("nop");
	}
	return tdo;
}

bool jtag_shift(uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
	if(wLength > 16) {
		return false;
	}
	
	uint32_t tdo = 0;
	
	for(int16_t i = 0; i < wLength; i++) {
		tdo |= jtag_tick(wValue & 1, wIndex & 1) ? 1 << i : 0;
		wValue >>= 1;
		wIndex >>= 1;
	}
	
	usb.write(0, &tdo, (wLength + 7) >> 3);
	return true;
}

template<>
void interrupt<(Interrupt::IRQ)77>() {
	usb.process();
}

void usb_main() {
	usb_vbus.set_mode(Pin::Input);
	usb_dm.set_mode(Pin::AF);
	usb_dm.set_pull(Pin::PullNone);
	usb_dm.set_af(12);
	usb_dp.set_mode(Pin::AF);
	usb_dp.set_pull(Pin::PullNone);
	usb_dp.set_af(12);
	
	RCC.enable(RCC.OTGHS);
	//Interrupt::enable((Interrupt::IRQ)77);
	
	usb.init();
	
	while(1) {
		usb.process();
		Thread::yield();
	}
}

uint32_t usb_stack[1024];

Thread usb_thread(usb_stack, sizeof(usb_stack), usb_main);

L3GD20 gyro(cs_gyro, SPI1);
LSM303DLM_A accel(I2C2);
LSM303DLM_M magn(I2C2);

PPMSum ppmsum;

int main() {
	// Initialize system timer.
	STK.LOAD = 168000000 / 8 / 1000; // 1000 Hz.
	STK.CTRL = 0x03;
	
	RCC.enable(RCC.GPIOA);
	RCC.enable(RCC.GPIOB);
	RCC.enable(RCC.GPIOC);
	RCC.enable(RCC.GPIOD);
	
	led_status.set_mode(Pin::Output);
	led_status.off();
	led_error.set_mode(Pin::Output);
	led_error.off();
	
	jtag_tdi.set_mode(Pin::Output);
	jtag_tms.set_mode(Pin::Output);
	jtag_tck.set_mode(Pin::Output);
	jtag_tdo.set_mode(Pin::Input);
	
	RCC.enable(RCC.I2C2);
	I2C2.enable(i2c_scl, i2c_sda);
	
	usb_thread.start();
	
	RCC.enable(RCC.SPI1);
	
	PA5.set_mode(Pin::AF);
	PA5.set_af(5);
	PA6.set_mode(Pin::AF);
	PA6.set_af(5);
	PA7.set_mode(Pin::AF);
	PA7.set_af(5);
	
	cs_gyro.on();
	cs_gyro.set_mode(Pin::Output);
	cs_pressure.on();
	cs_pressure.set_mode(Pin::Output);
	
	SPI1.reg.CR1 = 0x37f;
	
	Time::sleep(1000);
	
	gyro.init();
	accel.init();
	magn.init();
	
	PA0.set_mode(Pin::AF);
	PA0.set_af(1);
	ppmsum.enable();
	
	while(1) {
		led_error.toggle();
		Time::sleep(10);
		
		gyro.update();
		accel.update();
		magn.update();
		
		int16_t buf[] = {
			gyro.x,
			gyro.y,
			gyro.z,
			accel.x,
			accel.y,
			accel.z,
			magn.x,
			magn.y,
			magn.z,
		};
		
		if(usb.ep_ready(1)) {
			usb.write(1, (uint32_t*)buf, sizeof(buf));
		}
	}
}