1. Hardware introduction of functional modules
PWM is the abbreviation of Pulse Width Modulation, which means pulse width modulation in Chinese. It realizes dimming, frequency modulation and other functions by adjusting the time (width) of the effective level of the unit cycle.
In the CPK-RA6M4 development board, the control pin of user LED3 is P106.
In the FSP chip configuration diagram, we can see that the timer channel associated with P106 is GTIOC8B.
The connection of the development board still uses the front serial port 7 (P613, P614)
2. Instructions for use of functional modules
Open RTT Studio, and based on the original project, open FSP.
Create a new TIMER, General PWM
Select the newly created g_timer0
Modify the parameters as shown below
The Channel here should be bound to g_timer, and the two values should be consistent. You can see that the bottom pin is P106, which is consistent with the control pin of LED3.
After the settings are completed, click Generate Project Content above to generate the relevant configuration in the project document.
Next, open RTT Settings and enable PWM and PWM8
The GPT8 here matches the g-timer8 of FSP mentioned above.
Then save Ctrl-S.
In the RTT development documentation https://www.rt-thread.org/document/site/#/, through Standard Documents->Device and Driver->PWM Device
Copy the sample code.
3. Code Analysis and Description
In the RTT project tree, find SRC, right-click and create a new file
Name it pwm.c, paste the PWM sample code, and make some modifications
#include <rtthread.h>
#include <rtdevice.h>
#define PWM_DEV_NAME "pwm8" /* PWM设备名称 */
#define PWM_DEV_CHANNEL 0 /* PWM通道 */
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
rt_uint32_t period, pulse, dir;
//static int pwm_led_sample(int argc, char *argv[])
int pwm_led_sample(void)
{
period = 500000; /* 周期为0.5ms,单位为纳秒ns */
dir = 1; /* PWM脉冲宽度值的增减方向 */
pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
/* 查找设备 */
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
return RT_ERROR;
}
/* 设置PWM周期和脉冲宽度默认值 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/* 使能设备 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
return 0;
}
void PWM_Led(void)
{
//rt_thread_mdelay(50);
if (dir)
{
pulse += 5000; /* 从0值开始每次增加5000ns */
}
else
{
pulse -= 5000; /* 从最大值开始每次减少5000ns */
}
if (pulse >= period)
{
dir = 0;
}
if (0 == pulse)
{
dir = 1;
}
/* 设置PWM周期和脉冲宽度 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
Here, special mention should be made of the PWM device name and channel. The device name must be pwm8, and the channel must be changed to 0. Many channels have failed in the previous tests .
#define PWM_DEV_NAME "pwm8" /* PWM device name */
#define PWM_DEV_CHANNEL 0 /* PWM channel */
Then modify the main function in hal_entry.c
void hal_entry(void)
{
rt_kprintf("\nHello RT-Thread!\n");
pwm_led_sample();
while (1)
{
//rt_pin_write(LED3_PIN, PIN_HIGH);
// rt_thread_mdelay(500);
// rt_pin_write(LED3_PIN, PIN_LOW);
rt_thread_mdelay(50);
PWM_Led();
}
}
Here you can choose to start PWM directly without going through msh (you can still start it through pwm_led_sample in msh)
The following is compilation and downloading. There are two warning messages during the compilation process. They are the declarations of two calling functions in the hal_entry main function. They have little impact on the results, so I am too lazy to deal with them.
4. Experience
It is very convenient to use FSP and RTT Settings to handle hardware. There is no need to write driver documents and it can run directly. This is very convenient and fast. It is also the main reason why I like RTT very much. It is very helpful for today's rapid development.
But there must be detailed documentation support, for example, the description of PWM settings for PWM devices, timers and channels. If it is missing, you can only try them one by one, which is very frustrating.
PWM