uraimo / SwiftyGPIO

A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire.
MIT License
1.35k stars 134 forks source link

[Hypriot] Errors on Raspberry Pi 3 B+ #79

Open AdrianBinDC opened 5 years ago

AdrianBinDC commented 5 years ago

Board Type

Specify which kind of ARM board you are using, e.g. RaspberryPi3, RaspberryPi1, BBB, etc... Raspberry Pi 3 B+

Operating System

Which OS are you using? e.g. Ubuntu Mate 16.04, Raspbian, Armbian, etc... Hypriot 1.9.0

Swift Version

Which Swift version are you using? Did you compile it yourself or did you use prebuilt binaries? 4.1.2 using pre-built binaries

Description

Describe your hardware setup (e.g. board connected to i2c sensor) and describe briefly the issue, specifying the steps needed to reproduce it, if necessary. I have an LED on the breadboard with a resistor. The ground is connected to P6, the positive is connected to P4 on a Raspberry Pi 3 B+

I've got a project created that compiles. The code in main.swift is as follows:

import Glibc
import SwiftyGPIO

var gp4 = GPIO(name: "P4", id: 4)
gp4.direction = .OUT

repeat {
    gp4.value = (gp4.value == 0) ? 1 : 0
    usleep(150 * 1000)
} while(true)

I also tried declaring gp4 like this:

let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi3)
var gp4 = gpios[.P4]!

Please list any error message you've receveived during execution.

When gp4 is declared like this:

var gp4 = GPIO(name: "P4", id: 4)
gp4.direction = .OUT

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /home/swift/GPIOTest/.build/checkouts/SwiftyGPIO.git-1189449763/Sources/SwiftyGPIO.swift, line 104 Current stack trace: Illegal instruction (core dumped)

When gp4 is declared like this:

let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi3)
var gp4 = gpios[.P4]!

Fatal error: Can't open /dev/mem , use sudo!: file /home/swift/GPIOTest/.build/checkouts/SwiftyGPIO.git-1189449763/Sources/SwiftyGPIO.swift, line 353 Current stack trace: Illegal instruction (core dumped)

uraimo commented 5 years ago

Hi Adrian, I've never tried Hypriot but it should work. Where are you running this, directly in Hypriot or in a docker container (gpio tutorial for docker)? Both samples require that you launch them with root privileges using sudo, and the first should work if /sys/class/gpio/export is available. The second one should work too if they've imported the required kernel patches from Raspbian, but I'd say it's likely that they've done it. Can you try again using sudo and maybe with Swift 4.1.3 for Raspbian?

uraimo commented 5 years ago

Oh, and depending on the specific image, GPIO mapping could be disabled by default and could require manual activation, see: https://github.com/futurejones/swift-arm64/issues/2

Adding "iomem=relaxed" to /boot/firmware/cmdline.txt is enough to get around this.