2021-08-19 16:19:02 +08:00
|
|
|
/*
|
2023-02-11 08:14:33 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2021-08-19 16:19:02 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2021-11-11 15:55:40 +08:00
|
|
|
* 2021-11-11 breo.com first version
|
2021-08-19 16:19:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
#include "drv_pwm.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_PWM
|
2021-11-11 15:55:40 +08:00
|
|
|
#if !defined(BSP_USING_TIM1_CH1) && !defined(BSP_USING_TIM1_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM1_CH3) && !defined(BSP_USING_TIM1_CH4) && \
|
|
|
|
!defined(BSP_USING_TIM2_CH1) && !defined(BSP_USING_TIM2_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM2_CH3) && !defined(BSP_USING_TIM2_CH4) && \
|
|
|
|
!defined(BSP_USING_TIM3_CH1) && !defined(BSP_USING_TIM3_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM3_CH3) && !defined(BSP_USING_TIM3_CH4) && \
|
|
|
|
!defined(BSP_USING_TIM4_CH1) && !defined(BSP_USING_TIM4_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM4_CH3) && !defined(BSP_USING_TIM4_CH4) && \
|
|
|
|
!defined(BSP_USING_TIM5_CH1) && !defined(BSP_USING_TIM5_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM5_CH3) && !defined(BSP_USING_TIM5_CH4) && \
|
|
|
|
!defined(BSP_USING_TIM8_CH1) && !defined(BSP_USING_TIM8_CH2) && \
|
|
|
|
!defined(BSP_USING_TIM8_CH3) && !defined(BSP_USING_TIM8_CH4)
|
2021-10-27 09:57:55 +08:00
|
|
|
#error "Please define at least one BSP_USING_TIMx_CHx"
|
|
|
|
#endif
|
2021-08-19 16:19:02 +08:00
|
|
|
#endif /* RT_USING_PWM */
|
|
|
|
|
|
|
|
#define MAX_PERIOD 65535
|
2022-01-06 10:49:46 +08:00
|
|
|
#define MIN_PERIOD 3
|
2021-11-11 15:55:40 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_PWM
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-08-24 09:53:07 +08:00
|
|
|
struct n32_pwm
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-10-27 09:57:55 +08:00
|
|
|
TIM_Module *tim_handle;
|
2021-11-11 15:55:40 +08:00
|
|
|
const char *name;
|
|
|
|
struct rt_device_pwm pwm_device;
|
|
|
|
int8_t tim_en;
|
|
|
|
uint8_t ch_en;
|
|
|
|
uint32_t period;
|
|
|
|
uint32_t psc;
|
2021-08-19 16:19:02 +08:00
|
|
|
};
|
|
|
|
|
2021-08-24 09:53:07 +08:00
|
|
|
static struct n32_pwm n32_pwm_obj[] =
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
#if defined(BSP_USING_TIM1_CH1) || defined(BSP_USING_TIM1_CH2) || \
|
|
|
|
defined(BSP_USING_TIM1_CH3) || defined(BSP_USING_TIM1_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM1,
|
|
|
|
.name = "tim1pwm",
|
|
|
|
},
|
2021-08-24 09:53:07 +08:00
|
|
|
#endif
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
#if defined(BSP_USING_TIM2_CH1) || defined(BSP_USING_TIM2_CH2) || \
|
|
|
|
defined(BSP_USING_TIM2_CH3) || defined(BSP_USING_TIM2_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM2,
|
|
|
|
.name = "tim2pwm",
|
|
|
|
},
|
2021-08-24 09:53:07 +08:00
|
|
|
#endif
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
#if defined(BSP_USING_TIM3_CH1) || defined(BSP_USING_TIM3_CH2) || \
|
|
|
|
defined(BSP_USING_TIM3_CH3) || defined(BSP_USING_TIM3_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM3,
|
|
|
|
.name = "tim3pwm",
|
|
|
|
},
|
2021-08-24 09:53:07 +08:00
|
|
|
#endif
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
#if defined(BSP_USING_TIM4_CH1) || defined(BSP_USING_TIM4_CH2) || \
|
|
|
|
defined(BSP_USING_TIM4_CH3) || defined(BSP_USING_TIM4_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM4,
|
|
|
|
.name = "tim4pwm",
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BSP_USING_TIM5_CH1) || defined(BSP_USING_TIM5_CH2) || \
|
|
|
|
defined(BSP_USING_TIM5_CH3) || defined(BSP_USING_TIM5_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM5,
|
|
|
|
.name = "tim5pwm",
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BSP_USING_TIM8_CH1) || defined(BSP_USING_TIM8_CH2) || \
|
|
|
|
defined(BSP_USING_TIM8_CH3) || defined(BSP_USING_TIM8_CH4)
|
|
|
|
{
|
|
|
|
.tim_handle = TIM8,
|
|
|
|
.name = "tim8pwm",
|
|
|
|
}
|
2021-08-24 09:53:07 +08:00
|
|
|
#endif
|
2021-11-11 15:55:40 +08:00
|
|
|
|
2021-08-19 16:19:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
|
|
|
|
static struct rt_pwm_ops drv_ops =
|
|
|
|
{
|
|
|
|
drv_pwm_control
|
|
|
|
};
|
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
static rt_err_t drv_pwm_enable(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration, rt_bool_t enable)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
|
|
|
/* Get the value of channel */
|
|
|
|
rt_uint32_t channel = configuration->channel;
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_Module *TIMx = pwm_dev->tim_handle;
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
pwm_dev->ch_en |= 0x1 << channel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwm_dev->ch_en &= ~(0x1 << channel);
|
|
|
|
}
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
if (enable)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 1)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_ENABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 2)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_ENABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 3)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_ENABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 4)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_ENABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 1)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 2)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 3)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 4)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
if (pwm_dev->ch_en)
|
|
|
|
{
|
|
|
|
pwm_dev->tim_en = 0x1;
|
|
|
|
TIM_Enable(TIMx, ENABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwm_dev->tim_en = 0x0;
|
|
|
|
TIM_Enable(TIMx, DISABLE);
|
|
|
|
}
|
2021-08-24 09:53:07 +08:00
|
|
|
|
2021-08-19 16:19:02 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
static rt_err_t drv_pwm_get(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
RCC_ClocksType RCC_Clockstruct;
|
2021-08-19 16:19:02 +08:00
|
|
|
rt_uint32_t ar, div, cc1, cc2, cc3, cc4;
|
|
|
|
rt_uint64_t tim_clock;
|
2021-11-11 15:55:40 +08:00
|
|
|
rt_uint32_t channel = configuration->channel;
|
|
|
|
TIM_Module *TIMx = pwm_dev->tim_handle;
|
2021-08-19 16:19:02 +08:00
|
|
|
|
|
|
|
ar = TIMx->AR;
|
2021-08-24 09:53:07 +08:00
|
|
|
div = TIMx->PSC;
|
|
|
|
cc1 = TIMx->CCDAT1;
|
|
|
|
cc2 = TIMx->CCDAT2;
|
|
|
|
cc3 = TIMx->CCDAT3;
|
|
|
|
cc4 = TIMx->CCDAT4;
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-08-24 09:53:07 +08:00
|
|
|
RCC_GetClocksFreqValue(&RCC_Clockstruct);
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-08-24 09:53:07 +08:00
|
|
|
tim_clock = RCC_Clockstruct.Pclk2Freq;
|
2021-08-19 16:19:02 +08:00
|
|
|
|
|
|
|
/* Convert nanosecond to frequency and duty cycle. */
|
|
|
|
tim_clock /= 1000000UL;
|
|
|
|
configuration->period = (ar + 1) * (div + 1) * 1000UL / tim_clock;
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 1)
|
2021-08-24 09:53:07 +08:00
|
|
|
configuration->pulse = (cc1 + 1) * (div + 1) * 1000UL / tim_clock;
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 2)
|
|
|
|
configuration->pulse = (cc2 + 1) * (div + 1) * 1000UL / tim_clock;
|
|
|
|
if (channel == 3)
|
2021-08-24 09:53:07 +08:00
|
|
|
configuration->pulse = (cc3 + 1) * (div + 1) * 1000UL / tim_clock;
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 4)
|
2021-08-24 09:53:07 +08:00
|
|
|
configuration->pulse = (cc4 + 1) * (div + 1) * 1000UL / tim_clock;
|
|
|
|
|
2021-08-19 16:19:02 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:55:40 +08:00
|
|
|
static rt_err_t drv_pwm_set(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_Module *TIMx = pwm_dev->tim_handle;
|
|
|
|
rt_uint32_t channel = configuration->channel;
|
2022-01-06 10:49:46 +08:00
|
|
|
rt_uint32_t period;
|
|
|
|
rt_uint64_t psc;
|
|
|
|
rt_uint32_t pulse;
|
2021-11-11 15:55:40 +08:00
|
|
|
|
2021-08-24 15:35:33 +08:00
|
|
|
RCC_ClocksType RCC_Clock;
|
|
|
|
RCC_GetClocksFreqValue(&RCC_Clock);
|
|
|
|
rt_uint64_t input_clock;
|
|
|
|
if ((TIM1 == TIMx) || (TIM8 == TIMx))
|
|
|
|
{
|
|
|
|
RCC_ConfigTim18Clk(RCC_TIM18CLK_SRC_SYSCLK);
|
|
|
|
input_clock = RCC_Clock.SysclkFreq;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-27 09:57:55 +08:00
|
|
|
if (1 == (RCC_Clock.HclkFreq / RCC_Clock.Pclk1Freq))
|
2021-08-24 15:35:33 +08:00
|
|
|
input_clock = RCC_Clock.Pclk1Freq;
|
|
|
|
else
|
|
|
|
input_clock = RCC_Clock.Pclk1Freq * 2;
|
|
|
|
}
|
|
|
|
|
2022-01-06 10:49:46 +08:00
|
|
|
input_clock /= 1000000UL;
|
2021-08-19 16:19:02 +08:00
|
|
|
/* Convert nanosecond to frequency and duty cycle. */
|
2022-01-06 10:49:46 +08:00
|
|
|
period = (unsigned long long)configuration->period * input_clock / 1000ULL;
|
|
|
|
psc = period / MAX_PERIOD + 1;
|
2021-08-19 16:19:02 +08:00
|
|
|
period = period / psc;
|
2022-01-06 10:49:46 +08:00
|
|
|
if (period < MIN_PERIOD)
|
|
|
|
{
|
|
|
|
period = MIN_PERIOD;
|
|
|
|
}
|
2021-11-11 15:55:40 +08:00
|
|
|
if ((pwm_dev->period != period) || (pwm_dev->psc != psc))
|
|
|
|
{
|
2022-01-06 10:49:46 +08:00
|
|
|
/* Tim base configuration */
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_TimeBaseInitType TIM_TIMeBaseStructure;
|
|
|
|
TIM_InitTimBaseStruct(&TIM_TIMeBaseStructure);
|
2022-01-06 10:49:46 +08:00
|
|
|
TIM_TIMeBaseStructure.Period = period - 1;
|
2021-11-11 15:55:40 +08:00
|
|
|
TIM_TIMeBaseStructure.Prescaler = psc - 1;
|
|
|
|
TIM_TIMeBaseStructure.ClkDiv = 0;
|
|
|
|
TIM_TIMeBaseStructure.CntMode = TIM_CNT_MODE_UP;
|
|
|
|
TIM_InitTimeBase(TIMx, &TIM_TIMeBaseStructure);
|
|
|
|
}
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2022-01-06 10:49:46 +08:00
|
|
|
pulse = (unsigned long long)configuration->pulse * input_clock / psc / 1000ULL;
|
|
|
|
if (pulse > period)
|
|
|
|
{
|
|
|
|
pulse = period;
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:19:02 +08:00
|
|
|
/* PWM1 Mode configuration: Channel1 */
|
2021-08-24 15:35:33 +08:00
|
|
|
OCInitType TIM_OCInitStructure;
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_InitOcStruct(&TIM_OCInitStructure);
|
|
|
|
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
|
|
|
|
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
|
|
|
|
TIM_OCInitStructure.Pulse = pulse;
|
|
|
|
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
|
2021-08-19 16:19:02 +08:00
|
|
|
|
2021-10-27 09:57:55 +08:00
|
|
|
if (channel == 1)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_InitOc1(TIMx, &TIM_OCInitStructure);
|
|
|
|
TIM_ConfigOc1Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
|
2021-11-11 15:55:40 +08:00
|
|
|
if (!(pwm_dev->ch_en & (0x1 << channel)))
|
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 2)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_InitOc2(TIMx, &TIM_OCInitStructure);
|
|
|
|
TIM_ConfigOc2Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
|
2021-11-11 15:55:40 +08:00
|
|
|
if (!(pwm_dev->ch_en & (0x1 << channel)))
|
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 3)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_InitOc3(TIMx, &TIM_OCInitStructure);
|
|
|
|
TIM_ConfigOc3Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
|
2021-11-11 15:55:40 +08:00
|
|
|
if (!(pwm_dev->ch_en & (0x1 << channel)))
|
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
2021-10-27 09:57:55 +08:00
|
|
|
else if (channel == 4)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_InitOc4(TIMx, &TIM_OCInitStructure);
|
|
|
|
TIM_ConfigOc4Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
|
2021-11-11 15:55:40 +08:00
|
|
|
if (!(pwm_dev->ch_en & (0x1 << channel)))
|
|
|
|
TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
|
2021-08-24 09:53:07 +08:00
|
|
|
TIM_ConfigArPreload(TIMx, ENABLE);
|
2021-08-24 15:35:33 +08:00
|
|
|
TIM_EnableCtrlPwmOutputs(TIMx, ENABLE);
|
2021-08-19 16:19:02 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
|
2021-11-11 15:55:40 +08:00
|
|
|
struct n32_pwm *pwm_dev = (struct n32_pwm *)(device->parent.user_data);
|
2021-08-19 16:19:02 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2021-10-27 09:57:55 +08:00
|
|
|
case PWM_CMD_ENABLE:
|
2021-11-11 15:55:40 +08:00
|
|
|
return drv_pwm_enable(pwm_dev, configuration, RT_TRUE);
|
2021-10-27 09:57:55 +08:00
|
|
|
case PWM_CMD_DISABLE:
|
2021-11-11 15:55:40 +08:00
|
|
|
return drv_pwm_enable(pwm_dev, configuration, RT_FALSE);
|
2021-10-27 09:57:55 +08:00
|
|
|
case PWM_CMD_SET:
|
2021-11-11 15:55:40 +08:00
|
|
|
return drv_pwm_set(pwm_dev, configuration);
|
2021-10-27 09:57:55 +08:00
|
|
|
case PWM_CMD_GET:
|
2021-11-11 15:55:40 +08:00
|
|
|
return drv_pwm_get(pwm_dev, configuration);
|
2021-10-27 09:57:55 +08:00
|
|
|
default:
|
|
|
|
return RT_EINVAL;
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rt_hw_pwm_init(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int result = RT_EOK;
|
|
|
|
|
2021-10-27 09:57:55 +08:00
|
|
|
for (i = 0; i < sizeof(n32_pwm_obj) / sizeof(n32_pwm_obj[0]); i++)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2021-11-11 15:55:40 +08:00
|
|
|
if (rt_device_pwm_register(&n32_pwm_obj[i].pwm_device,
|
|
|
|
n32_pwm_obj[i].name, &drv_ops, &(n32_pwm_obj[i])) == RT_EOK)
|
2021-08-19 16:19:02 +08:00
|
|
|
{
|
2022-01-18 16:51:46 +08:00
|
|
|
/* Init timer pin and enable clock */
|
|
|
|
void n32_msp_tim_init(void *Instance);
|
|
|
|
n32_msp_tim_init(n32_pwm_obj[i].tim_handle);
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-24 09:53:07 +08:00
|
|
|
result = -RT_ERROR;
|
2021-08-19 16:19:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(rt_hw_pwm_init);
|
2021-11-11 15:55:40 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|