fdcavalcanti commented on code in PR #12619:
URL: https://github.com/apache/nuttx/pull/12619#discussion_r1665582862


##########
arch/risc-v/src/common/espressif/esp_mcpwm.c:
##########
@@ -0,0 +1,682 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_mcpwm.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 <sys/types.h>
+#include <stdbool.h>
+#include <string.h>
+#include <assert.h>
+#include <debug.h>
+#include <inttypes.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/capture.h>
+
+#include "esp_gpio.h"
+#include "esp_irq.h"
+
+#include "esp_attr.h"
+#include "hal/mcpwm_hal.h"
+#include "hal/mcpwm_ll.h"
+#include "soc/mcpwm_periph.h"
+#include "periph_ctrl.h"
+#include "hal/clk_tree_hal.h"
+#include "esp_clk_tree.h"
+
+#ifdef CONFIG_ESP_MCPWM
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_CHIP_ESP32H2
+#define MCPWM_DEV_CLK_SOURCE MCPWM_TIMER_CLK_SRC_PLL96M
+#else 
+#define MCPWM_DEV_CLK_SOURCE SOC_MOD_CLK_PLL_F160M
+#endif
+
+#define MCPWM_DEV_CLK_PRESCALE 4
+#define MCPWM_CAPTURE_DEFAULT_GROUP 0
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct mcpwm_dev_common_s
+{
+  mcpwm_hal_init_config_t group;
+  mcpwm_hal_context_t hal;
+  spinlock_t mcpwm_spinlock;
+  bool initialized;          /* MCPWM peripheral and HAL has been initialized 
*/
+  bool capture_initialized;  /* Capture submodule has been initialized */
+  int group_prescale;
+};
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE
+/* Capture event data. The 'last_' value is used to calculate frequency */
+
+struct mcpwm_capture_event_data_s
+{
+  uint32_t pos_edge_count;
+  uint32_t neg_edge_count;
+  uint32_t last_pos_edge_count;
+};
+
+enum mcpwm_capture_channel_e
+{
+  MCPWM_CAP_CHANNEL_0,  /* MCPWM capture channel number 0 */
+  MCPWM_CAP_CHANNEL_1,  /* MCPWM capture channel number 1 */
+  MCPWM_CAP_CHANNEL_2,  /* MCPWM capture channel number 2 */
+  MCPWM_CAP_CHANNEL_MAX /* Number of MCPWM capture channels */
+};
+
+/* Lowe-half data structure for a capture channel */
+
+struct mcpwm_cap_channel_lowerhalf_s
+{
+  /* The following block is part of the upper-half device struct */
+
+  FAR const struct cap_ops_s *ops;
+
+  /* The following is private to the ESP MCPWM driver */
+
+  struct mcpwm_dev_common_s *common;
+  struct mcpwm_capture_event_data_s *data;
+  int channel_id;
+  int gpio_pin;
+  uint32_t clock;
+  uint32_t freq;
+  uint8_t duty;
+  uint8_t isr_count;
+  bool ready;
+  bool enabled;
+};
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void esp_mcpwm_group_start(void);
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE
+static int esp_mcpwm_capture_set_gpio(
+  struct mcpwm_cap_channel_lowerhalf_s *lower);
+static int esp_mcpwm_capture_isr_register(int (*fn)(int, void *, void *),
+                                          void *arg);
+static int IRAM_ATTR mcpwm_capture_driver_isr_default(int irq, void *context,
+                                                      void *arg);
+
+/* Lower half methods required by capture driver */
+
+static int esp_capture_start(struct cap_lowerhalf_s *lower);
+static int esp_capture_stop(struct cap_lowerhalf_s *lower);
+static int esp_capture_getduty(struct cap_lowerhalf_s *lower,
+                               uint8_t *duty);
+static int esp_capture_getfreq(struct cap_lowerhalf_s *lower,
+                               uint32_t *freq);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct mcpwm_dev_common_s mcpwm_common =
+{
+  .group.group_id      = MCPWM_CAPTURE_DEFAULT_GROUP,
+  .initialized         = false,
+  .capture_initialized = false,
+  .group_prescale      = MCPWM_DEV_CLK_PRESCALE,
+};
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE
+/* Lower half methods required by the capture driver */
+
+static const struct cap_ops_s mcpwm_cap_ops =
+{
+  .start   = esp_capture_start,
+  .stop    = esp_capture_stop,
+  .getduty = esp_capture_getduty,
+  .getfreq = esp_capture_getfreq,
+};
+
+/* Data structures for the available capture channels */
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE_CH0
+static struct mcpwm_capture_event_data_s event_data_ch0;
+static struct mcpwm_cap_channel_lowerhalf_s mcpwm_cap_ch0_lowerhalf =
+{
+  .ops        = &mcpwm_cap_ops,
+  .common     = &mcpwm_common,
+  .data       = &event_data_ch0,
+  .channel_id = MCPWM_CAP_CHANNEL_0,
+  .ready      = false,
+};
+#endif
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE_CH1
+static struct mcpwm_capture_event_data_s event_data_ch1;
+static struct mcpwm_cap_channel_lowerhalf_s mcpwm_cap_ch1_lowerhalf =
+{
+  .ops        = &mcpwm_cap_ops,
+  .common     = &mcpwm_common,
+  .data       = &event_data_ch1,
+  .channel_id = MCPWM_CAP_CHANNEL_1,
+  .ready      = false,
+};
+#endif
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE_CH2
+static struct mcpwm_capture_event_data_s event_data_ch2;
+static struct mcpwm_cap_channel_lowerhalf_s mcpwm_cap_ch2_lowerhalf =
+{
+  .ops        = &mcpwm_cap_ops,
+  .common     = &mcpwm_common,
+  .data       = &event_data_ch2,
+  .channel_id = MCPWM_CAP_CHANNEL_2,
+  .ready      = false,
+};
+#endif
+#endif  /* CONFIG_ESP_MCPWM_CAPTURE */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_capture_start
+ *
+ * Description:
+ *   This function is a requirement of the upper-half driver. When called,
+ *   enables the capture channel, interruption routine, sets the positive
+ *   edge to trigger this interrupt and resets the frequency and duty
+ *   values. The positive edge is always the first expected.
+ *
+ * Input Parameters:
+ *   lower - Pointer to the capture channel lower-half data structure.
+ *
+ * Returned Value:
+ *   Returns OK on success.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ESP_MCPWM_CAPTURE
+static int esp_capture_start(struct cap_lowerhalf_s *lower)
+{
+  struct mcpwm_cap_channel_lowerhalf_s *priv = (
+    struct mcpwm_cap_channel_lowerhalf_s *)lower;
+  mcpwm_hal_context_t *hal = &priv->common->hal;
+
+  DEBUGASSERT(priv != NULL);
+  DEBUGASSERT(priv->common->initialized);

Review Comment:
   Removing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to