source: avrstuff/kbd/xtk/code/main.c@ ea32107

main
Last change on this file since ea32107 was ea32107, checked in by Adrien Destugues <pulkomandy@…>, 10 years ago

kbd: working XTK on ATtiny2313

  • Move more stuff to common.mk and handle multicpu support
  • Fix some pins mixups to get things working

git-svn-id: svn://pulkomandy.tk/avrstuff@113 c6672c3c-f6b6-47f9-9001-1fd6b12fecbe

  • Property mode set to 100644
File size: 2.5 KB
Line 
1#include <avr/io.h>
2#include <avr/pgmspace.h>
3#include <util/delay.h>
4
5#include "../../../libs/ps2_keyboard/ps2_keyboard.h"
6
7static volatile uint8_t key;
8
9const uint8_t at2xt[128] PROGMEM = {
10// 0 1 2 3 4 5 6 7 8 9 A B C D E F
11'?' ,0x43,'?' ,0x3F,0x3D,0x3B,0x3C,0x58,0x64,0x44,0x42,0x40,0x3E,0x0F,0x29,'?', // 0
120x65,0x38,0x2A,0x70,0x1D,0x10,0x02,'?' ,0x66,0x71,0x2C,0x1F,0x1E,0x11,0x03,0x5B, // 1
130x67,0x2E,0x2D,0x20,0x12,0x05,0x04,0x5C,0x68,0x39,0x2F,0x21,0x14,0x13,0x06,0x5D, // 2
140x69,0x31,0x30,0x23,0x22,0x15,0x07,0x5E,0x6A,0x72,0x32,0x24,0x16,0x08,0x09,0x5F, // 3
150x6B,0x33,0x25,0x17,0x18,0x0B,0x0A,0x6C,'?' ,0x34,0x35,0x26,0x27,0x19,0x0C,'?', // 4
160x6D,0x73,0x28,0x74,0x1A,0x0D,'?' ,'?' ,0x3A,0x36,0x1C,0x1B,0x75,0x2B,0x63,0x76, // 5
17'?' ,0x56,0x77,'?' ,0x79,'?' ,0x0E,0x7B,'?' ,0x4F,0x7D,0x4B,0x47,0x7E,'?' ,0x6F, // 6
180x52,0x53,0x50,0x4C,0x4D,0x48,0x01,0x45,0x57,0x4E,0x51,0x4A,0x37,0x49,0x46,'?' // 7
19};
20
21// 83 > 41 (F7)
22// 84 > 54 (SysRq)
23
24void callback()
25{
26 uint8_t key_code = 0;
27 key_code = read_char(); // TODO this function is blocking. Can it disturb main?
28
29 key = pgm_read_byte(&(at2xt[key_code]));
30 if(release)
31 key |= 0x80;
32}
33
34
35int main() {
36 key = 0xAA;
37 init_keyboard(); // PS/2 KBD handler
38
39
40#ifdef __AVR_ATtiny2313__
41#define PORTXT PORTD
42#define DDRXT DDRD
43#define PINXT PIND
44 static const int PCLK = (1<<PD0);
45 static const int PDAT = (1<<PD1);
46#else
47#define PORTXT PORTB
48#define DDRXT DDRB
49#define PINXT PINB
50 static const int PCLK = (1<<PB1);
51 static const int PDAT = (1<<PB2);
52#endif
53
54 static const int delay = 25;
55
56 // XT init - configure pins directions
57 PORTXT &= ~(PCLK | PDAT);
58 DDRXT &= ~(PCLK | PDAT); // both pins as inputs (floating)
59
60 DDRB |= (1<<PB2); // LED
61
62 uint8_t k;
63 while(1) {
64 PORTB ^= (1<<PB2); // LED
65
66 while ((PINXT & (PDAT|PCLK)) != (PDAT|PCLK))
67 ; // Wait for PC to be ready to receive data
68
69 while(key == 0xFF)
70 ; // Wait for data to send
71
72 k = key; // local copy so we can receive another code from PS/2
73 // before we're done sending this one.
74 key = 0xFF;
75
76 // SEND START BIT
77 DDRXT &= ~PDAT; // DAT HI
78 _delay_us(delay);
79 DDRXT |= PCLK; // CLK LOW
80 _delay_us(delay);
81 DDRXT &= ~PCLK; // CLK HI
82
83 for(int i = 0; i < 8; i++)
84 {
85 if (k & 1)
86 DDRXT &= ~PDAT;
87 else
88 DDRXT |= PDAT;
89
90 _delay_us(delay);
91 DDRXT |= PCLK; // CLK LOW
92 _delay_us(delay);
93 DDRXT &= ~PCLK; // CLK HI
94
95 k >>= 1;
96 }
97
98 DDRXT &= ~PDAT; // DAT HI
99
100 _delay_us(delay * 2);
101 }
102
103 return 0;
104}
105
Note: See TracBrowser for help on using the repository browser.