diff --git a/boards/xtensa/esp32/common/include/board_qencoder.h b/boards/xtensa/esp32/common/include/board_qencoder.h new file mode 100644 index 00000000000..97c3915991b --- /dev/null +++ b/boards/xtensa/esp32/common/include/board_qencoder.h @@ -0,0 +1,73 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/include/board_qencoder.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_QENCODER_H +#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_QENCODER_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_qencoder_initialize + * + * Description: + * Initialize the quadrature encoder driver for the given timer + * + ****************************************************************************/ + +int board_qencoder_initialize(int devno, int timerno); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_QENCODER_H */ diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs index 84b6e11c972..b62c3ac229a 100644 --- a/boards/xtensa/esp32/common/src/Make.defs +++ b/boards/xtensa/esp32/common/src/Make.defs @@ -36,6 +36,10 @@ ifeq ($(CONFIG_ESP32_I2S),y) CSRCS += esp32_board_i2sdev.c endif +ifeq ($(CONFIG_ESP32_PCNT_AS_QE),y) + CSRCS += esp32_qencoder.c +endif + ifeq ($(CONFIG_AUDIO_CS4344),y) CSRCS += esp32_cs4344.c endif diff --git a/boards/xtensa/esp32/common/src/esp32_qencoder.c b/boards/xtensa/esp32/common/src/esp32_qencoder.c new file mode 100644 index 00000000000..cf1ecdd3d6d --- /dev/null +++ b/boards/xtensa/esp32/common/src/esp32_qencoder.c @@ -0,0 +1,65 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/src/esp32_qencoder.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include +#include + +#include "esp32_qencoder.h" +#include "chip.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_qencoder_initialize + * + * Description: + * Initialize the quadrature encoder driver for the given timer + * + ****************************************************************************/ + +int board_qencoder_initialize(int devno, int pcntno) +{ + int ret; + char devpath[12]; + + /* Initialize a quadrature encoder interface. */ + + sninfo("Initializing the quadrature encoder using PCNT%d\n", pcntno); + snprintf(devpath, sizeof(devpath), "/dev/qe%d", devno); + ret = esp32_qeinitialize(devpath, pcntno); + if (ret < 0) + { + snerr("ERROR: esp32_qeinitialize failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h index d173179f5a6..fae556a816d 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h @@ -46,6 +46,10 @@ #define GPIO_LED1 2 +/* PCNT Quadrature Encoder IDs */ + +#define PCNT_QE0_ID 0 + /* MCP2515 Interrupt pin */ #define GPIO_MCP2515_IRQ 22 diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c index 2566b5af0e4..2335a31a1b0 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c @@ -91,6 +91,10 @@ # include "esp32_i2s.h" #endif +#ifdef CONFIG_ESP32_PCNT_AS_QE +# include "board_qencoder.h" +#endif + #ifdef CONFIG_I2CMULTIPLEXER_TCA9548A # include "esp32_tca9548a.h" #endif @@ -398,6 +402,19 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_SENSORS_QENCODER + /* Initialize and register the qencoder driver */ + + ret = board_qencoder_initialize(0, PCNT_QE0_ID); + if (ret != OK) + { + syslog(LOG_ERR, + "ERROR: Failed to register the qencoder: %d\n", + ret); + return ret; + } +#endif + /* Register the TCA9548A Multiplexer before others I2C drivers to allow it * be used by other drivers. Look at esp32_ms5611.c how to use it. */