add support for zero-cross driver

This commit is contained in:
halyssonJr 2024-05-11 17:37:38 -03:00 committed by Alan Carvalho de Assis
parent 1f3c47754d
commit e67df47b64
5 changed files with 243 additions and 0 deletions

View File

@ -0,0 +1,73 @@
/****************************************************************************
* boards/xtensa/esp32/common/include/esp32_zerocross.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_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H
#define __BOARDS_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* 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: esp32_zerocross_initialize
*
* Description:
* Initialize and register the zero cross driver
*
****************************************************************************/
int board_zerocross_initialize(int devno);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __BOARDS_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H */

View File

@ -148,6 +148,10 @@ endif
CSRCS += esp32_board_dac.c
#endif
ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
CSRCS += esp32_zerocross.c
endif
DEPPATH += --dep-path src
VPATH += :src
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src

View File

@ -0,0 +1,148 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_zerocross.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 <nuttx/config.h>
#include <stdint.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <arch/board/board.h>
#include <nuttx/sensors/zerocross.h>
#include "esp32_gpio.h"
#include "hardware/esp32_gpio_sigmap.h"
#include "esp32-wrover-kit.h"
#include "esp32_zerocross.h"
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void zcross_enable(const struct zc_lowerhalf_s *lower,
zc_interrupt_t handler, void *arg);
static int zcross_interrupt(int irq, void *context, void *arg);
/****************************************************************************
* Private Data
****************************************************************************/
/* Current interrupt handler and argument */
static zc_interrupt_t g_zcrosshandler;
static void *g_zcrossarg;
/* This is the zero cross lower half driver interface */
static struct zc_lowerhalf_s g_zcrosslower =
{
.zc_enable = zcross_enable,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: zcross_enable
*
* Description:
* Enable interrupts on the selected zero cross pin. And empty
* set will disable all interrupts.
*
****************************************************************************/
static void zcross_enable(const struct zc_lowerhalf_s *lower,
zc_interrupt_t handler, void *arg)
{
irqstate_t flags;
int irq = ESP32_PIN2IRQ(GPIO_ZERO_CROSS_IRQ);
int ret;
flags = enter_critical_section();
if (handler)
{
g_zcrosshandler = handler;
g_zcrossarg = arg;
}
/* Start with all interrupts disabled */
esp32_gpioirqdisable(irq);
ret = irq_attach(irq, zcross_interrupt, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: zcross_enable() failed: %d\n", ret);
leave_critical_section(flags);
}
esp32_gpioirqenable(irq, RISING);
leave_critical_section(flags);
}
/****************************************************************************
* Name: zcross_interrupt
*
* Description:
* Zero Cross interrupt handler
*
****************************************************************************/
static int zcross_interrupt(int irq, void *context, void *arg)
{
DEBUGASSERT(g_zcrosshandler != NULL);
if (g_zcrosshandler)
{
g_zcrosshandler(&g_zcrosslower, g_zcrossarg);
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp32_zerocross_initialize
*
* Description:
* Initialize and register the zero cross driver
*
****************************************************************************/
int board_zerocross_initialize(int devno)
{
esp32_configgpio(GPIO_ZERO_CROSS_IRQ, INPUT_FUNCTION_3 | PULLUP);
/* Register the zero cross device as /dev/zc0 */
return zc_register("/dev/zc0", &g_zcrosslower);
}

View File

@ -57,6 +57,9 @@
#define ONESHOT_TIMER 1
#define ONESHOT_RESOLUTION_US 1
/* Zero Cross */
#define GPIO_ZERO_CROSS_IRQ 22
/****************************************************************************
* Public Types
****************************************************************************/

View File

@ -105,6 +105,10 @@
# include "esp32_lcd_backpack.h"
#endif
#ifdef CONFIG_SENSORS_ZEROCROSS
# include "esp32_zerocross.h"
#endif
#include "esp32-wrover-kit.h"
/****************************************************************************
@ -385,6 +389,17 @@ int esp32_bringup(void)
}
#endif
#ifdef CONFIG_SENSORS_ZEROCROSS
/* Register Zero Cross Driver */
ret = board_zerocross_initialize(0);
if (ret < 0)
{
syslog(LOG_ERR,
"ERROR: board_zerocross_initialize() failed: %d\n", ret);
}
#endif
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.