Skip to content

Commit

Permalink
[PCE] Rework 2 btns to ignore OE line and handle OE via interrupt for…
Browse files Browse the repository at this point in the history
… MT & 6 btns
  • Loading branch information
darthcloud committed Mar 26, 2022
1 parent 4f488f1 commit 497ecb9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 83 deletions.
4 changes: 2 additions & 2 deletions main/adapter/wired/pce.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static void pce_mouse_from_generic(struct generic_ctrl *ctrl_data, struct wired_
static void pce_ctrl_from_generic(struct generic_ctrl *ctrl_data, struct wired_data *wired_data) {
struct pce_map map_tmp;
uint32_t map_mask[3];
const uint32_t (*btns_mask)[32] = (config.out_cfg[ctrl_data->index].dev_mode == DEV_PAD_ALT) ? pce_6btns_btns_mask : pce_btns_mask;
const uint32_t (*btns_mask)[32] = (config.out_cfg[0].dev_mode == DEV_PAD_ALT) ? pce_6btns_btns_mask : pce_btns_mask;

memset(map_mask, 0xFF, sizeof(map_mask));
memcpy((void *)&map_tmp, wired_data->output, sizeof(map_tmp));
Expand Down Expand Up @@ -250,7 +250,7 @@ void pce_meta_init(struct generic_ctrl *ctrl_data) {

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
for (uint32_t j = 0; j < ADAPTER_MAX_AXES; j++) {
switch (config.out_cfg[i].dev_mode) {
switch (config.out_cfg[0].dev_mode) {
case DEV_MOUSE:
ctrl_data[i].mask = pce_mouse_mask;
ctrl_data[i].desc = pce_mouse_desc;
Expand Down
194 changes: 113 additions & 81 deletions main/wired/pce_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "system/intr.h"
#include "pce_io.h"

#define GPIO_INTR_NUM 21

#define P1_SEL_PIN 33
#define P1_OE_PIN 26
#define P1_U_I_PIN 3
Expand Down Expand Up @@ -52,6 +54,7 @@ static uint32_t *map[] = {
(uint32_t *)wired_adapter.data[0].output,
(uint32_t *)wired_adapter.data[0].output,
};
static uint8_t cycle = 0;
static uint8_t frame_cnt = 0;
static uint8_t mouse_cnt = 0;
static uint32_t axes[4] = {0};
Expand Down Expand Up @@ -97,22 +100,15 @@ static inline void load_mouse_axes() {
}

static void pce_ctrl_task(void) {
uint32_t timeout, cur_in0, prev_in0, change0 = 0, cur_in1, prev_in1, change1 = 0, lock = 0;
uint8_t cycle = 0;
uint32_t output = PCE_OUT_SET;
uint32_t timeout, cur_in1, prev_in1, idx, change1 = 0, lock = 0;

timeout = 0;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
while (1) {
prev_in0 = cur_in0;
prev_in1 = cur_in1;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
while (!(change0 = cur_in0 ^ prev_in0) && !(change1 = cur_in1 ^ prev_in1)) {
prev_in0 = cur_in0;
while (!(change1 = cur_in1 ^ prev_in1)) {
prev_in1 = cur_in1;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
if (lock) {
if (++timeout > POLL_TIMEOUT) {
Expand All @@ -123,38 +119,38 @@ static void pce_ctrl_task(void) {
}
}

if (change0 & P1_OE_MASK) {
if (cur_in0 & P1_OE_MASK) {
GPIO.out = PCE_OUT_DISABLE;
cycle = 0;
/* This force 2P-5P to follow 1P Gamepad type */
/* This save a few ns is rsp time */
if (config.out_cfg[0].dev_mode == DEV_PAD_ALT) {
++frame_cnt;
}
if (cur_in1 & P1_SEL_MASK) {
if (frame_cnt & 0x01) {
output = PCE_OUT_CLR;
}
else {
output = map[cycle][0];
}
}
else {
if (frame_cnt & 0x01) {
output = map[cycle][2];
}
else {
output = map[cycle][1];
}
}
continue;
}
else {
GPIO.out = output;
if (change1 & P1_SEL_MASK) {
if (!lock) {
core0_stall_start();
++lock;
}
idx = ((cur_in1 & P1_SEL_MASK) >> 1) ^ 0x01;

GPIO.out = map[0][idx];
timeout = 0;
}
}
}

static void pce_mt_task(void) {
uint32_t timeout, cur_in1, prev_in1, change1 = 0, lock = 0;

timeout = 0;
cur_in1 = GPIO.in1.val;
while (1) {
prev_in1 = cur_in1;
cur_in1 = GPIO.in1.val;
while (!(change1 = cur_in1 ^ prev_in1)) {
prev_in1 = cur_in1;
cur_in1 = GPIO.in1.val;
if (lock) {
if (++timeout > POLL_TIMEOUT) {
core0_stall_end();
lock = 0;
timeout = 0;
}
}
}

if (change1 & P1_SEL_MASK) {
if (!lock) {
Expand Down Expand Up @@ -185,23 +181,53 @@ static void pce_ctrl_task(void) {
}
}

static uint32_t pce_mt_oe_isr(uint32_t cause) {
uint32_t cur_in1 = GPIO.in1.val;
cycle = 0;
if (cur_in1 & P1_SEL_MASK) {
GPIO.out = map[cycle][0];
}
else {
GPIO.out = map[cycle][1];
}
GPIO.status_w1tc = P1_OE_MASK;
return 0;
}

static uint32_t pce_6btns_oe_isr(uint32_t cause) {
uint32_t cur_in1 = GPIO.in1.val;
cycle = 0;
++frame_cnt;
if (cur_in1 & P1_SEL_MASK) {
if (frame_cnt & 0x01) {
GPIO.out = PCE_OUT_CLR;
}
else {
GPIO.out = map[cycle][0];
}
}
else {
if (frame_cnt & 0x01) {
GPIO.out = map[cycle][2];
}
else {
GPIO.out = map[cycle][1];
}
}
GPIO.status_w1tc = P1_OE_MASK;
return 0;
}

static void pce_mouse_task(void) {
uint32_t timeout, cur_in0, prev_in0, change0 = 0, cur_in1, prev_in1, change1 = 0, lock = 0;
uint8_t cycle = 0;
uint32_t output = PCE_OUT_SET;
uint32_t timeout, cur_in1, prev_in1, change1 = 0, lock = 0;

timeout = 0;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
while (1) {
prev_in0 = cur_in0;
prev_in1 = cur_in1;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
while (!(change0 = cur_in0 ^ prev_in0) && !(change1 = cur_in1 ^ prev_in1)) {
prev_in0 = cur_in0;
while (!(change1 = cur_in1 ^ prev_in1)) {
prev_in1 = cur_in1;
cur_in0 = GPIO.in;
cur_in1 = GPIO.in1.val;
if (lock) {
if (++timeout > POLL_TIMEOUT) {
Expand All @@ -213,28 +239,6 @@ static void pce_mouse_task(void) {
}
}

if (change0 & P1_OE_MASK) {
if (cur_in0 & P1_OE_MASK) {
GPIO.out = PCE_OUT_DISABLE;
cycle = 0;
++frame_cnt;
++mouse_cnt;
if (cur_in1 & P1_SEL_MASK) {
if (mouse_cnt) {
output = axes[mouse_cnt - 1];
}
}
else {
output = map[0][2];
}
continue;
}
else {
GPIO.out = output;
}
timeout = 0;
}

if (change1 & P1_SEL_MASK) {
if (!lock) {
core0_stall_start();
Expand All @@ -259,6 +263,14 @@ static void pce_mouse_task(void) {
}
}

static uint32_t pce_mouse_oe_isr(uint32_t cause) {
cycle = 0;
++frame_cnt;
++mouse_cnt;
GPIO.status_w1tc = P1_OE_MASK;
return 0;
}

void pce_io_init(void) {
gpio_config_t io_conf = {0};

Expand All @@ -271,7 +283,12 @@ void pce_io_init(void) {
gpio_config_iram(&io_conf);

/* OE */
io_conf.intr_type = GPIO_INTR_DISABLE;
if (config.global_cfg.multitap_cfg == MT_SLOT_1 || config.out_cfg[0].dev_mode != DEV_PAD) {
io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
}
else {
io_conf.intr_type = GPIO_INTR_DISABLE;
}
io_conf.pin_bit_mask = 1ULL << P1_OE_PIN;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
Expand All @@ -296,17 +313,32 @@ void pce_io_init(void) {
gpio_set_level_iram(P1_D_SL_PIN, 1);
gpio_set_level_iram(P1_L_RN_PIN, 1);

if (config.out_cfg[0].dev_mode == DEV_MOUSE) {
pce_mouse_task();
}
else {
if (config.global_cfg.multitap_cfg == MT_SLOT_1) {
map[0] = (uint32_t *)wired_adapter.data[0].output;
map[1] = (uint32_t *)wired_adapter.data[1].output;
map[2] = (uint32_t *)wired_adapter.data[2].output;
map[3] = (uint32_t *)wired_adapter.data[3].output;
map[4] = (uint32_t *)wired_adapter.data[4].output;
if (config.global_cfg.multitap_cfg == MT_SLOT_1) {
map[0] = (uint32_t *)wired_adapter.data[0].output;
map[1] = (uint32_t *)wired_adapter.data[1].output;
map[2] = (uint32_t *)wired_adapter.data[2].output;
map[3] = (uint32_t *)wired_adapter.data[3].output;
map[4] = (uint32_t *)wired_adapter.data[4].output;
if (config.out_cfg[0].dev_mode == DEV_PAD_ALT) {
intexc_alloc_iram(ETS_GPIO_INTR_SOURCE, GPIO_INTR_NUM, pce_6btns_oe_isr);
}
pce_ctrl_task();
else {
intexc_alloc_iram(ETS_GPIO_INTR_SOURCE, GPIO_INTR_NUM, pce_mt_oe_isr);
}
pce_mt_task();
}

switch (config.out_cfg[0].dev_mode) {
case DEV_MOUSE:
intexc_alloc_iram(ETS_GPIO_INTR_SOURCE, GPIO_INTR_NUM, pce_mouse_oe_isr);
pce_mouse_task();
break;
case DEV_PAD_ALT:
intexc_alloc_iram(ETS_GPIO_INTR_SOURCE, GPIO_INTR_NUM, pce_6btns_oe_isr);
pce_mt_task();
break;
default:
pce_ctrl_task();
break;
}
}

0 comments on commit 497ecb9

Please sign in to comment.