pwm_cli.py 2.19 KB
#!/usr/bin/env python3
import sys

from litex import RemoteClient

import argparse

def pwm_cli_handler(dict_cfg):
    wb = RemoteClient()
    wb.open()

    print(wb)
    print(wb.regs.pwm0_period.read())
    print(wb.regs.pwm0_duty.read())
    print(wb.regs.pwm0_enable.read())

    if "period" in dict_cfg:
        period  = int(float(dict_cfg["period"]))
        print(f"period {period}")
        wb.regs.pwm0_period.write(period)
        print(wb.regs.pwm0_period.read())
    if "duty" in dict_cfg:
        duty  = int(float(dict_cfg["duty"]))
        print(f"duty {duty}")
        wb.regs.pwm0_duty.write(duty)
        print(wb.regs.pwm0_duty.read())
    if "oneshot" in dict_cfg:
        oneshot  = int(float(dict_cfg["oneshot"]))
        print(f"oneshot {oneshot}")
        wb.regs.pwm0_period_oneshot.write(int(oneshot))
    if "enable" in dict_cfg and dict_cfg["enable"]:
        print("enable")
        period = wb.regs.pwm0_period.read()
        duty = wb.regs.pwm0_duty.read()
        if period == 0 or duty == 0:
            print("Error: period and/or duty must be configured before enable output")
            return
        wb.regs.pwm0_enable.write(1)
        print(wb.regs.pwm0_enable.read())
    if "disable" in dict_cfg and dict_cfg["disable"]:
        print("disable")
        wb.regs.pwm0_enable.write(0)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="pwm CLI")
    parser.add_argument("--enable",  action="store_true", help="enable PWM output")
    parser.add_argument("--disable", action="store_true", help="disable PWM output")
    parser.add_argument("--period",  help="period (in clock cycle)")
    parser.add_argument("--duty",    help="duty cycle (in clock cycle)")
    parser.add_argument("--oneshot", help="period correction (in clock cycle)")
    args = parser.parse_args()

    dict_cfg = {}
    if args.period:
        dict_cfg["period"] = args.period
    if args.duty:
        dict_cfg["duty"] = args.duty
    if args.oneshot:
        dict_cfg["oneshot"] = args.oneshot
    if args.enable:
        dict_cfg["enable"] = True
    if args.disable:
        dict_cfg["disable"] = True

    pwm_cli_handler(dict_cfg)