-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrp2040_button_state.cc
275 lines (221 loc) · 7.55 KB
/
rp2040_button_state.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//---------------------------------------------------------------------------
#include "rp2040_button_state.h"
#include "javelin/mem.h"
#include "javelin/split/split.h"
#include <hardware/gpio.h>
#include <hardware/structs/ioqspi.h>
#include <hardware/structs/sio.h>
#include <hardware/sync.h>
#include <hardware/timer.h>
#include JAVELIN_BOARD_CONFIG
//---------------------------------------------------------------------------
#if JAVELIN_BUTTON_MATRIX
#if JAVELIN_SPLIT
#if defined(JAVELIN_SPLIT_IS_LEFT)
#if JAVELIN_SPLIT_IS_LEFT
#define COLUMN_PINS LEFT_COLUMN_PINS
#define COLUMN_PIN_MASK LEFT_COLUMN_PIN_MASK
#define ROW_PINS LEFT_ROW_PINS
#define ROW_PIN_MASK LEFT_ROW_PIN_MASK
#define KEY_MAP LEFT_KEY_MAP
const size_t COLUMN_PIN_COUNT = sizeof(LEFT_COLUMN_PINS);
const size_t ROW_PIN_COUNT = sizeof(LEFT_ROW_PINS);
#else // JAVELIN_SPLIT_IS_LEFT
#define COLUMN_PINS RIGHT_COLUMN_PINS
#define COLUMN_PIN_MASK RIGHT_COLUMN_PIN_MASK
#define ROW_PINS RIGHT_ROW_PINS
#define ROW_PIN_MASK RIGHT_ROW_PIN_MASK
#define KEY_MAP RIGHT_KEY_MAP
const size_t COLUMN_PIN_COUNT = sizeof(RIGHT_COLUMN_PINS);
const size_t ROW_PIN_COUNT = sizeof(RIGHT_ROW_PINS);
#endif // JAVELIN_SPLIT_IS_LEFT
#else // defined(JAVELIN_SPLIT_IS_LEFT)
auto COLUMN_PINS = LEFT_COLUMN_PINS;
auto COLUMN_PIN_MASK = LEFT_COLUMN_PIN_MASK;
auto ROW_PINS = LEFT_ROW_PINS;
auto ROW_PIN_MASK = LEFT_ROW_PIN_MASK;
auto KEY_MAP = LEFT_KEY_MAP;
const size_t COLUMN_PIN_COUNT = sizeof(LEFT_COLUMN_PINS);
const size_t ROW_PIN_COUNT = sizeof(LEFT_ROW_PINS);
#endif // defined(JAVELIN_SPLIT_IS_LEFT)
#else // JAVELIN_SPLIT
const size_t COLUMN_PIN_COUNT = sizeof(COLUMN_PINS);
const size_t ROW_PIN_COUNT = sizeof(ROW_PINS);
#endif
#endif // JAVELIN_BUTTON_MATRIX
#if JAVELIN_BUTTON_TOUCH
uint32_t touchPadThreshold[sizeof(BUTTON_TOUCH_PINS)];
#if !defined(JAVELIN_TOUCH_CALIBRATION_COUNT)
#define JAVELIN_TOUCH_CALIBRATION_COUNT 5
#endif
#endif
//---------------------------------------------------------------------------
void Rp2040ButtonState::Initialize() {
#if JAVELIN_BUTTON_MATRIX
#if JAVELIN_SPLIT
#if !defined(JAVELIN_SPLIT_IS_LEFT)
if (!Split::IsLeft()) {
COLUMN_PINS = RIGHT_COLUMN_PINS;
COLUMN_PIN_MASK = RIGHT_COLUMN_PIN_MASK;
ROW_PINS = RIGHT_ROW_PINS;
ROW_PIN_MASK = RIGHT_ROW_PIN_MASK;
KEY_MAP = RIGHT_KEY_MAP;
}
#endif
#endif
gpio_init_mask(COLUMN_PIN_MASK | ROW_PIN_MASK);
gpio_set_dir_masked(COLUMN_PIN_MASK | ROW_PIN_MASK, ROW_PIN_MASK);
for (size_t i = 0; i < COLUMN_PIN_COUNT; ++i) {
gpio_pull_up(COLUMN_PINS[i]);
}
gpio_put_masked(ROW_PIN_MASK, ROW_PIN_MASK);
#endif
#if JAVELIN_BUTTON_PINS
gpio_init_mask(BUTTON_PIN_MASK);
gpio_set_dir_masked(BUTTON_PIN_MASK, 0);
for (const uint8_t pinAndPolarity : BUTTON_PINS) {
const uint8_t pin = pinAndPolarity & 0x7f;
if (pin == 0x7f) {
continue;
}
if (pinAndPolarity >> 7) {
gpio_pull_down(pin);
} else {
gpio_pull_up(pin);
}
}
#endif
#if JAVELIN_BUTTON_TOUCH
gpio_init_mask(BUTTON_TOUCH_PIN_MASK);
for (const uint8_t pin : BUTTON_TOUCH_PINS) {
gpio_disable_pulls(pin);
gpio_set_drive_strength(pin, GPIO_DRIVE_STRENGTH_12MA);
}
gpio_set_dir_masked(BUTTON_TOUCH_PIN_MASK, BUTTON_TOUCH_PIN_MASK);
gpio_put_masked(BUTTON_TOUCH_PIN_MASK, BUTTON_TOUCH_PIN_MASK);
#endif
#if JAVELIN_BUTTON_TOUCH
for (size_t i = 0; i < 4; ++i) {
uint32_t counters[sizeof(BUTTON_TOUCH_PINS)];
ReadTouchCounters(counters);
for (size_t j = 0; j < sizeof(BUTTON_TOUCH_PINS); ++j) {
touchPadThreshold[j] += counters[j];
}
}
for (size_t j = 0; j < sizeof(BUTTON_TOUCH_PINS); ++j) {
touchPadThreshold[j] =
touchPadThreshold[j] * (int)(BUTTON_TOUCH_THRESHOLD * 256) >> 10;
}
#endif
}
#if defined(BOOTSEL_BUTTON_INDEX)
// Picoboard has a button attached to the flash CS pin, which the bootrom
// checks, and jumps straight to the USB bootcode if the button is pressed
// (pulling flash CS low). We can check this pin in by jumping to some code in
// SRAM (so that the XIP interface is not required), floating the flash CS
// pin, and observing whether it is pulled low.
//
// This doesn't work if others are trying to access flash at the same time,
// e.g. XIP streamer, or the other core.
static bool __no_inline_not_in_flash_func(isBootSelButtonPressed)() {
const int CS_PIN_INDEX = 1;
// Must disable interrupts, as interrupt handlers may be in flash, and flash
// access it about to be disabled temporarily.
const uint32_t flags = save_and_disable_interrupts();
// Set chip select to Hi-Z
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_LOW << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
// Note that no sleep function in flash can be called right now.
// This is a ~10us wait.
for (int i = 0; i < 125; ++i) {
asm volatile("" ::: "memory");
}
// The HI GPIO registers in SIO can observe and control the 6 QSPI pins.
// Note the button pulls the pin *low* when pressed.
const bool buttonState = (sio_hw->gpio_hi_in & (1u << CS_PIN_INDEX)) == 0;
// Restore the state of chip select
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
restore_interrupts(flags);
return buttonState;
}
#endif
#if JAVELIN_BUTTON_TOUCH
void Rp2040ButtonState::ReadTouchCounters(uint32_t *counters) {
gpio_set_dir_masked(BUTTON_TOUCH_PIN_MASK, BUTTON_TOUCH_PIN_MASK);
gpio_put_masked(BUTTON_TOUCH_PIN_MASK, BUTTON_TOUCH_PIN_MASK);
// This is a ~100us wait.
for (int i = 0; i < 1250; ++i) {
asm volatile("" ::: "memory");
}
for (size_t i = 0; i < sizeof(BUTTON_TOUCH_PINS); ++i) {
const uint8_t pin = BUTTON_TOUCH_PINS[i];
gpio_set_dir(pin, false);
size_t counter = 0;
for (; counter < 100000; ++counter) {
if (!gpio_get(pin)) {
break;
}
}
counters[i] = counter;
}
}
#endif
ButtonState Rp2040ButtonState::Read() {
ButtonState state;
state.ClearAll();
#if JAVELIN_BUTTON_MATRIX
for (int r = 0; r < ROW_PIN_COUNT; ++r) {
gpio_put_masked(ROW_PIN_MASK, ROW_PIN_MASK & ~(1 << ROW_PINS[r]));
// Seems to work solidly with 2us wait. Use 10 for safety.
busy_wait_us_32(10);
const int columnMask = gpio_get_all();
#pragma GCC unroll 1
for (int c = 0; c < COLUMN_PIN_COUNT; ++c) {
if (((columnMask >> COLUMN_PINS[c]) & 1) == 0) {
const int buttonIndex = KEY_MAP[r][c];
if (buttonIndex >= 0) {
state.Set(buttonIndex);
}
}
}
}
gpio_put_masked(ROW_PIN_MASK, ROW_PIN_MASK);
#endif
#if JAVELIN_BUTTON_PINS
#if !defined(JAVELIN_BUTTON_PINS_OFFSET)
#define JAVELIN_BUTTON_PINS_OFFSET 0
#endif
const int buttonMask = gpio_get_all();
#pragma GCC unroll 1
for (size_t b = 0; b < sizeof(BUTTON_PINS); ++b) {
const uint8_t pinAndPolarity = BUTTON_PINS[b];
const uint8_t pin = pinAndPolarity & 0x7f;
if (pin == 0x7f) {
continue;
}
if (((buttonMask >> pin) & 1) == pinAndPolarity >> 7) {
state.Set(JAVELIN_BUTTON_PINS_OFFSET + b);
}
}
#endif
#if JAVELIN_BUTTON_TOUCH
uint32_t counters[sizeof(BUTTON_TOUCH_PINS)];
ReadTouchCounters(counters);
for (size_t i = 0; i < sizeof(BUTTON_TOUCH_PINS); ++i) {
const bool isTouched = counters[i] > touchPadThreshold[i];
if (isTouched) {
state.Set(i);
}
}
#endif
#if defined(BOOTSEL_BUTTON_INDEX)
if (isBootSelButtonPressed()) {
state.Set(BOOTSEL_BUTTON_INDEX);
}
#endif
return state;
}
//---------------------------------------------------------------------------