stianeikeland / go-rpio

:electric_plug: Raspberry Pi GPIO library for go-lang
MIT License
2.16k stars 221 forks source link

SPI Locking? #62

Closed codedninja closed 4 years ago

codedninja commented 4 years ago

Converted a driver for TLC95711 pwm board which uses SPI. I got it working fully with one weird issue when I have to rpio.Open() within the same function when I am writing to SPI.

With that being said, I found an issue when looping though colors and sending it through SPI, I am getting a "memory issue" according to the panic that gets thrown. I figure it's being caused when writing to SPI while a previous write hasn't finish being sent just yet. I have set up sleeps but you really can only delay so long before effects don't look right.

Would it be possible to add Locking capabilities similar to Arduino?

codedninja commented 4 years ago

`unexpected fault address 0xb6f9f000 fatal error: fault [signal SIGSEGV: segmentation violation code=0x1 addr=0xb6f9f000 pc=0xcf444]

goroutine 5476 [running]: runtime.throw(0x294c4f, 0x5) /usr/local/go/src/runtime/panic.go:1116 +0x5c fp=0x2421ec4 sp=0x2421eb0 pc=0x44288 runtime.sigpanic() /usr/local/go/src/runtime/signal_unix.go:702 +0x310 fp=0x2421edc sp=0x2421ec4 pc=0x5afa8 github.com/stianeikeland/go-rpio.PinMode(0x407) /root/go/src/github.com/stianeikeland/go-rpio/rpio.go:314 +0xac fp=0x2421f04 sp=0x2421ee0 pc=0xcf444 github.com/stianeikeland/go-rpio.Pin.Mode(...) /root/go/src/github.com/stianeikeland/go-rpio/rpio.go:209 github.com/stianeikeland/go-rpio.SpiBegin(0x0, 0x0, 0x8) /root/go/src/github.com/stianeikeland/go-rpio/spi.go:44 +0xd0 fp=0x2421f34 sp=0x2421f04 pc=0xd057c github.com/tehcodedninja/RPIO-TLC59711.TLC59711.spiwriteMSB(0x1, 0x24161a0, 0xc, 0xc, 0x0, 0x75, 0x75, 0x75, 0x0) /root/go/src/github.com/tehcodedninja/RPIO-TLC59711/TLC59711.go:31 +0x2c fp=0x2421f5c sp=0x2421f34 pc=0xd0aac github.com/tehcodedninja/RPIO-TLC59711.TLC59711.Write(0x1, 0x24161a0, 0xc, 0xc, 0x0, 0x75, 0x75, 0x75) /root/go/src/github.com/tehcodedninja/RPIO-TLC59711/TLC59711.go:75 +0x5c fp=0x2421f98 sp=0x2421f5c pc=0xd0c94 main.(LedDriver).setColor.func1(0x0, 0x499fd8, 0xdbf2, 0x240d) /root/go/src/led_driver/driver.go:132 +0x98 fp=0x2421fdc sp=0x2421f98 pc=0x22ba78 runtime.goexit() /usr/local/go/src/runtime/asm_arm.s:857 +0x4 fp=0x2421fdc sp=0x2421fdc pc=0x72f28 created by main.(LedDriver).setColor /root/go/src/led_driver/driver.go:126 +0x44

goroutine 1 [IO wait, 7 minutes]: internal/poll.runtime_pollWait(0xa5b10f10, 0x72, 0x0) /usr/local/go/src/runtime/netpoll.go:203 +0x44 internal/poll.(pollDesc).wait(0x246e1a4, 0x72, 0x0, 0x0, 0x2951fc) /usr/local/go/src/internal/poll/fd_poll_runtime.go:87 +0x30 internal/poll.(pollDesc).waitRead(...) /usr/local/go/src/internal/poll/fd_poll_runtime.go:92 internal/poll.(FD).Accept(0x246e190, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) /usr/local/go/src/internal/poll/fd_unix.go:384 +0x1b0 net.(netFD).accept(0x246e190, 0x4aa388, 0x0, 0x1) /usr/local/go/src/net/fd_unix.go:238 +0x20 net.(TCPListener).accept(0x240e120, 0x247c4c0, 0xd022c700, 0x4eb98) /usr/local/go/src/net/tcpsock_posix.go:139 +0x20 net.(TCPListener).Accept(0x240e120, 0x2437ee8, 0xc, 0x24000e0, 0x2080b0) /usr/local/go/src/net/tcpsock.go:261 +0x54 net/http.(Server).Serve(0x24603f0, 0x2fa3f8, 0x240e120, 0x0, 0x0) /usr/local/go/src/net/http/server.go:2901 +0x1fc net/http.(Server).ListenAndServe(0x24603f0, 0x24603f0, 0xffff) /usr/local/go/src/net/http/server.go:2830 +0xa0 net/http.ListenAndServe(...) /usr/local/go/src/net/http/server.go:3086 main.main() /root/go/src/led_driver/main.go:49 +0x1f0

goroutine 5477 [runnable]: sync.runtime_Semrelease(0x4a8edc, 0x1, 0x1) /usr/local/go/src/runtime/sema.go:66 +0x2c sync.(Mutex).unlockSlow(0x4a8ed8, 0xc) /usr/local/go/src/sync/mutex.go:224 +0xc0 sync.(Mutex).Unlock(0x4a8ed8) /usr/local/go/src/sync/mutex.go:190 +0x48 github.com/stianeikeland/go-rpio.Close(0x0, 0x0) /root/go/src/github.com/stianeikeland/go-rpio/rpio.go:712 +0x1f0 github.com/tehcodedninja/RPIO-TLC59711.TLC59711.spiwriteMSB(0x1, 0x24161a0, 0xc, 0xc, 0x0, 0x75, 0x75, 0x75, 0x96dd7af5) /root/go/src/github.com/tehcodedninja/RPIO-TLC59711/TLC59711.go:46 +0x140 github.com/tehcodedninja/RPIO-TLC59711.TLC59711.Write(0x1, 0x24161a0, 0xc, 0xc, 0x0, 0x75, 0x75, 0x75) /root/go/src/github.com/tehcodedninja/RPIO-TLC59711/TLC59711.go:71 +0x15c main.(LedDriver).setColor.func1(0x0, 0x499fd8, 0xd6bc, 0x2943) /root/go/src/led_driver/driver.go:132 +0x98 created by main.(LedDriver).setColor /root/go/src/led_driver/driver.go:126 +0x44

goroutine 9 [sleep]: time.Sleep(0x5f5e100, 0x0) /usr/local/go/src/runtime/time.go:188 +0xec main.(LedDriver).fade(0x499fd8, 0x0, 0x0, 0xffff, 0x2a05f200, 0x1, 0x1) /root/go/src/led_driver/driver.go:115 +0xe4 main.(LedDriver).fadeLoop.func1(0x499fd8) /root/go/src/led_driver/driver.go:29 +0xb8 created by main.(*LedDriver).fadeLoop /root/go/src/led_driver/driver.go:21 +0x40`

codedninja commented 4 years ago

Found the issue, it was when I was setting a color on my code it would be ran as a goroutine and random chance of over writing the buffer to SPI.