1194 views|0 replies

155

Posts

1

Resources
The OP
 

[Luckfox RV1106 Linux Development Board Review] 7. PWM Control - Python and Device Tree 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

6. PWM control - command and C mode

In the previous review, I used console commands to control PWM to light up the LED, and used C program to achieve the breathing light effect. This review continues to implement it in Python and device tree.

1. Python controls PWM

The Buildroot image compiled based on Luckfox SDK contains Python3 and the python-periphery library. You can use this library to complete the peripheral driver under the Linux system. The corresponding code is provided by Luckfox Wiki, and PWM10 is still used.

# 导入periphery.PWM库
from periphery import PWM
import time

# 创建一个PWM对象,使用PWM chip-10,channel-0
pwm = PWM(10, 0)

try:
    # 设置PWM的频率为1000Hz
    pwm.frequency = 1000
    # 设置PWM的初始占空比为0
    pwm.duty_cycle = 0
    # 设置PWM的极性为正常
    pwm.polarity = "normal"
    # 启用PWM输出
    pwm.enable()

    # 定义方向变量,初始值为1,表示增加占空比的方向
    direction = 1

    # 无限循环,持续改变PWM的占空比
    while True:
        # 每次循环增加或减少占空比0.01,具体方向由direction变量决定
        pwm.duty_cycle += 0.01 * direction
        # 保留两位小数,确保占空比的精度
        pwm.duty_cycle = round(pwm.duty_cycle, 2)
        # 当占空比达到1时,改变方向为减少占空比
        if pwm.duty_cycle == 1.0:
            direction = -1
        # 当占空比达到0时,改变方向为增加占空比
        elif pwm.duty_cycle == 0.0:
            direction = 1

        # 每次循环后暂停0.05秒,控制PWM的速度变化
        time.sleep(0.05)

# 当用户按下Ctrl+C时,捕获键盘中断异常并退出循环
except KeyboardInterrupt:
    pass

# 不论程序是否正常结束,最后都要关闭PWM通道以释放资源
finally:
    pwm.close()

Figure 7-1 Push pwm.py to the development board and execute

2. Modify the device file to enable PWM0

The PWM section of the document mentions that the default setting on the development board is PWM10, which corresponds to the GPIO1_C6_d pin on the Pro Max board. It also gives a method for modifying the device file to enable the PWM function of other pins.

There are many RV1106 related device files in the SDK. A bold guess is "rv1106g-luckfox-pico-pro-max.dts". However, for safety, we still follow the document method to check the relevant setting values of the configuration file to determine the device file corresponding to the board. Because it is a Buildroot system on Pro Max NAND Flash, the configuration file is "../luckfox-pico/project/cfg/BoardConfig_IPC/BoardConfig-SPI_NAND-Buildroot-RV1106_Luckfox_Pico_Pro_Max-IPC.mk". The fourth item of the file is the device file name:

Figure 7-2 rv1106 related device files

Figure 7-3 Configuration file shows the device file corresponding to the Pro Max board

In addition, from the pin diagram, you can see that PWM0 and I2C3_SDA are the same pin, so when editing the device file, you need to comment out the I2C3 code (because I2C3 is enabled by default), and then add the PWM0 code (the file actually comments the relevant code, just remove the comments). Here, please note that the Pro Max board is PWM0_M1 as seen in the pin diagram, and the device file pins have m0 and m1, so don't mistakenly select m0.

Figure 7-4 PWM0 and I2C3 shared pins

Figure 7-5 How to modify the device file

After modifying and saving the device file, return to the root path of the SDK and continue the process of compiling the kernel: ./build.sh lunch-->select 8 (for Pro Max)-->./build.sh. Because I had compiled the entire system before (which took 3 hours), this time I built it in just a few minutes - the document said ./build.sh kernel only compiles the kernel, and I forgot to add the kernel parameter, but it took less than 10 minutes to compile.

Then copy the image from the virtual machine to the local machine, and then use the tool SocToolKit to burn it to the development board. After that, restart the development board and enter the ADB console. Execute the command ls -l /sys/class/pwm to get:

Figure 7-6 PWM0 is seen after updating the device tree

If you want to test the program, just modify the instance definition of pwm in the previous Python code and pass the constructor parameter to PWM(0, 0).

Figure 7-7 Modifying Python code

This post is from Domestic Chip Exchange
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Design experience exchange: Matters needing attention in AD sampling

Mainly for high-precision measurement AD. 1: The reference voltage needs to be accurate enough, and it is recommende ...

ARM Embedded Learning Resource Sharing——"ARM Embedded Architecture and Interface Technology".pdf

This content is originally created by EEWORLD forum user ritinde . If you need to reprint or use it for commercial pu ...

Digi XBee 3 supports MicroPython programming

Digi mentions "MicroPython edge computing capabilities" at Embedded World 2020 The tiny Digi XBee 3 offers a modular ap ...

Show development board, STM32H747-DK, NUCLEO-L552ZE

514135 514134 514136 There are several distinctive wiring 514137 514138

[Runhe Neptune Review] Five PWM

PWM: Pulse Width Modulation It is to periodically control the time (duty cycle) of IO pulling high and low to control th ...

When studying the working principle of RCD circuit, what is the energy accumulated in the parasitic inductance?

Is it the parasitic inductance of the switch? 626540 Then I saw a circuit similar to RCD function in the PFC circuit. Is ...

【Beetle ESP32-C3】2. PWM breathing light (Arduino)

This article introduces the process of PWM development of Beetle ESP32-C3 in Arduino IDE to realize the breathing light ...

Some use cases of MicroPython mpremote tool, chat

# Some use cases for the MicroPython mpremote tool, I've tried a lot of different third-party micropython tools, some ar ...

[DigiKey "Smart Manufacturing, Non-stop Happiness" Creative Competition] Unboxing Post

After more than ten days, it finally arrived. I quickly opened the box and found that the packaging was still as tight a ...

Fill in another interesting thermal imaging

High resolution does work better 818014

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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