Git Product home page Git Product logo

Comments (7)

drahoslove avatar drahoslove commented on July 27, 2024 2

Hi,
I think this is the same issue as #35,
There is already a warning comment in the documentation of EdgeDetect:

// WARNING: this might make your Pi unresponsive, if this happens, you should either run the code as root,
// or add `dtoverlay=gpio-no-irq` to `/boot/config.txt` and restart your pi,

Problem is that normally the edge events produces interruptions, which are not handles by the system, and after some number of unhandled interruptions the pi freezes.

We are trying to disable the interruptions, but it only works with root:

go-rpio/rpio.go

Lines 383 to 387 in a36b96d

func DetectEdge(pin Pin, edge Edge) {
if edge != NoEdge {
// disable GPIO event interruption to prevent freezing in some cases
DisableIRQs(1<<49 | 1<<52) // gpio_int[0] and gpio_int[3]
}

If you don't call EdgeDetect(noEdge) Close() before your program ends, the interruptions will stay disabled until next restart of your pi, this explains the "interesting" behavior you observed.

@stianeikeland
We should probably find some better way to solve this issue.

from go-rpio.

ifree92 avatar ifree92 commented on July 27, 2024

Hm, pretty strange. When I do the same but manually using pin.Read() in cycle inside go routine - then no stuck.
But stuck only by using EdgeDetected in go routine.

And when I try to put everything inside go routine, then the device is getting stuck immediately after application runs.

	go func() {
		pin := rpio.Pin(10)
		pin.Input()
		pin.PullUp()
		pin.Detect(rpio.FallEdge)
		lastTime := time.Now().UnixNano() / 1000000
		for {
			if pin.EdgeDetected() {
				now := time.Now().UnixNano() / 1000000
				diff := now - lastTime
				lastTime = now
				fmt.Println("Heyhey!", diff)
			}
			time.Sleep(time.Millisecond * 35)
		}
	}()

UPD: strange. After reboot the stuck is happening not always after run.
The kind of random...

UPD2: for now I'm using this "rakes" to keep it working.

package main

import (
	"fmt"
	"github.com/stianeikeland/go-rpio"
	"log"
	"time"
)

// This function getting my device stuck!
func pinWatcher(pinNum uint, ch *chan int64) {
	pin := rpio.Pin(pinNum)
	pin.Input()
	pin.PullUp()
	pin.Detect(rpio.FallEdge)
	lastTime := time.Now().UnixNano() / 1000000
	for {
		if pin.EdgeDetected() {
			now := time.Now().UnixNano() / 1000000
			diff := now - lastTime
			lastTime = now
			*ch <- diff
		}
		time.Sleep(time.Millisecond * 35)
	}
}

// Plan B for a while...
func pinWatcher2(pinNum uint, ch *chan int64) {
	pin := rpio.Pin(pinNum)
	pin.Input()
	pin.PullUp()
	lastTime := time.Now().UnixNano() / 1000000
	lastState := rpio.High
	for {
		state := pin.Read()
		if state == rpio.High {
		} else if state == rpio.Low {
			if lastState == rpio.High {
				now := time.Now().UnixNano() / 1000000
				diff := now - lastTime
				lastTime = now
				*ch <- diff
			}
		}
		lastState = state
		time.Sleep(time.Millisecond)
	}
}

func main() {
	err := rpio.Open()
	if err != nil {
		log.Fatal(err)
	}

	ch := make(chan struct{})

	gpio10pulses := make(chan int64)

	go pinWatcher2(uint(27), &gpio10pulses)

	go func() {
		log.Println("Init reader")
		for {
			select {
			case pin10Diff := <-gpio10pulses:
				fmt.Println("Pulse", pin10Diff)
			}
		}
	}()

	fmt.Println("init")
	<-ch
}

from go-rpio.

stianeikeland avatar stianeikeland commented on July 27, 2024

I'm wondering if this could be related how gpio memory is accessed.

go-rpio/rpio.go

Lines 634 to 637 in a36b96d

file, err = os.OpenFile("/dev/mem", os.O_RDWR|os.O_SYNC, 0)
if os.IsPermission(err) { // try gpiomem otherwise (some extra functions like clock and pwm setting wont work)
file, err = os.OpenFile("/dev/gpiomem", os.O_RDWR|os.O_SYNC, 0)
}

Are you running as root or regular user?
Could you try to run as root if not?

from go-rpio.

ifree92 avatar ifree92 commented on July 27, 2024

@stianeikeland I have made some experiments.
Yes, the stuck possible only if I run application as regular user. With root everything is stable.

But I found one interesting behaviour. If I run firstly with root, but then re-run with regular user - then everything good.
Pretty strange

from go-rpio.

ifree92 avatar ifree92 commented on July 27, 2024

Yes, that's it. Now everything clear.
Thank you for support and help. Basically I don't think this issue is related somehow to the library, more to raspberry workflow.
I think the issue might be closed.

from go-rpio.

ifree92 avatar ifree92 commented on July 27, 2024

And I have faced with one mandatory bug.
We are using on our terminals this displays: https://www.waveshare.com/wiki/10.1inch_HDMI_LCD#Driver

The resistive touchscreen is connecting through the GPIO pins. So, when I'm running with sudo my application with edge detection - the touchscreen is not working.

In this case I have to use manual reading inputs, no other solution for me right now.
Just FYI, or maybe for someone who will face with the same issue.

from go-rpio.

drahoslove avatar drahoslove commented on July 27, 2024

Yes, unfortunately, disabling the IRQ may cause some other GPIO libs/drivers to not working. I forgot to mention that.

from go-rpio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.