Skip to content

Commit

Permalink
Merge pull request #4 from kanru/fix/preemption
Browse files Browse the repository at this point in the history
fix: more conservative timing
  • Loading branch information
kanru authored Jan 23, 2023
2 parents 475befa + 95f43be commit 7f9a9ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion config/drivers/kscan/kscan_gpio_topre.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ struct kscan_gpio_topre_config
struct gpio_dt_spec key;
struct gpio_dt_spec hys;
struct gpio_dt_spec strobe;
const uint16_t pin_change_delay_us;
const uint16_t matrix_relax_us;
const uint16_t adc_read_settle_us;
const uint16_t adc_relax_us;
const uint16_t active_polling_interval_ms;
const uint16_t idle_polling_interval_ms;
const uint16_t sleep_polling_interval_ms;
Expand Down Expand Up @@ -105,21 +107,29 @@ static void kscan_gpio_topre_work_handler(struct k_work *work)
gpio_pin_set(cfg->bits[3].port, cfg->bits[3].pin, c & BIT(0));
gpio_pin_set(cfg->bits[4].port, cfg->bits[4].pin, c & BIT(1));
gpio_pin_set(cfg->bits[5].port, cfg->bits[5].pin, c & BIT(2));
k_busy_wait(cfg->pin_change_delay_us);

int cell = (r * MATRIX_COLS) + c;
const bool prev = data->matrix_state[cell];
gpio_pin_set(cfg->hys.port, cfg->hys.pin, prev);

k_busy_wait(cfg->matrix_relax_us);

k_sched_lock();
const unsigned int lock = irq_lock();

// Pull low strobe line to trigger sensing
gpio_pin_set(cfg->strobe.port, cfg->strobe.pin, 0);
gpio_pin_set(cfg->strobe.port, cfg->strobe.pin, 1);
k_busy_wait(cfg->adc_read_settle_us);
const bool pressed = gpio_pin_get(cfg->key.port, cfg->key.pin);

irq_unlock(lock);
k_sched_unlock();

k_busy_wait(cfg->pin_change_delay_us);
gpio_pin_set(cfg->hys.port, cfg->hys.pin, 0);
gpio_pin_set(cfg->strobe.port, cfg->strobe.pin, 1);
k_busy_wait(cfg->adc_relax_us);

matrix_read[cell] = pressed;
}
Expand Down Expand Up @@ -226,8 +236,10 @@ static const struct kscan_driver_api kscan_gpio_topre_api = {
.key = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 1), \
.hys = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 2), \
.strobe = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 9), \
.pin_change_delay_us = DT_INST_PROP(inst, pin_change_delay_us), \
.matrix_relax_us = DT_INST_PROP(inst, matrix_relax_us), \
.adc_read_settle_us = DT_INST_PROP(inst, adc_read_settle_us), \
.adc_relax_us = DT_INST_PROP(inst, adc_relax_us), \
.active_polling_interval_ms = DT_INST_PROP(inst, active_polling_interval_ms), \
.idle_polling_interval_ms = DT_INST_PROP(inst, idle_polling_interval_ms), \
.sleep_polling_interval_ms = DT_INST_PROP(inst, sleep_polling_interval_ms), \
Expand Down
8 changes: 7 additions & 1 deletion config/dts/bindings/kscan/zmk,kscan-gpio-topre.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ properties:
gpios:
type: phandle-array
required: true
pin-change-delay-us:
type: int
default: 5
matrix-relax-us:
type: int
default: 6
default: 10
adc-read-settle-us:
type: int
default: 5
adc-relax-us:
type: int
default: 30
active-polling-interval-ms:
type: int
default: 8
Expand Down

0 comments on commit 7f9a9ac

Please sign in to comment.