keyboard: avr: Forward printer UART packets

This commit is contained in:
Markus Koch 2019-12-27 23:13:51 +01:00
parent 7269db9e0a
commit 7a7399a602

View File

@ -1,5 +1,6 @@
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define MATRIX_X(t) t##B
#define MATRIX_Y1(t) t##C
@ -13,6 +14,8 @@
#define BAUDRATE 19200
#define PRINTER_PACKET_SIZE 4
/* Keycode from include/linux/input.h */
#define KEY_RESERVED 0
#define KEY_ESC 1
@ -193,15 +196,48 @@ uint8_t matrix_state_last_vector[9] = {0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF};
uint8_t fn_active = 0;
/* UART forwarding */
#define RING_BUFFER_SIZE 256 // Important: must be 256 atm, when changed, you need to change code handling print_buffer_*.
volatile char ring_buffer[RING_BUFFER_SIZE];
volatile uint8_t ring_buffer_wr = 0;
volatile uint8_t ring_buffer_rd = 0;
#define UART_BYTE_READY (UCSRA & (1 << RXC))
void uart_putc(char c)
{
while (!(UCSRA & (1 << UDRE)));
UDR = c;
}
ISR(USART_RXC_vect)
{
if (UCSRA & (1 << RXC)) {
ring_buffer[ring_buffer_wr++] = UDR;// WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
}
}
void uart_forward_packets()
{
uint8_t size;
uint8_t i;
if (ring_buffer_wr > ring_buffer_rd)
size = ring_buffer_wr - ring_buffer_rd;
else
size = ring_buffer_rd - ring_buffer_wr;
if (size > PRINTER_PACKET_SIZE) {
uart_putc(0xFF);
uart_putc(0xAA);
for (i = 0; i < PRINTER_PACKET_SIZE; ++i) {
uart_putc(ring_buffer[ring_buffer_rd++]); // Will auto wrap at 256
}
}
}
void process_key_event(uint8_t col, uint8_t row, uint8_t state)
{
char c;
@ -269,16 +305,23 @@ int main()
MATRIX_Y2(DDR) &= ~MASK_Y2; /* Disable rows */
MATRIX_Y1(PORT) &= ~MASK_Y1; /* Pull row low when selected */
MATRIX_Y2(PORT) &= ~MASK_Y2; /* Pull row low when selected */
UCSRB = (1 << TXEN);
/* Set up UART */
UCSRA = 0;
UCSRB = (1 << TXEN) | (1 << RXEN) | (1 << RXCIE);
UCSRC = 0;
UBRRH = ((((F_CPU / BAUDRATE) / 16) - 1) >> 8);
UBRRL = (((F_CPU / BAUDRATE) / 16) - 1);
fn_active = 0;
sei();
/* Continuously scan matrix */
while (1) {
for (col = 0; col < 9; ++col) {
matrix_scan_col(col);
}
uart_forward_packets();
}
}