Changeset 88a38a8 in avrstuff for kbd/pcw2hid/code/protocol.cpp


Ignore:
Timestamp:
Feb 7, 2023, 11:43:54 PM (16 months ago)
Author:
PulkoMandy <pulkomandy@…>
Branches:
main
Children:
53cf5f5
Parents:
8d96947
Message:

pcw2hid: first working version

Remaining problem: ESC seems to be pressed with alt and shift, I don't
see why. Probqbly should have a key acting as numlock, otherwise there's
no way to have cursor keys...

This message is typed with a PCW keyboard :D

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kbd/pcw2hid/code/protocol.cpp

    r8d96947 r88a38a8  
    11#include <avr/io.h>
     2#include <avr/interrupt.h>
    23
    34#include <stdbool.h>
     
    1112#define PCW_DAT_A  (1 << 1)
    1213
    13 static uint8_t data;
    14 static uint8_t address;
     14uint8_t matrix[16];
    1515
    16 static uint8_t oldv;
    17 static uint8_t newv;
     16static uint16_t scanbyte()
     17{
     18        uint8_t pulses = 0;
     19        uint8_t oldv, newv;
     20        uint16_t rxbuffer = 0;
    1821
    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();
    2824        oldv = PCW_PIN;
    2925
    30         for (int i = 0; i < 2048; i++) {
     26        for (;;) {
    3127                newv = PCW_PIN;
    3228
     
    3733                                // We are not in sync, start over
    3834                                pulses = 0;
    39                                 address = 0;
     35                                rxbuffer = 0;
    4036                        }
    4137                        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);
    4838                                pulses++;
    4939                        }
     
    5141                        // Receiving data (8 bits)
    5242                        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;
    5446                                pulses++;
    55                         }
    5647
    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                                }
    6154                        }
    6255                }
     
    6457                oldv = newv;
    6558        }
    66 
    67         // TODO set up some error LED or something?
    68         return 255;
    6959}
    7060
    71 extern "C" uint8_t* scanall()
     61extern "C" void scanall()
    7262{
    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        }
    8269}
    8370
    8471extern "C" void PCW_Init() {
    8572        // GrIP input
    86         PCW_PORT = 0xFF; // Enable pull-ups
    87         PCW_DDR = 0;  // Port as input
     73        PCW_PORT |= 0x3; // Enable pull-ups
     74        PCW_DDR &= 0xFC;  // Port as input
    8875}
    8976
Note: See TracChangeset for help on using the changeset viewer.