Changeset - 9716361162e0
[Not reviewed]
default
0 5 0
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-11-08 15:40:33
ethanzonca@CL-SEC241-08.cedarville.edu
ax25 implementation complete (compiles)
5 files changed with 27 insertions and 6 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
/*
 
 * config.h
 
 *
 
 * Created: 10/25/2012 3:28:22 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef CONFIG_H_
 
#define CONFIG_H_
 
 
#define F_CPU 11059200
 
#define MODULE_ID '1'
 
 
// --------------------------------------------------------------------------
 
// AX.25 config (ax25.c)
 
// --------------------------------------------------------------------------
 

	
 
// TX delay in milliseconds
 
#define TX_DELAY      300
 

	
 
// Maximum packet delay
 
#define MAX_PACKET_LEN 512  // bytes
 
 
 
 
#endif /* CONFIG_H_ */
 
\ No newline at end of file
master/master/lib/afsk.c
Show inline comments
 
/*
 
 * fsk.c
 
 *
 
 * Created: 10/29/2012 7:40:34 PM
 
 *  Author: ethanzonca
 
 */
 

	
 
#define F_CPU 11059200
 

	
 
#include "../config.h"
 

	
 
#include <stdint.h>
 
#include <stdbool.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h> 
 
#include <util/delay.h>
 
#include <avr/pgmspace.h>
 

	
 

	
 
PROGMEM extern const uint8_t afsk_sine_table[512] = {
 
  127, 129, 130, 132, 133, 135, 136, 138, 139, 141, 143, 144, 146, 147, 149, 150, 152, 153, 155, 156, 158, 
 
  159, 161, 163, 164, 166, 167, 168, 170, 171, 173, 174, 176, 177, 179, 180, 182, 183, 184, 186, 187, 188, 
 
  190, 191, 193, 194, 195, 197, 198, 199, 200, 202, 203, 204, 205, 207, 208, 209, 210, 211, 213, 214, 215, 
 
  216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 
 
  236, 237, 238, 239, 239, 240, 241, 242, 242, 243, 244, 244, 245, 245, 246, 247, 247, 248, 248, 249, 249, 
 
  249, 250, 250, 251, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 
 
  254, 254, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 253, 253, 253, 253, 252, 252, 252, 251, 
 
  251, 251, 250, 250, 249, 249, 249, 248, 248, 247, 247, 246, 245, 245, 244, 244, 243, 242, 242, 241, 240, 
 
  239, 239, 238, 237, 236, 236, 235, 234, 233, 232, 231, 230, 229, 228, 228, 227, 226, 225, 224, 223, 221, 
 
  220, 219, 218, 217, 216, 215, 214, 213, 211, 210, 209, 208, 207, 205, 204, 203, 202, 200, 199, 198, 197, 
 
  195, 194, 193, 191, 190, 188, 187, 186, 184, 183, 182, 180, 179, 177, 176, 174, 173, 171, 170, 168, 167, 
 
  166, 164, 163, 161, 159, 158, 156, 155, 153, 152, 150, 149, 147, 146, 144, 143, 141, 139, 138, 136, 135, 
 
  133, 132, 130, 129, 127, 125, 124, 122, 121, 119, 118, 116, 115, 113, 111, 110, 108, 107, 105, 104, 102, 
 
  101,  99,  98,  96,  95,  93,  91,  90,  88,  87,  86,  84,  83,  81,  80,  78,  77,  75,  74,  72,  71, 
 
   70,  68,  67,  66,  64,  63,  61,  60,  59,  57,  56,  55,  54,  52,  51,  50,  49,  47,  46,  45,  44, 
 
   43,  41,  40,  39,  38,  37,  36,  35,  34,  33,  31,  30,  29,  28,  27,  26,  26,  25,  24,  23,  22, 
 
   21,  20,  19,  18,  18,  17,  16,  15,  15,  14,  13,  12,  12,  11,  10,  10,   9,   9,   8,   7,   7, 
 
    6,   6,   5,   5,   5,   4,   4,   3,   3,   3,   2,   2,   2,   1,   1,   1,   1,   0,   0,   0,   0, 
 
    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1, 
 
    2,   2,   2,   3,   3,   3,   4,   4,   5,   5,   5,   6,   6,   7,   7,   8,   9,   9,  10,  10,  11, 
 
   12,  12,  13,  14,  15,  15,  16,  17,  18,  18,  19,  20,  21,  22,  23,  24,  25,  26,  26,  27,  28, 
 
   29,  30,  31,  33,  34,  35,  36,  37,  38,  39,  40,  41,  43,  44,  45,  46,  47,  49,  50,  51,  52, 
 
   54,  55,  56,  57,  59,  60,  61,  63,  64,  66,  67,  68,  70,  71,  72,  74,  75,  77,  78,  80,  81, 
 
   83,  84,  86,  87,  88,  90,  91,  93,  95,  96,  98,  99, 101, 102, 104, 105, 107, 108, 110, 111, 113, 
 
  115, 116, 118, 119, 121, 122, 124, 125
 
};
 

	
 
static inline uint8_t afsk_read_sample(int phase)
 
{
 
  return pgm_read_byte_near(afsk_sine_table + phase);
 
}
 

	
 
volatile uint16_t phase = 10;
 
volatile uint16_t phasedelta = 3300;
 
extern const uint16_t TABLE_SIZE = sizeof(afsk_sine_table);
 

	
 
// constants
 
#define MODEM_CLOCK_RATE F_CPU
 
#define PLAYBACK_RATE MODEM_CLOCK_RATE / 256  // Fast PWM
 

	
 
#define BAUD_RATE 1200
 
//#define SAMPLES_PER_BAUD PLAYBACK_RATE / BAUD_RATE // = 36
 
//#define SAMPLES_PER_BAUD 36
 
#define SAMPLES_PER_BAUD 36
 

	
 

	
 
// phase offset of 1800 gives ~1900 Hz
 
// phase offset of 3300 gives ~2200 Hz
 
#define PHASE_DELTA_1200 1800
 
#define PHASE_DELTA_2200 2200
 

	
 
// Module globals
 
volatile unsigned char current_byte;
 
volatile unsigned char current_sample_in_baud;    // 1 bit = SAMPLES_PER_BAUD samples
 
volatile bool go = false;                 
 

	
 
volatile unsigned int packet_pos;                 // Next bit to be sent out
 

	
 

	
 
volatile unsigned int afsk_packet_size = 10;
 
volatile const uint8_t *afsk_packet;
 

	
 

	
 

	
 

	
 

	
 

	
 
void afsk_ptt_off() {
 
	// turn ptt off
 
}
 

	
 
void afsk_ptt_on() {
 
	// turn the ptt on... possibly delay based on spec
 
}
 

	
 

	
 

	
 

	
 

	
 

	
 

	
 
ISR(TIMER2_OVF_vect) 
 
{
 
	if (go) {
 

	
 
		// If done sending packet
 
		if (packet_pos == afsk_packet_size) {
 
			go = false;         // End of transmission
 
			afsk_timer_stop();  // Disable modem
 
			afsk_ptt_off();    // Release PTT
 
			return; // done
 
		}
 

	
 
		// If sent SAMPLES_PER_BAUD already, go to the next bit
 
		if (current_sample_in_baud == 0) {    // Load up next bit
 
			if ((packet_pos & 7) == 0) {          // Load up next byte
 
				current_byte = afsk_packet[packet_pos >> 3];
 
			}				
 
			else {
 
				current_byte = current_byte / 2;  // ">>1" forces int conversion
 
			}			
 
			if ((current_byte & 1) == 0) {
 
				// Toggle tone (1200 <> 2200)
 
				phasedelta ^= (PHASE_DELTA_1200 ^ PHASE_DELTA_2200);
 
			}
 
		}
 
	
 
		phase += phasedelta;
 
		uint8_t s = afsk_read_sample((phase >> 7) & (TABLE_SIZE - 1));
 
		afsk_output_sample(s);
 

	
 
		if(++current_sample_in_baud == SAMPLES_PER_BAUD) {
 
			// sounds fun when this is commented out... but why??!?!
 
			current_sample_in_baud = 0;
 
			packet_pos++;
 
		}
 
		
 
	}
 
}	
 

	
 
void afsk_output_sample(uint8_t s) {
 
	OCR2B = s;
 
}
 

	
 
void afsk_timer_start()
 
{
 
	// Clear the overflow flag, so that the interrupt doesn't go off
 
	// immediately and overrun the next one (p.163).
 
	TIFR2 |= _BV(TOV2);       // Yeah, writing a 1 clears the flag.
 

	
 
	// Enable interrupt when TCNT2 reaches TOP (0xFF) (p.151, 163)
 
	TIMSK2 |= _BV(TOIE2);
 
}
 

	
 
void afsk_timer_stop()
 
{
 
	// Resting duty cycle
 
	// Output 0v (could be 255/2, which is 0v after coupling... doesn't really matter)
 
	OCR2B = 0x80;
 

	
 
	// Disable playback interrupt
 
	TIMSK2 &= ~_BV(TOIE2);
 
}
 

	
 

	
 

	
 

	
 

	
 
// External
 

	
 
void afsk_start()
 
{
 
	phasedelta = PHASE_DELTA_1200;
 
	phase = 0;
 
	packet_pos = 0;
 
	current_sample_in_baud = 0;
 
	go = true;
 

	
 
	// Key the radio
 
	afsk_ptt_on();
 

	
 
	// Start transmission
 
	afsk_timer_start();
 
}
 

	
 
int afsk_busy()
 
{
 
	return go;
 
}
 

	
 
void afsk_send(const uint8_t *buffer, int len)
 
{
 
	afsk_packet_size = len;
 
	afsk_packet = buffer;
 
}
 

	
 

	
 
void afsk_setup() {
 
	
 
	// Source timer2 from clkIO (datasheet p.164)
 
	ASSR &= ~(_BV(EXCLK) | _BV(AS2));
 

	
master/master/lib/afsk.h
Show inline comments
 
/*
 
 * afsk.h
 
 *
 
 * Created: 10/29/2012 7:43:03 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef AFSK_H_
 
#define AFSK_H_
 
 
// Private
 
void afsk_output_sample(uint8_t s);
 
void afsk_timer_stop();
 
 
// Public
 
void afsk_setup();
 
void afsk_test();
 
void afsk_send(const uint8_t *buffer, int len);
 
int afsk_busy();
 
void afsk_start();
 
 
#endif /* AFSK_H_ */
 
\ No newline at end of file
master/master/lib/ax25.c
Show inline comments
 
/*
 
 * ax25.c
 
 *
 
 * Created: 10/30/2012 12:16:41 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
/*
 

	
 
#include <inttypes.h>
 
#include "../config.h"
 
#include "ax25.h"
 
#include "afsk.h"
 

	
 
// Module constants
 
#define MAX_PACKET_LEN 512  // bytes
 

	
 
// Module globals
 
static uint16_t crc;
 
static uint8_t ones_in_a_row;
 
static uint8_t packet[MAX_PACKET_LEN];
 
static unsigned int packet_size;
 

	
 
// Module functions
 
void update_crc(uint8_t a_bit)
 
{
 
	crc ^= a_bit;
 
	if (crc & 1)
 
	crc = (crc >> 1) ^ 0x8408;  // X-modem CRC poly
 
	else
 
	crc = crc >> 1;
 
}
 

	
 
void send_byte(uint8_t a_byte)
 
{
 
	uint8_t i = 0;
 
	while (i++ < 8) {
 
		uint8_t a_bit = a_byte & 1;
 
		a_byte >>= 1;
 
		update_crc(a_bit);
 
		if (a_bit) {
 
			// Next bit is a '1'
 
			if (packet_size >= MAX_PACKET_LEN * 8)  // Prevent buffer overrun
 
			return;
 
			packet[packet_size >> 3] |= (1 << (packet_size & 7));
 
			packet_size++;
 
			if (++ones_in_a_row < 5) continue;
 
		}
 
		// Next bit is a '0' or a zero padding after 5 ones in a row
 
		if (packet_size >= MAX_PACKET_LEN * 8)    // Prevent buffer overrun
 
		return;
 
		packet[packet_size >> 3] &= ~(1 << (packet_size & 7));
 
		packet_size++;
 
		ones_in_a_row = 0;
 
	}
 
}
 

	
 
// Exported functions
 
void ax25_send_byte(uint8_t a_byte)
 
{
 
	// Wrap around send_byte, but prints debug info
 
	send_byte(a_byte);
 
}
 

	
 
void ax25_send_flag()
 
{
 
	uint8_t flag = 0x7e;
 
	int i;
 
	for (i = 0; i < 8; i++, packet_size++) {
 
		if (packet_size >= MAX_PACKET_LEN * 8)  // Prevent buffer overrun
 
		return;
 
		if ((flag >> i) & 1)
 
		packet[packet_size >> 3] |= (1 << (packet_size & 7));
 
		else
 
		packet[packet_size >> 3] &= ~(1 << (packet_size & 7));
 
	}
 
}
 

	
 
void ax25_send_string(const char *string)
 
{
 
	int i;
 
	for (i = 0; string[i]; i++) {
 
		ax25_send_byte(string[i]);
 
	}
 
}
 

	
 
void ax25_send_header(const struct s_address *addresses, int num_addresses)
 
{
 
	int i, j;
 
	packet_size = 0;
 
	ones_in_a_row = 0;
 
	crc = 0xffff;
 
	
 
	// Send flags during TX_DELAY milliseconds (8 bit-flag = 8000/1200 ms)
 
	for (i = 0; i < TX_DELAY * 3 / 20; i++) {
 
		ax25_send_flag();
 
	}
 
	
 
	for (i = 0; i < num_addresses; i++) {
 
		// Transmit callsign
 
		for (j = 0; addresses[i].callsign[j]; j++)
 
		send_byte(addresses[i].callsign[j] << 1);
 
		// Transmit pad
 
		for ( ; j < 6; j++)
 
		send_byte(' ' << 1);
 
		// Transmit SSID. Termination signaled with last bit = 1
 
		if (i == num_addresses - 1)
 
		send_byte(('0' + addresses[i].ssid) << 1 | 1);
 
		else
 
		send_byte(('0' + addresses[i].ssid) << 1);
 
	}
 
	
 
	// Control field: 3 = APRS-UI frame
 
	send_byte(0x03);
 
	
 
	// Protocol ID: 0xf0 = no layer 3 data
 
	send_byte(0xf0);
 
}
 

	
 
void ax25_send_footer()
 
{
 
	// Save the crc so that it can be treated it atomically
 
	uint16_t final_crc = crc;
 
	
 
	// Send the CRC
 
	send_byte(~(final_crc & 0xff));
 
	final_crc >>= 8;
 
	send_byte(~(final_crc & 0xff));
 
	
 
	// Signal the end of frame
 
	ax25_send_flag();
 
}
 

	
 
void ax25_flush_frame()
 
{
 
	// Key the transmitter and send the frame
 
	afsk_send(packet, packet_size);
 
	afsk_start();
 
}
 
*/
 
\ No newline at end of file
master/master/lib/ax25.h
Show inline comments
 
/*
 
 * ax25.h
 
 *
 
 * Created: 10/30/2012 12:14:05 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef AX25_H_
 
#define AX25_H_
 
 
struct s_address {
 
	char callsign[7];
 
	unsigned char ssid;
 
};
 
 
void ax25_send_byte(uint8_t a_byte);
 
void ax25_send_flag();
 
void ax25_send_string(const char *string);
 
void ax25_send_header(const struct s_address *addresses, int num_addresses);
 
void ax25_send_footer();
 
void ax25_flush_frame();
 
 
#endif /* AX25_H_ */
 
\ No newline at end of file
0 comments (0 inline, 0 general)