Trying Some PWM Source with the /dev/bone/pwm/1/b/ Accesses

Hello,

I am currently working from some Python3 source I found years ago from Mark Yoder. Here is the breakdown of the changes I made…w/out importing glob w/ its use in play:

#!/usr/bin/python3

import pathlib
from time import sleep

class PWM:

    def start(duty, freq=2000, polarity=0):
        """ Set up and start the PWM channel. """

        path = pathlib.Path("/dev/bone/pwm/1/b")
        if path == None:
            return None

        period_ns = 1e9 / freq
        duty_ns = (period_ns * (duty / 100.0))

        # export
        try:
            fd = open(path + "/export", 'w')
            fd.write(str(path))
            fd.close()
        except:
            pass
        time.sleep(0.05)    # Give export a chance

        # Duty Cycle - Set to 0 so period can be changed
        try:
            fd = open(pathpwm + "/duty_cycle", 'w')
            fd.write('0')
            fd.close()
        except:
            pass

        # Period
        fd = open(pathpwm + "/period", 'w')
        fd.write(str(int(period_ns)))
        fd.close()

        # Duty Cycle
        fd = open(pathpwm + "/duty_cycle", 'w')
        fd.write(str(int(duty_ns)))
        fd.close()

        # Enable
        fd = open(pathpwm + "/enable", 'w')
        fd.write('1')
        fd.close()

        return 'Started'

    def set_frequency(freq):
        """ Change the frequency

        frequency - frequency in Hz (freq > 0.0) """
        path = Path("/dev/bone/pwm/1")
        pathpwm = path + "/b"

        # compute current duty cycle
        fd = open(pathpwm + "/duty_cycle", 'r')
        duty_cycle_ns = int(fd.read())
        fd.close()

        fd = open(pathpwm + "/period", 'r')
        period_ns = int(fd.read())
        fd.close()

        duty_cycle = duty_cycle_ns/period_ns          # compute current duty cycle as fraction
        period_ns = 1e9 / freq                  # compute new period
        duty_cycle_ns = period_ns*duty_cycle # compute new duty cycle as fraction of period

    # print('duty_cycle: ' + str(duty_cycle))
    # print('period_ns: ' + str(period_ns))
    # print('duty_cycle_ns: ' + str(duty_cycle_ns))

        # Duty Cycle - Set to 0
        fd = open(pathpwm + "/duty_cycle", 'w')
        fd.write('0')
        fd.close()

        # Period
        fd = open(pathpwm + "/period", 'w')
        fd.write(str(int(period_ns)))
        fd.close()

        # Duty Cycle
        fd = open(pathpwm + "/duty_cycle", 'w')
        fd.write(str(int(duty_cycle_ns)))
        fd.close()

    def set_duty_cycle(duty):
        """ Change the duty cycle.

        dutycycle - between 0.0 and 100.0 """
        path = get_pwm_path()
        pathpwm = path + "/b"

        fd = open(pathpwm + "/period", 'r')
        period_ns = int(fd.read())
        fd.close()

        duty_cycle_ns = duty/100 * period_ns
        # Duty Cycle
        fd = open(pathpwm + "/duty_cycle", 'w')
        fd.write(str(int(duty_cycle_ns)))
        fd.close()

    def stop():
        """ Stop the PWM channel. """

        path = pwm_path()
        pathpwm = path + "/b"

        # Disable
        fd = open(pathpwm + "/enable", 'w')
        fd.write('0')
        fd.close()

        # unexport
        fd = open(path + "/unexport", 'w')
        fd.write(str(path[1]))
        fd.close()

I am currently working w/ an updated, Bullseye image and kernel 5.10.x. I am seemingly getting the above source to run w/out error. This is the good news.

Currently though, I am running some source outside of this small file to call this file into it for use.

Both files work but do not light my LED via PWM. Anyway, I cannot remember exactly where in the www I got the file but I know it is from Mr. Yoder.

Seth

P.S. If you or anyone you know may be able to point me in the correct direction, please let me know. I tried config-pin p9.16 pwm and this is not the issue. GPIO works! Oh and I am using a BBB Rev. C.

My other file looks similar to this…


import MyOtherFile
from time import sleep

while True:
    ration = int(input("Enter Your Favorite Number...: ")
    if ration == 0:
        PWM.start = 0
        PWM.set_frequency  = 50
        PWM.set_duty_cycle = 75
        sleep(5)
    else:
        pass

I am starting small here and getting setbacks. There is more to the source but I have removed the other glob portions in the first file and removed other functions that glob was using to get file access.

based on irc, this works now right? make sure to update your post here…

Regards,

1 Like

Hello Everyone,

What I was trying does not work…

For one:

The items being opened by the file descriptors (fd) did not need to be opened.

This actually caused a permissions error. Since the open option was used and the file(s) already existed, I was getting returns on output of permission errors.

Seth

P.S. I will keep trying w/ this bundled source but there was a nice fellow on IRC that seemingly wrote a library for PWM willy-nilly and showed me. I tested it w/ config-pin p9.16 pwm and the LED lit up!

I do not know now if the lib. is public domain or not. I will get his opinion and return service.

Hello @RobertCNelson ,

I am using another lib. now instead of what was posted here by me. So far, I have not been able to make this lib. work. I am missing glob.glob and tuples from a table that are there for use w/ muxing w/ config-pin.

Anyway, there was a nice person on IRC that helped me a bit w/ the pwm functionality on the BBB at /dev/bone/pwm/1/b which is actually P9.16.

I found the info. about what to call /dev/bone/pwm/1/b on the header pins here:

https://elinux.org/Beagleboard:BeagleBone_cape_interface_spec

Seth

P.S. The person that showed me the current lib. I am using wanted me to let people know that it is HIGHLY untested and will be tweaked in the future at any given time. Anyway, it can be found here:

https://pastebin.com/R70P1wAn

Hopefully, this helps people looking to dive into PWM peripherals on the BBB and future boards.