1551 views|2 replies

155

Posts

1

Resources
The OP
 

[Luckfox RV1106 Linux Development Board Review] 6. PWM Control - Command and C Method [Copy link]

 

My post [Luckfox RV1106 Linux Development Board Review] link:

1. Unboxing and testing

2. SDK acquisition and image compilation

3. GPIO lighting

4. Access the Internet through PC shared network and light up Python in Ubuntu

5. Compile the Buildroot system and test the camera

After testing the Luckfox Pro Max with camera and RKNN image recognition, I plan to experiment with various interfaces of the board according to the evaluation plan. For this evaluation of PWM, I still refer to the PWM article of Luckfox Wiki: https://wiki.luckfox.com/zh/Luckfox-Pico/Luckfox-Pico-PWM .

1. Preparation for PWM evaluation

First look at the PWM pin distribution of the board. The manual indicates that the system defaults to PWM10, which is the GPIO1_C6_d pin.

Figure 6-1 Pro Max board IO distribution

Then, as in my third GPIO review, connect an LED through a breadboard for a breathing light test. The pin (board pin 5) is connected to the LED cathode, and the LED anode is connected in series with a 1k resistor to 3V3 (board pin 36).

The local Win10 system still logs in to the development board through ADB and uses the echo command to operate PWM. During the test, I personally felt that the execution order of the test commands written in the manual was a bit messy. After several attempts, the commands are summarized as follows:

// 查看PWM设备
# ls -l /sys/class/pwm
total 0
lrwxrwxrwx    1 root     root             0 Jan  1 12:00 pwmchip10 -> ../../devices/platform/ff490020.pwm/pwm/pwmchip10
lrwxrwxrwx    1 root     root             0 Jan  1 12:00 pwmchip11 -> ../../devices/platform/ff490030.pwm/pwm/pwmchip11
lrwxrwxrwx    1 root     root             0 Jan  1 12:00 pwmchip5 -> ../../devices/platform/ff360010.pwm/pwm/pwmchip5
lrwxrwxrwx    1 root     root             0 Jan  1 12:00 pwmchip6 -> ../../devices/platform/ff360020.pwm/pwm/pwmchip6

// 将PWM10_M1(GPIO1_C6_d)导出到用户空间
# echo 0 > /sys/class/pwm/pwmchip10/export
// 查看PWM10单个通道的属性文件(不做上一步导出到用户空间,这条命令会失败)
# ls /sys/class/pwm/pwmchip10/pwm0/
capture      enable       period       power
duty_cycle   output_type  polarity     uevent

// 使能PWM10通道,但是会提示参数错误
# echo 1 > /sys/class/pwm/pwmchip10/pwm0/enable
sh: write error: Invalid argument

// 但是如果先设置PWM周期——这里单位ns,1000000ns即1KHz频率
# echo 1000000 > /sys/class/pwm/pwmchip10/pwm0/period
// 然后再使能PWM10就成功了——必须先使能,不然没有PWM输出
# echo 1 > /sys/class/pwm/pwmchip10/pwm0/enable

// 此时输出最高值LED最亮(LED是低电平点亮),输出0 LED完全熄灭
# echo 1000000 > /sys/class/pwm/pwmchip10/pwm0/duty_cycle
# echo 0 > /sys/class/pwm/pwmchip10/pwm0/duty_cycle

// 设置PWM极性正常
# echo "normal" > /sys/class/pwm/pwmchip10/pwm0/polarity
// 结果发现输出0 LED最亮了,这说明PWM初始是inversed状态
# echo 0 > /sys/class/pwm/pwmchip10/pwm0/duty_cycle

// 设置PWM极性翻转
# echo "inversed" > /sys/class/pwm/pwmchip10/pwm0/polarity
// 果然又恢复输出最高值LED最亮
# echo 1000000 > /sys/class/pwm/pwmchip10/pwm0/duty_cycle

2. C program controls PWM

Still using the C code provided in the document, create the pwm.c source file in the virtual machine, and then compile it using the compilation tool chain provided in the luckfox-pico SDK to get the program file pwm. Next, copy it to the local machine, and then push it to the directory app created by the development board through adb. Finally, give pwm execution permission on the development board, and then run the program to get the breathing light experimental effect.

Figure 6-2 Development board executes PWM program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// 定义PWM路径
#define PWM_PATH "/sys/class/pwm/pwmchip10"
// 定义周期(纳秒)
#define PERIOD_NS 1000000
// 定义最小占空比(纳秒)
#define MIN_DUTY_CYCLE_NS 0
// 定义最大占空比(纳秒)
#define MAX_DUTY_CYCLE_NS 1000000

int main() {
    // 打开PWM的export文件,准备导出PWM通道
    FILE *pwm_export = fopen(PWM_PATH "/export", "w");
    if (!pwm_export) {
        perror("Failed to open PWM export");
        return 1;
    }
    fprintf(pwm_export, "0"); // 导出第一个PWM通道(索引为0)
    fclose(pwm_export);

    // 打开PWM通道的周期配置文件,设置周期为PERIOD_NS纳秒
    FILE *period_file = fopen(PWM_PATH "/pwm0/period", "w");
    if (!period_file) {
        perror("Failed to open PWM period");
        return 1;
    }
    fprintf(period_file, "%d", PERIOD_NS);
    fclose(period_file);

    // 打开PWM通道的使能文件,启用PWM通道
    FILE *enable_file = fopen(PWM_PATH "/pwm0/enable", "w");
    if (!enable_file) {
        perror("Failed to open PWM enable");
        return 1;
    }
    fprintf(enable_file, "1"); // 启用PWM通道
    fclose(enable_file);

    int direction = 1; // 方向,决定占空比是增加还是减少
    int duty_cycle_ns = 0; // 当前占空比,初始值为0

    while (1) { // 无限循环,使PWM保持持续输出
        duty_cycle_ns += 10000 * direction; // 根据方向增加或减少占空比的值
        if(duty_cycle_ns == MAX_DUTY_CYCLE_NS) // 如果达到最大占空比
            direction = -1; // 改变方向,从增加变为减少
        else if(duty_cycle_ns == MIN_DUTY_CYCLE_NS) // 如果达到最小占空比
            direction = 1; // 改变方向,从减少变为增加

        // 打开PWM通道的占空比配置文件,设置占空比为duty_cycle_ns纳秒
        FILE *duty_cycle_file = fopen(PWM_PATH "/pwm0/duty_cycle", "w");
        if (!duty_cycle_file) {
            perror("Failed to open PWM duty cycle");
            return 1;
        }
        fprintf(duty_cycle_file, "%d", duty_cycle_ns);
        fclose(duty_cycle_file);

        usleep(50000); // 暂停50000微秒(即0.05秒)再更新占空比,以实现PWM效果
    }

    // 最后,别忘了在程序结束前取消导出PWM通道,以释放资源
    FILE *pwm_unexport = fopen(PWM_PATH "/unexport", "w");
    if (!pwm_unexport) {
        perror("Failed to open PWM unexport");
        return 1;
    }
    fprintf(pwm_unexport, "0"); // 取消导出第一个PWM通道(索引为0)
    fclose(pwm_unexport);

    return 0; // 主函数返回0,表示程序正常结束
}

This post is from Domestic Chip Exchange

Latest reply

Isn't this method of directly calling files rather troublesome? There should be a corresponding third-party C library, right?   Details Published on 2024-1-31 18:20
 
 

6748

Posts

2

Resources
2
 

Isn't this method of directly calling files rather troublesome? There should be a corresponding third-party C library, right?

This post is from Domestic Chip Exchange

Comments

I have been looking up some knowledge about Buildroot these days. It should be a simplified version of Linux based on the file system, and it should not provide an interface library. According to the official information, in addition to operating device files, it is also about updating the device tree. However, the Ubuntu device tree can be loaded separately.  Details Published on 2024-1-31 20:46
 
 
 

155

Posts

1

Resources
3
 
wangerxian published on 2024-1-31 18:20 Is this way of directly calling files more troublesome? There should be a corresponding third-party C library, right?

I have been looking up some knowledge about Buildroot these days. It should be a simplified version of Linux based on the file system, and it should not provide an interface library. According to the official information, in addition to operating device files, it is also about updating the device tree. And unlike Ubuntu, the device tree can be loaded separately. After updating the device tree, the kernel must be recompiled. I plan to try the device tree tomorrow. I guess after compiling the kernel, I may only need to update some img files, such as rootfs.

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Optimum load impedance——PA

When designing an amplifier, the first problem is to determine the optimum load impedance. The reason for the optimum im ...

R329 Tutorial 1 | Zhouyi AIPU Deployment and Simulation Tutorial

R329 Development Board Tutorial Series This series of tutorials mainly introduces the deployment of AI models on the R32 ...

Silicon Labs Development Kit Review – First Look

This period of time can really be described as a mess. My children went to school, I moved, and I changed jobs. All ...

ssd1306 Chinese and picture display

This post was last edited by lemon1394 on 2021-8-17 23:19 I have found many Chinese displays of ssd1306 introduced on t ...

Relationship between PN conduction voltage drop and current and temperature

*) , the E junction is affected by temperature, and the change in on-state voltage drop is related to Is and Ic The cond ...

Does the material of the transformer determine the operating frequency of the transformer?

The efficiency of the 50HZ transformer is very low at high frequency. I don't know why. Some say it is caused by eddy cu ...

The Pentagon released UFO footage. Are there aliens?

On May 17, local time, a committee of the U.S. House of Representatives held a public hearing on the topic of UFOs. At t ...

RTD Circuit Design

RTD Circuit Design PT100/1000 meaning Common RTDs are PT100 and PT1000. 100 and 1000 mean that when the temperature is 0 ...

MOS tube selection problem

I want to use a SOT23 MOS tube to drive a 24V/180MA solenoid valve. The maximum power dissipation here is 2.5W, but 24*0 ...

【ST NUCLEO-U083RC】Study Notes 06 (Low Power Stop Mode)

停止模式有三种:停止0,停止1,停止2 ```C Stop 0, Stop 1, and Stop 2 modes The Stop modes achieve a lowest power consump ...

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list