PWM python API?

Is there a python API for PWM (similar to GPIOD)?

I am having trouble writing to the pwmchip files (“enable”, “period”, ect). I keep getting permission errors like “read only”

Example code for what I have tried that returns read only erorrs
“”"
file = open(“/sys/class/pwm/pwmchip0/pwm0/enable”, “w”)
file.write(“1”)
“”"

I have tried chmod but it doesnt like that either

Module: Verdin IMX8M Plus
Carrier: Verdin Dev Board V1.1E

Have you written '‘0’ to the /sys/class/pwm/pwmchip1/? before accessing to the /sys/class/pwm/pwmchip0/pwm0/xxxx

Does PWM act as expected when you test it from the command line as it shown here?

Hi Alex, thanks for the speedy reply.

PWM outputs perfectly through command line.

I have 0 written to export file from the command line

Coud youplease provide a full description of Linux version you are using on Verdin module (for example:
Linux Reference Minimal
Downstream SoC vendor based kernel
Wayland/XWayland graphics back-end supported but not included
5.7.2+build.21
Please also share a minimal reproducible example of your Python program.

Software summary

Bootloader: U-Boot
Kernel version: 5.15.77-6.3.0+git.ddc6ca4d76ea #1-TorizonCore SMP PREE MPT Thu Jun 29 10:14:22 UTC 2023
Kernel command line: root=LABEL=otaroot rootfstype=ext4 quiet logo.nologo v t.global_cursor_default=0 plymouth.ignore-serial-consoles splash fbcon=map:3 ost ree=/ostree/boot.1/torizon/ee3fafa8509e5d129b5f0391fe21f05fe84d1aba2db982df57876 66f787de5a9/0
Distro name: NAME=“TorizonCore”
Distro version: VERSION_ID=6.3.0-build.4
Hostname: verdin-imx8mp-14871539

Hardware info

HW model: Toradex Verdin iMX8M Plus on Verdin Development Board
Toradex version: 0063 V1.1A
Serial number: 14871539
Processor arch: aarch64

Linux Info

PRETTY_NAME=“Ubuntu 22.04.3 LTS”
NAME=“Ubuntu”
VERSION_ID=“22.04”
VERSION=“22.04.3 LTS (Jammy Jellyfish)”
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian

Code

import os
import subprocess

def pwm_func():

# files = [f for f in os.listdir(dir)]
# print(files)

file = open("/sys/class/pwm/pwmchip0/export", "w")
file.write('0')

file = open("/sys/class/pwm/pwmchip0/pwm0/enable", "w")
file.write('0')



# path = "/sys/class/pwm/pwmchip0/pwm0/enable"
# text = '1'
# # subprocess.run(['echo ', text, ' > ', path], shell = True, check = True)

# path = "/sys/class/pwm/pwmchip0/pwm0/enable"
# text = '1'

# # Construct the entire shell command as a single string
# command = f'echo {text} > {path}'

# # Run the command with shell=True
# subprocess.run(command, shell=True, check=True)

Are you running Python from a container or directly on Torizon Core ?

Running it from a container. It is setup with the Torizon IDE extension for visual studio code

According to your statement, “PWM outputs perfectly through the command line” - has the command shell you used for testing been run inside the container? Have you exposed HW to the container? Please refer to this article.

It appears that I need to “expose” the hardware to the container as I have done with the gpiochip in the docker-compose.yml. Can you point me to an example of how to add the pwm modules properly?


version: “3.9”
services:
emittancescanner-debug:
build:
context: .
dockerfile: Dockerfile.debug
image: ${LOCAL_REGISTRY}:5002/emittancescanner-debug:${TAG}
ports:
- 6502:6502
- 6512:6512
devices:
- dev/gpiochip0
- dev/gpiochip1
- dev/gpiochip2
- dev/gpiochip3
- dev/gpiochip4
- dev/gpiochip5
# - dev/pwmchip0
# - dev/pwmchip1

We have an article on using PWMs in containers here: How to Use PWM on TorizonCore | Toradex Developer Center

Notice in the article we add the PWM file paths as volumes not as devices. This is because unlike other peripherals PWMs traditionally don’t have entries in /dev, but in /sys as you have noticed. Once this is done and the PWM file path is made available in the container then the permissions should work out, assuming your container is based on top of one of our Debian containers.

Best Regards,
Jeremias

Thanks, that did the trick

volumes:
  - /sys/class/pwm/pwmchip0:/sys/class/pwm/pwmchip0

Glad I was able to help!