Changeset 88a38a8 in avrstuff for kbd/pcw2hid/code/protocol.cpp
- Timestamp:
- Feb 7, 2023, 11:43:54 PM (16 months ago)
- Branches:
- main
- Children:
- 53cf5f5
- Parents:
- 8d96947
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kbd/pcw2hid/code/protocol.cpp
r8d96947 r88a38a8 1 1 #include <avr/io.h> 2 #include <avr/interrupt.h> 2 3 3 4 #include <stdbool.h> … … 11 12 #define PCW_DAT_A (1 << 1) 12 13 13 static uint8_t data; 14 static uint8_t address; 14 uint8_t matrix[16]; 15 15 16 static uint8_t oldv; 17 static uint8_t newv; 16 static uint16_t scanbyte() 17 { 18 uint8_t pulses = 0; 19 uint8_t oldv, newv; 20 uint16_t rxbuffer = 0; 18 21 19 static uint8_t pulses; 20 21 static uint8_t kmatrix[16]; 22 23 uint8_t scanbyte() 24 { 25 pulses = 0; 26 address = 0; 27 22 // This is a bit timing critical so don't be interrupted while we do it 23 cli(); 28 24 oldv = PCW_PIN; 29 25 30 for ( int i = 0; i < 2048; i++) {26 for (;;) { 31 27 newv = PCW_PIN; 32 28 … … 37 33 // We are not in sync, start over 38 34 pulses = 0; 39 address= 0;35 rxbuffer = 0; 40 36 } 41 37 if (((newv & PCW_DAT_A) == 0) && ((oldv & PCW_DAT_A) != 0)) { 42 pulses++;43 }44 } else if (pulses < 6) {45 // Receiving address (4 bits)46 if (((newv & PCW_CLK_A) == 0) && ((oldv & PCW_CLK_A) != 0)) {47 address = (address << 1) | ((newv & PCW_DAT_A) ? 1 : 0);48 38 pulses++; 49 39 } … … 51 41 // Receiving data (8 bits) 52 42 if (((newv & PCW_CLK_A) == 0) && ((oldv & PCW_CLK_A) != 0)) { 53 data = (data << 1) | ((newv & PCW_DAT_A) ? 1 : 0); 43 rxbuffer = (rxbuffer << 1); 44 if (newv & PCW_DAT_A) 45 rxbuffer |= 1; 54 46 pulses++; 55 }56 47 57 // If we reached the last bit, we can stop 58 if (pulses >= 10) { 59 kmatrix[address] = data; 60 return address; 48 // If we reached the last bit, we can stop 49 if (pulses == 14) { 50 // We can be interrupted again. 51 sei(); 52 return rxbuffer; 53 } 61 54 } 62 55 } … … 64 57 oldv = newv; 65 58 } 66 67 // TODO set up some error LED or something?68 return 255;69 59 } 70 60 71 extern "C" uint8_t*scanall()61 extern "C" void scanall() 72 62 { 73 uint8_t address; 74 75 do { 76 address = scanbyte(); 77 // } while ((address < 15) && ((kmatrix[15] & 0x80) != 0)); 78 } while ((address != 10) && ((kmatrix[15] & 0x80) != 0)); 79 // nothing interesting past row 10, so stop scanning there. 80 81 return kmatrix; 63 uint16_t rxbuffer; 64 65 for (int i = 0; i < 17; i++) { 66 rxbuffer = scanbyte(); 67 matrix[rxbuffer >> 8] = rxbuffer & 0xFF; 68 } 82 69 } 83 70 84 71 extern "C" void PCW_Init() { 85 72 // GrIP input 86 PCW_PORT = 0xFF; // Enable pull-ups87 PCW_DDR = 0; // Port as input73 PCW_PORT |= 0x3; // Enable pull-ups 74 PCW_DDR &= 0xFC; // Port as input 88 75 } 89 76
Note:
See TracChangeset
for help on using the changeset viewer.