From e52ad4a912bc91bd6d3f2d43b7708c88a68f620a Mon Sep 17 00:00:00 2001 From: Dozingfiretruck <1473454180@qq.com> Date: Fri, 19 Mar 2021 15:17:06 +0800 Subject: [PATCH] =?UTF-8?q?add:pwm=E5=8D=A0=E7=A9=BA=E6=AF=94=E6=B5=AE?= =?UTF-8?q?=E7=82=B9=E6=95=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luat/include/luat_pwm.h | 7 +++++++ luat/modules/luat_lib_pwm.c | 5 +++++ luat/rtt/luat_pwm_rtt.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/luat/include/luat_pwm.h b/luat/include/luat_pwm.h index 486bc7631..dad887595 100644 --- a/luat/include/luat_pwm.h +++ b/luat/include/luat_pwm.h @@ -4,7 +4,14 @@ #include "luat_base.h" +//#define USE_PWM_FLOAT + +#if defined USE_PWM_FLOAT +int luat_pwm_open(int channel, float period, float pulse); +#else int luat_pwm_open(int channel, size_t period, size_t pulse); +#endif + int luat_pwm_close(int channel); #endif diff --git a/luat/modules/luat_lib_pwm.c b/luat/modules/luat_lib_pwm.c index e170cbdb3..7076fd9e7 100644 --- a/luat/modules/luat_lib_pwm.c +++ b/luat/modules/luat_lib_pwm.c @@ -20,8 +20,13 @@ pwm.open(5, 1000, 50) */ static int l_pwm_open(lua_State *L) { int channel = luaL_checkinteger(L, 1); +#if defined USE_PWM_FLOAT + float period = luaL_checkinteger(L, 2); + float pulse = luaL_checknumber(L, 3); +#else size_t period = luaL_checkinteger(L, 2); size_t pulse = luaL_checkinteger(L, 3); +#endif if (luat_pwm_open(channel, period, pulse) == 0) { lua_pushboolean(L, 1); } diff --git a/luat/rtt/luat_pwm_rtt.c b/luat/rtt/luat_pwm_rtt.c index b89eb1201..11faea2e0 100644 --- a/luat/rtt/luat_pwm_rtt.c +++ b/luat/rtt/luat_pwm_rtt.c @@ -24,7 +24,7 @@ static int luat_pwm_rtt_init() { name[1] = 'w'; name[2] = 'm'; name[4] = 0x00; - + // 搜索pwm0,pwm1,pwm2 .... for (size_t i = 0; i < DEVICE_ID_MAX; i++) { @@ -62,6 +62,31 @@ INIT_COMPONENT_EXPORT(luat_pwm_rtt_init); // - I: 十位 // - N: 个位 // @return -1 打开失败。 0 打开成功 +#if defined USE_PWM_FLOAT +int luat_pwm_open(int channel, float period, float pulse) { + int i = channel / 10; + int n = channel - (i * 10); + if (i < 0 || i >= DEVICE_ID_MAX ) + return -1; + if (period < 1 || period > 1000000) + return -1; + if (pulse > 100) + pulse = 100; + + struct rt_device_pwm *dev = pwm_devs[i]; + if(RT_NULL == dev) + return -1; + + // 与Luat的定义不同, rtt的period和pulse是按时长作为单位的,单位是ns,即1/1000000000秒 + // rt_period = 1000000000 / luat_period + // rt_pulse = (1000000000 / luat_period) * pulse / 100 + + rt_pwm_set(dev, n, 1000000000 / period, (1000000000 / period) * pulse / 100); + rt_pwm_enable(dev, n); + + return 0; +} +#else int luat_pwm_open(int channel, size_t period, size_t pulse) { int i = channel / 10; int n = channel - (i * 10); @@ -79,12 +104,13 @@ int luat_pwm_open(int channel, size_t period, size_t pulse) { // 与Luat的定义不同, rtt的period和pulse是按时长作为单位的,单位是ns,即1/1000000000秒 // rt_period = 1000000000 / luat_period // rt_pulse = (1000000000 / luat_period) * pulse / 100 - + rt_pwm_set(dev, n, 1000000000 / period, (1000000000 / period) * pulse / 100); rt_pwm_enable(dev, n); return 0; } +#endif // @return -1 关闭失败。 0 关闭成功 int luat_pwm_close(int channel) { @@ -96,7 +122,7 @@ int luat_pwm_close(int channel) { struct rt_device_pwm *dev = pwm_devs[i]; if(RT_NULL == dev) return -1; - + rt_pwm_disable(dev, n); return 0; -- Gitee