-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
219 lines (186 loc) · 5.85 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Copyright 2012 Alistair Buxton <[email protected]>
This file is part of avr-teletext.
avr-teletext is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
avr-teletext is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with avr-teletext. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "globals.h"
#include "console.h"
#include "utils.h"
#define WITH_LM1881
#define nop() __asm__ __volatile__("nop\nnop\nnop\nnop");
void io_setup(void)
{
// +---+-----+-----+-----+-----+-----+-----+-----+-----+
// | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
// +---+-----+-----+-----+-----+-----+-----+-----+-----+
// | B | OUT | OUT | OUT | OUT | IN | IN | Crystal |
// +---+-----+-----+-----+-----+-----+-----+-----+-----+
// | C | OUT | OUT | OUT | OUT | SDA | SCL | RST | - |
// +---+-----+-----+-----+-----+-----+-----+-----+-----+
// | D | RXD | TXD |INT0 |INT1 | T0 | T1 | OUT | OUT |
// +---+-----+-----+-----+-----+-----+-----+-----+-----+
// D[7] = shifter enable
DDRD = 0x12; PORTD = 0;
// SPI setup
// baud Fosc/2
UBRR0H = 0;
UBRR0L = 0;
// USART0 MSPIM mode, LSB
UCSR0C = 0xc4;
// enable Tx
UCSR0B = 0x18;
// TWI setup
// no need to set baud rate for slave mode
// enable pull-ups
DDRC = 0x00; PORTC = 0x30;
// match addresses 0x40 - 0x7f
TWAR = 0x40<<1;
TWAMR = 0x1f<<1;
// enable TWI, no interrupt request
TWCR = _BV(TWEN) | _BV(TWEA);
// external INT0 and INT1 enable
EICRA = 0x0b;
EIMSK = 0x03;
sei();
}
volatile uint8_t line_counter;
volatile uint8_t tt_buffer[NBUFFERS][42];
volatile uint8_t buffer_head = 0;
volatile uint8_t buffer_tail = 0;
/* Buffer that we send when we have nothing else to do. */
const uint8_t fill_buffer[42] PROGMEM = {
0x49, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
0x15, 0x15,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x31, 0xb0, 0xb0, 0x20, 0x54, 0x75, 0xe5,
0x20, 0x31, 0x37, 0x20, 0x4a, 0x61, 0x6e, 0x83,
0x32, 0x31, 0xba, 0xb5, 0xb0, 0x2f, 0xb3, 0xb6
};
/* hmm */
#define BLACK "\x80"
#define RED "\x01"
#define GREEN "\x02"
#define YELLOW "\x83"
#define BLUE "\x04"
#define MAGENTA "\x85"
#define CYAN "\x86"
#define WHITE "\x07"
#define FLASHON "\x08"
#define FLASHOFF "\x09"
#define ENDBOX "\x0A"
#define STARTBOX "\x0B"
#define NORMAL "\x0C"
#define DOUBLEH "\x0D"
#define DOUBLEW "\x0E"
#define DOUBLEWH "\x0F"
/*
0123456789012345678901234567890123456789
*/
const char long_text[] PROGMEM =
" "DOUBLEWH"H e l l o w o r l d !\n"
"\n"
"\n"
" "RED"* "GREEN"* "YELLOW"* "BLUE"* "MAGENTA"* "CYAN"*\n\n"
" The"CYAN"AVR"WHITE"is a modified Harvard\n"
"architecture 8-bit RISC single chip\n"
"microcontroller which was developed by\n"
"Atmel in"GREEN"1996."WHITE"The AVR was one of the\n"
"first microcontroller families to use\n"
"on-chip flash memory for program\n"
"storage, as opposed to one-time\n"
"programmable ROM, EPROM, or EEPROM used\n"
"by other microcontrollers at the time.\n"
"\n"
" "RED"* "GREEN"* "YELLOW"* "BLUE"* "MAGENTA"* "CYAN"*\n\n"
" "YELLOW"Teletext"WHITE"(or \"broadcast teletext\") is\n"
"a television information retrieval\n"
"service developed in the United Kingdom\n"
"in the early"GREEN"1970s."WHITE"It offers a range\n"
"of text-based information, typically\n"
"including national, international and\n"
"sporting news, weather and television\n"
"schedules. Subtitle information is also\n"
"transmitted in the teletext signal,\n"
"typically on page 888, 777 or 333."
;
extern uint8_t first_row;
void run_demo(void) {
for(;;)
{
int text_pos;
char c;
text_pos = 0;
//console_clear();
while((c = pgm_read_byte(&(long_text[text_pos]))) != 0) {
console_putchar(c);
delay_ms(10);
text_pos++;
}
//delay_s(1);
console_putchar('\n');
console_putchar('\n');
}
}
int main(void)
{
uint8_t dest_register = 0xff;
uint8_t control = 0;
uint8_t twdr = 0;
uint8_t twsr = 0;
io_setup();
console_setup();
control=0;
//run_demo();
for(;;)
{
if(TWCR&0x80) {
twdr = TWDR;
twsr = TWSR;
TWCR |= 0x80;
switch(twsr&0xf8) {
case 0x60:
case 0x68:
dest_register = (twdr>>1)&(TWAMR>>1);
if(control&0x01) passthrough_start();
break;
case 0x80:
switch(dest_register) {
case 0:
if(control&0x01) passthrough_putchar(twdr);
else console_putchar(twdr&0x7f);
break;
case 1:
control = twdr;
if(control&0x01) passthrough_setup();
else console_setup();
break;
default:
break;
}
break;
default:
dest_register = 0xff;
//error - bail out
break;
}
// clear interrupt
}
//delay_ms(1);
}
// Never reached.
return(0);
}