summaryrefslogtreecommitdiffstats
path: root/pit.c
blob: 12417669ac7b4b7d93b87ad5e3b48c8a5ef562aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * pit.c -- Periodic Interrupt Timer interface
 *
 * Copyright (C) 2017 Tomasz Kramkowski <tk@the-tk.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <arrlen.h>
#include <cm4.h>
#include <reg/gpio.h>
#include <reg/pit.h>
#include <reg/port.h>
#include <reg/sim.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "pit.h"
#include "usb/endpt1.h"
#include "usb/keycodes.h"

enum {
	T0_CYCLES = 5000,
};

struct gpio {
	uint32_t port;
	uint32_t gpio;
	uint32_t bit;
	unsigned char pin;
};
#define GPIO_PIN(port, pin) { \
	PORT##port##_BASE, \
	GPIO##port##_BASE, \
	BV((pin)), \
	(pin), \
}

static const struct gpio cols[] = {
	GPIO_PIN(B,  2),
	GPIO_PIN(B,  3),
	GPIO_PIN(B, 18),
	GPIO_PIN(B, 19),
	GPIO_PIN(C,  0),
	GPIO_PIN(C,  8),
	GPIO_PIN(C,  9),
	GPIO_PIN(C, 10),
	GPIO_PIN(C, 11),
};

static const struct gpio rows[] = {
	GPIO_PIN(D, 0),
	GPIO_PIN(D, 1),
	GPIO_PIN(D, 4),
	GPIO_PIN(D, 5),
	GPIO_PIN(D, 6),
	GPIO_PIN(D, 7),
	GPIO_PIN(C, 1),
	GPIO_PIN(C, 2),
};

/* COL AND ROW SWAPPED!!! */
static enum keycode keymap[ARRLEN(rows)][ARRLEN(cols)] = {
	{ /* S01 */ KEY_ESCAPE, KEY_1,		KEY_2,		KEY_3,		KEY_4,		KEY_5,		KEY_6,		KEY_7,		KEY_8, },
	{ /* S10 */ KEY_9,	KEY_0,		KEY_MINUS,	KEY_EQUAL,	KEY_NONE,	KEY_BACKSPACE,	KEY_GRAVE,	KEY_TAB,	KEY_Q, },
	{ /* S19 */ KEY_W,	KEY_E,		KEY_R,		KEY_T,		KEY_Y,		KEY_U,		KEY_I,		KEY_O,		KEY_P, },
	{ /* S28 */ KEY_LSQUARE, KEY_RSQUARE,	KEY_NONE,	KEY_DELETE,	KEY_CAPSLK,	KEY_A,		KEY_S,		KEY_D,		KEY_F, },
	{ /* S37 */ KEY_G,	KEY_H,		KEY_J,		KEY_K,		KEY_L,		KEY_SEMICOLON,	KEY_QUOTE,	KEY_BACKSLASH,	KEY_ENTER, },
	{ /* S46 */ KEY_PGUP,	KEY_LSHIFT,	KEY_NU_BACKSLASH, KEY_Z,	KEY_X,		KEY_C,		KEY_V,		KEY_B,		KEY_N, },
	{ /* S55 */ KEY_M,	KEY_COMMA,	KEY_PERIOD,	KEY_SLASH,	KEY_RSHIFT,	KEY_UP,		KEY_PGDN,	KEY_LCTRL,	KEY_LGUI, },
	{ /* S64 */ KEY_LALT,	KEY_SPACE,	KEY_RALT,	KEY_NONE,	KEY_RCTRL,	KEY_LEFT,	KEY_DOWN,	KEY_RIGHT,	KEY_NONE, },
};

void pit_setup(void)
{
	/* Set up ports (could be done with GPC registers */
	for (size_t i = 0; i < ARRLEN(cols); i++) {
		SET_BIT(GPIO_PDDR(cols[i].gpio), cols[i].pin);
		GPIO_PCOR(cols[i].gpio) = cols[i].bit;
		PORT_PCR(cols[i].port, cols[i].pin) = BV(PCR_SRE) | BV(PCR_DSE)
			| 1 << PCR_MUX;
	}
	for (size_t i = 0; i < ARRLEN(rows); i++) {
		UNSET_BIT(GPIO_PDDR(rows[i].gpio), rows[i].pin);
		PORT_PCR(rows[i].port, rows[i].pin) = BV(PCR_PFE) | BV(PCR_PE)
			| 1 << PCR_MUX;
	}

	/* Enable clock */
	SET_BIT(SIM_SCGC6, SCGC6_PIT);

	/* NVIC Enable interrupt */
	ISR_CLRPEND(68);
	ISR_SETENA(68);
	INTPRI(68) = 0x10;

	/* First column strobe */
	GPIO_PSOR(cols[0].gpio) = cols[0].bit;

	/* Set up PIT0 */
	PIT_MCR = 0;

	PIT_LDVAL(0) = T0_CYCLES - 1;
	PIT_TFLG(0) = BV(TFLG_TIF);
	PIT_TCTRL(0) = BV(TCTRL_TIE) | BV(TCTRL_TEN);
}

static uint8_t kstates[ARRLEN(cols)][ARRLEN(rows)] = { 0 };

static void key_state(int col, int row, bool state)
{
	uint8_t kstate;

	kstate = kstates[col][row] = kstates[col][row] << 1 | state;

	if (kstate == 0x7f)
		usb_endpt1_setkey(keymap[row][col], true);
	else if (kstate == 0x80)
		usb_endpt1_setkey(keymap[row][col], false);
}

void pit0_isr(void)
{
	static unsigned char col = 0;

	for (size_t row = 0; row < ARRLEN(rows); row++)
		key_state(col, row, !!(GPIO_PDIR(rows[row].gpio) & rows[row].bit));

	GPIO_PCOR(cols[col].gpio) = cols[col].bit;
	col = (col + 1) % ARRLEN(cols);
	GPIO_PSOR(cols[col].gpio) = cols[col].bit;

	if (col == 0)
		usb_endpt1_send();

	PIT_TFLG(0) = BV(TFLG_TIF);
}