/
main.c
297 lines (240 loc) · 8.59 KB
/
main.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
#include "main.h"
#include "91x_lib.h"
#include "eeprom.h"
#include "flight_ctrl_comms.h"
#include "i2c.h"
#include "irq_priority.h"
#include "kalman_filter.h"
#include "led.h"
#include "logging.h"
#include "lsm303dl.h"
#include "mag_calibration.h"
#include "navigation.h"
#include "sd_card.h"
#include "spi_slave.h"
#include "timing.h"
#include "uart1.h"
#include "uart2.h"
#include "ublox.h"
#include "vision.h"
// =============================================================================
// Private data:
#define MAX_CALLBACKS_POW_OF_2 (2) // 2^2 = 4
#define MAX_CALLBACKS (1 << MAX_CALLBACKS_POW_OF_2)
static volatile Callback callback_buffer_[4] = { 0 };
static volatile size_t callback_buffer_head_ = 0;
static size_t callback_buffer_tail_ = 0;
static uint32_t overrun_counter_ = 0;
#ifndef LOGGING_BUTTON
static uint32_t mag_calibration_ = 0;
#endif
// =============================================================================
// Private function declarations:
int main(void) __attribute__ ((noreturn));
// =============================================================================
// Public functions:
void FiftyHzInterruptHandler(void)
{
VIC_SWITCmd(EXTIT3_ITLine, DISABLE);
uint16_t button = GPIO_ReadBit(GPIO3, GPIO_Pin_1);
static uint16_t button_pv = 0;
#ifdef LOGGING_BUTTON
if (button && (button_pv == 0x7FFF))
{
if (LoggingActive())
CloseLogFile();
else
OpenLogFile(0);
}
#else
// Start and stop magnetometer calibration.
if (button && (button_pv == 0x7FFF)) mag_calibration_ = !mag_calibration_;
#endif
// Reset GPS home position.
// if (button && (button_pv == 0x7FFF)) SetGeodeticHome();
button_pv = (button_pv << 1) | button;
}
//------------------------------------------------------------------------------
// This function responds to the interrupt triggered when an external device
// pulls down the interrupt line (pin 5 on the FlightCtrl header). This is a
// low-priority interrupt, so some computation can be safely added here.
void FlightCtrlInterruptHandler(void)
{
WIU_ClearITPendingBit(WIU_Line16);
VIC_SWITCmd(EXTIT2_ITLine, DISABLE);
PrepareFlightCtrlDataExchange();
}
//------------------------------------------------------------------------------
// This is a low-priority interrupt that is triggered by high-frequency data
// collecting interrupts such as SPI and I2C. It enables the data processing to
// be performed at a lower priority than the data collection, but at a higher
// priority that slower computations.
void NewDataInterruptHandler(void)
{
VIC_SWITCmd(EXTIT0_ITLine, DISABLE);
while (callback_buffer_head_ != callback_buffer_tail_)
{
callback_buffer_tail_ = (callback_buffer_tail_ + 1) % MAX_CALLBACKS;
(*callback_buffer_[callback_buffer_tail_])();
}
}
// -----------------------------------------------------------------------------
// This puts a callback into the callback ring buffer.
void SetNewDataCallback(Callback callback)
{
if (!callback) return;
callback_buffer_head_ = (callback_buffer_head_ + 1) % MAX_CALLBACKS;
callback_buffer_[callback_buffer_head_] = callback;
VIC_SWITCmd(EXTIT0_ITLine, ENABLE);
}
// =============================================================================
// Private functions:
// Configure the interrupt vector.
static void VICConfig(void)
{
// Enable the AHB (advanced high-performance but) clock for VIC (vectored
// interrupt controller).
SCU_AHBPeriphClockConfig(__VIC,ENABLE);
// Reset the VIC registers (to their default reset values).
VIC_DeInit();
// Initialize VICs default vector registers.
VIC_InitDefaultVectors();
// Reset the wakeup interrupt unit registers.
WIU_DeInit();
}
//------------------------------------------------------------------------------
static void ExternalButtonInit(void)
{
SCU_APBPeriphClockConfig(__GPIO3 ,ENABLE);
GPIO_InitTypeDef gpio_init;
// Configure P3.1 -> external button as an input pin.
gpio_init.GPIO_Direction = GPIO_PinInput;
gpio_init.GPIO_Pin = GPIO_Pin_1;
gpio_init.GPIO_Type = GPIO_Type_PushPull;
gpio_init.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
gpio_init.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init(GPIO3, &gpio_init);
}
//------------------------------------------------------------------------------
int main(void)
{
VICConfig();
TimingInit();
LEDInit();
UART1Init();
UART2Init();
I2CInit();
SPISlaveInit();
Wait(100);
UART1Printf("University of Tokyo NaviCtrl firmware V2");
ReadEEPROM();
UBloxInit();
LSM303DLInit();
FlightCtrlCommsInit();
SDCardInit();
NavigationInit();
ExternalButtonInit();
// Enable the "new data" interrupt.
VIC_Config(EXTIT0_ITLine, VIC_IRQ, IRQ_PRIORITY_NEW_DATA);
VIC_ITCmd(EXTIT0_ITLine, ENABLE);
// Enable the 50Hz Interrupt.
VIC_Config(EXTIT3_ITLine, VIC_IRQ, IRQ_PRIORITY_50HZ);
VIC_ITCmd(EXTIT3_ITLine, ENABLE);
// Main loop.
uint32_t led_timer = GetTimestamp();
for (;;)
{
// Check for new data from the magnetometer.
ProcessIncomingLSM303DL();
#ifndef LOGGING_BUTTON
// Skip the rest of the main loop if mag calibration is ongoing.
if (MagCalibration(mag_calibration_)) continue;
#endif
// Check for new data on the GPS UART port.
ProcessIncomingUBlox();
// Check for new data from the FlightCtrl.
if (NewDataFromFlightCtrl())
{
ClearNewDataFromFlightCtrlFlag();
KalmanAccelerometerUpdate();
UpdateNavigation();
PrepareFlightCtrlDataExchange();
RequestLSM303DL();
// Check if new data has come while processing the data. This indicates
// that processing did not complete fast enough.
if (NewDataFromFlightCtrl())
{
overrun_counter_++;
}
}
// Check for incoming data on the "update & debug" UART port.
ProcessIncomingUART1();
// Check for incoming vision data on the "FligthCtrl" UART port.
ProcessIncomingUART2();
// ProcessLogging();
// Check sensor data freshness.
CheckUBXFreshness();
CheckVisionFreshness();
CheckLSM303DLFreshness();
// Normally the magnetometer is read every time new data comes from the
// FlightCtrl. The following statement is a backup that ensures the
// magnetometer is updated even if there is no connection to the FlightCtrl
// and also deals with read errors.
if (LSM303DLDataStale())
{
if (MillisSinceTimestamp(LSM303DLLastRequestTimestamp()) > 20)
RequestLSM303DL();
if (LSM303DLErrorBits() & LSM303DL_ERROR_BIT_I2C_BUSY)
I2CReset();
}
if (TimestampInPast(led_timer))
{
GreenLEDToggle();
while (TimestampInPast(led_timer)) led_timer += 200;
// Debug output
// UART1Printf("%+5.2f,%+5.2f,%+5.2f",
// MagneticVector()[0],
// MagneticVector()[1],
// MagneticVector()[2]);
// UART1Printf("%i,%i,%i",
// MagnetometerVector()[0],
// MagnetometerVector()[1],
// MagnetometerVector()[2]);
// UART1Printf("%i,%i,%i",
// MagnetometerBiasVector()[0],
// MagnetometerBiasVector()[1],
// MagnetometerBiasVector()[2]);
// UART1Printf("%f", HeadingAngle());
// UART1Printf("%f,%f,%f",
// (float)(UBXPosLLH()->longitude * 1e-7),
// (float)(UBXPosLLH()->latitude * 1e-7),
// (float)(UBXPosLLH()->height_above_ellipsoid * 1e-3));
// UART1PrintfSafe("C:(%+6.2f,%+6.2f,%+6.2f) H:%+4.0f",
// VisionPositionVector()[0],
// VisionPositionVector()[1],
// VisionPositionVector()[2],
// HeadingAngle() * 180.0 / 3.141596);
// UART1PrintfSafe("C:(%+6.2f,%+6.2f,%+6.2f) D:(%+6.2f,%+6.2f,%+6.2f) H:%+4.0f",
// PositionVector()[0],
// PositionVector()[1],
// PositionVector()[2],
// NavDeltaPosition(0),
// NavDeltaPosition(1),
// NavDeltaPosition(2),
// HeadingAngle() * 180.0 / 3.141596);
// UART1PrintfSafe("C:(%+6.2f,%+6.2f,%+6.2f) D:(%+6.2f,%+6.2f,%+6.2f) H:%+4.0f | V:(%+6.2f,%+6.2f,%+6.2f,%i)",
// PositionVector()[0],
// PositionVector()[1],
// PositionVector()[2],
// NavDeltaPosition(0),
// NavDeltaPosition(1),
// NavDeltaPosition(2),
// HeadingAngle() * 180.0 / 3.141596,
// VisionPositionVector()[0],
// VisionPositionVector()[1],
// VisionPositionVector()[2],
// VisionStatus()
// );
}
}
}