【国民技术N32项目移植】FOC项目移植之RT-Thread下的pwm驱动移植
1、新建RT-Thread项目
**该小结可参考上一章文章 **FOC项目移植之环境搭建
2、新建驱动文件
新建驱动源文件(drv_pwm.c)和头文件(drv_pwm.h),内容如下所示
drv_pwm.h
#ifndef LIBRARIES_N32_DRIVERS_DRV_PWM_H_
#define LIBRARIES_N32_DRIVERS_DRV_PWM_H_
#include <rtthread.h>
#include <drivers/rt_drv_pwm.h>
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef BSP_USING_TIM1_CH1
#ifndef PWM1_TIM1_CONFIG
#define PWM1_TIM1_CONFIG \
{ \
.tim_handle = TIM1, \
.name = "tim1pwm1", \
.channel = 1 \
}
#endif
#endif
#ifdef BSP_USING_TIM3_CH1
#ifndef PWM1_TIM3_CONFIG
#define PWM1_TIM3_CONFIG \
{ \
.tim_handle = TIM3, \
.name = "tim3pwm1", \
.channel = 1 \
}
#endif
#endif
#ifdef BSP_USING_TIM3_CH2
#ifndef PWM2_TIM3_CONFIG
#define PWM2_TIM3_CONFIG \
{ \
.tim_handle = TIM3, \
.name = "tim3pwm2", \
.channel = 2 \
}
#endif
#endif
#ifdef BSP_USING_TIM3_CH3
#ifndef PWM3_TIM3_CONFIG
#define PWM3_TIM3_CONFIG \
{ \
.tim_handle = TIM3, \
.name = "tim3pwm3", \
.channel = 3 \
}
#endif
#endif
#ifdef BSP_USING_TIM3_CH4
#ifndef PWM4_TIM3_CONFIG
#define PWM4_TIM3_CONFIG \
{ \
.tim_handle = TIM3, \
.name = "tim3pwm4", \
.channel = 4 \
}
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif
drv_pwm.c
#include <board.h>
#include "drv_pwm.h"
#ifdef RT_USING_PWM
#if !defined(BSP_USING_TIM3_CH1) && !defined(BSP_USING_TIM3_CH2) && \
!defined(BSP_USING_TIM3_CH3) && !defined(BSP_USING_TIM3_CH4) && \
!defined(BSP_USING_TIM1_CH1)
#error "Please define at least one BSP_USING_TIMx_CHx"
#endif
#endif
#define MAX_PERIOD 65535
struct rt_device_pwm pwm_device;
struct n32_pwm
{
struct rt_device_pwm pwm_device;
TIM_Module* tim_handle;
rt_uint8_t channel;
char *name;
};
static struct n32_pwm n32_pwm_obj[] =
{
#ifdef BSP_USING_TIM1_CH1
PWM1_TIM1_CONFIG,
#endif
#ifdef BSP_USING_TIM3_CH1
PWM1_TIM3_CONFIG,
#endif
#ifdef BSP_USING_TIM3_CH2
PWM2_TIM3_CONFIG,
#endif
#ifdef BSP_USING_TIM3_CH3
PWM3_TIM3_CONFIG,
#endif
#ifdef BSP_USING_TIM3_CH4
PWM4_TIM3_CONFIG,
#endif
};
#ifdef BSP_USING_PWM
static void n32_msp_tim_init(void *Instance)
{
GPIO_InitType GPIO_InitCtlStructure;
GPIO_InitStruct(&GPIO_InitCtlStructure);
TIM_Module *TIMx = (TIM_Module *)Instance;
if(TIMx == TIM1)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
GPIO_InitCtlStructure.Pin = GPIO_PIN_8 | GPIO_PIN_11;
GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
}
if(TIMx == TIM2)
{
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
}
if(TIMx == TIM3)
{
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA|RCC_APB2_PERIPH_GPIOB, ENABLE);
GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStructure);
}
}
#endif
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
};
static rt_err_t drv_pwm_enable(TIM_Module* TIMx, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{
rt_uint32_t channel = configuration->channel;
if (!enable)
{
if(channel == 1)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
}
else if(channel == 2)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
}
else if(channel == 3)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
}
else if(channel == 4)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
}
}
else
{
if(channel == 1)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_ENABLE);
}
else if(channel == 2)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_ENABLE);
}
else if(channel == 3)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_ENABLE);
}
else if(channel == 4)
{
TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_ENABLE);
}
}
TIM_Enable(TIMx, ENABLE);
return RT_EOK;
}
static rt_err_t drv_pwm_get(TIM_Module* TIMx, struct rt_pwm_configuration *configuration)
{
RCC_ClocksType RCC_Clockstruct;
rt_uint32_t ar, div, cc1, cc2, cc3, cc4;
rt_uint32_t channel = configuration->channel;
rt_uint64_t tim_clock;
ar = TIMx->AR;
div = TIMx->PSC;
cc1 = TIMx->CCDAT1;
cc2 = TIMx->CCDAT2;
cc3 = TIMx->CCDAT3;
cc4 = TIMx->CCDAT4;
RCC_GetClocksFreqValue(&RCC_Clockstruct);
tim_clock = RCC_Clockstruct.Pclk2Freq;
tim_clock /= 1000000UL;
configuration->period = (ar + 1) * (div + 1) * 1000UL / tim_clock;
if(channel == 1)
configuration->pulse = (cc1 + 1) * (div + 1) * 1000UL / tim_clock;
if(channel == 2)
configuration->pulse = (cc2 + 1) * (div+ 1) * 1000UL / tim_clock;
if(channel == 3)
configuration->pulse = (cc3 + 1) * (div + 1) * 1000UL / tim_clock;
if(channel == 4)
configuration->pulse = (cc4 + 1) * (div + 1) * 1000UL / tim_clock;
return RT_EOK;
}
static rt_err_t drv_pwm_set(TIM_Module* TIMx, struct rt_pwm_configuration *configuration)
{
n32_msp_tim_init(TIMx);
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
{
if (1 == (RCC_Clock.HclkFreq/RCC_Clock.Pclk1Freq))
input_clock = RCC_Clock.Pclk1Freq;
else
input_clock = RCC_Clock.Pclk1Freq * 2;
}
rt_uint32_t period = (unsigned long long)configuration->period ;
rt_uint64_t psc = period / MAX_PERIOD + 1;
period = period / psc;
psc = psc * (input_clock / 1000000);
TIM_TimeBaseInitType TIM_TIMeBaseStructure;
TIM_InitTimBaseStruct(&TIM_TIMeBaseStructure);
TIM_TIMeBaseStructure.Period = period;
TIM_TIMeBaseStructure.Prescaler = psc - 1;
TIM_TIMeBaseStructure.ClkDiv = 0;
TIM_TIMeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIMx, &TIM_TIMeBaseStructure);
rt_uint32_t pulse = (unsigned long long)configuration->pulse;
OCInitType TIM_OCInitStructure;
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;
rt_uint32_t channel = configuration->channel;
if(channel == 1)
{
TIM_InitOc1(TIMx, &TIM_OCInitStructure);
TIM_ConfigOc1Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
}
else if(channel == 2)
{
TIM_InitOc2(TIMx, &TIM_OCInitStructure);
TIM_ConfigOc2Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
}
else if(channel == 3)
{
TIM_InitOc3(TIMx, &TIM_OCInitStructure);
TIM_ConfigOc3Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
}
else if(channel == 4)
{
TIM_InitOc4(TIMx, &TIM_OCInitStructure);
TIM_ConfigOc4Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
}
TIM_ConfigArPreload(TIMx, ENABLE);
TIM_EnableCtrlPwmOutputs(TIMx, ENABLE);
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;
TIM_Module *TIMx = (TIM_Module *)device->parent.user_data;
switch (cmd)
{
case PWM_CMD_ENABLE:
return drv_pwm_enable(TIMx, configuration, RT_TRUE);
case PWM_CMD_DISABLE:
return drv_pwm_enable(TIMx, configuration, RT_FALSE);
case PWM_CMD_SET:
return drv_pwm_set(TIMx, configuration);
case PWM_CMD_GET:
return drv_pwm_get(TIMx, configuration);
default:
return RT_EINVAL;
}
}
static int rt_hw_pwm_init(void)
{
int i = 0;
int result = RT_EOK;
for(i = 0; i < sizeof(n32_pwm_obj) / sizeof(n32_pwm_obj[0]); i++)
{
if(rt_device_pwm_register(&n32_pwm_obj[i].pwm_device, n32_pwm_obj[i].name, &drv_ops, n32_pwm_obj[i].tim_handle) == RT_EOK)
{
rt_kprintf("%s register success", n32_pwm_obj[i].name);
}
else
{
rt_kprintf("%s register failed", n32_pwm_obj[i].name);
result = -RT_ERROR;
}
}
return result;
}
INIT_BOARD_EXPORT(rt_hw_pwm_init);
3、修改配置文件
打开rtconfig.h文件,在图中位置输入如下内容:
#define BSP_USING_PWM
#define BSP_USING_TIM1
#define BSP_USING_TIM1_CH1
#define BSP_USING_TIM3
#define BSP_USING_TIM3_CH2
4、打开pwm驱动选项
5、测试pwm驱动
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* [url=home.php?mod=space&uid=1455510]@file[/url] main.c
* [url=home.php?mod=space&uid=40524]@author[/url] Nations
* [url=home.php?mod=space&uid=644434]@version[/url] v1.0.0
*
* [url=home.php?mod=space&uid=855824]@copyright[/url] Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define LED1_PIN GET_PIN(A, 1)
#define LED_PIN_NUM GET_PIN(B, 5) /* PA8 LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
#define PWM_DEV_LED1 "tim1pwm1" /* PWM设备名称 */
#define PWM_DEV_LED1_CHANNEL 1 /* PWM通道 */
struct rt_device_pwm *pwm_led1; /* PWM设备句柄 */
int pwm_led_sample()
{
rt_uint32_t period, led1_pulse, dir;
period = 5000; /* 周期为0.5ms,单位为纳秒ns */
dir = 1; /* PWM脉冲宽度值的增减方向 */
led1_pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
/* 设置LED引脚脚模式为输出 */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
/* 拉高LED引脚 */
//rt_pin_write(LED_PIN_NUM, PIN_HIGH);
/* 查找设备 */
//rt_device_pwm_register(pwm_dev, 'pwm3', ops, user_data)
pwm_led1 = (struct rt_device_pwm *)rt_device_find(PWM_DEV_LED1);
if (pwm_led1 == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_LED1);
return RT_ERROR;
}
/* 设置PWM周期和脉冲宽度默认值 */
rt_pwm_set(pwm_led1, PWM_DEV_LED1_CHANNEL, period, led1_pulse);
/* 使能设备 */
rt_pwm_enable(pwm_led1, PWM_DEV_LED1_CHANNEL);
while (1)
{
rt_thread_mdelay(50);
if (dir)
{
led1_pulse += 200;
}
else
{
led1_pulse -= 200;
}
if (led1_pulse >= period)
{
dir = 0;
}
if (0 == led1_pulse)
{
dir = 1;
}
/* 设置PWM周期和脉冲宽度 */
rt_pwm_set(pwm_led1, PWM_DEV_LED1_CHANNEL, period, led1_pulse);
}
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
/* defined the LED3 pin: PB5 */
#define LED3_PIN GET_PIN(B, 5)
int main(void)
{
/* set LED3 pin mode to output */
rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED3_PIN, PIN_HIGH);
rt_thread_mdelay(1000);
rt_pin_write(LED3_PIN, PIN_LOW);
rt_thread_mdelay(1000);
}
}
6、测试结果