├── images └── .keepdir ├── flash ├── protocol ├── HDLC.h ├── Digipeater.h ├── AX25.h ├── AX25.c └── Digipeater.c ├── .gitignore ├── util ├── constants.h ├── CRC-CCIT.h ├── time.h ├── FIFO.h └── CRC-CCIT.c ├── README.md ├── hardware ├── Serial.h ├── Serial.c ├── AFSK.h └── AFSK.c ├── device.h ├── config.h ├── main.c ├── Makefile └── LICENSE /images/.keepdir: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /flash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | avrdude -p $2 -c arduino -P /dev/tty$1 -b 115200 -F -U flash:w:images/MicroDigi.hex 3 | -------------------------------------------------------------------------------- /protocol/HDLC.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOCOL_HDLC_H 2 | #define PROTOCOL_HDLC_H 3 | 4 | #define HDLC_FLAG 0x7E 5 | #define HDLC_RESET 0x7F 6 | #define AX25_ESC 0x1B 7 | 8 | #endif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | obj 2 | *.project 3 | *.workspace 4 | *.o 5 | *.d 6 | resources 7 | images/*.bin 8 | images/*.s19 9 | images/*.eep 10 | images/*.lss 11 | images/*.map 12 | images/*.elf 13 | images/*.sym 14 | images/*.hex 15 | -------------------------------------------------------------------------------- /util/constants.h: -------------------------------------------------------------------------------- 1 | #define PROTOCOL_KISS 0x01 2 | #define PROTOCOL_SIMPLE_SERIAL 0x02 3 | 4 | #define m328p 0x01 5 | #define m1284p 0x02 6 | #define m644p 0x03 7 | 8 | #define REF_3V3 0x01 9 | #define REF_5V 0x02 10 | 11 | #define ROLE_FILLIN 0x01 12 | #define ROLE_WIDENN 0x02 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MicroDigi 2 | ========== 3 | 4 | MicroDigi is a standalone digipeater firmware for [MicroModem](http://unsigned.io/micromodem). It supports FILL-IN and WIDEn-N operation. The firmware is currently in an early beta state, and I do not recommend using this for live unattended setups yet. 5 | -------------------------------------------------------------------------------- /hardware/Serial.h: -------------------------------------------------------------------------------- 1 | #ifndef SERIAL_H 2 | #define SERIAL_H 3 | 4 | #include "device.h" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef struct Serial { 11 | FILE uart0; 12 | } Serial; 13 | 14 | void serial_init(Serial *serial); 15 | bool serial_available(uint8_t index); 16 | void uart0_putchar(char c); 17 | char uart0_getchar(void); 18 | char uart0_getchar_nowait(void); 19 | 20 | #endif -------------------------------------------------------------------------------- /util/CRC-CCIT.h: -------------------------------------------------------------------------------- 1 | // CRC-CCIT Implementation based on work by Francesco Sacchi 2 | 3 | #ifndef CRC_CCIT_H 4 | #define CRC_CCIT_H 5 | 6 | #include 7 | #include 8 | 9 | #define CRC_CCIT_INIT_VAL ((uint16_t)0xFFFF) 10 | 11 | extern const uint16_t crc_ccit_table[256]; 12 | 13 | inline uint16_t update_crc_ccit(uint8_t c, uint16_t prev_crc) { 14 | return (prev_crc >> 8) ^ pgm_read_word(&crc_ccit_table[(prev_crc ^ c) & 0xff]); 15 | } 16 | 17 | 18 | #endif -------------------------------------------------------------------------------- /device.h: -------------------------------------------------------------------------------- 1 | #include "util/constants.h" 2 | 3 | #ifndef DEVICE_CONFIGURATION 4 | #define DEVICE_CONFIGURATION 5 | 6 | // CPU settings 7 | #define TARGET_CPU m328p 8 | #define F_CPU 16000000 9 | #define FREQUENCY_CORRECTION 0 10 | 11 | // ADC settings 12 | #define OPEN_SQUELCH false 13 | #define ADC_REFERENCE REF_3V3 14 | // OR 15 | //#define ADC_REFERENCE REF_5V 16 | 17 | // Sampling & timer setup 18 | #define CONFIG_AFSK_DAC_SAMPLERATE 9600 19 | 20 | // AX25 settings 21 | #define CUSTOM_FRAME_SIZE 330 22 | 23 | // Serial settings 24 | #define BAUD 9600 25 | #define SERIAL_DEBUG true 26 | #define TX_MAXWAIT 2UL 27 | 28 | // Port settings 29 | #if TARGET_CPU == m328p 30 | #define DAC_PORT PORTD 31 | #define DAC_DDR DDRD 32 | #define LED_PORT PORTB 33 | #define LED_DDR DDRB 34 | #define ADC_PORT PORTC 35 | #define ADC_DDR DDRC 36 | #endif 37 | 38 | #endif -------------------------------------------------------------------------------- /protocol/Digipeater.h: -------------------------------------------------------------------------------- 1 | #ifndef _PROTOCOL_DIGIPEATER 2 | #define _PROTOCOL_DIGIPEATER 0x03 3 | 4 | #include "../hardware/AFSK.h" 5 | #include "../hardware/Serial.h" 6 | #include "../util/time.h" 7 | #include "config.h" 8 | #include "AX25.h" 9 | 10 | typedef struct AX25Call { 11 | char call[6]; 12 | uint8_t ssid; 13 | } AX25Call; 14 | 15 | #define AX25_CALL(str, id) {.call = (str), .ssid = (id) } 16 | #define AX25_MAX_RPT 8 17 | 18 | uint8_t rpt_hbits; 19 | uint8_t rpt_hbits_out; 20 | AX25Call rpt_list[AX25_MAX_RPT]; 21 | AX25Call rpt_list_out[AX25_MAX_RPT]; 22 | AX25Call src; 23 | AX25Call dst; 24 | 25 | void digipeater_init(AX25Ctx *ax25, Afsk *afsk, Serial *ser); 26 | void digipeater_csma(AX25Ctx *ctx, uint8_t *buf, size_t len); 27 | void digipeater_messageCallback(AX25Ctx *ctx); 28 | bool is_duplicate(uint8_t crcl, uint8_t crch); 29 | void digipeater_processPackets(void); 30 | 31 | #endif -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | // Configure the digipeaters callsign and SSID 5 | #define DIGIPEATER_CALLSIGN "NOCALL" 6 | #define DIGIPEATER_SSID 0 7 | 8 | // Configure digipeater type. Available values 9 | // are ROLE_WIDENN and ROLE_FILLIN 10 | #define DIGIPEATER_ROLE ROLE_WIDENN 11 | 12 | // Define the max hop count that the digipeater 13 | // will relay. This looks at both n and N. 14 | #define DIGIPEATER_CLAMP_N 2 15 | 16 | // Whether to digipeat packets that specify a 17 | // path directly through the digipeaters call- 18 | // sign and SSID 19 | #define SPECIFIC_DIGIPEAT true 20 | 21 | // Set CSMA slot time 22 | #define DIGIPEATER_CSMA_SLOT_TIME 200 23 | 24 | // Set CSMA persistence value 25 | #define DIGIPEATER_CSMA_PERSISTENCE 127 26 | 27 | // How many packets to keep in the duplicate 28 | // checklist 29 | #define DUPL_LIST_SIZE 32 30 | 31 | // How long a packet is kept in the duplicate 32 | // checklist 33 | #define DUPL_STALE_TIME 30 34 | 35 | #endif -------------------------------------------------------------------------------- /protocol/AX25.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOCOL_AX25_H 2 | #define PROTOCOL_AX25_H 3 | 4 | #include 5 | #include 6 | #include "device.h" 7 | 8 | #define AX25_MIN_FRAME_LEN 18 9 | #ifndef CUSTOM_FRAME_SIZE 10 | #define AX25_MAX_FRAME_LEN 330 11 | #else 12 | #define AX25_MAX_FRAME_LEN CUSTOM_FRAME_SIZE 13 | #endif 14 | 15 | #define AX25_CRC_CORRECT 0xF0B8 16 | 17 | #define AX25_CTRL_UI 0x03 18 | #define AX25_PID_NOLAYER3 0xF0 19 | 20 | struct AX25Ctx; // Forward declarations 21 | struct AX25Msg; 22 | 23 | typedef void (*ax25_callback_t)(struct AX25Ctx *ctx); 24 | 25 | 26 | typedef struct AX25Ctx { 27 | uint8_t buf[AX25_MAX_FRAME_LEN]; 28 | FILE *ch; 29 | size_t frame_len; 30 | uint16_t crc_in; 31 | uint16_t crc_out; 32 | ax25_callback_t hook; 33 | bool sync; 34 | bool escape; 35 | } AX25Ctx; 36 | 37 | void ax25_poll(AX25Ctx *ctx); 38 | void ax25_sendRaw(AX25Ctx *ctx, void *_buf, size_t len); 39 | void ax25_init(AX25Ctx *ctx, FILE *channel, ax25_callback_t hook); 40 | 41 | #endif -------------------------------------------------------------------------------- /util/time.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_TIME_H 2 | #define UTIL_TIME_H 3 | 4 | #include 5 | #include "device.h" 6 | 7 | #define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor)) 8 | #define CLOCK_TICKS_PER_SEC CONFIG_AFSK_DAC_SAMPLERATE 9 | 10 | typedef int32_t ticks_t; 11 | typedef int32_t mtime_t; 12 | 13 | volatile ticks_t _clock; 14 | 15 | inline ticks_t timer_clock(void) { 16 | ticks_t result; 17 | 18 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 19 | result = _clock; 20 | } 21 | 22 | return result; 23 | } 24 | 25 | 26 | inline ticks_t ms_to_ticks(mtime_t ms) { 27 | return ms * DIV_ROUND(CLOCK_TICKS_PER_SEC, 1000); 28 | } 29 | 30 | inline ticks_t s_to_ticks(mtime_t s) { 31 | return s * CLOCK_TICKS_PER_SEC; 32 | } 33 | 34 | inline void cpu_relax(void) { 35 | // Do nothing! 36 | } 37 | 38 | inline void delay_ms(unsigned long ms) { 39 | ticks_t start = timer_clock(); 40 | unsigned long n_ticks = ms_to_ticks(ms); 41 | while (timer_clock() - start < n_ticks) { 42 | cpu_relax(); 43 | } 44 | } 45 | 46 | 47 | #endif -------------------------------------------------------------------------------- /hardware/Serial.c: -------------------------------------------------------------------------------- 1 | #include "Serial.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void serial_init(Serial *serial) { 7 | memset(serial, 0, sizeof(*serial)); 8 | UBRR0H = UBRRH_VALUE; 9 | UBRR0L = UBRRL_VALUE; 10 | 11 | #if USE_2X 12 | UCSR0A |= _BV(U2X0); 13 | #else 14 | UCSR0A &= ~(_BV(U2X0)); 15 | #endif 16 | 17 | // Set to 8-bit data, enable RX and TX 18 | UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); 19 | UCSR0B = _BV(RXEN0) | _BV(TXEN0); 20 | 21 | FILE uart0_fd = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW); 22 | 23 | serial->uart0 = uart0_fd; 24 | } 25 | 26 | bool serial_available(uint8_t index) { 27 | if (index == 0) { 28 | if (UCSR0A & _BV(RXC0)) return true; 29 | } 30 | return false; 31 | } 32 | 33 | 34 | void uart0_putchar(char c) { 35 | loop_until_bit_is_set(UCSR0A, UDRE0); 36 | UDR0 = c; 37 | } 38 | 39 | char uart0_getchar(void) { 40 | loop_until_bit_is_set(UCSR0A, RXC0); 41 | return UDR0; 42 | } 43 | 44 | char uart0_getchar_nowait(void) { 45 | if (!(UCSR0A & _BV(RXC0))) return EOF; 46 | return UDR0; 47 | } -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "device.h" 6 | #include "util/FIFO.h" 7 | #include "util/time.h" 8 | #include "hardware/AFSK.h" 9 | #include "hardware/Serial.h" 10 | #include "protocol/AX25.h" 11 | #include "protocol/Digipeater.h" 12 | 13 | Serial serial; 14 | Afsk modem; 15 | AX25Ctx AX25; 16 | 17 | unsigned long custom_preamble = CONFIG_AFSK_PREAMBLE_LEN; 18 | unsigned long custom_tail = CONFIG_AFSK_TRAILER_LEN; 19 | 20 | static void ax25_callback(struct AX25Ctx *ctx) { 21 | digipeater_messageCallback(ctx); 22 | } 23 | 24 | void init(void) { 25 | sei(); 26 | 27 | AFSK_init(&modem); 28 | ax25_init(&AX25, &modem.fd, ax25_callback); 29 | digipeater_init(&AX25, &modem, &serial); 30 | 31 | serial_init(&serial); 32 | stdout = &serial.uart0; 33 | stdin = &serial.uart0; 34 | 35 | } 36 | 37 | int main (void) { 38 | init(); 39 | 40 | while (true) { 41 | ax25_poll(&AX25); 42 | digipeater_processPackets(); 43 | 44 | if (serial_available(0)) { 45 | //char sbyte = uart0_getchar_nowait(); 46 | // Do something with the data :) 47 | } 48 | } 49 | 50 | return(0); 51 | } -------------------------------------------------------------------------------- /util/FIFO.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_FIFO_H 2 | #define UTIL_FIFO_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct FIFOBuffer 8 | { 9 | unsigned char *begin; 10 | unsigned char *end; 11 | unsigned char * volatile head; 12 | unsigned char * volatile tail; 13 | } FIFOBuffer; 14 | 15 | inline bool fifo_isempty(const FIFOBuffer *f) { 16 | return f->head == f->tail; 17 | } 18 | 19 | inline bool fifo_isfull(const FIFOBuffer *f) { 20 | return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1); 21 | } 22 | 23 | inline void fifo_push(FIFOBuffer *f, unsigned char c) { 24 | *(f->tail) = c; 25 | 26 | if (f->tail == f->end) { 27 | f->tail = f->begin; 28 | } else { 29 | f->tail++; 30 | } 31 | } 32 | 33 | inline unsigned char fifo_pop(FIFOBuffer *f) { 34 | if(f->head == f->end) { 35 | f->head = f->begin; 36 | return *(f->end); 37 | } else { 38 | return *(f->head++); 39 | } 40 | } 41 | 42 | inline void fifo_flush(FIFOBuffer *f) { 43 | f->head = f->tail; 44 | } 45 | 46 | inline bool fifo_isempty_locked(const FIFOBuffer *f) { 47 | bool result; 48 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 49 | result = fifo_isempty(f); 50 | } 51 | return result; 52 | } 53 | 54 | inline bool fifo_isfull_locked(const FIFOBuffer *f) { 55 | bool result; 56 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 57 | result = fifo_isfull(f); 58 | } 59 | return result; 60 | } 61 | 62 | inline void fifo_push_locked(FIFOBuffer *f, unsigned char c) { 63 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 64 | fifo_push(f, c); 65 | } 66 | } 67 | 68 | inline unsigned char fifo_pop_locked(FIFOBuffer *f) { 69 | unsigned char c; 70 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 71 | c = fifo_pop(f); 72 | } 73 | return c; 74 | } 75 | 76 | inline void fifo_init(FIFOBuffer *f, unsigned char *buffer, size_t size) { 77 | f->head = f->tail = f->begin = buffer; 78 | f->end = buffer + size -1; 79 | } 80 | 81 | inline size_t fifo_len(FIFOBuffer *f) { 82 | return f->end - f->begin; 83 | } 84 | 85 | #endif -------------------------------------------------------------------------------- /util/CRC-CCIT.c: -------------------------------------------------------------------------------- 1 | #include "CRC-CCIT.h" 2 | 3 | const uint16_t crc_ccit_table[256] PROGMEM = { 4 | 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 5 | 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 6 | 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 7 | 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 8 | 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 9 | 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 10 | 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 11 | 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 12 | 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 13 | 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 14 | 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 15 | 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 16 | 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 17 | 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 18 | 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 19 | 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 20 | 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 21 | 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 22 | 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 23 | 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 24 | 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 25 | 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 26 | 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 27 | 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 28 | 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 29 | 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 30 | 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 31 | 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 32 | 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 33 | 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 34 | 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 35 | 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78, 36 | }; -------------------------------------------------------------------------------- /protocol/AX25.c: -------------------------------------------------------------------------------- 1 | // Based on work by Francesco Sacchi 2 | 3 | #include 4 | #include 5 | #include "AX25.h" 6 | #include "protocol/HDLC.h" 7 | #include "util/CRC-CCIT.h" 8 | #include "../hardware/AFSK.h" 9 | 10 | #define countof(a) sizeof(a)/sizeof(a[0]) 11 | #define MIN(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); ((typeof(_a))((_a < _b) ? _a : _b)); }) 12 | #define DECODE_CALL(buf, addr) for (unsigned i = 0; i < sizeof((addr)); i++) { char c = (*(buf)++ >> 1); (addr)[i] = (c == ' ') ? '\x0' : c; } 13 | #define AX25_SET_REPEATED(msg, idx, val) do { if (val) { (msg)->rpt_flags |= _BV(idx); } else { (msg)->rpt_flags &= ~_BV(idx) ; } } while(0) 14 | 15 | void ax25_init(AX25Ctx *ctx, FILE *channel, ax25_callback_t hook) { 16 | memset(ctx, 0, sizeof(*ctx)); 17 | ctx->ch = channel; 18 | ctx->hook = hook; 19 | ctx->crc_in = ctx->crc_out = CRC_CCIT_INIT_VAL; 20 | } 21 | 22 | static void ax25_decode(AX25Ctx *ctx) { 23 | if (ctx->hook) ctx->hook(ctx); 24 | } 25 | 26 | void ax25_poll(AX25Ctx *ctx) { 27 | int c; 28 | 29 | while ((c = fgetc(ctx->ch)) != EOF) { 30 | if (!ctx->escape && c == HDLC_FLAG) { 31 | if (ctx->frame_len >= AX25_MIN_FRAME_LEN) { 32 | if (ctx->crc_in == AX25_CRC_CORRECT) { 33 | #if OPEN_SQUELCH == true 34 | LED_RX_ON(); 35 | #endif 36 | ax25_decode(ctx); 37 | } 38 | } 39 | ctx->sync = true; 40 | ctx->crc_in = CRC_CCIT_INIT_VAL; 41 | ctx->frame_len = 0; 42 | continue; 43 | } 44 | 45 | if (!ctx->escape && c == HDLC_RESET) { 46 | ctx->sync = false; 47 | continue; 48 | } 49 | 50 | if (!ctx->escape && c == AX25_ESC) { 51 | ctx->escape = true; 52 | continue; 53 | } 54 | 55 | if (ctx->sync) { 56 | if (ctx->frame_len < AX25_MAX_FRAME_LEN) { 57 | ctx->buf[ctx->frame_len++] = c; 58 | ctx->crc_in = update_crc_ccit(c, ctx->crc_in); 59 | } else { 60 | ctx->sync = false; 61 | } 62 | } 63 | ctx->escape = false; 64 | } 65 | } 66 | 67 | static void ax25_putchar(AX25Ctx *ctx, uint8_t c) 68 | { 69 | if (c == HDLC_FLAG || c == HDLC_RESET || c == AX25_ESC) fputc(AX25_ESC, ctx->ch); 70 | ctx->crc_out = update_crc_ccit(c, ctx->crc_out); 71 | fputc(c, ctx->ch); 72 | } 73 | 74 | void ax25_sendRaw(AX25Ctx *ctx, void *_buf, size_t len) { 75 | ctx->crc_out = CRC_CCIT_INIT_VAL; 76 | fputc(HDLC_FLAG, ctx->ch); 77 | const uint8_t *buf = (const uint8_t *)_buf; 78 | while (len--) ax25_putchar(ctx, *buf++); 79 | 80 | uint8_t crcl = (ctx->crc_out & 0xff) ^ 0xff; 81 | uint8_t crch = (ctx->crc_out >> 8) ^ 0xff; 82 | ax25_putchar(ctx, crcl); 83 | ax25_putchar(ctx, crch); 84 | 85 | fputc(HDLC_FLAG, ctx->ch); 86 | } 87 | -------------------------------------------------------------------------------- /hardware/AFSK.h: -------------------------------------------------------------------------------- 1 | #ifndef AFSK_H 2 | #define AFSK_H 3 | 4 | #include "device.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "util/FIFO.h" 10 | #include "util/time.h" 11 | #include "protocol/HDLC.h" 12 | 13 | #define SIN_LEN 512 14 | static const uint8_t sin_table[] PROGMEM = 15 | { 16 | 128, 129, 131, 132, 134, 135, 137, 138, 140, 142, 143, 145, 146, 148, 149, 151, 17 | 152, 154, 155, 157, 158, 160, 162, 163, 165, 166, 167, 169, 170, 172, 173, 175, 18 | 176, 178, 179, 181, 182, 183, 185, 186, 188, 189, 190, 192, 193, 194, 196, 197, 19 | 198, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 213, 214, 215, 217, 20 | 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 21 | 234, 234, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243, 243, 244, 245, 22 | 245, 246, 246, 247, 248, 248, 249, 249, 250, 250, 250, 251, 251, 252, 252, 252, 23 | 253, 253, 253, 253, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 24 | }; 25 | 26 | inline static uint8_t sinSample(uint16_t i) { 27 | uint16_t newI = i % (SIN_LEN/2); 28 | newI = (newI >= (SIN_LEN/4)) ? (SIN_LEN/2 - newI -1) : newI; 29 | uint8_t sine = pgm_read_byte(&sin_table[newI]); 30 | return (i >= (SIN_LEN/2)) ? (255 - sine) : sine; 31 | } 32 | 33 | 34 | #define SWITCH_TONE(inc) (((inc) == MARK_INC) ? SPACE_INC : MARK_INC) 35 | #define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01) 36 | #define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03) 37 | #define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2) 38 | #define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1) 39 | 40 | #define CPU_FREQ F_CPU 41 | 42 | #define CONFIG_AFSK_RX_BUFLEN 64 43 | #define CONFIG_AFSK_TX_BUFLEN 64 44 | #define CONFIG_AFSK_RXTIMEOUT 0 45 | #define CONFIG_AFSK_PREAMBLE_LEN 150UL 46 | #define CONFIG_AFSK_TRAILER_LEN 50UL 47 | #define BIT_STUFF_LEN 5 48 | 49 | #define SAMPLERATE 9600 50 | #define BITRATE 1200 51 | 52 | #define SAMPLESPERBIT (SAMPLERATE / BITRATE) 53 | #define PHASE_INC 1 // Nudge by an eigth of a sample each adjustment 54 | 55 | #if BITRATE == 960 56 | #define FILTER_CUTOFF 600 57 | #define MARK_FREQ 960 58 | #define SPACE_FREQ 1600 59 | #define PHASE_BITS 10 // How much to increment phase counter each sample 60 | #elif BITRATE == 1200 61 | #define FILTER_CUTOFF 600 62 | #define MARK_FREQ 1200 63 | #define SPACE_FREQ 2200 64 | #define PHASE_BITS 8 65 | #elif BITRATE == 1600 66 | #define FILTER_CUTOFF 800 67 | #define MARK_FREQ 1600 68 | #define SPACE_FREQ 2600 69 | #define PHASE_BITS 8 70 | #elif BITRATE == 2400 71 | #define FILTER_CUTOFF 1600 72 | #define MARK_FREQ 2400 73 | #define SPACE_FREQ 4200 74 | #define PHASE_BITS 4 75 | #else 76 | #error Unsupported bitrate! 77 | #endif 78 | 79 | #define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // Resolution of our phase counter = 64 80 | #define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window 81 | 82 | typedef struct Hdlc 83 | { 84 | uint8_t demodulatedBits; 85 | uint8_t bitIndex; 86 | uint8_t currentByte; 87 | bool receiving; 88 | } Hdlc; 89 | 90 | typedef struct Afsk 91 | { 92 | // Stream access to modem 93 | FILE fd; 94 | 95 | // General values 96 | Hdlc hdlc; // We need a link control structure 97 | uint16_t preambleLength; // Length of sync preamble 98 | uint16_t tailLength; // Length of transmission tail 99 | 100 | // Modulation values 101 | uint8_t sampleIndex; // Current sample index for outgoing bit 102 | uint8_t currentOutputByte; // Current byte to be modulated 103 | uint8_t txBit; // Mask of current modulated bit 104 | bool bitStuff; // Whether bitstuffing is allowed 105 | 106 | uint8_t bitstuffCount; // Counter for bit-stuffing 107 | 108 | uint16_t phaseAcc; // Phase accumulator 109 | uint16_t phaseInc; // Phase increment per sample 110 | 111 | FIFOBuffer txFifo; // FIFO for transmit data 112 | uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actial data storage for said FIFO 113 | 114 | volatile bool sending; // Set when modem is sending 115 | 116 | // Demodulation values 117 | FIFOBuffer delayFifo; // Delayed FIFO for frequency discrimination 118 | int8_t delayBuf[SAMPLESPERBIT / 2 + 1]; // Actual data storage for said FIFO 119 | 120 | FIFOBuffer rxFifo; // FIFO for received data 121 | uint8_t rxBuf[CONFIG_AFSK_RX_BUFLEN]; // Actual data storage for said FIFO 122 | 123 | int16_t iirX[2]; // IIR Filter X cells 124 | int16_t iirY[2]; // IIR Filter Y cells 125 | 126 | uint8_t sampledBits; // Bits sampled by the demodulator (at ADC speed) 127 | int8_t currentPhase; // Current phase of the demodulator 128 | uint8_t actualBits; // Actual found bits at correct bitrate 129 | 130 | volatile int status; // Status of the modem, 0 means OK 131 | 132 | } Afsk; 133 | 134 | #define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor)) 135 | #define MARK_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_AFSK_DAC_SAMPLERATE)) 136 | #define SPACE_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_AFSK_DAC_SAMPLERATE)) 137 | 138 | #define AFSK_DAC_IRQ_START() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = true; } while (0) 139 | #define AFSK_DAC_IRQ_STOP() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = false; } while (0) 140 | #define AFSK_DAC_INIT() do { DAC_DDR |= 0xF8; } while (0) 141 | 142 | // Here's some macros for controlling the RX/TX LEDs 143 | // THE _INIT() functions writes to the DDRB register 144 | // to configure the pins as output pins, and the _ON() 145 | // and _OFF() functions writes to the PORT registers 146 | // to turn the pins on or off. 147 | #define LED_TX_INIT() do { LED_DDR |= _BV(1); } while (0) 148 | #define LED_TX_ON() do { LED_PORT |= _BV(1); } while (0) 149 | #define LED_TX_OFF() do { LED_PORT &= ~_BV(1); } while (0) 150 | 151 | #define LED_RX_INIT() do { LED_DDR |= _BV(2); } while (0) 152 | #define LED_RX_ON() do { LED_PORT |= _BV(2); } while (0) 153 | #define LED_RX_OFF() do { LED_PORT &= ~_BV(2); } while (0) 154 | 155 | void AFSK_init(Afsk *afsk); 156 | void AFSK_transmit(char *buffer, size_t size); 157 | void AFSK_poll(Afsk *afsk); 158 | 159 | #endif -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # AVR Sample makefile written by Eric B. Weddington, Jörg Wunsch, et al. 2 | # Modified (bringing often-changed options to the top) by Elliot Williams 3 | 4 | # make all = Make software and program 5 | # make clean = Clean out built project files. 6 | # make program = Download the hex file to the device, using avrdude. Please 7 | # customize the avrdude settings below first! 8 | 9 | # Microcontroller Type 10 | #MCU = atmega1284p 11 | #MCU = atmega644p 12 | MCU = atmega328p 13 | 14 | # Target file name (without extension). 15 | TARGET = images/MicroDigi 16 | 17 | # Programming hardware: type avrdude -c ? 18 | # to get a full listing. 19 | AVRDUDE_PROGRAMMER = arduino 20 | 21 | AVRDUDE_PORT = /dev/usb # not really needed for usb 22 | #AVRDUDE_PORT = /dev/parport0 # linux 23 | # AVRDUDE_PORT = lpt1 # windows 24 | 25 | ############# Don't need to change below here for most purposes (Elliot) 26 | 27 | # Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization. 28 | # (Note: 3 is not always the best optimization level. See avr-libc FAQ.) 29 | OPT = s 30 | 31 | # Output format. (can be srec, ihex, binary) 32 | FORMAT = ihex 33 | 34 | # List C source files here. (C dependencies are automatically generated.) 35 | #SRC = $(TARGET).c 36 | SRC = main.c hardware/Serial.c hardware/AFSK.c util/CRC-CCIT.c protocol/AX25.c protocol/Digipeater.c 37 | 38 | # If there is more than one source file, append them above, or modify and 39 | # uncomment the following: 40 | #SRC += foo.c bar.c 41 | 42 | # You can also wrap lines by appending a backslash to the end of the line: 43 | #SRC += baz.c \ 44 | #xyzzy.c 45 | 46 | 47 | 48 | # List Assembler source files here. 49 | # Make them always end in a capital .S. Files ending in a lowercase .s 50 | # will not be considered source files but generated files (assembler 51 | # output from the compiler), and will be deleted upon "make clean"! 52 | # Even though the DOS/Win* filesystem matches both .s and .S the same, 53 | # it will preserve the spelling of the filenames, and gcc itself does 54 | # care about how the name is spelled on its command-line. 55 | ASRC = 56 | 57 | 58 | # List any extra directories to look for include files here. 59 | # Each directory must be seperated by a space. 60 | EXTRAINCDIRS = 61 | 62 | 63 | # Optional compiler flags. 64 | # -g: generate debugging information (for GDB, or for COFF conversion) 65 | # -O*: optimization level 66 | # -f...: tuning, see gcc manual and avr-libc documentation 67 | # -Wall...: warning level 68 | # -Wa,...: tell GCC to pass this to the assembler. 69 | # -ahlms: create assembler listing 70 | CFLAGS = -g -O$(OPT) \ 71 | -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \ 72 | -Wall -Wstrict-prototypes \ 73 | -Wa,-adhlns=$(<:.c=.lst) \ 74 | $(patsubst %,-I%,$(EXTRAINCDIRS)) 75 | 76 | 77 | # Set a "language standard" compiler flag. 78 | # Unremark just one line below to set the language standard to use. 79 | # gnu99 = C99 + GNU extensions. See GCC manual for more information. 80 | #CFLAGS += -std=c89 81 | #CFLAGS += -std=gnu89 82 | #CFLAGS += -std=c99 83 | CFLAGS += -std=gnu99 84 | 85 | 86 | 87 | # Optional assembler flags. 88 | # -Wa,...: tell GCC to pass this to the assembler. 89 | # -ahlms: create listing 90 | # -gstabs: have the assembler create line number information; note that 91 | # for use in COFF files, additional information about filenames 92 | # and function names needs to be present in the assembler source 93 | # files -- see avr-libc docs [FIXME: not yet described there] 94 | ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 95 | 96 | 97 | 98 | # Optional linker flags. 99 | # -Wl,...: tell GCC to pass this to linker. 100 | # -Map: create map file 101 | # --cref: add cross reference to map file 102 | LDFLAGS = -Wl,-Map=$(TARGET).map,--cref 103 | 104 | 105 | 106 | # Additional libraries 107 | 108 | # Minimalistic printf version 109 | #LDFLAGS += -Wl,-u,vfprintf -lprintf_min 110 | 111 | # Floating point printf version (requires -lm below) 112 | #LDFLAGS += -Wl,-u,vfprintf -lprintf_flt 113 | 114 | # -lm = math library 115 | LDFLAGS += -lm 116 | 117 | 118 | # Programming support using avrdude. Settings and variables. 119 | 120 | 121 | AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex 122 | #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep 123 | 124 | AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) 125 | 126 | # Uncomment the following if you want avrdude's erase cycle counter. 127 | # Note that this counter needs to be initialized first using -Yn, 128 | # see avrdude manual. 129 | #AVRDUDE_ERASE += -y 130 | 131 | # Uncomment the following if you do /not/ wish a verification to be 132 | # performed after programming the device. 133 | #AVRDUDE_FLAGS += -V 134 | 135 | # Increase verbosity level. Please use this when submitting bug 136 | # reports about avrdude. See 137 | # to submit bug reports. 138 | #AVRDUDE_FLAGS += -v -v 139 | 140 | #Run while cable attached or don't 141 | AVRDUDE_FLAGS += -E reset #keep chip disabled while cable attached 142 | #AVRDUDE_FLAGS += -E noreset 143 | 144 | #AVRDUDE_WRITE_FLASH = -U lfuse:w:0x04:m #run with 8 Mhz clock 145 | 146 | #AVRDUDE_WRITE_FLASH = -U lfuse:w:0x21:m #run with 1 Mhz clock #default clock mode 147 | 148 | #AVRDUDE_WRITE_FLASH = -U lfuse:w:0x01:m #run with 1 Mhz clock no start up time 149 | 150 | # --------------------------------------------------------------------------- 151 | 152 | # Define programs and commands. 153 | SHELL = sh 154 | 155 | CC = avr-gcc 156 | 157 | OBJCOPY = avr-objcopy 158 | OBJDUMP = avr-objdump 159 | SIZE = avr-size 160 | 161 | 162 | # Programming support using avrdude. 163 | AVRDUDE = avrdude 164 | 165 | 166 | REMOVE = rm -f 167 | COPY = cp 168 | 169 | HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex 170 | ELFSIZE = $(SIZE) -C $(TARGET).elf 171 | 172 | 173 | 174 | # Define Messages 175 | # English 176 | MSG_ERRORS_NONE = Firmware compiled successfully! 177 | MSG_BEGIN = Starting build... 178 | MSG_END = -------- Done -------- 179 | MSG_SIZE_BEFORE = Size before: 180 | MSG_SIZE_AFTER = Size after: 181 | MSG_COFF = Converting to AVR COFF: 182 | MSG_EXTENDED_COFF = Converting to AVR Extended COFF: 183 | MSG_FLASH = Creating load file for Flash: 184 | MSG_EEPROM = Creating load file for EEPROM: 185 | MSG_EXTENDED_LISTING = Creating Extended Listing: 186 | MSG_SYMBOL_TABLE = Creating Symbol Table: 187 | MSG_LINKING = Linking: 188 | MSG_COMPILING = Compiling: 189 | MSG_ASSEMBLING = Assembling: 190 | MSG_CLEANING = Cleaning project: 191 | 192 | 193 | 194 | 195 | # Define all object files. 196 | OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 197 | 198 | # Define all listing files. 199 | LST = $(ASRC:.S=.lst) $(SRC:.c=.lst) 200 | 201 | # Combine all necessary flags and optional flags. 202 | # Add target processor to flags. 203 | ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) 204 | ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) 205 | 206 | 207 | 208 | # Default target: make program! 209 | #all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \ 210 | # $(TARGET).lss $(TARGET).sym sizeafter finished end 211 | 212 | all: begin $(TARGET).elf $(TARGET).hex $(TARGET).eep \ 213 | $(TARGET).lss $(TARGET).sym cleanup sizeafter finished 214 | # $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) 215 | 216 | # Eye candy. 217 | # AVR Studio 3.x does not check make's exit code but relies on 218 | # the following magic strings to be generated by the compile job. 219 | begin: 220 | @echo 221 | @echo $(MSG_BEGIN) 222 | 223 | finished: 224 | @echo $(MSG_ERRORS_NONE) 225 | 226 | end: 227 | @echo $(MSG_END) 228 | @echo 229 | 230 | 231 | # Display size of file. 232 | sizebefore: 233 | @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi 234 | 235 | sizeafter: 236 | @if [ -f $(TARGET).elf ]; then echo; $(ELFSIZE); echo; fi 237 | 238 | 239 | 240 | # Display compiler version information. 241 | gccversion : 242 | @$(CC) --version 243 | 244 | 245 | 246 | 247 | # Convert ELF to COFF for use in debugging / simulating in 248 | # AVR Studio or VMLAB. 249 | COFFCONVERT=$(OBJCOPY) --debugging \ 250 | --change-section-address .data-0x800000 \ 251 | --change-section-address .bss-0x800000 \ 252 | --change-section-address .noinit-0x800000 \ 253 | --change-section-address .eeprom-0x810000 254 | 255 | 256 | coff: $(TARGET).elf 257 | # @echo 258 | # @echo $(MSG_COFF) $(TARGET).cof 259 | @$(COFFCONVERT) -O coff-avr $< $(TARGET).cof 260 | 261 | 262 | extcoff: $(TARGET).elf 263 | # @echo 264 | # @echo $(MSG_EXTENDED_COFF) $(TARGET).cof 265 | @$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof 266 | 267 | 268 | 269 | 270 | # Program the device. 271 | program: $(TARGET).hex $(TARGET).eep 272 | @$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) 273 | 274 | 275 | 276 | 277 | # Create final output files (.hex, .eep) from ELF output file. 278 | %.hex: %.elf 279 | # @echo 280 | # @echo $(MSG_FLASH) $@ 281 | @$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ 282 | 283 | %.eep: %.elf 284 | # @echo 285 | # @echo $(MSG_EEPROM) $@ 286 | # @echo Not generating any EEPROM images 287 | @-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ 288 | 289 | # Create extended listing file from ELF output file. 290 | %.lss: %.elf 291 | # @echo 292 | # @echo $(MSG_EXTENDED_LISTING) $@ 293 | @$(OBJDUMP) -h -S $< > $@ 294 | 295 | # Create a symbol table from ELF output file. 296 | %.sym: %.elf 297 | # @echo 298 | # @echo $(MSG_SYMBOL_TABLE) $@ 299 | @avr-nm -n $< > $@ 300 | 301 | 302 | 303 | # Link: create ELF output file from object files. 304 | .SECONDARY : $(TARGET).elf 305 | .PRECIOUS : $(OBJ) 306 | %.elf: $(OBJ) 307 | @echo $(MSG_LINKING) $@ 308 | @$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) 309 | 310 | 311 | # Compile: create object files from C source files. 312 | %.o : %.c 313 | @echo $(MSG_COMPILING) $< 314 | @$(CC) -c $(ALL_CFLAGS) $< -o $@ 315 | 316 | 317 | # Compile: create assembler files from C source files. 318 | %.s : %.c 319 | @$(CC) -S $(ALL_CFLAGS) $< -o $@ 320 | 321 | 322 | # Assemble: create object files from assembler source files. 323 | %.o : %.S 324 | @echo 325 | @echo $(MSG_ASSEMBLING) $< 326 | @$(CC) -c $(ALL_ASFLAGS) $< -o $@ 327 | 328 | 329 | 330 | # Target: clean project. 331 | clean: clean_list finished 332 | 333 | clean_list : 334 | @echo 335 | @echo $(MSG_CLEANING) 336 | $(REMOVE) $(TARGET).hex 337 | $(REMOVE) $(TARGET).eep 338 | $(REMOVE) $(TARGET).obj 339 | $(REMOVE) $(TARGET).cof 340 | $(REMOVE) $(TARGET).elf 341 | $(REMOVE) $(TARGET).map 342 | $(REMOVE) $(TARGET).obj 343 | $(REMOVE) $(TARGET).a90 344 | $(REMOVE) $(TARGET).sym 345 | $(REMOVE) $(TARGET).lnk 346 | $(REMOVE) $(TARGET).lss 347 | $(REMOVE) $(OBJ) 348 | $(REMOVE) $(LST) 349 | $(REMOVE) $(SRC:.c=.s) 350 | $(REMOVE) $(SRC:.c=.d) 351 | $(REMOVE) *~ 352 | 353 | cleanup: 354 | @$(REMOVE) $(SRC:.c=.s) 355 | @$(REMOVE) $(SRC:.c=.d) 356 | @$(REMOVE) $(LST) 357 | 358 | # Automatically generate C source code dependencies. 359 | # (Code originally taken from the GNU make user manual and modified 360 | # (See README.txt Credits).) 361 | # 362 | # Note that this will work with sh (bash) and sed that is shipped with WinAVR 363 | # (see the SHELL variable defined above). 364 | # This may not work with other shells or other seds. 365 | # 366 | %.d: %.c 367 | @set -e; $(CC) -MM $(ALL_CFLAGS) $< \ 368 | | sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \ 369 | [ -s $@ ] || rm -f $@ 370 | 371 | 372 | # Remove the '-' if you want to see the dependency files generated. 373 | -include $(SRC:.c=.d) 374 | 375 | 376 | 377 | # Listing of phony targets. 378 | .PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \ 379 | clean clean_list program 380 | -------------------------------------------------------------------------------- /protocol/Digipeater.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "device.h" 6 | #include "Digipeater.h" 7 | #include "util/CRC-CCIT.h" 8 | 9 | #define MIN(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); ((typeof(_a))((_a < _b) ? _a : _b)); }) 10 | #define countof(a) sizeof(a)/sizeof(a[0]) 11 | #define DECODE_CALL(buf, addr) for (unsigned i = 0; i < sizeof((addr)); i++) { char c = (*(buf)++ >> 1); (addr)[i] = (c == ' ') ? '\x0' : c; } 12 | 13 | uint8_t packetBuffer[AX25_MAX_FRAME_LEN]; // Buffer for holding incoming serial data 14 | uint8_t packetBufferOut[AX25_MAX_FRAME_LEN]; 15 | AX25Ctx *ax25ctx; 16 | Afsk *channel; 17 | Serial *serial; 18 | size_t frame_len; 19 | size_t frame_len_out; 20 | bool csma_waiting = false; 21 | 22 | unsigned long slotTime = DIGIPEATER_CSMA_SLOT_TIME; 23 | uint8_t p = DIGIPEATER_CSMA_PERSISTENCE; 24 | bool specificDigipeat = SPECIFIC_DIGIPEAT; 25 | 26 | typedef struct dupl_entry { 27 | bool active; 28 | uint8_t crcl; 29 | uint8_t crch; 30 | ticks_t timestamp; 31 | } dupl_entry; 32 | 33 | dupl_entry dupl_list[DUPL_LIST_SIZE]; 34 | uint8_t dupl_i = 0; 35 | 36 | #if DIGIPEATER_ROLE == ROLE_FILLIN 37 | static int clamp_n = 1; 38 | #else 39 | static int clamp_n = DIGIPEATER_CLAMP_N; 40 | #endif 41 | 42 | void digipeater_processPackets(void) { 43 | // If we're waiting in CSMA, drop this packet 44 | if (csma_waiting) frame_len = 0; 45 | 46 | if (frame_len != 0) { 47 | // We have a packet for digipeating, 48 | // let's process it 49 | uint8_t *buf = packetBuffer; 50 | uint8_t *bufStart = packetBuffer; 51 | 52 | bool repeat = false; 53 | bool unrelated = false; 54 | 55 | DECODE_CALL(buf, dst.call); 56 | dst.ssid = (*buf++ >> 1) & 0x0F; 57 | 58 | DECODE_CALL(buf, src.call); 59 | src.ssid = (*buf >> 1) & 0x0F; 60 | 61 | uint8_t rpt_count; 62 | uint8_t rpt_count_out = 0; 63 | rpt_hbits = 0x00; 64 | for (rpt_count = 0; !(*buf++ & 0x01) && (rpt_count < countof(rpt_list)); rpt_count++) { 65 | DECODE_CALL(buf, rpt_list[rpt_count].call); 66 | rpt_list[rpt_count].ssid = (*buf >> 1) & 0x0F; 67 | 68 | bool hbit = (*buf >> 7); 69 | rpt_hbits |= (hbit << rpt_count); 70 | 71 | if (!hbit && !repeat && !unrelated) { 72 | // If the H-bit is not set, this path 73 | // component is active, and we should 74 | // check whether to digipeat 75 | AX25Call *path_call = &rpt_list[rpt_count]; 76 | 77 | if (specificDigipeat && memcmp(DIGIPEATER_CALLSIGN, path_call->call, MIN(sizeof(path_call->call), strlen(path_call->call))) == 0 && rpt_list[rpt_count].ssid == DIGIPEATER_SSID) { 78 | // This packet is relayed through 79 | // us specifically 80 | memcpy(rpt_list_out[rpt_count_out].call, DIGIPEATER_CALLSIGN, 6); 81 | rpt_list_out[rpt_count_out].ssid = DIGIPEATER_SSID; 82 | rpt_hbits_out |= (0x01 << rpt_count_out); 83 | rpt_count_out++; 84 | repeat = true; 85 | frame_len_out = 0; 86 | } else { 87 | if (rpt_list[rpt_count].ssid > 0) { 88 | if (memcmp("WIDE", path_call->call, 4) == 0) { 89 | char *p = path_call->call + 4; 90 | int n = atoi(p); 91 | int N = rpt_list[rpt_count].ssid; 92 | bool dupl_match = false; 93 | 94 | if (n <= clamp_n && N <= clamp_n && !dupl_match) { 95 | repeat = true; 96 | frame_len_out = 0; 97 | uint8_t rssid = rpt_list[rpt_count].ssid - 1; 98 | if (rssid == 0) { 99 | // If n has reached 0, replace the 100 | // WIDE with our own call, and set 101 | // the H-bit 102 | //memset(rpt_list[rpt_count].call, 0, 6); 103 | memcpy(rpt_list_out[rpt_count_out].call, DIGIPEATER_CALLSIGN, 6); 104 | rpt_list_out[rpt_count_out].ssid = DIGIPEATER_SSID; 105 | rpt_hbits_out |= (0x01 << rpt_count_out); 106 | rpt_count_out++; 107 | } else { 108 | // If not, insert our own callsign, 109 | // set the H-bit and then add the 110 | // Rest of the WIDE, decrementing 111 | // the n part 112 | memcpy(rpt_list_out[rpt_count_out].call, DIGIPEATER_CALLSIGN, 6); 113 | rpt_list_out[rpt_count_out].ssid = DIGIPEATER_SSID; 114 | rpt_hbits_out |= (0x01 << rpt_count_out); 115 | rpt_count_out++; 116 | 117 | memcpy(rpt_list_out[rpt_count_out].call, rpt_list[rpt_count].call, 6); 118 | rpt_list_out[rpt_count_out].ssid = rssid; 119 | rpt_hbits_out &= 0xFF ^ (0x01 << rpt_count_out); 120 | rpt_count_out++; 121 | } 122 | } else { 123 | unrelated = true; 124 | } 125 | } else { 126 | unrelated = true; 127 | } 128 | } else { 129 | unrelated = true; 130 | } 131 | } 132 | } else { 133 | memcpy(rpt_list_out[rpt_count_out].call, rpt_list[rpt_count].call, 6); 134 | rpt_list_out[rpt_count_out].ssid = rpt_list[rpt_count].ssid; 135 | if (hbit) rpt_hbits_out |= (0x01 << rpt_count_out); 136 | rpt_count_out++; 137 | } 138 | 139 | } 140 | 141 | #if SERIAL_DEBUG 142 | printf_P(PSTR("SRC[%.6s-%d] "), src.call, src.ssid); 143 | printf_P(PSTR("DST[%.6s-%d] "), dst.call, dst.ssid); 144 | printf("\nRXd Path (%d): ", rpt_count); 145 | for (uint8_t i = 0; i < rpt_count; i++) { 146 | if ((rpt_hbits >> i) & 0x01) { 147 | // This path component has beeen 148 | // repeated (used). 149 | printf_P(PSTR("[%.6s-%d*] "), rpt_list[i].call, rpt_list[i].ssid); 150 | } else { 151 | // Not yet repeated 152 | printf_P(PSTR("[%.6s-%d] "), rpt_list[i].call, rpt_list[i].ssid); 153 | } 154 | } 155 | 156 | if (repeat && !unrelated) { 157 | printf("\nTXd Path (%d): ", rpt_count_out); 158 | for (uint8_t i = 0; i < rpt_count_out; i++) { 159 | if ((rpt_hbits_out >> i) & 0x01) { 160 | // This path component has beeen 161 | // repeated (used). 162 | printf_P(PSTR("[%.6s-%d*] "), rpt_list_out[i].call, rpt_list_out[i].ssid); 163 | } else { 164 | // Not yet repeated 165 | printf_P(PSTR("[%.6s-%d] "), rpt_list_out[i].call, rpt_list_out[i].ssid); 166 | } 167 | } 168 | } else { 169 | printf("\nNot digipeating"); 170 | } 171 | printf("\n"); 172 | #endif 173 | 174 | if (repeat && !unrelated) { 175 | // Calculate payload length 176 | int payloadLength = frame_len - (buf - bufStart); 177 | //printf_P(PSTR("Payload length: %d"), payloadLength); 178 | 179 | // Init outgoing buffer to all zeroes 180 | memset(packetBufferOut, 0, AX25_MAX_FRAME_LEN); 181 | 182 | // We need to calculate a CRC checksum for 183 | // the src, dst and information fields only, 184 | // used to check for duplicates. 185 | ax25ctx->crc_out = CRC_CCIT_INIT_VAL; 186 | 187 | char c; 188 | 189 | // Add destination address 190 | for (unsigned i = 0; i < sizeof(dst.call); i++) { 191 | c = dst.call[i]; 192 | if (c == '\x0') c = ' '; 193 | c = c << 1; 194 | packetBufferOut[frame_len_out++] = c; 195 | // Update CRC 196 | ax25ctx->crc_out = update_crc_ccit(c, ax25ctx->crc_out); 197 | } 198 | packetBufferOut[frame_len_out++] = 0x60 | (dst.ssid << 1); 199 | 200 | // Add source address 201 | for (unsigned i = 0; i < sizeof(src.call); i++) { 202 | c = src.call[i]; 203 | if (c == '\x0') c = ' '; 204 | c = c << 1; 205 | packetBufferOut[frame_len_out++] = c; 206 | // Update CRC 207 | ax25ctx->crc_out = update_crc_ccit(c, ax25ctx->crc_out); 208 | } 209 | packetBufferOut[frame_len_out++] = 0x60 | (src.ssid << 1); 210 | 211 | // Add path 212 | for (int i = 0; i < rpt_count_out; i++) { 213 | AX25Call p = rpt_list_out[i]; 214 | bool set_hbit = false; 215 | if ((rpt_hbits_out >> i) & 0x01) set_hbit = true; 216 | for (unsigned i = 0; i < sizeof(p.call); i++) { 217 | c = p.call[i]; 218 | if (c == '\x0') c = ' '; 219 | c = c << 1; 220 | packetBufferOut[frame_len_out++] = c; 221 | } 222 | packetBufferOut[frame_len_out++] = 0x60 | (p.ssid << 1) | (set_hbit ? 0x80 : 0x00) | (i == rpt_count_out-1 ? 0x01 : 0); 223 | } 224 | 225 | packetBufferOut[frame_len_out++] = AX25_CTRL_UI; 226 | packetBufferOut[frame_len_out++] = AX25_PID_NOLAYER3; 227 | 228 | // Add payload 229 | for (int i = 0; i < payloadLength-2; i++) { 230 | if (i > 1) { 231 | packetBufferOut[frame_len_out++] = buf[i]; 232 | // Update CRC 233 | ax25ctx->crc_out = update_crc_ccit(buf[i], ax25ctx->crc_out); 234 | } 235 | } 236 | uint8_t crcl = (ax25ctx->crc_out & 0xff) ^ 0xff; 237 | uint8_t crch = (ax25ctx->crc_out >> 8) ^ 0xff; 238 | 239 | if (!is_duplicate(crcl, crch)) { 240 | // Send it out! 241 | digipeater_csma(ax25ctx, packetBufferOut, frame_len_out); 242 | 243 | // Add packet to duplicate checklist 244 | dupl_list[dupl_i].crcl = crcl; 245 | dupl_list[dupl_i].crch = crch; 246 | dupl_list[dupl_i].timestamp = timer_clock(); 247 | dupl_list[dupl_i].active = true; 248 | dupl_i = (dupl_i + 1) % DUPL_LIST_SIZE; 249 | } else { 250 | #if SERIAL_DEBUG 251 | printf("Duplicate detected, dropping packet\n"); 252 | #endif 253 | } 254 | } 255 | 256 | #if SERIAL_DEBUG 257 | printf("\n"); 258 | #endif 259 | 260 | // Reset frame_len to 0 261 | frame_len = 0; 262 | rpt_hbits_out = 0x00; 263 | } 264 | } 265 | 266 | bool is_duplicate(uint8_t crcl, uint8_t crch) { 267 | ticks_t now = timer_clock(); 268 | for (int i = 0; i < DUPL_LIST_SIZE; i++) { 269 | if (dupl_list[i].active && now - dupl_list[i].timestamp < s_to_ticks(DUPL_STALE_TIME)) { 270 | if (dupl_list[i].crcl == crcl && dupl_list[i].crch == crch) { 271 | return true; 272 | } 273 | } else { 274 | dupl_list[i].active = false; 275 | } 276 | } 277 | return false; 278 | } 279 | 280 | void digipeater_init(AX25Ctx *ax25, Afsk *afsk, Serial *ser) { 281 | ax25ctx = ax25; 282 | serial = ser; 283 | channel = afsk; 284 | frame_len = 0; 285 | } 286 | 287 | void digipeater_csma(AX25Ctx *ctx, uint8_t *buf, size_t len) { 288 | bool sent = false; 289 | csma_waiting = true; 290 | while (!sent) { 291 | if(!channel->hdlc.receiving) { 292 | uint8_t tp = rand() & 0xFF; 293 | if (tp < p) { 294 | ax25_sendRaw(ctx, buf, len); 295 | sent = true; 296 | csma_waiting = false; 297 | } else { 298 | ticks_t start = timer_clock(); 299 | long slot_ticks = ms_to_ticks(slotTime); 300 | while (timer_clock() - start < slot_ticks) { 301 | cpu_relax(); 302 | } 303 | } 304 | } else { 305 | while (!sent && channel->hdlc.receiving) { 306 | // Continously poll the modem for data 307 | // while waiting, so we don't overrun 308 | // receive buffers 309 | ax25_poll(ax25ctx); 310 | 311 | if (channel->status != 0) { 312 | // If an overflow or other error 313 | // occurs, we'll back off and drop 314 | // this packet silently. 315 | channel->status = 0; 316 | sent = true; 317 | csma_waiting = false; 318 | } 319 | } 320 | } 321 | 322 | } 323 | } 324 | 325 | void digipeater_messageCallback(AX25Ctx *ctx) { 326 | if (frame_len == 0) { 327 | cli(); 328 | 329 | for (size_t i = 0; i < ctx->frame_len; i++) { 330 | packetBuffer[i] = ctx->buf[i]; 331 | } 332 | 333 | frame_len = ctx->frame_len; 334 | sei(); 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /hardware/AFSK.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "AFSK.h" 3 | #include "util/time.h" 4 | 5 | extern volatile ticks_t _clock; 6 | extern unsigned long custom_preamble; 7 | extern unsigned long custom_tail; 8 | 9 | bool hw_afsk_dac_isr = false; 10 | bool hw_5v_ref = false; 11 | Afsk *AFSK_modem; 12 | 13 | // Forward declerations 14 | int afsk_getchar(void); 15 | void afsk_putchar(char c); 16 | 17 | void AFSK_hw_refDetect(void) { 18 | // This is manual for now 19 | #if ADC_REFERENCE == REF_5V 20 | hw_5v_ref = true; 21 | #else 22 | hw_5v_ref = false; 23 | #endif 24 | } 25 | 26 | void AFSK_hw_init(void) { 27 | // Set up ADC 28 | 29 | AFSK_hw_refDetect(); 30 | 31 | TCCR1A = 0; 32 | TCCR1B = _BV(CS10) | _BV(WGM13) | _BV(WGM12); 33 | ICR1 = (((CPU_FREQ+FREQUENCY_CORRECTION)) / 9600) - 1; 34 | 35 | if (hw_5v_ref) { 36 | ADMUX = _BV(REFS0) | 0; 37 | } else { 38 | ADMUX = 0; 39 | } 40 | 41 | ADC_DDR &= ~_BV(0); 42 | ADC_PORT &= ~_BV(0); 43 | DIDR0 |= _BV(0); 44 | ADCSRB = _BV(ADTS2) | 45 | _BV(ADTS1) | 46 | _BV(ADTS0); 47 | ADCSRA = _BV(ADEN) | 48 | _BV(ADSC) | 49 | _BV(ADATE)| 50 | _BV(ADIE) | 51 | _BV(ADPS2); 52 | 53 | AFSK_DAC_INIT(); 54 | LED_TX_INIT(); 55 | LED_RX_INIT(); 56 | } 57 | 58 | void AFSK_init(Afsk *afsk) { 59 | // Allocate modem struct memory 60 | memset(afsk, 0, sizeof(*afsk)); 61 | AFSK_modem = afsk; 62 | // Set phase increment 63 | afsk->phaseInc = MARK_INC; 64 | // Initialise FIFO buffers 65 | fifo_init(&afsk->delayFifo, (uint8_t *)afsk->delayBuf, sizeof(afsk->delayBuf)); 66 | fifo_init(&afsk->rxFifo, afsk->rxBuf, sizeof(afsk->rxBuf)); 67 | fifo_init(&afsk->txFifo, afsk->txBuf, sizeof(afsk->txBuf)); 68 | 69 | // Fill delay FIFO with zeroes 70 | for (int i = 0; idelayFifo, 0); 72 | } 73 | 74 | AFSK_hw_init(); 75 | 76 | // Set up streams 77 | FILE afsk_fd = FDEV_SETUP_STREAM(afsk_putchar, afsk_getchar, _FDEV_SETUP_RW); 78 | afsk->fd = afsk_fd; 79 | } 80 | 81 | static void AFSK_txStart(Afsk *afsk) { 82 | if (!afsk->sending) { 83 | afsk->phaseInc = MARK_INC; 84 | afsk->phaseAcc = 0; 85 | afsk->bitstuffCount = 0; 86 | afsk->sending = true; 87 | LED_TX_ON(); 88 | afsk->preambleLength = DIV_ROUND(custom_preamble * BITRATE, 8000); 89 | AFSK_DAC_IRQ_START(); 90 | } 91 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 92 | afsk->tailLength = DIV_ROUND(custom_tail * BITRATE, 8000); 93 | } 94 | } 95 | 96 | void afsk_putchar(char c) { 97 | AFSK_txStart(AFSK_modem); 98 | while(fifo_isfull_locked(&AFSK_modem->txFifo)) { /* Wait */ } 99 | fifo_push_locked(&AFSK_modem->txFifo, c); 100 | } 101 | 102 | int afsk_getchar(void) { 103 | if (fifo_isempty_locked(&AFSK_modem->rxFifo)) { 104 | return EOF; 105 | } else { 106 | return fifo_pop_locked(&AFSK_modem->rxFifo); 107 | } 108 | } 109 | 110 | void AFSK_transmit(char *buffer, size_t size) { 111 | fifo_flush(&AFSK_modem->txFifo); 112 | int i = 0; 113 | while (size--) { 114 | afsk_putchar(buffer[i++]); 115 | } 116 | } 117 | 118 | uint8_t AFSK_dac_isr(Afsk *afsk) { 119 | if (afsk->sampleIndex == 0) { 120 | if (afsk->txBit == 0) { 121 | if (fifo_isempty(&afsk->txFifo) && afsk->tailLength == 0) { 122 | AFSK_DAC_IRQ_STOP(); 123 | afsk->sending = false; 124 | LED_TX_OFF(); 125 | return 0; 126 | } else { 127 | if (!afsk->bitStuff) afsk->bitstuffCount = 0; 128 | afsk->bitStuff = true; 129 | if (afsk->preambleLength == 0) { 130 | if (fifo_isempty(&afsk->txFifo)) { 131 | afsk->tailLength--; 132 | afsk->currentOutputByte = HDLC_FLAG; 133 | } else { 134 | afsk->currentOutputByte = fifo_pop(&afsk->txFifo); 135 | } 136 | } else { 137 | afsk->preambleLength--; 138 | afsk->currentOutputByte = HDLC_FLAG; 139 | } 140 | if (afsk->currentOutputByte == AX25_ESC) { 141 | if (fifo_isempty(&afsk->txFifo)) { 142 | AFSK_DAC_IRQ_STOP(); 143 | afsk->sending = false; 144 | LED_TX_OFF(); 145 | return 0; 146 | } else { 147 | afsk->currentOutputByte = fifo_pop(&afsk->txFifo); 148 | } 149 | } else if (afsk->currentOutputByte == HDLC_FLAG || afsk->currentOutputByte == HDLC_RESET) { 150 | afsk->bitStuff = false; 151 | } 152 | } 153 | afsk->txBit = 0x01; 154 | } 155 | 156 | if (afsk->bitStuff && afsk->bitstuffCount >= BIT_STUFF_LEN) { 157 | afsk->bitstuffCount = 0; 158 | afsk->phaseInc = SWITCH_TONE(afsk->phaseInc); 159 | } else { 160 | if (afsk->currentOutputByte & afsk->txBit) { 161 | afsk->bitstuffCount++; 162 | } else { 163 | afsk->bitstuffCount = 0; 164 | afsk->phaseInc = SWITCH_TONE(afsk->phaseInc); 165 | } 166 | afsk->txBit <<= 1; 167 | } 168 | 169 | afsk->sampleIndex = SAMPLESPERBIT; 170 | } 171 | 172 | afsk->phaseAcc += afsk->phaseInc; 173 | afsk->phaseAcc %= SIN_LEN; 174 | afsk->sampleIndex--; 175 | 176 | return sinSample(afsk->phaseAcc); 177 | } 178 | 179 | static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { 180 | // Initialise a return value. We start with the 181 | // assumption that all is going to end well :) 182 | bool ret = true; 183 | 184 | // Bitshift our byte of demodulated bits to 185 | // the left by one bit, to make room for the 186 | // next incoming bit 187 | hdlc->demodulatedBits <<= 1; 188 | // And then put the newest bit from the 189 | // demodulator into the byte. 190 | hdlc->demodulatedBits |= bit ? 1 : 0; 191 | 192 | // Now we'll look at the last 8 received bits, and 193 | // check if we have received a HDLC flag (01111110) 194 | if (hdlc->demodulatedBits == HDLC_FLAG) { 195 | // If we have, check that our output buffer is 196 | // not full. 197 | if (!fifo_isfull(fifo)) { 198 | // If it isn't, we'll push the HDLC_FLAG into 199 | // the buffer and indicate that we are now 200 | // receiving data. For bling we also turn 201 | // on the RX LED. 202 | fifo_push(fifo, HDLC_FLAG); 203 | hdlc->receiving = true; 204 | #if OPEN_SQUELCH == false 205 | LED_RX_ON(); 206 | #endif 207 | } else { 208 | // If the buffer is full, we have a problem 209 | // and abort by setting the return value to 210 | // false and stopping the here. 211 | 212 | ret = false; 213 | hdlc->receiving = false; 214 | LED_RX_OFF(); 215 | } 216 | 217 | // Everytime we receive a HDLC_FLAG, we reset the 218 | // storage for our current incoming byte and bit 219 | // position in that byte. This effectively 220 | // synchronises our parsing to the start and end 221 | // of the received bytes. 222 | hdlc->currentByte = 0; 223 | hdlc->bitIndex = 0; 224 | return ret; 225 | } 226 | 227 | // Check if we have received a RESET flag (01111111) 228 | // In this comparison we also detect when no transmission 229 | // (or silence) is taking place, and the demodulator 230 | // returns an endless stream of zeroes. Due to the NRZ 231 | // coding, the actual bits send to this function will 232 | // be an endless stream of ones, which this AND operation 233 | // will also detect. 234 | if ((hdlc->demodulatedBits & HDLC_RESET) == HDLC_RESET) { 235 | // If we have, something probably went wrong at the 236 | // transmitting end, and we abort the reception. 237 | hdlc->receiving = false; 238 | LED_RX_OFF(); 239 | return ret; 240 | } 241 | 242 | // If we have not yet seen a HDLC_FLAG indicating that 243 | // a transmission is actually taking place, don't bother 244 | // with anything. 245 | if (!hdlc->receiving) 246 | return ret; 247 | 248 | // First check if what we are seeing is a stuffed bit. 249 | // Since the different HDLC control characters like 250 | // HDLC_FLAG, HDLC_RESET and such could also occur in 251 | // a normal data stream, we employ a method known as 252 | // "bit stuffing". All control characters have more than 253 | // 5 ones in a row, so if the transmitting party detects 254 | // this sequence in the _data_ to be transmitted, it inserts 255 | // a zero to avoid the receiving party interpreting it as 256 | // a control character. Therefore, if we detect such a 257 | // "stuffed bit", we simply ignore it and wait for the 258 | // next bit to come in. 259 | // 260 | // We do the detection by applying an AND bit-mask to the 261 | // stream of demodulated bits. This mask is 00111111 (0x3f) 262 | // if the result of the operation is 00111110 (0x3e), we 263 | // have detected a stuffed bit. 264 | if ((hdlc->demodulatedBits & 0x3f) == 0x3e) 265 | return ret; 266 | 267 | // If we have an actual 1 bit, push this to the current byte 268 | // If it's a zero, we don't need to do anything, since the 269 | // bit is initialized to zero when we bitshifted earlier. 270 | if (hdlc->demodulatedBits & 0x01) 271 | hdlc->currentByte |= 0x80; 272 | 273 | // Increment the bitIndex and check if we have a complete byte 274 | if (++hdlc->bitIndex >= 8) { 275 | // If we have a HDLC control character, put a AX.25 escape 276 | // in the received data. We know we need to do this, 277 | // because at this point we must have already seen a HDLC 278 | // flag, meaning that this control character is the result 279 | // of a bitstuffed byte that is equal to said control 280 | // character, but is actually part of the data stream. 281 | // By inserting the escape character, we tell the protocol 282 | // layer that this is not an actual control character, but 283 | // data. 284 | if ((hdlc->currentByte == HDLC_FLAG || 285 | hdlc->currentByte == HDLC_RESET || 286 | hdlc->currentByte == AX25_ESC)) { 287 | // We also need to check that our received data buffer 288 | // is not full before putting more data in 289 | if (!fifo_isfull(fifo)) { 290 | fifo_push(fifo, AX25_ESC); 291 | } else { 292 | // If it is, abort and return false 293 | hdlc->receiving = false; 294 | LED_RX_OFF(); 295 | ret = false; 296 | } 297 | } 298 | 299 | // Push the actual byte to the received data FIFO, 300 | // if it isn't full. 301 | if (!fifo_isfull(fifo)) { 302 | fifo_push(fifo, hdlc->currentByte); 303 | } else { 304 | // If it is, well, you know by now! 305 | hdlc->receiving = false; 306 | LED_RX_OFF(); 307 | ret = false; 308 | } 309 | 310 | // Wipe received byte and reset bit index to 0 311 | hdlc->currentByte = 0; 312 | hdlc->bitIndex = 0; 313 | 314 | } else { 315 | // We don't have a full byte yet, bitshift the byte 316 | // to make room for the next bit 317 | hdlc->currentByte >>= 1; 318 | } 319 | 320 | //digitalWrite(13, LOW); 321 | return ret; 322 | } 323 | 324 | 325 | void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { 326 | // To determine the received frequency, and thereby 327 | // the bit of the sample, we multiply the sample by 328 | // a sample delayed by (samples per bit / 2). 329 | // We then lowpass-filter the samples with a 330 | // Chebyshev filter. The lowpass filtering serves 331 | // to "smooth out" the variations in the samples. 332 | 333 | afsk->iirX[0] = afsk->iirX[1]; 334 | 335 | #if FILTER_CUTOFF == 600 336 | afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2; 337 | // The above is a simplification of: 338 | // afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 3.558147322; 339 | #elif FILTER_CUTOFF == 800 340 | afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2; 341 | // The above is a simplification of: 342 | // afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 2.899043379; 343 | #elif FILTER_CUTOFF == 1200 344 | afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 1; 345 | // The above is a simplification of: 346 | // afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 2.228465666; 347 | #elif FILTER_CUTOFF == 1600 348 | afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 1; 349 | // The above is a simplification of: 350 | // afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 1.881349100; 351 | #else 352 | #error Unsupported filter cutoff! 353 | #endif 354 | 355 | afsk->iirY[0] = afsk->iirY[1]; 356 | 357 | #if FILTER_CUTOFF == 600 358 | afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] >> 1); 359 | // The above is a simplification of a first-order 600Hz chebyshev filter: 360 | // afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.4379097269); 361 | #elif FILTER_CUTOFF == 800 362 | afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] / 3); 363 | // The above is a simplification of a first-order 800Hz chebyshev filter: 364 | // afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.3101172565); 365 | #elif FILTER_CUTOFF == 1200 366 | afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] / 10); 367 | // The above is a simplification of a first-order 800Hz chebyshev filter: 368 | // afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.1025215106); 369 | #elif FILTER_CUTOFF == 1600 370 | afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + -1*(afsk->iirY[0] / 17); 371 | // The above is a simplification of a first-order 800Hz chebyshev filter: 372 | // afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * -0.0630669239); 373 | #else 374 | #error Unsupported filter cutoff! 375 | #endif 376 | 377 | 378 | // We put the sampled bit in a delay-line: 379 | // First we bitshift everything 1 left 380 | afsk->sampledBits <<= 1; 381 | // And then add the sampled bit to our delay line 382 | afsk->sampledBits |= (afsk->iirY[1] > 0) ? 1 : 0; 383 | 384 | // Put the current raw sample in the delay FIFO 385 | fifo_push(&afsk->delayFifo, currentSample); 386 | 387 | // We need to check whether there is a signal transition. 388 | // If there is, we can recalibrate the phase of our 389 | // sampler to stay in sync with the transmitter. A bit of 390 | // explanation is required to understand how this works. 391 | // Since we have PHASE_MAX/PHASE_BITS = 8 samples per bit, 392 | // we employ a phase counter (currentPhase), that increments 393 | // by PHASE_BITS everytime a sample is captured. When this 394 | // counter reaches PHASE_MAX, it wraps around by modulus 395 | // PHASE_MAX. We then look at the last three samples we 396 | // captured and determine if the bit was a one or a zero. 397 | // 398 | // This gives us a "window" looking into the stream of 399 | // samples coming from the ADC. Sort of like this: 400 | // 401 | // Past Future 402 | // 0000000011111111000000001111111100000000 403 | // |________| 404 | // || 405 | // Window 406 | // 407 | // Every time we detect a signal transition, we adjust 408 | // where this window is positioned little. How much we 409 | // adjust it is defined by PHASE_INC. If our current phase 410 | // phase counter value is less than half of PHASE_MAX (ie, 411 | // the window size) when a signal transition is detected, 412 | // add PHASE_INC to our phase counter, effectively moving 413 | // the window a little bit backward (to the left in the 414 | // illustration), inversely, if the phase counter is greater 415 | // than half of PHASE_MAX, we move it forward a little. 416 | // This way, our "window" is constantly seeking to position 417 | // it's center at the bit transitions. Thus, we synchronise 418 | // our timing to the transmitter, even if it's timing is 419 | // a little off compared to our own. 420 | if (SIGNAL_TRANSITIONED(afsk->sampledBits)) { 421 | if (afsk->currentPhase < PHASE_THRESHOLD) { 422 | afsk->currentPhase += PHASE_INC; 423 | } else { 424 | afsk->currentPhase -= PHASE_INC; 425 | } 426 | } 427 | 428 | // We increment our phase counter 429 | afsk->currentPhase += PHASE_BITS; 430 | 431 | // Check if we have reached the end of 432 | // our sampling window. 433 | if (afsk->currentPhase >= PHASE_MAX) { 434 | // If we have, wrap around our phase 435 | // counter by modulus 436 | afsk->currentPhase %= PHASE_MAX; 437 | 438 | // Bitshift to make room for the next 439 | // bit in our stream of demodulated bits 440 | afsk->actualBits <<= 1; 441 | 442 | // We determine the actual bit value by reading 443 | // the last 3 sampled bits. If there is three or 444 | // more 1's, we will assume that the transmitter 445 | // sent us a one, otherwise we assume a zero 446 | uint8_t bits = afsk->sampledBits & 0x07; 447 | if (bits == 0x07 || // 111 448 | bits == 0x06 || // 110 449 | bits == 0x05 || // 101 450 | bits == 0x03 // 011 451 | ) { 452 | afsk->actualBits |= 1; 453 | } 454 | 455 | //// Alternative using five bits //////////////// 456 | // uint8_t bits = afsk->sampledBits & 0x0f; 457 | // uint8_t c = 0; 458 | // c += bits & BV(1); 459 | // c += bits & BV(2); 460 | // c += bits & BV(3); 461 | // c += bits & BV(4); 462 | // c += bits & BV(5); 463 | // if (c >= 3) afsk->actualBits |= 1; 464 | ///////////////////////////////////////////////// 465 | 466 | // Now we can pass the actual bit to the HDLC parser. 467 | // We are using NRZ coding, so if 2 consecutive bits 468 | // have the same value, we have a 1, otherwise a 0. 469 | // We use the TRANSITION_FOUND function to determine this. 470 | // 471 | // This is smart in combination with bit stuffing, 472 | // since it ensures a transmitter will never send more 473 | // than five consecutive 1's. When sending consecutive 474 | // ones, the signal stays at the same level, and if 475 | // this happens for longer periods of time, we would 476 | // not be able to synchronize our phase to the transmitter 477 | // and would start experiencing "bit slip". 478 | // 479 | // By combining bit-stuffing with NRZ coding, we ensure 480 | // that the signal will regularly make transitions 481 | // that we can use to synchronize our phase. 482 | // 483 | // We also check the return of the Link Control parser 484 | // to check if an error occured. 485 | 486 | if (!hdlcParse(&afsk->hdlc, !TRANSITION_FOUND(afsk->actualBits), &afsk->rxFifo)) { 487 | afsk->status |= 1; 488 | if (fifo_isfull(&afsk->rxFifo)) { 489 | fifo_flush(&afsk->rxFifo); 490 | afsk->status = 0; 491 | } 492 | } 493 | } 494 | 495 | } 496 | 497 | 498 | ISR(ADC_vect) { 499 | TIFR1 = _BV(ICF1); 500 | AFSK_adc_isr(AFSK_modem, ((int16_t)((ADC) >> 2) - 128)); 501 | if (hw_afsk_dac_isr) { 502 | DAC_PORT = (AFSK_dac_isr(AFSK_modem) & 0xF0) | _BV(3); 503 | } else { 504 | DAC_PORT = 128; 505 | } 506 | ++_clock; 507 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | --------------------------------------------------------------------------------