/
buzzIface.c
373 lines (312 loc) · 7 KB
/
buzzIface.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* ============================================================================
*
* Filename: buzzIface.c
*
* Description: this is the first test
*
* Version: 0.0
* Revision: none
* Compiler: gcc
*
* Author: Johannes Steinmetz (js), john ät tuxcode dot org
*
* ============================================================================
*/
#include <stdlib.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
/*#include <avr/pgmspace.h>*/
#include <avr/sleep.h>
#include <util/delay.h>
#include "usart.h"
#include "buzzIface.h"
/* functions: */
//uint8_t wait_keys_blocking(uint8_t keymask /* TODO timeout?,*/ );
uint8_t send_uart_key(uint8_t key , uint8_t is_key_up_ev);
static void PCI_init(void );
static void phasentimer_init(void);
static uint8_t do_command (uint8_t * commline);
/* GLOBALZ */
volatile uint8_t key_buf;
#define MASKI_B (0x03)
#define MASKI_D (0xd0)
#define SENDPLACESIZE (8)
#define DEBUG_SENDPLACE_ON
#undef KEYS_HAVE_EXTERN_PULLUP
volatile uint8_t changedmask;
volatile uint8_t hasunicorn;
volatile uint8_t pendingmask;
#define MAXUNICORN (6)
volatile uint8_t phasechanged[MAXUNICORN];
volatile uint8_t unicorn; //index to last free in phasechanged
volatile uint8_t sendline[SENDPLACESIZE] ;
volatile uint8_t sendplace ;
struct channel cz[5];
ISR(PCINT0_vect)
{
uint8_t act ;
act = ~PINB & (MASKI_B);
if (act){ //one ore some of allowed ins are high
switch (act){
case (1 << 0): //Chan A key is B0 --> PCINT0
case (1 << 1): //Chan B key is B1 --> PCINT1
PCMSK0 &= ~(act);
changedmask |= act;
if (act & ~(hasunicorn)){
phasechanged[unicorn++] = act;
hasunicorn |= act ;
}
break;
default :
sendline[sendplace++] = 'G'; //invalid
}
if(unicorn >= MAXUNICORN) USART_puts("unicornl> MAX\n\r");
}
}
ISR(PCINT2_vect) {
uint8_t act,c ;
act = ~PIND & MASKI_D ;
if (act){
switch (act){
case (1 << 6): //Chan C key is D6 --> PCINT22
c=CHAN_C;
PCMSK2 &= ~(act);
break;
case (1 << 7): //Chan D key is D7 --> PCINT23
c=CHAN_D;
PCMSK2 &= ~(act);
break;
case (1<<4): //Chan E key is D4 --> PCINT20
c=CHAN_E;
PCMSK2 &= ~(act);
break;
default :
c= 0x0;
}
if (c )
{
changedmask |= (1<<c);
if (c & ~(hasunicorn)) {
phasechanged[unicorn++] = c;
hasunicorn |= c;
}
if(unicorn >= MAXUNICORN) USART_puts("unicornl> MAX\n\r");
}
}
}
ISR(TIMER0_COMPA_vect) {
uint8_t j ,c;
if(!changedmask) {//es gab keine flanken
/* flanken wieder erlauben zu finden*/
for (j=0; j <8 ; j++){
c = phasechanged[j];
if (c)
send_uart_key(c , (*(cz[c-1].keyport) & (1 << cz[c-1].keypin) ) ) ;
phasechanged[j] =0;
}
unicorn =0;
hasunicorn =0;
}
//flanken wieder suchen:
changedmask = 0;
PCMSK0 |= (MASKI_B) ;
PCMSK2 |= (MASKI_D) ;
}
static inline void hardware_init(void)
{
uint8_t i ;
cz[0].led1port = &PORTC ;
cz[0].led1pin = PIN0;
cz[0].led2port = &PORTC;
cz[0].led2pin = PIN1;
cz[0].keyport = &PINB;
cz[0].keypin = PIN0;
cz[1].led1port = &PORTC ;
cz[1].led1pin = PIN2;
cz[1].led2port = &PORTC;
cz[1].led2pin = PIN3;
cz[1].keyport = &PINB;
cz[1].keypin = PIN1;
cz[2].led1port = &PORTC ;
cz[2].led1pin = PIN4;
cz[2].led2port = &PORTC;
cz[2].led2pin = PIN5;
cz[2].keyport = &PIND;
cz[2].keypin = PIN6;
cz[3].led1port = &PORTD ;
cz[3].led1pin = PIN5;
cz[3].led2port = &PORTB;
cz[3].led2pin = PIN2;
cz[3].keyport = &PIND;
cz[3].keypin = PIN7;
cz[4].led1port = &PORTD ;
cz[4].led1pin = PIN2;
cz[4].led2port = &PORTD;
cz[4].led2pin = PIN3;
cz[4].keyport = &PIND;
cz[4].keypin = PIN4;
for(i=0; i< CHAN_NUM ; i++){
/*leds get outputs */
*(port2ddr(cz[i].led1port)) |= cz[i].led1pin ;
*(port2ddr(cz[i].led2port)) |= cz[i].led2pin ;
/* initially set to 1 (led off) */
*(cz[i].led1port) |= cz[i].led1pin ;
*(cz[i].led2port) |= cz[i].led2pin ;
/* keys inputs */
*(pin2ddr(cz[i].keyport)) &= ~(cz[i].keypin) ;
#ifndef KEYS_HAVE_EXTERN_PULLUP
*(pin2port(cz[i].keyport)) |= cz[i].keypin;
#else
*(pin2port(cz[i].keyport)) &= ~(cz[i].keypin);
#endif
}
sendplace = 0 ;
USART_Init();
PCI_init();
phasentimer_init();
}
static inline void phasentimer_init()
{
//20MHz / 1024 = 19 KHz
//prescaler
//(1.0/19) * 18 = 0.9473684 -> phasenwechsel alle ~ millisekunde
TCCR0A = 0 | (1 << WGM01); // CTC Modus
TCCR0B = 0 | (1<< CS02) | (1<< CS00); //Prescale 1024
OCR0A = 20; //0x09 ; //9 ist 18/2 s.o.
TIMSK0 = 0 | (1<<OCIE0A); //IRQ CTM erlauben
return;
}
static inline void PCI_init()
{
PCIFR = 0; //sanity
PCMSK0 = MASKI_B;
PCMSK2 = MASKI_D;
PCICR = (1<<PCIE0) | (1<<PCIE2); //dont have keys at port c ...
return;
}
uint8_t set_led_state(ledid_t led_id, uint8_t brightness)
{
uint8_t ch=led_id.channel;
switch (led_id.led){
case (0) :
if (brightness) {
USART_puts("on(l0)\r\n)");
*(cz[ch].led2port) &= ~(cz[ch].led1pin) ;
} else {
USART_puts("off(l0)\r\n)");
*(cz[ch].led1port) |= cz[ch].led1pin ;
}
break ;
case (1) :
if (brightness) {
USART_puts("on(l1)\r\n)");
*(cz[ch].led2port) &= ~(cz[ch].led2pin) ;
} else {
USART_puts("off(l1)\r\n)");
*(cz[ch].led2port) |= cz[ch].led2pin ;
}
break ;
default:
return ERROR_LED;
}
return 0;
}
uint8_t send_uart_key(uint8_t key ,uint8_t is_key_up_ev){
char c ;
char buf[5] = "K%\n\r";
if ( is_key_up_ev ){
//key down event get small k
buf[0] = 'k';
}
switch (key){
case CHAN_A:
c = 'A';
break;
case CHAN_B:
c = 'B';
break;
case CHAN_C:
c = 'C';
break;
case CHAN_D:
c = 'D';
break;
case CHAN_E:
c = 'E';
break;
default:
return ERROR_IKEY;
}
buf[1] = c;
USART_puts(buf);
return 0;
}
static inline uint8_t do_setled_command(uint8_t * commline){
ledid_t lid;
switch (commline[0]){
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
USART_puts("\r\nsetled ");
USART_putc(commline[0]);
lid.channel = commline[0] - 'A';
break;
default:
return ERROR_COMCHAN ;
}
switch (commline[1]){
case '1':
case '2':
USART_putc(commline[1]);
lid.led = commline[1] - '1';
break;
default:
return ERROR_COMLED ;
}
return set_led_state (lid, commline[2]) ;
}
static uint8_t do_command (uint8_t * commline){
switch (commline[0]){
case 'L':
return do_setled_command(commline+1);
default:
return ERROR_COMAND ;
}
return 0; //useless
}
int main(void)
{
uint8_t commandbuf[8];
uint8_t commidx=0 , i=0 ;
hardware_init();
sei();
//------------------------------------
// USART_puts("Test");
// USART_puts("\n\r");
for(;;){ //ever
/* PART 1 :check if host tells us to do something:*/
i = uart_getc_nb(&commandbuf[commidx]);
if(i){
if (commandbuf[commidx] == 'Q'){
//befehlszeile zu ende :-)
commandbuf[commidx] = 0 ;
USART_puts(commandbuf); //DEBUG
do_command(commandbuf);
commidx = 0;
commandbuf[0] = 0 ;
}else{
commidx ++ ;
if(commidx >= 8 ){
commidx = 0;
commandbuf[0] = 0 ;
}
}
}
}
/* never gets here*/
return 0;
}