pezi / dart_periphery

dart_periphery is a Dart port of the native c-periphery library
BSD 3-Clause "New" or "Revised" License
39 stars 11 forks source link

PWM issue on pi5 #28

Open TheSkangel opened 1 week ago

TheSkangel commented 1 week ago

Hello, I'm currently trying to run the PWM example on my pi5 but I keep getting the following error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PWMerrorCode.pwmErrorOpen
#0      _checkError (package:dart_periphery/src/pwm.dart:120)
#1      PWM._openPWM (package:dart_periphery/src/pwm.dart:165)
#2      new PWM (package:dart_periphery/src/pwm.dart:150)
#3      main (package:flutter_pi_demo_app/main.dart:42)
#4      _runMain.<anonymous closure> (dart:ui/hooks.dart:301)
#5      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297)
#6      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184)

I'm using the GPIO18 and the following code:

import 'package:dart_periphery/dart_periphery.dart';
import 'dart:io';

void main() {
setCustomLibrary("/usr/local/lib/libperiphery_arm64.so");
  final pwm = PWM(2, 2);
  try {
    print('start');
    print(pwm.getPWMinfo());
    pwm.setPeriodNs(10000000);
    pwm.setDutyCycleNs(8000000);
    print(pwm.getPeriodNs());
    pwm.enable();
    print("Wait 20 seconds");
    sleep(Duration(seconds: 20));
    pwm.disable();
  } finally {
    pwm.dispose();
    print('end');
  }
 }

I tried accessing the provided documentation for pwm in the example but it seems like the provided url was hacked and now points to another website... Linux kernel Linux 6.6.31+rpt-rpi-2712 aarch64 configurations are dtoverlay=pwm,pin=18,func=2.

I also tried using pwm directly with a bash file and there it works just fine:

#!/bin/bash
CHANNEL=2
CHIP=pwmchip2
PWM=pwm$CHANNEL

echo $CHANNEL > /sys/class/pwm/$CHIP/export
sleep 1
echo 1000 > /sys/class/pwm/$CHIP/$PWM/period
echo 500 > /sys/class/pwm/$CHIP/$PWM/duty_cycle
echo 1 > /sys/class/pwm/$CHIP/$PWM/enable

Does anybody had this issue before or does know how to fix it?

pezi commented 1 week ago

dart_periphery comes will all needed libs on board. // setCustomLibrary("/usr/local/lib/libperiphery_arm64.so"); What happens when you comment out this line?

TheSkangel commented 1 week ago

I'm still getting the same error whether I set a custom library or use the libperiphery.so which is shared.

pezi commented 1 week ago

Thanks for the hijacked PWM website - I wiil replace the URL.

I added your dtoverlay=pwm,pin=18,func=2 defintion to my RPI.

But on my RPI3 (32bit) this definition creates a /sys/class/pwm/pwmchip0/pwm0 device tree

and the demo works: https://github.com/pezi/dart_periphery/blob/main/example/pwm_example.dart dart pwm_example.dart

PWM 0, chip 0 (period=0.000000 sec, duty_cycle=nan%, polarity=normal, enabled=false)
10000000
Wait 20 seconds