1 | /* Amiga Keyboard reading on Atmel AVR
|
---|
2 | * Copyright 2010, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
3 | * Distributed under the terms of the MIT Licence */
|
---|
4 |
|
---|
5 | #define F_CPU 16000000UL
|
---|
6 |
|
---|
7 | #include <avr/io.h>
|
---|
8 | #include <avr/interrupt.h>
|
---|
9 | #include <avr/pgmspace.h>
|
---|
10 | #include <util/delay.h>
|
---|
11 |
|
---|
12 | #include "keymap.h"
|
---|
13 |
|
---|
14 |
|
---|
15 | //PIN configuration
|
---|
16 | #define AK_CLK PD3 /* Also INT1 */
|
---|
17 | #define AK_PORT PIND
|
---|
18 | #define AK_DATA PD4
|
---|
19 |
|
---|
20 | volatile uint8_t kbd_data;
|
---|
21 | volatile uint8_t char_waiting;
|
---|
22 | uint8_t started;
|
---|
23 | uint8_t bit_count;
|
---|
24 | uint8_t shift;
|
---|
25 |
|
---|
26 |
|
---|
27 | // Notify the keyboard we got the last char right
|
---|
28 | inline void acknowledge_char()
|
---|
29 | {
|
---|
30 | // We have to pull down the "DATA" line
|
---|
31 |
|
---|
32 | // Set it as an input
|
---|
33 | DDRD |= (1<<AK_DATA);
|
---|
34 |
|
---|
35 | // Down for 85 us
|
---|
36 | PORTD &= ~(1<<AK_DATA);
|
---|
37 | _delay_us(85);
|
---|
38 |
|
---|
39 | // Up again to let the keybord talk
|
---|
40 | PORTD |= (1<<AK_DATA);
|
---|
41 |
|
---|
42 | // And it's now an input again
|
---|
43 | DDRD &= ~(1<<AK_DATA);
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | // Interrupt vector - Triggered when there is activity on the clock line
|
---|
48 | ISR(INT1_vect)
|
---|
49 | {
|
---|
50 | PORTD ^= (1<<PD6);
|
---|
51 |
|
---|
52 | //make sure clock line is low, if not ignore this transition
|
---|
53 | if(AK_PORT & (1<<AK_CLK)){
|
---|
54 | return;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // First bit, reset everything then continue below
|
---|
58 | if(!started){
|
---|
59 | started = 1;
|
---|
60 | bit_count = 0;
|
---|
61 | kbd_data = 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if(bit_count < 8) { //we started, read in the new bit
|
---|
65 | //put a 1 in the right place of kdb_data if PC2 is high, leave
|
---|
66 | //a 0 otherwise
|
---|
67 | if(!(AK_PORT & (1<<AK_DATA))) kbd_data |= (128>>bit_count);
|
---|
68 | bit_count++;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if(bit_count >= 8)
|
---|
72 | { // enough bits
|
---|
73 | started = 0;
|
---|
74 | bit_count = 0;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | if(kbd_data == 0xF0){ //release code
|
---|
78 | release = 1;
|
---|
79 | kbd_data = 0;
|
---|
80 | return;
|
---|
81 | } else if (kbd_data == 0x12) { //hanlde shift key
|
---|
82 | if(release == 0){
|
---|
83 | shift = 1;
|
---|
84 | } else {
|
---|
85 | shift = 0;
|
---|
86 | release = 0;
|
---|
87 | }
|
---|
88 | return;
|
---|
89 | } else { //not a special character
|
---|
90 | if(release){ //we were in release mode - exit release mode
|
---|
91 | release = 0;
|
---|
92 | //ignore that character
|
---|
93 | } else {
|
---|
94 | char_waiting = 1;
|
---|
95 | }
|
---|
96 | }*/
|
---|
97 | char_waiting=1;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | char ak_scancode_to_ascii(uint8_t data){
|
---|
103 | char to_ret = pgm_read_byte(&(keymap[data])); //grab character from array
|
---|
104 | if(shift) to_ret -= 0x20;
|
---|
105 | return to_ret;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | uint8_t ak_read_scancode(){
|
---|
110 | while(!char_waiting);
|
---|
111 |
|
---|
112 | while (AK_PORT & (1<<AK_CLK) == 0);
|
---|
113 | _delay_us(30);
|
---|
114 | acknowledge_char();
|
---|
115 |
|
---|
116 | char_waiting = 0;
|
---|
117 | return kbd_data;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | void ak_init_keyboard(){
|
---|
122 | started = 0;
|
---|
123 | kbd_data = 0;
|
---|
124 | bit_count = 0;
|
---|
125 |
|
---|
126 | //make AK_CLK input pin
|
---|
127 | DDRD &= ~(1<<AK_CLK);
|
---|
128 | //turn on pullup resistor
|
---|
129 | AK_PORT |= (1<<AK_CLK);
|
---|
130 |
|
---|
131 | // Interrupt configuration
|
---|
132 | // We use INT1 falling edge
|
---|
133 | MCUCR |= (1<<ISC11);
|
---|
134 | MCUCR &= ~(1<<ISC10);
|
---|
135 |
|
---|
136 | // Enable INT1
|
---|
137 | GIMSK |= (1<<INT1);
|
---|
138 |
|
---|
139 | // Enable interrupts
|
---|
140 | sei();
|
---|
141 |
|
---|
142 | acknowledge_char();
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|