-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.c
executable file
·141 lines (115 loc) · 3.27 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
#include <stdbool.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "device.h"
#include "hardware/VREF.h"
#include "hardware/AFSK.h"
#include "hardware/Serial.h"
#include "hardware/LED.h"
#include "hardware/UserIO.h"
#include "hardware/SD.h"
#include "hardware/Crypto.h"
#include "hardware/Bluetooth.h"
#include "hardware/BME280.h"
#include "hardware/GPS.h"
#include "protocol/AX25.h"
#include "protocol/KISS.h"
#include "util/Config.h"
#include "util/time.h"
#include "util/FIFO.h"
uint8_t boot_vector = 0x00;
uint8_t OPTIBOOT_MCUSR __attribute__ ((section(".noinit")));
void resetFlagsInit(void) __attribute__ ((naked)) __attribute__ ((used)) __attribute__ ((section (".init0")));
void resetFlagsInit(void) {
__asm__ __volatile__ ("sts %0, r2\n" : "=m" (OPTIBOOT_MCUSR) :);
}
Serial serial;
Afsk modem;
AX25Ctx AX25;
static void ax25_callback(struct AX25Ctx *ctx) {
kiss_messageCallback(ctx);
}
void system_check(void) {
// Check boot vector
if (OPTIBOOT_MCUSR & (1<<PORF)) {
boot_vector = START_FROM_POWERON;
} else if (OPTIBOOT_MCUSR & (1<<BORF)) {
boot_vector = START_FROM_BROWNOUT;
} else if (OPTIBOOT_MCUSR & (1<<WDRF)) {
boot_vector = START_FROM_BOOTLOADER;
} else {
printf(PSTR("Error, indeterminate boot vector %d\r\n"), OPTIBOOT_MCUSR);
printf(PSTR("System start has been halted\r\n"));
while (true) {
LED_TX_ON();
LED_COM_ON();
}
}
// If encryption was previously enabled, require
// it to be initialised to start system.
if (config_crypto_lock) {
if (!crypto_wait()) {
// If initialising crypto times out,
// halt system and display error signal
LED_indicate_error_crypto();
}
}
// TODO: Check GPS_REQUIRED and BLUETOOTH_REQUIRED
// here before giving green light.
// Give the green light if everything checks out
LED_STATUS_ON();
}
void init(void) {
sei();
serial_init(&serial);
stdout = &serial.uart0;
stdin = &serial.uart0;
config_init();
VREF_init();
LED_init();
AFSK_init(&modem);
ax25_init(&AX25, &modem, &modem.fd, ax25_callback);
kiss_init(&AX25, &modem, &serial);
sd_init();
bluetooth_init();
gps_init(&serial);
usrio_init();
if (config_sensors_enabled) {
if (config_sensor_bme280_enabled) bme280_init(config_sensor_bme280_cs_pin);
}
system_check();
if (config_user_jobs_enabled) user_init();
}
mtime_t sensor_poll_time = 0;
void sensor_jobs(void) {
if (rtc_milliseconds() > sensor_poll_time+config_sensor_interval_ms) {
if (config_sensor_bme280_enabled && bme280_ready) {
bme280_poll();
}
sensor_poll_time = rtc_milliseconds();
}
}
void user_init(void) {
// Place user-specified initialisation code here
}
void user_jobs(void) {
// Place user-specified tasks and jobs here
}
int main (void) {
init();
while (true) {
ax25_poll(&AX25);
kiss_poll();
kiss_csma();
sd_jobs();
gps_poll();
if (config_sensors_enabled) sensor_jobs();
if (config_user_jobs_enabled) user_jobs();
}
return(0);
}