fix interrupt handling

This commit is contained in:
Dylan (Luberry) Kozicki 2022-04-17 12:47:44 -04:00
parent ab7673f268
commit 218c5b0572
4 changed files with 60 additions and 32 deletions

View file

@ -52,7 +52,11 @@ static int pmw33xx_access(const struct device *dev, const uint8_t reg, uint8_t *
}, },
.count = 1, .count = 1,
}; };
uint8_t result[1] = {*value};
uint8_t result[1];
if (value != NULL) {
result[0] = *value;
}
struct spi_buf_set rx = { struct spi_buf_set rx = {
.buffers = .buffers =
&(struct spi_buf){ &(struct spi_buf){
@ -77,7 +81,7 @@ static int pmw33xx_access(const struct device *dev, const uint8_t reg, uint8_t *
err = spi_read(data->bus, spi_cfg, &rx); err = spi_read(data->bus, spi_cfg, &rx);
pmw33xx_cs_select(cs_gpio_cfg, 1); pmw33xx_cs_select(cs_gpio_cfg, 1);
k_sleep(K_USEC(160)); k_sleep(K_USEC(160));
if ((reg & PMW33XX_WR_MASK) == 0) if ((reg & PMW33XX_WR_MASK) == 0 && value != NULL)
*value = result[0]; *value = result[0];
return err; return err;
} }
@ -88,6 +92,7 @@ static int pmw33xx_write_reg(const struct device *dev, const uint8_t reg, const
uint8_t v = value; uint8_t v = value;
return pmw33xx_access(dev, reg | PMW33XX_WR_MASK, &v); return pmw33xx_access(dev, reg | PMW33XX_WR_MASK, &v);
} }
static int pmw33xx_write_srom(const struct device *dev) { static int pmw33xx_write_srom(const struct device *dev) {
struct pmw33xx_data *data = dev->data; struct pmw33xx_data *data = dev->data;
const struct pmw33xx_config *cfg = dev->config; const struct pmw33xx_config *cfg = dev->config;
@ -166,9 +171,19 @@ static int pmw33xx_read_motion_burst(const struct device *dev, struct pmw33xx_mo
} }
err = spi_read(data->bus, spi_cfg, &rx); err = spi_read(data->bus, spi_cfg, &rx);
pmw33xx_cs_select(cs_gpio_cfg, 1); pmw33xx_cs_select(cs_gpio_cfg, 1);
#ifdef CONFIG_PMW33XX_TRIGGER
pmw33xx_reset_motion(dev);
#endif
return err; return err;
} }
#ifdef CONFIG_PMW33XX_TRIGGER
void pmw33xx_reset_motion(const struct device *dev) {
// reset motswk interrupt
pmw33xx_read_reg(dev, PMW33XX_REG_MOTION, NULL);
}
#endif
int pmw33xx_spi_init(const struct device *dev) { int pmw33xx_spi_init(const struct device *dev) {
const struct pmw33xx_config *cfg = dev->config; const struct pmw33xx_config *cfg = dev->config;
const struct pmw33xx_gpio_dt_spec *cs_gpio_cfg = &cfg->bus_cfg.spi_cfg->cs_spec; const struct pmw33xx_gpio_dt_spec *cs_gpio_cfg = &cfg->bus_cfg.spi_cfg->cs_spec;
@ -269,7 +284,8 @@ static int pmw33xx_init_chip(const struct device *dev) {
LOG_ERR("pid does not match expected: got (%x), expected(%x)", pid, PMW33XX_PID); LOG_ERR("pid does not match expected: got (%x), expected(%x)", pid, PMW33XX_PID);
return -EIO; return -EIO;
} }
pmw33xx_write_reg(dev, PMW33XX_REG_CONFIG2, 0x00); // clear rest enable pmw33xx_write_reg(dev, PMW33XX_REG_CONFIG2,
config->disable_rest ? 0x00 : PMW33XX_RESTEN); // set rest enable
err = pmw33xx_write_srom(dev); err = pmw33xx_write_srom(dev);
if (err) { if (err) {
@ -361,9 +377,10 @@ static int pmw33xx_init(const struct device *dev) {
{ \ { \
.bus_name = DT_INST_BUS_LABEL(n), .bus_init = pmw33xx_spi_init, \ .bus_name = DT_INST_BUS_LABEL(n), .bus_init = pmw33xx_spi_init, \
.bus_cfg = {.spi_cfg = PMW33XX_SPI_CFG(n)}, \ .bus_cfg = {.spi_cfg = PMW33XX_SPI_CFG(n)}, \
.disable_rest = DT_INST_NODE_HAS_PROP(n, disable_rest), \
COND_CODE_0(DT_INST_NODE_HAS_PROP(n, cpi), (0), (DT_INST_PROP(n, cpi))) \ COND_CODE_0(DT_INST_NODE_HAS_PROP(n, cpi), (0), (DT_INST_PROP(n, cpi))) \
COND_CODE_1(CONFIG_PMW33XX_TRIGGER, \ COND_CODE_1(CONFIG_PMW33XX_TRIGGER, \
(PMW33XX_GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), motswk_gpios, 0)), ()) \ (, PMW33XX_GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), motswk_gpios, 0)), ()) \
} }
#define PMW33XX_INST(n) \ #define PMW33XX_INST(n) \

View file

@ -65,6 +65,7 @@
/* power up reset cmd */ /* power up reset cmd */
#define PMW33XX_RESET_CMD 0x5A #define PMW33XX_RESET_CMD 0x5A
/* cpi max and min values */
#define PMW33XX_3389_CPI_MIN 50 #define PMW33XX_3389_CPI_MIN 50
#define PMW33XX_3389_CPI_MAX 16000 #define PMW33XX_3389_CPI_MAX 16000
#define PMW33XX_3360_CPI_MIN 100 #define PMW33XX_3360_CPI_MIN 100
@ -89,6 +90,7 @@ struct pmw33xx_config {
char *bus_name; char *bus_name;
int (*bus_init)(const struct device *dev); int (*bus_init)(const struct device *dev);
const union pmw33xx_bus_cfg bus_cfg; const union pmw33xx_bus_cfg bus_cfg;
bool disable_rest;
int cpi; int cpi;
#if CONFIG_PMW33XX_TRIGGER #if CONFIG_PMW33XX_TRIGGER
struct pmw33xx_gpio_dt_spec motswk_spec; struct pmw33xx_gpio_dt_spec motswk_spec;
@ -98,7 +100,7 @@ struct pmw33xx_config {
struct pmw33xx_data; struct pmw33xx_data;
struct pmw33xx_transfer_function { struct pmw33xx_transfer_function {
int (*read_data)(const struct device *dev, uint16_t *value); int (*read_data)(const struct device *dev, int16_t *value);
}; };
struct pmw33xx_data { struct pmw33xx_data {
@ -136,6 +138,8 @@ int pmw33xx_trigger_set(const struct device *dev, const struct sensor_trigger *t
sensor_trigger_handler_t handler); sensor_trigger_handler_t handler);
int pmw33xx_init_interrupt(const struct device *dev); int pmw33xx_init_interrupt(const struct device *dev);
void pmw33xx_reset_motion(const struct device *dev);
#endif #endif
#endif /* ZEPHYR_DRIVERS_SENSOR_PIXART_PMW33XX_H_ */ #endif /* ZEPHYR_DRIVERS_SENSOR_PIXART_PMW33XX_H_ */

View file

@ -23,6 +23,7 @@ static inline void setup_int(const struct device *dev, bool enable) {
struct pmw33xx_data *data = dev->data; struct pmw33xx_data *data = dev->data;
const struct pmw33xx_config *cfg = dev->config; const struct pmw33xx_config *cfg = dev->config;
gpio_pin_configure(cfg->motswk_spec.port, cfg->motswk_spec.pin, cfg->motswk_spec.dt_flags);
if (gpio_pin_interrupt_configure(cfg->motswk_spec.port, cfg->motswk_spec.pin, if (gpio_pin_interrupt_configure(cfg->motswk_spec.port, cfg->motswk_spec.pin,
enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE)) { enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE)) {
LOG_WRN("Unable to set MOTSWK GPIO interrupt"); LOG_WRN("Unable to set MOTSWK GPIO interrupt");
@ -72,7 +73,7 @@ static void pmw33xx_thread(int dev_ptr, int unused) {
static void pmw33xx_work_cb(struct k_work *work) { static void pmw33xx_work_cb(struct k_work *work) {
struct pmw33xx_data *drv_data = CONTAINER_OF(work, struct pmw33xx_data, work); struct pmw33xx_data *drv_data = CONTAINER_OF(work, struct pmw33xx_data, work);
LOG_DBG(""); LOG_DBG(" ");
pmw33xx_thread_cb(drv_data->dev); pmw33xx_thread_cb(drv_data->dev);
} }
@ -91,6 +92,8 @@ int pmw33xx_trigger_set(const struct device *dev, const struct sensor_trigger *t
setup_int(dev, true); setup_int(dev, true);
// reset motion on int setup
pmw33xx_reset_motion(dev);
return 0; return 0;
} }

View file

@ -20,3 +20,7 @@ properties:
type: int type: int
description: mouse cpi description: mouse cpi
required: false required: false
disable-rest:
type: boolean
description: disables mouse rest mode, will decrease battery life
required: false