acassis commented on code in PR #18649: URL: https://github.com/apache/nuttx/pull/18649#discussion_r3009067323
########## arch/arm/src/ht32f491x3/ht32f491x3_pwm.c: ########## @@ -0,0 +1,385 @@ +/**************************************************************************** + * arch/arm/src/ht32f491x3/ht32f491x3_pwm.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <inttypes.h> +#include <stdbool.h> +#include <stdint.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/arch.h> +#include <nuttx/timers/pwm.h> + +#include <arch/board/board.h> + +#include "arm_internal.h" +#include "chip.h" +#include "ht32f491x3_gpio.h" +#include "ht32f491x3_pwm.h" + +#include "hardware/ht32f491x3_crm.h" +#include "hardware/ht32f491x3_tmr.h" + +#ifdef CONFIG_HT32F491X3_TMR3_PWM + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ht32f491x3_pwmtimer_s +{ + const struct pwm_ops_s *ops; + uint8_t timid; + uint8_t channel; + bool advanced; + uintptr_t base; + uint32_t gpio_clken; + uintptr_t gpio_base; + uint8_t pin; + uint8_t af; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int pwm_setup(struct pwm_lowerhalf_s *dev); +static int pwm_shutdown(struct pwm_lowerhalf_s *dev); +static int pwm_start(struct pwm_lowerhalf_s *dev, + const struct pwm_info_s *info); +static int pwm_stop(struct pwm_lowerhalf_s *dev); +static int pwm_ioctl(struct pwm_lowerhalf_s *dev, + int cmd, unsigned long arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct pwm_ops_s g_pwmops = +{ + .setup = pwm_setup, + .shutdown = pwm_shutdown, + .start = pwm_start, + .stop = pwm_stop, + .ioctl = pwm_ioctl, +}; + +static struct ht32f491x3_pwmtimer_s g_pwm3dev = +{ + .ops = &g_pwmops, + .timid = 3, + .channel = CONFIG_HT32F491X3_TMR3_CHANNEL, + .advanced = false, + .base = HT32_TMR3_BASE, + .gpio_clken = BOARD_PWM0_GPIO_CLKEN, + .gpio_base = BOARD_PWM0_GPIO_BASE, + .pin = BOARD_PWM0_GPIO_PIN, + .af = BOARD_PWM0_GPIO_AF, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static inline uint32_t pwm_getreg(FAR struct ht32f491x3_pwmtimer_s *priv, + unsigned int offset) +{ + return getreg32(priv->base + offset); +} + +static inline void pwm_putreg(FAR struct ht32f491x3_pwmtimer_s *priv, + unsigned int offset, uint32_t value) +{ + putreg32(value, priv->base + offset); +} + +static inline unsigned int pwm_cm_offset(uint8_t channel) +{ + return channel <= 2 ? HT32_TMR_CM1_OFFSET : HT32_TMR_CM2_OFFSET; +} + +static inline unsigned int pwm_cm_slot(uint8_t channel) +{ + return (channel - 1u) & 1u; +} + +static inline uint32_t pwm_cm_mask(uint8_t channel) +{ + unsigned int slot = pwm_cm_slot(channel); + + return HT32_TMR_CM_CAPTURE_SEL_MASK(slot) | + HT32_TMR_CM_OUTPUT_BUFFER(slot) | + HT32_TMR_CM_OUTPUT_MODE_MASK(slot); +} + +static inline uint32_t pwm_cm_value(uint8_t channel) +{ + unsigned int slot = pwm_cm_slot(channel); + + return HT32_TMR_CM_OUTPUT_BUFFER(slot) | + HT32_TMR_CM_OUTPUT_MODE(slot, HT32_TMR_OUTPUT_CONTROL_PWM_A); +} + +static void ht32f491x3_pwm_enableclk(FAR struct ht32f491x3_pwmtimer_s *priv, + bool enable) +{ + switch (priv->timid) + { + case 3: Review Comment: Why is only timid 3 enabled? I think it is a good idea add some comments here ########## arch/arm/src/ht32f491x3/ht32f491x3_pwm.c: ########## @@ -0,0 +1,385 @@ +/**************************************************************************** + * arch/arm/src/ht32f491x3/ht32f491x3_pwm.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <inttypes.h> +#include <stdbool.h> +#include <stdint.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/arch.h> +#include <nuttx/timers/pwm.h> + +#include <arch/board/board.h> + +#include "arm_internal.h" +#include "chip.h" +#include "ht32f491x3_gpio.h" +#include "ht32f491x3_pwm.h" + +#include "hardware/ht32f491x3_crm.h" +#include "hardware/ht32f491x3_tmr.h" + +#ifdef CONFIG_HT32F491X3_TMR3_PWM + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ht32f491x3_pwmtimer_s +{ + const struct pwm_ops_s *ops; + uint8_t timid; + uint8_t channel; + bool advanced; + uintptr_t base; + uint32_t gpio_clken; + uintptr_t gpio_base; + uint8_t pin; + uint8_t af; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int pwm_setup(struct pwm_lowerhalf_s *dev); +static int pwm_shutdown(struct pwm_lowerhalf_s *dev); +static int pwm_start(struct pwm_lowerhalf_s *dev, + const struct pwm_info_s *info); +static int pwm_stop(struct pwm_lowerhalf_s *dev); +static int pwm_ioctl(struct pwm_lowerhalf_s *dev, + int cmd, unsigned long arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct pwm_ops_s g_pwmops = +{ + .setup = pwm_setup, + .shutdown = pwm_shutdown, + .start = pwm_start, + .stop = pwm_stop, + .ioctl = pwm_ioctl, +}; + +static struct ht32f491x3_pwmtimer_s g_pwm3dev = +{ + .ops = &g_pwmops, + .timid = 3, + .channel = CONFIG_HT32F491X3_TMR3_CHANNEL, + .advanced = false, + .base = HT32_TMR3_BASE, + .gpio_clken = BOARD_PWM0_GPIO_CLKEN, + .gpio_base = BOARD_PWM0_GPIO_BASE, + .pin = BOARD_PWM0_GPIO_PIN, + .af = BOARD_PWM0_GPIO_AF, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static inline uint32_t pwm_getreg(FAR struct ht32f491x3_pwmtimer_s *priv, + unsigned int offset) +{ + return getreg32(priv->base + offset); +} + +static inline void pwm_putreg(FAR struct ht32f491x3_pwmtimer_s *priv, + unsigned int offset, uint32_t value) +{ + putreg32(value, priv->base + offset); +} + +static inline unsigned int pwm_cm_offset(uint8_t channel) +{ + return channel <= 2 ? HT32_TMR_CM1_OFFSET : HT32_TMR_CM2_OFFSET; +} + +static inline unsigned int pwm_cm_slot(uint8_t channel) +{ + return (channel - 1u) & 1u; +} + +static inline uint32_t pwm_cm_mask(uint8_t channel) +{ + unsigned int slot = pwm_cm_slot(channel); + + return HT32_TMR_CM_CAPTURE_SEL_MASK(slot) | + HT32_TMR_CM_OUTPUT_BUFFER(slot) | + HT32_TMR_CM_OUTPUT_MODE_MASK(slot); +} + +static inline uint32_t pwm_cm_value(uint8_t channel) +{ + unsigned int slot = pwm_cm_slot(channel); + + return HT32_TMR_CM_OUTPUT_BUFFER(slot) | + HT32_TMR_CM_OUTPUT_MODE(slot, HT32_TMR_OUTPUT_CONTROL_PWM_A); +} + +static void ht32f491x3_pwm_enableclk(FAR struct ht32f491x3_pwmtimer_s *priv, + bool enable) +{ + switch (priv->timid) + { + case 3: + modifyreg32(HT32_CRM_APB1EN, + HT32_CRM_APB1EN_TMR3EN, + enable ? HT32_CRM_APB1EN_TMR3EN : 0); + break; + + default: + break; + } +} + +static void ht32f491x3_pwm_reset(FAR struct ht32f491x3_pwmtimer_s *priv) +{ + switch (priv->timid) + { + case 3: Review Comment: Ditto -- 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]
