Renesas R7FA8D1BH (Cortex®-M85) 生成4路PWM
目录
概述
1 软硬件环境
1.1 软件版本信息
1.2 硬件接口
2 FSP生成配置
2.1 配置参数
2.2 创建Stack和配置PWM参数
3 FSP库函数介绍
3.1 R_GPT_Open()
3.2 R_GPT_Stop()
3.3 R_GPT_Start()
3.4 R_GPT_Enable()
3.5 R_GPT_PeriodSet()
3.6 R_GPT_DutyCycleSet()
3.7 R_GPT_OutputEnable()
3.8 R_GPT_PwmOutputDelaySet()
3.9 R_GPT_OutputDisable()
概述
本文主要介绍使用Renesas 提供的FSP工具配置参数实现PWM功能,内容包括参数配置,PWM功能代码的实现,以及如何生成项目工程,还使用逻辑分析仪捕捉波形,以验证PWM波形的准确性。
1 软硬件环境
1.1 软件版本信息
软硬件信息 | 版本信息 |
---|---|
Renesas MCU | R7FA8D1BH(Cortex®-M85) |
Keil | MDK ARM 5.38 |
FSP 版本 | 5.3.0 |
调试工具:dap-link | CMSIS-DAP-NSLink V2.0.0 |
1.2 硬件接口
1) GPT-1: P105
2) GPT-2: P102
3) GPT-6: PA11 and PA12
2 FSP生成配置
2.1 配置参数
1)配置GPT参数(GPT1)
2)配置GPT参数(GPT2)
3) 配置GPT参数(GPT6)
2.2 创建Stack和配置PWM参数
1) 创建Stack
2) 配置参数
配置channel-1参数
配置channel-2参数
配置channel-6参数
3 FSP库函数介绍
3.1 R_GPT_Open()
函数原型:
fsp_err_t R_GPT_Open ( timer_ctrl_t *const p_ctrl,timer_cfg_t const *const p_cfg
)
初始化定时器模块并应用配置。实现timer_api_t::开放。
GPT硬件本身不支持一次性功能。当使用单镜头模式时,计时器将在请求的时间段过去后在ISR中停止。
通用定时器的GPT实现可以接受gpt_extended_cfg_t扩展参数。
应用范例:
/* Initializes the module. */err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
返回值:
Return values
FSP_SUCCESS Initialization was successful and timer has started. FSP_ERR_ASSERTION A required input pointer is NULL or the source divider is invalid. FSP_ERR_ALREADY_OPEN Module is already open. FSP_ERR_IRQ_BSP_DISABLED timer_cfg_t::mode is TIMER_MODE_ONE_SHOT or timer_cfg_t::p_callback is not NULL, but ISR is not enabled. ISR must be enabled to use one-shot mode or callback. FSP_ERR_INVALID_MODE Triangle wave PWM is only supported if GPT_CFG_OUTPUT_SUPPORT_ENABLE is 2. Selected channel does not support external count sources. External and event count sources not are available in this mode. FSP_ERR_IP_CHANNEL_NOT_PRESENT The channel requested in the p_cfg parameter is not available on this device.
3.2 R_GPT_Stop()
函数原型:
fsp_err_t R_GPT_Stop ( timer_ctrl_t *const p_ctrl )
应用案例:
/* (Optional) Stop the timer. */(void) R_GPT_Stop(&g_timer0_ctrl);
返回值
Return values
FSP_SUCCESS Timer successfully stopped. FSP_ERR_ASSERTION p_ctrl was NULL. FSP_ERR_NOT_OPEN The instance is not opened.
3.3 R_GPT_Start()
函数原型
fsp_err_t R_GPT_Start ( timer_ctrl_t *const p_ctrl )
应用范例:
/* Start the timer. */(void) R_GPT_Start(&g_timer0_ctrl);
返回值:
Return values
FSP_SUCCESS Timer successfully started. FSP_ERR_ASSERTION p_ctrl was NULL. FSP_ERR_NOT_OPEN The instance is not opened.
3.4 R_GPT_Enable()
函数原型:
fsp_err_t R_GPT_Enable ( timer_ctrl_t *const p_ctrl )
使用案例:
/* Enable captures. Captured values arrive in the interrupt. */(void) R_GPT_Enable(&g_timer0_ctrl);
返回值:
Return values
FSP_SUCCESS External events successfully enabled. FSP_ERR_ASSERTION p_ctrl was NULL. FSP_ERR_NOT_OPEN The instance is not opened.
3.5 R_GPT_PeriodSet()
函数原型:
fsp_err_t R_GPT_PeriodSet ( timer_ctrl_t *const p_ctrl,uint32_t const period_counts
)
设置提供的周期值。
如果定时器正在运行,则在下一个计数器溢出后更新周期。如果计时器停止,该函数重置计数器并更新周期。实现timer_api_t:: periodSet。
警告
如果使用周期性输出,则占空比缓冲寄存器在周期缓冲寄存器之后更新。如果在计时器运行时调用此函数,并且在处理过程中发生GPT溢出,则在处理完成后计数器溢出之前,占空比将不会是期望的50%占空比。
应用范例:
/* Get the source clock frequency (in Hz). There are 3 ways to do this in FSP:* - If the PCLKD frequency has not changed since reset, the source clock frequency is* BSP_STARTUP_PCLKD_HZ >> timer_cfg_t::source_div* - Use the R_GPT_InfoGet function (it accounts for the divider).* - Calculate the current PCLKD frequency using R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD) and right shift* by timer_cfg_t::source_div.** This example uses the 3rd option (R_FSP_SystemClockHzGet).*/uint32_t pclkd_freq_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD) >> g_timer0_cfg.source_div;/* Calculate the desired period based on the current clock. Note that this calculation could overflow if the* desired period is larger than UINT32_MAX / pclkd_freq_hz. A cast to uint64_t is used to prevent this. */uint32_t period_counts =(uint32_t) (((uint64_t) pclkd_freq_hz * GPT_EXAMPLE_DESIRED_PERIOD_MSEC) / GPT_EXAMPLE_MSEC_PER_SEC);/* Set the calculated period. */err = R_GPT_PeriodSet(&g_timer0_ctrl, period_counts);assert(FSP_SUCCESS == err);
3.6 R_GPT_DutyCycleSet()
函数原型:
fsp_err_t R_GPT_DutyCycleSet ( timer_ctrl_t *const p_ctrl,uint32_t const duty_cycle_counts,uint32_t const pin
)
在请求引脚上设置占空比。实现timer_api_t:: dutyCycleSet。
占空比在缓冲寄存器中更新。更新后的占空比在下一个周期结束后反映(计数器溢出)。
应用案例:
/* Get the current period setting. */timer_info_t info;(void) R_GPT_InfoGet(&g_timer0_ctrl, &info);uint32_t current_period_counts = info.period_counts;/* Calculate the desired duty cycle based on the current period. Note that if the period could be larger than* UINT32_MAX / 100, this calculation could overflow. A cast to uint64_t is used to prevent this. The cast is* not required for 16-bit timers. */uint32_t duty_cycle_counts =(uint32_t) (((uint64_t) current_period_counts * GPT_EXAMPLE_DESIRED_DUTY_CYCLE_PERCENT) /GPT_EXAMPLE_MAX_PERCENT);/* Set the calculated duty cycle. */err = R_GPT_DutyCycleSet(&g_timer0_ctrl, duty_cycle_counts, GPT_IO_PIN_GTIOCB);assert(FSP_SUCCESS == err);
3.7 R_GPT_OutputEnable()
函数原型:
fsp_err_t R_GPT_OutputEnable ( timer_ctrl_t *const p_ctrl,
gpt_io_pin_t pin
)
Enable output for GTIOCA and/or GTIOCB.
Return values
FSP_SUCCESS Output is enabled. FSP_ERR_ASSERTION p_ctrl or p_status was NULL. FSP_ERR_NOT_OPEN The instance is not opened.
3.8 R_GPT_PwmOutputDelaySet()
函数原型:
fsp_err_t R_GPT_PwmOutputDelaySet ( timer_ctrl_t *const p_ctrl,
gpt_pwm_output_delay_edge_t edge,
gpt_pwm_output_delay_setting_t delay_setting,
uint32_t const pin
)
Set the Output Delay setting for the PWM output pin.
Return values
FSP_SUCCESS The output delay was set. FSP_ERR_ASSERTION An input parameter was invalid. FSP_ERR_NOT_OPEN The instance is not opened. FSP_ERR_INVALID_CHANNEL The channel does not support this feature. FSP_ERR_NOT_INITIALIZED The PWM Output Delay Circuit has not been initialized. FSP_ERR_INVALID_STATE The PWM Output Delay setting cannot be updated in the current state. FSP_ERR_UNSUPPORTED This feature is not supported on this MCU.
3.9 R_GPT_OutputDisable()
函数原型:
fsp_err_t R_GPT_OutputDisable ( timer_ctrl_t *const p_ctrl,gpt_io_pin_t pin
)
Disable output for GTIOCA and/or GTIOCB.
Return values
FSP_SUCCESS Output is disabled. FSP_ERR_ASSERTION p_ctrl or p_status was NULL. FSP_ERR_NOT_OPEN The instance is not opened.