前面已经分析过瑞萨适配RTT pwm框架的通用实现,这里在野火的RA6M5上把PWM模块的配置方法也实现一遍。
芯片PWM资源分配情况
其中,GTIOC0~GTIOC3是32位的pwm,GTIOC4~GTIOC9是16位的pwm。
配置过程
RTT配置添加
Kconfig中添加如下信息:
另外,由于之前配板卡时,已经在drv_config.h中预留了PWM编译引入的配置,因此不需要再去添加这部分。
配置代码
在emu中运行menuconfig,打开PWM配置,由于启明6M5引出的GPT0默认被RTT串口占用,GPT9无对应引脚,因此暂不设置该接口。
运行scons --target=mdk5,把dac部分导入工程
配置PWM
打开新生成的keil工程后运行RASC,增加PWM相关配置。
需要注意的是,RA6M5的PWM是通过定时器实现的,因此PWM配置依赖于定时器配置。
通用部分
配置GPT1
配置GPT2
配置GPT3
配置GPT4
配置GPT5
配置GPT6
配置GPT7
配置GPT8
生成代码配置
修改代码
关闭rtt默认的点灯代码(位于hal_entry.c)
/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2021-10-10 Sherman first version */ #include <rtthread.h> #include "hal_data.h" #include <rtdevice.h> #define LED_PIN BSP_IO_PORT_04_PIN_00 /* RED LED pins */ void hal_entry(void) { rt_kprintf("\nHello RT-Thread!\n"); while (1) { //rt_pin_write(LED_PIN, PIN_HIGH); rt_thread_mdelay(500); //rt_pin_write(LED_PIN, PIN_LOW); rt_thread_mdelay(500); } }
编译固件
结果验证
由于启明6M5引出的金针座仅J29方便使用母头杜邦线,因此仅测试J29座子上的PWM功能,以及呼吸灯,蜂鸣器效果。
\ | / - RT - Thread Operating System / | \ 5.2.0 build Jun 10 2024 17:21:33 2006 - 2024 Copyright by RT-Thread team Hello RT-Thread! msh >pwm probe pwm2 probe pwm2 success Usage: pwm probe <device name> - probe pwm by name pwm enable <channel> - enable pwm channel pwm disable <channel> - disable pwm channel pwm get <channel> - get pwm channel info pwm set <channel> <period> <pulse> - set pwm channel info pwm phase <channel> <phase> - set pwm phase pwm dead_time <channel> <dead_time> - set pwm dead time msh >pwm enable 0 pwm2 channel 0 is enabled success msh >pwm set 0 1000000000 500000000 pwm info set on pwm2 at channel 0 msh >pwm probe pwm3 probe pwm3 success Usage: pwm probe <device name> - probe pwm by name pwm enable <channel> - enable pwm channel pwm disable <channel> - disable pwm channel pwm get <channel> - get pwm channel info pwm set <channel> <period> <pulse> - set pwm channel info pwm phase <channel> <phase> - set pwm phase pwm dead_time <channel> <dead_time> - set pwm dead time msh >pwm enable 0 pwm3 channel 0 is enabled success msh >pwm set 0 1000000000 500000000 pwm info set on pwm3 at channel 0 msh >pwm probe pwm4 probe pwm4 success Usage: pwm probe <device name> - probe pwm by name pwm enable <channel> - enable pwm channel pwm disable <channel> - disable pwm channel pwm get <channel> - get pwm channel info pwm set <channel> <period> <pulse> - set pwm channel info pwm phase <channel> <phase> - set pwm phase pwm dead_time <channel> <dead_time> - set pwm dead time msh >pwm enable 0 pwm4 channel 0 is enabled success msh >pwm set 0 655250 327680 pwm info set on pwm4 at channel 0 msh >pwm probe pwm6 probe pwm6 success Usage: pwm probe <device name> - probe pwm by name pwm enable <channel> - enable pwm channel pwm disable <channel> - disable pwm channel pwm get <channel> - get pwm channel info pwm set <channel> <period> <pulse> - set pwm channel info pwm phase <channel> <phase> - set pwm phase pwm dead_time <channel> <dead_time> - set pwm dead time msh >pwm enable 0 pwm6 channel 0 is enabled success msh >pwm set 0 655250 327680 pwm info set on pwm6 at channel 0 msh >pwm set 0 655250 32768 pwm info set on pwm6 at channel 0 msh >pwm set 0 655250 3276 msh >pwm probe pwm8 probe pwm8 success Usage: pwm probe <device name> - probe pwm by name pwm enable <channel> - enable pwm channel pwm disable <channel> - disable pwm channel pwm get <channel> - get pwm channel info pwm set <channel> <period> <pulse> - set pwm channel info pwm phase <channel> <phase> - set pwm phase pwm dead_time <channel> <dead_time> - set pwm dead time msh >pwm enable 0 pwm8 channel 0 is enabled success msh >pwm set 0 655250 3276 pwm info set on pwm8 at channel 0 msh >pwm set 0 655250 32768 pwm info set on pwm8 at channel 0 msh >pwm set 0 655250 327680 pwm info set on pwm8 at channel 0 msh >
运行过程讲解:
在使能PWM2时,输入 pwm set 0 1000000000 500000000 时,用示波器查看,发现P103可调制出1Hz 50%占空比波形,与期望一致
在使能PWM3时,输入 pwm set 0 1000000000 500000000 时,发现蓝灯和绿灯以1Hz为周期交替亮灭,与期望一致
在使能PWM4时,输入 pwm set 0 655250 327680 时,用示波器查看,发现P608可调制出2000Hz 50%占空比波形
在使能PWM6时,输入 pwm set 0 655250 327680 时,发现红灯变暗
在使能PWM6时,输入 pwm set 0 655250 32768 时,发现红灯继续变暗
在使能PWM6时,输入 pwm set 0 655250 3276 时,发现红灯暗的几乎无感
在使能PWM8时,输入 pwm set 0 655250 3276 时,蜂鸣器响
在使能PWM8时,输入 pwm set 0 655250 32768 时,蜂鸣器响的声音变化
在使能PWM8时,输入 pwm set 0 655250 327680 时,蜂鸣器响的声音继续变化
PWM5和PWM7由于缺乏引线,未测试