stianeikeland / go-rpio

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

traffic light test faild on RPi 4 #47

Open kadisi opened 4 years ago

kadisi commented 4 years ago

go version: go version go1.12.9 linux/arm

export GO111MODULE=on

go code:

import (
        "log"
        "time"

        rpio "github.com/stianeikeland/go-rpio"
)

func main() {

        err := rpio.Open()
        if err != nil {
                log.Fatalf("open rpio error %v", err)
        }
        defer rpio.Close()

        red := rpio.Pin(13)
        red.Output()

        red.Low()

        for {
                red.High()
                time.Sleep(time.Second)
                red.Low()
                time.Sleep(time.Second)
        }
}

we i run my binary, the red light didn't respond.

pinout

,--------------------------------.
| oooooooooooooooooooo J8   +======
| 1ooooooooooooooooooo  PoE |   Net
|  Wi                    oo +======
|  Fi  Pi Model 4B  V1.1 oo      |
|        ,----.               +====
| |D|    |SoC |               |USB3
| |S|    |    |               +====
| |I|    `----'                  |
|                   |C|       +====
|                   |S|       |USB2
| pwr   |HD|   |HD| |I||A|    +====
`-| |---|MI|---|MI|----|V|-------'

Revision           : b03111
SoC                : BCM2711
RAM                : 2048Mb
Storage            : MicroSD
USB ports          : 4 (excluding power)
Ethernet ports     : 1
Wi-fi              : True
Bluetooth          : True
Camera ports (CSI) : 1
Display ports (DSI): 1

J8:
   3V3  (1) (2)  5V
 GPIO2  (3) (4)  5V
 GPIO3  (5) (6)  GND
 GPIO4  (7) (8)  GPIO14
   GND  (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND
GPIO22 (15) (16) GPIO23
   3V3 (17) (18) GPIO24
GPIO10 (19) (20) GND
 GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8
   GND (25) (26) GPIO7
 GPIO0 (27) (28) GPIO1
 GPIO5 (29) (30) GND
 GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
   GND (39) (40) GPIO21

For further information, please refer to https://pinout.xyz/

lscpu:

Architecture:        armv7l
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  1
Core(s) per socket:  4
Socket(s):           1
Vendor ID:           ARM
Model:               3
Model name:          Cortex-A72
Stepping:            r0p3
CPU max MHz:         1500.0000
CPU min MHz:         600.0000
BogoMIPS:            108.00
Flags:               half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32

and when i use python to test , it works.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

RED = 13

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(RED, GPIO.OUT)

def Close():
        GPIO.output(RED,False)

try:
        while(1):
            GPIO.output(RED,True)
            time.sleep(1)
            GPIO.output(RED,False)
            time.sleep(1)
except KeyboardInterrupt:
        Close()
        pass
kadisi commented 4 years ago

ping @stianeikeland

kadisi commented 4 years ago

ping @drahoslove

drahoslove commented 4 years ago

@kadisi Try to look again at your logic, especially the part inside your loop ;)

Hint 1: it's not the same as in your python code.

Hint 2: toggle

kadisi commented 4 years ago

Try to look again at your logic, especially the part inside your loop ;) Hint 1: it's not the same as in your python code. Hint 2: toggle

@drahoslove I don't think this is the reason.

Is it a problem with bcm?

my bcm is bcm2711 while go-rpio support bcm2835.

https://github.com/stianeikeland/go-rpio/blob/a36b96d0b1a40b37017d64875275e7e6312408c7/rpio.go#L32

drahoslove commented 4 years ago

Ah sorry, was too sleepy while responding before, now I see,...

You should use this pin: https://pinout.xyz/pinout/pin33_gpio13

But I'm not sure if bcm2711 is compatible with bcm2835. Maybe it's not.

kadisi commented 4 years ago

@drahoslove thanks for responding

i did use 13 in my code.

        red := rpio.Pin(13)
        red.Output()

did you tested on RPi 4?

wfd3 commented 4 years ago

This is, I think, due to the Pi 4 having a different GPIO base address than the 3. Moreover, the address is at a different offset in /proc/device-tree/soc/ranges.

The Pi3 has a base address of 0x3f00000, which can be found at offset 4 in /proc/device-tree/soc/ranges. The Pi4 has a base address of 0xfe000000, which is at offset 8.

If you override the return value in getBase(), it works (at least on my Pi 4). I'll take a pass at an update to getBase() but it'll take me a while to get to it.

kadisi commented 4 years ago

@wfd3 thanks you.

wfd3 commented 4 years ago

Turns out it was easier than I thought. Try this patch. I've tested it on my Pi3 and Pi4, but I can't speak to other Pi variants. rpio.patch.txt

drahoslove commented 4 years ago

@wfd3 This looks good. Could you please create a pull request with this patch, after kadisi confirms it works for him? I don't have a Pi 4 to test it.

kadisi commented 4 years ago

@wfd3 i have testd, it works. I am looking forward to your pr。

@drahoslove thank you

wfd3 commented 4 years ago

Pull Request is in the queue.