Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
730 | woggle | 1 | /***************************************************************************** |
2 | * Copyright (C) 2009-2010 Peter "woggle" Mack, mac@denich.net * |
||
3 | * * |
||
4 | * see this fine thread on RCLine: * |
||
5 | * http://www.rclineforum.de/forum/thread.php?threadid=226786 * |
||
6 | * * |
||
7 | * This program is free software; you can redistribute it and/or modify * |
||
8 | * it under the terms of the GNU General Public License as published by * |
||
9 | * the Free Software Foundation; either version 2 of the License. * |
||
10 | * * |
||
11 | * This program is distributed in the hope that it will be useful, * |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
14 | * GNU General Public License for more details. * |
||
15 | * * |
||
16 | * You should have received a copy of the GNU General Public License * |
||
17 | * along with this program; if not, write to the * |
||
18 | * Free Software Foundation, Inc., * |
||
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
20 | * * |
||
21 | *****************************************************************************/ |
||
22 | |||
23 | #include <avr/io.h> |
||
24 | #include <avr/interrupt.h> |
||
25 | #include <avr/pgmspace.h> |
||
26 | #include <util/delay.h> |
||
27 | |||
28 | #include "main.h" // LEDs |
||
29 | #include "timer.h" // Keys |
||
30 | #include "lcd.h" |
||
31 | #include "jeti.h" |
||
32 | |||
33 | #define font8x8 |
||
34 | |||
35 | uint8_t JetiBuffer[32]; // 32 characters |
||
36 | volatile uint8_t JetiBufferReady; |
||
37 | |||
38 | |||
39 | //***************************************************************************** |
||
40 | // |
||
41 | ISR (USART1_RX_vect) |
||
42 | { |
||
43 | uint8_t stat; |
||
44 | uint8_t rh; |
||
45 | uint8_t rl; |
||
46 | static uint8_t jbp; |
||
47 | |||
48 | stat = UCSR1A; |
||
49 | rh = UCSR1B; |
||
50 | rl = UDR1; |
||
51 | |||
52 | if (stat & ((1 << FE1) | (1 << DOR1) | (1 << UPE1))) |
||
53 | { // discard buffer and start new on any error |
||
54 | JetiBufferReady = 0; |
||
55 | jbp = 0; |
||
56 | //1234567890123456 |
||
57 | lcd_printpj_at (0, 3, PSTR(" Communication "), 0); |
||
58 | lcd_printpj_at (0, 4, PSTR(" Error "), 0); |
||
59 | LED6_TOGGLE; |
||
60 | } |
||
61 | else if ((rh & (1 << RXB81)) == 0) |
||
62 | { // control |
||
63 | if (rl == 0xfe) |
||
64 | { // start condition |
||
65 | JetiBufferReady = 0; |
||
66 | jbp = 0; |
||
67 | LED1_ON; |
||
68 | } |
||
69 | else if (rl == 0xff) |
||
70 | { // stop condition |
||
71 | JetiBufferReady = 1; |
||
72 | LED1_OFF; |
||
73 | } |
||
74 | } |
||
75 | else |
||
76 | { // data |
||
77 | if (jbp < 32) |
||
78 | { |
||
79 | JetiBuffer[jbp++] = rl; |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | //***************************************************************************** |
||
85 | // |
||
86 | void JETI_Init (void) |
||
87 | { |
||
88 | DDRD &= ~(1 << DDD3); // set TXD1 pin as input |
||
89 | PORTD &= ~(1 << PORTD3); // disable pullup on TXD1 pin |
||
90 | |||
91 | // set clock divider |
||
92 | #undef BAUD |
||
93 | #define BAUD 9600 |
||
94 | #include <util/setbaud.h> |
||
95 | UBRR1H = UBRRH_VALUE; |
||
96 | UBRR1L = UBRRL_VALUE; |
||
97 | |||
98 | #if USE_2X |
||
99 | UCSR1A |= (1 << U2X1); // enable double speed operation |
||
100 | #else |
||
101 | UCSR1A &= ~(1 << U2X1); // disable double speed operation |
||
102 | #endif |
||
103 | |||
104 | // set 9O1 |
||
105 | UCSR1C = (1 << UPM11) | (1 << UPM10) | (1 << UCSZ11) | (1 << UCSZ10); |
||
106 | UCSR1B = (1 << UCSZ12); |
||
107 | |||
108 | // flush receive buffer |
||
109 | while ( UCSR1A & (1 << RXC1) ) UDR1; |
||
110 | } |
||
111 | |||
112 | //***************************************************************************** |
||
113 | // disable the txd pin of usart |
||
114 | void JETI_DisableTXD (void) |
||
115 | { |
||
116 | UCSR1B &= ~(1 << TXEN1); // disable TX} |
||
117 | } |
||
118 | |||
119 | //***************************************************************************** |
||
120 | // enable the txd pin of usart |
||
121 | void JETI_EnableTXD (void) |
||
122 | { |
||
123 | UCSR1B |= (1 << TXEN1); // enable TX |
||
124 | } |
||
125 | |||
126 | |||
127 | //***************************************************************************** |
||
128 | // |
||
129 | void JETI_putw (uint16_t c) |
||
130 | { |
||
131 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
132 | UCSR1B &= ~(1 << TXB81); |
||
133 | if (c & 0x0100) |
||
134 | { |
||
135 | UCSR1B |= (1 << TXB81); |
||
136 | } |
||
137 | UDR1 = c; |
||
138 | } |
||
139 | |||
140 | //***************************************************************************** |
||
141 | // |
||
142 | void JETI_putc (uint8_t c) |
||
143 | { |
||
144 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
145 | // UCSRB &= ~(1 << TXB8); |
||
146 | UCSR1B |= (1 << TXB81); |
||
147 | UDR1 = c; |
||
148 | } |
||
149 | |||
150 | //***************************************************************************** |
||
151 | // |
||
152 | void JETI_puts (char *s) |
||
153 | { |
||
154 | while (*s) |
||
155 | { |
||
156 | JETI_putc (*s); |
||
157 | s++; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | //***************************************************************************** |
||
162 | // |
||
163 | void JETI_puts_p (const char *s) |
||
164 | { |
||
165 | while (pgm_read_byte(s)) |
||
166 | { |
||
167 | JETI_putc (pgm_read_byte(s)); |
||
168 | s++; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | //***************************************************************************** |
||
173 | // |
||
174 | void JETI_put_start (void) |
||
175 | { |
||
176 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
177 | UCSR1B &= ~(1 << TXB81); |
||
178 | UDR1 = 0xFE; |
||
179 | } |
||
180 | |||
181 | //***************************************************************************** |
||
182 | // |
||
183 | void JETI_put_stop (void) |
||
184 | { |
||
185 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
186 | UCSR1B &= ~(1 << TXB81); |
||
187 | UDR1 = 0xFF; |
||
188 | } |
||
189 | |||
190 | //***************************************************************************** |
||
191 | // |
||
192 | void jeti (void) |
||
193 | { |
||
194 | uint8_t key; |
||
195 | uint8_t i; |
||
196 | |||
197 | // enable Rx |
||
198 | UCSR1B |= (1 << RXEN1); |
||
199 | UCSR1B |= (1 << RXCIE1); |
||
200 | |||
201 | lcd_cls (); |
||
202 | lcd_printp_at (0, 0, PSTR("Jeti Box Display"), 0); |
||
203 | lcd_printpns_at (0, 7, PSTR(" \x1a \x1b \x1c \x1d"), 0); |
||
204 | |||
205 | #ifdef font8x8 |
||
206 | lcd_line(0, 21, 127, 21, 1); |
||
207 | lcd_line(0, 22, 127, 22, 1); |
||
208 | lcd_line(0, 40, 127, 40, 1); |
||
209 | lcd_line(0, 41, 127, 41, 1); |
||
210 | #else |
||
211 | lcd_rect(10, 22, 98, 18, 1); |
||
212 | #endif |
||
213 | |||
214 | do |
||
215 | { |
||
216 | if (JetiBufferReady) |
||
217 | { |
||
218 | JETI_EnableTXD(); |
||
219 | LED2_ON; |
||
220 | for (i = 0; i < 16; i++) |
||
221 | { |
||
222 | #ifdef font8x8 |
||
223 | lcd_putc_jeti(i, 3, JetiBuffer[i], 0); |
||
224 | #else |
||
225 | lcd_putc (2 + i, 3, JetiBuffer[i], 0); |
||
226 | #endif |
||
227 | } |
||
228 | for (i = 0; i < 16; i++) |
||
229 | { |
||
230 | #ifdef font8x8 |
||
231 | lcd_putc_jeti(i, 4, JetiBuffer[i + 16], 0); |
||
232 | #else |
||
233 | lcd_putc (2 + i, 4, JetiBuffer[i + 16], 0); |
||
234 | #endif |
||
235 | } |
||
236 | JetiBufferReady = 0; // invalidate buffer |
||
237 | LED2_OFF; |
||
238 | |||
239 | _delay_ms (1); // |
||
240 | // Writing to the display takes aprox. 5.8 ms @ 7 MHz and 3.2 ms @ 20 MHz. |
||
241 | // With the additional 4 ms we had a 10 ms delay. |
||
242 | // 10 ms works perfect with the MUI30 and the MT125 |
||
243 | // But not with the TU transceiver module. |
||
244 | |||
245 | key = get_key_short ((1 << KEY_MINUS) | (1 << KEY_PLUS) | (1 << KEY_ESC) | (1 << KEY_ENTER)); |
||
246 | key = (key << 1) | (key >> 3); |
||
247 | key = (~key) & 0xf0; |
||
248 | |||
249 | JETI_putw((uint16_t) key); |
||
250 | _delay_ms (1); |
||
251 | JETI_DisableTXD(); |
||
252 | } |
||
253 | } |
||
254 | while (!get_key_long (1 << KEY_ESC)); |
||
255 | get_key_press(KEY_ALL); |
||
256 | |||
257 | // disable Rx |
||
258 | UCSR1B &= ~(1 << RXCIE1); |
||
259 | UCSR1B &= ~(1 << RXEN1); |
||
260 | } |