-
Notifications
You must be signed in to change notification settings - Fork 4
/
rtty.c
115 lines (96 loc) · 2.57 KB
/
rtty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* hadie - High Altitude Balloon flight software */
/*============================================================*/
/* Copyright (C)2010 Philip Heron <phil@sanslogic.co.uk> */
/* */
/* This program is distributed under the terms of the GNU */
/* General Public License, version 2. You may use, modify, */
/* and redistribute it under the terms of this license. A */
/* copy should be included with this source. */
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "rtty.h"
/* For camera and GPS timeout */
extern inline void c3_tick(void);
extern inline void gps_tick(void);
/* MARK = Upper tone, Idle, bit */
#define TXPIN (1 << 0) /* PB0 */
#define TXENABLE (1 << 1) /* PB1 */
#define TXBIT(b) PORTB = (PORTB & ~TXPIN) | ((b) ? TXPIN : 0)
volatile static uint8_t txpgm = 0;
volatile static uint8_t *txbuf = 0;
volatile static uint16_t txlen = 0;
ISR(TIMER0_COMPA_vect)
{
/* The currently transmitting byte, including framing */
static uint8_t byte = 0x00;
static uint8_t bit = 0x00;
uint8_t b = 0;
switch(bit++)
{
case 0: b = 0; break; /* Start bit */
case 9: b = 1; break; /* Stop bit */
//case 10: b = 1; TCNT0 += OCR0A >> 1; bit = 0; break; /* Stop bit 0.5 */
case 10: b = 1; bit = 0; break; /* Stop bit part 2 */
default: b = byte & 1; byte >>= 1; break;
}
TXBIT(b);
if(bit == 0 && txlen > 0)
{
if(txpgm == 0) byte = *(txbuf++);
else byte = pgm_read_byte(txbuf++);
txlen--;
}
/* Camera and GPS timeout tick */
c3_tick();
gps_tick();
}
void rtx_enable(char en)
{
if(en) PORTB |= TXENABLE;
else PORTB &= ~TXENABLE;
}
void rtx_init(void)
{
/* RTTY is driven by TIMER0 in CTC mode */
TCCR0A = _BV(WGM01); /* Mode 2, CTC */
TCCR0B = _BV(CS02) | _BV(CS00); /* prescaler 1024 */
OCR0A = F_CPU / 1024 / RTTY_BAUD - 1;
TIMSK0 = _BV(OCIE0A); /* Enable interrupt */
/* We use Port B pins 1 and 2 */
TXBIT(1);
rtx_enable(0);
DDRB |= TXPIN | TXENABLE;
}
void inline rtx_wait(void)
{
/* Wait for interrupt driven TX to finish */
while(txlen > 0) while(txlen > 0);
}
void rtx_data(uint8_t *data, size_t length)
{
rtx_wait();
txpgm = 0;
txbuf = data;
txlen = length;
}
void rtx_data_P(PGM_P data, size_t length)
{
rtx_wait();
txpgm = 1;
txbuf = (uint8_t *) data;
txlen = length;
}
void rtx_string(char *s)
{
uint16_t length = strlen(s);
rtx_data((uint8_t *) s, length);
}
void rtx_string_P(PGM_P s)
{
uint16_t length = strlen_P(s);
rtx_data_P(s, length);
}