risc-v/esp32-c3: Adds freerun wrapper

This commit is contained in:
Sara Souza
2021-05-04 15:22:26 -03:00
committed by Gustavo Henrique Nihei
parent 2c91805d8f
commit b01ddef61b
7 changed files with 542 additions and 2 deletions
+7
View File
@@ -403,6 +403,13 @@ config ESP32C3_ONESHOT
Enable a wrapper around the low level timer/counter functions to
support one-shot timer.
config ESP32C3_FREERUN
bool "TIM free-running wrapper"
default n
---help---
Enable a wrapper around the low level timer/counter functions to
support a free-running timer.
endmenu # Timer/counter Configuration
endif # ESP32C3_TIMER
+4
View File
@@ -100,6 +100,10 @@ ifeq ($(CONFIG_ESP32C3_RT_TIMER),y)
CHIP_CSRCS += esp32c3_rt_timer.c
endif
ifeq ($(CONFIG_ESP32C3_FREERUN),y)
CHIP_CSRCS += esp32c3_freerun.c
endif
ifeq ($(CONFIG_ESP32C3_DISABLE_STDC_ATOMIC),)
CHIP_CSRCS += esp32c3_std_atomic.c
endif
File diff suppressed because it is too large Load Diff
+145
View File
@@ -0,0 +1,145 @@
/****************************************************************************
* arch/risc-v/src/esp32c3/esp32c3_freerun.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 __ARCH_RISCV_SRC_ESP32C3_FREERUN_H
#define __ARCH_RISCV_SRC_ESP32C3_FREERUN_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <time.h>
#include "esp32c3_tim.h"
#ifdef CONFIG_ESP32C3_FREERUN
/****************************************************************************
* Public Types
****************************************************************************/
/* The freerun client must allocate an instance of this structure and called
* esp32c3_freerun_initialize() before using the freerun facilities. The
* client should not access the contents of this structure directly since
* the contents are subject to change.
*/
struct esp32c3_freerun_s
{
uint8_t chan; /* The timer/counter in use */
uint32_t overflow; /* Timer counter overflow */
uint16_t resolution; /* Timer resolution */
uint64_t max_timeout; /* Maximum timeout to overflow */
FAR struct esp32c3_tim_dev_s *tch; /* Handle returned by esp32c3_tim_init() */
};
/****************************************************************************
* Public Data
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32c3_freerun_initialize
*
* Description:
* Initialize the freerun timer wrapper.
*
* Input Parameters:
* freerun - Caller allocated instance of the freerun
* state structure
* chan - Timer counter channel to be used.
* resolution - The required resolution of the timer in units of
* microseconds. NOTE that the range is restricted to
* the range of uint16_t (excluding zero).
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on failure.
*
****************************************************************************/
int esp32c3_freerun_initialize(struct esp32c3_freerun_s *freerun, int chan,
uint16_t resolution);
/****************************************************************************
* Name: esp32c3_freerun_counter
*
* Description:
* Read the counter register of the free-running timer.
*
* Input Parameters:
* freerun - Caller allocated instance of the freerun state
* structure. This structure must have been previously
* initialized via a call to
* esp32c3_freerun_initialize();
* ts - The location in which to return the time from the
* free-running timer.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on failure.
*
****************************************************************************/
int esp32c3_freerun_counter(struct esp32c3_freerun_s *freerun,
struct timespec *ts);
/****************************************************************************
* Name: esp32c3_freerun_uninitialize
*
* Description:
* Stop the free-running timer and release all resources that it uses.
*
* Input Parameters:
* freerun - Caller allocated instance of the freerun state
* structure. This structure must have been previously
* initialized via a call to
* esp32c3_freerun_initialize();
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on failure.
*
****************************************************************************/
int esp32c3_freerun_uninitialize(struct esp32c3_freerun_s *freerun);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_ESP32C3_FREERUN */
#endif /* __ARCH_RISCV_SRC_ESP32C3_FREERUN_H */
+29 -1
View File
@@ -34,6 +34,7 @@
#include "esp32c3_tim.h"
#include "esp32c3_irq.h"
#include "esp32c3_gpio.h"
/****************************************************************************
* Private Types
@@ -82,6 +83,7 @@ static int esp32c3_tim_setisr(FAR struct esp32c3_tim_dev_s *dev,
static void esp32c3_tim_enableint(FAR struct esp32c3_tim_dev_s *dev);
static void esp32c3_tim_disableint(FAR struct esp32c3_tim_dev_s *dev);
static void esp32c3_tim_ackint(FAR struct esp32c3_tim_dev_s *dev);
static int esp32c3_tim_checkint(FAR struct esp32c3_tim_dev_s *dev);
/****************************************************************************
* Private Data
@@ -107,7 +109,8 @@ struct esp32c3_tim_ops_s esp32c3_tim_ops =
.setisr = esp32c3_tim_setisr,
.enableint = esp32c3_tim_enableint,
.disableint = esp32c3_tim_disableint,
.ackint = esp32c3_tim_ackint
.ackint = esp32c3_tim_ackint,
.checkint = esp32c3_tim_checkint
};
#ifdef CONFIG_ESP32C3_TIMER0
@@ -654,6 +657,31 @@ static void esp32c3_tim_ackint(FAR struct esp32c3_tim_dev_s *dev)
modifyreg32(TIMG_INT_CLR_TIMERS_REG(priv->id), 0, TIMG_T0_INT_CLR_M);
}
/****************************************************************************
* Name: esp32c3_tim_checkint
*
* Description:
* Check the interrupt status bit.
*
* Parameters:
* dev - Pointer to the timer driver struct.
*
* Returned Values:
* Return 1 in case of an interrupt is triggered, otherwise 0.
*
****************************************************************************/
static int esp32c3_tim_checkint(FAR struct esp32c3_tim_dev_s *dev)
{
struct esp32c3_tim_priv_s *priv = (struct esp32c3_tim_priv_s *)dev;
uint32_t reg_value;
DEBUGASSERT(dev != NULL);
reg_value = getreg32(TIMG_INT_ST_TIMERS_REG(priv->id));
return ((reg_value & TIMG_T0_INT_ST_V) >> TIMG_T0_INT_ST_S);
}
/****************************************************************************
* Public Functions
****************************************************************************/
+2
View File
@@ -53,6 +53,7 @@
#define ESP32C3_TIM_ENABLEINT(d) ((d)->ops->enableint(d))
#define ESP32C3_TIM_DISABLEINT(d) ((d)->ops->disableint(d))
#define ESP32C3_TIM_ACKINT(d) ((d)->ops->ackint(d))
#define ESP32C3_TIM_CHECKINT(d) ((d)->ops->checkint(d))
/****************************************************************************
* Public Types
@@ -126,6 +127,7 @@ struct esp32c3_tim_ops_s
CODE void (*enableint)(FAR struct esp32c3_tim_dev_s *dev);
CODE void (*disableint)(FAR struct esp32c3_tim_dev_s *dev);
CODE void (*ackint)(FAR struct esp32c3_tim_dev_s *dev);
CODE int (*checkint)(FAR struct esp32c3_tim_dev_s *dev);
};
/****************************************************************************
@@ -72,7 +72,7 @@
/* Maximum value in the high 22 bits from timer counters */
#define LOW_32_MASK 0xffffffff
#define LOW_22_MASK 0x3fffff
#define LOW_22_MASK 0x003fffff
#define SHIFT_32 32
#define TIMG_T0CONFIG_REG(i) (REG_TIMG_BASE(i) + 0x0000)