2014-12-02 17:10:06 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <avr/io.h>
|
2019-01-27 12:25:11 -07:00
|
|
|
#include <avr/wdt.h>
|
2019-01-29 08:41:27 -07:00
|
|
|
#include <avr/pgmspace.h>
|
2019-02-08 05:18:49 -07:00
|
|
|
#include <util/atomic.h>
|
2019-01-29 08:41:27 -07:00
|
|
|
#include <stdio.h>
|
2019-01-27 12:25:11 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-12-02 17:10:06 -07:00
|
|
|
|
|
|
|
#include "device.h"
|
2019-01-04 08:13:29 -07:00
|
|
|
#include "hardware/VREF.h"
|
2014-12-02 17:10:06 -07:00
|
|
|
#include "hardware/AFSK.h"
|
|
|
|
#include "hardware/Serial.h"
|
2019-01-12 07:12:51 -07:00
|
|
|
#include "hardware/LED.h"
|
2019-01-29 08:41:27 -07:00
|
|
|
#include "hardware/UserIO.h"
|
2019-01-27 12:25:11 -07:00
|
|
|
#include "hardware/SD.h"
|
2019-02-04 14:30:52 -07:00
|
|
|
#include "hardware/Crypto.h"
|
2019-01-29 08:41:27 -07:00
|
|
|
#include "hardware/Bluetooth.h"
|
2019-02-04 09:02:19 -07:00
|
|
|
#include "hardware/GPS.h"
|
2014-12-02 17:10:06 -07:00
|
|
|
#include "protocol/AX25.h"
|
2018-12-27 12:24:21 -07:00
|
|
|
#include "protocol/KISS.h"
|
2019-02-08 05:18:49 -07:00
|
|
|
#include "util/Config.h"
|
2019-01-01 12:34:02 -07:00
|
|
|
#include "util/time.h"
|
|
|
|
#include "util/FIFO.h"
|
2014-12-02 17:10:06 -07:00
|
|
|
|
2019-01-27 12:25:11 -07:00
|
|
|
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) :);
|
|
|
|
}
|
|
|
|
|
2014-12-02 17:10:06 -07:00
|
|
|
Serial serial;
|
|
|
|
Afsk modem;
|
|
|
|
AX25Ctx AX25;
|
|
|
|
|
2018-12-27 12:24:21 -07:00
|
|
|
static void ax25_callback(struct AX25Ctx *ctx) {
|
|
|
|
kiss_messageCallback(ctx);
|
|
|
|
}
|
2014-12-02 17:10:06 -07:00
|
|
|
|
2019-01-12 07:12:51 -07:00
|
|
|
void system_check(void) {
|
2019-02-08 05:18:49 -07:00
|
|
|
// Check boot vector
|
2019-01-27 12:25:11 -07:00
|
|
|
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 {
|
2019-01-29 08:41:27 -07:00
|
|
|
printf(PSTR("Error, indeterminate boot vector %d\r\n"), OPTIBOOT_MCUSR);
|
|
|
|
printf(PSTR("System start has been halted\r\n"));
|
2019-01-27 12:25:11 -07:00
|
|
|
while (true) {
|
|
|
|
LED_TX_ON();
|
|
|
|
LED_COM_ON();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 05:18:49 -07:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 08:36:01 -07:00
|
|
|
// TODO: Check GPS_REQUIRED and BLUETOOTH_REQUIRED
|
|
|
|
// here before giving green light.
|
|
|
|
|
2019-02-08 05:18:49 -07:00
|
|
|
// Give the green light if everything checks out
|
2019-01-12 07:12:51 -07:00
|
|
|
LED_STATUS_ON();
|
|
|
|
}
|
|
|
|
|
2014-12-02 17:10:06 -07:00
|
|
|
void init(void) {
|
2019-02-08 05:18:49 -07:00
|
|
|
|
2014-12-04 07:22:25 -07:00
|
|
|
sei();
|
2014-12-18 15:45:36 -07:00
|
|
|
|
2014-12-18 16:15:56 -07:00
|
|
|
serial_init(&serial);
|
|
|
|
stdout = &serial.uart0;
|
|
|
|
stdin = &serial.uart0;
|
|
|
|
|
2019-02-08 05:18:49 -07:00
|
|
|
config_init();
|
|
|
|
|
2019-01-04 08:13:29 -07:00
|
|
|
VREF_init();
|
2019-01-12 07:12:51 -07:00
|
|
|
LED_init();
|
2018-12-27 12:24:21 -07:00
|
|
|
AFSK_init(&modem);
|
|
|
|
ax25_init(&AX25, &modem, &modem.fd, ax25_callback);
|
|
|
|
kiss_init(&AX25, &modem, &serial);
|
2019-01-27 12:25:11 -07:00
|
|
|
sd_init();
|
2019-01-29 08:41:27 -07:00
|
|
|
bluetooth_init();
|
2019-02-04 09:02:19 -07:00
|
|
|
gps_init(&serial);
|
2019-01-29 08:41:27 -07:00
|
|
|
usrio_init();
|
2019-01-12 07:12:51 -07:00
|
|
|
|
|
|
|
system_check();
|
2014-12-02 17:10:06 -07:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:30:26 -07:00
|
|
|
|
2014-12-02 17:10:06 -07:00
|
|
|
int main (void) {
|
|
|
|
init();
|
|
|
|
|
2018-12-27 12:24:21 -07:00
|
|
|
while (true) {
|
|
|
|
ax25_poll(&AX25);
|
2019-01-08 12:56:58 -07:00
|
|
|
kiss_poll();
|
|
|
|
kiss_csma();
|
2019-01-29 08:41:27 -07:00
|
|
|
sd_jobs();
|
2019-02-04 09:02:19 -07:00
|
|
|
gps_poll();
|
2018-12-27 12:24:21 -07:00
|
|
|
}
|
2014-12-02 17:10:06 -07:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|