Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UART adaptations for modern Raspbian #56

Merged
merged 2 commits into from
Sep 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions Sources/UART.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ extension SwiftyGPIO {
public static func UARTs(for board: SupportedBoard) -> [UARTInterface]? {
switch board {
case .CHIP:
return [SysFSUART("S0")!]
return [SysFSUART(["serial0","ttyAMA0"])!]
case .RaspberryPiRev1:
fallthrough
case .RaspberryPiRev2:
fallthrough
case .RaspberryPiPlusZero:
fallthrough
case .RaspberryPi2:
fallthrough
return [SysFSUART(["serial0","ttyAMA0"])!]
case .RaspberryPi3:
return [SysFSUART("AMA0")!]
return [SysFSUART(["serial0","ttyS0"])!,
SysFSUART(["serial1","ttyAMA0"])!]
default:
return nil
}
Expand Down Expand Up @@ -111,6 +112,8 @@ public enum StopBits {
}

public enum UARTSpeed {
case S2400
case S4800
case S9600
case S19200
case S38400
Expand All @@ -119,6 +122,12 @@ public enum UARTSpeed {

public func configure(_ cfg: inout termios) {
switch self {
case .S2400:
cfsetispeed(&cfg, speed_t(B2400))
cfsetospeed(&cfg, speed_t(B2400))
case .S4800:
cfsetispeed(&cfg, speed_t(B4800))
cfsetospeed(&cfg, speed_t(B4800))
case .S9600:
cfsetispeed(&cfg, speed_t(B9600))
cfsetospeed(&cfg, speed_t(B9600))
Expand All @@ -144,22 +153,39 @@ public final class SysFSUART: UARTInterface {
var tty: termios
var fd: Int32

public init?(_ uartId: String) {
device = "/dev/tty"+uartId
tty = termios()
public init?(_ uartIdList: [String]) {
// try all items in list until one works
for uartId in uartIdList {

device = "/dev/"+uartId
tty = termios()

fd = open(device, O_RDWR | O_NOCTTY | O_SYNC)
guard fd>0 else {
if errno == ENOENT {
// silently return nil if no such device
continue
}
perror("Couldn't open UART device")
continue
}

fd = open(device, O_RDWR | O_NOCTTY | O_SYNC)
guard fd>0 else {
perror("Couldn't open UART device")
abort()
}
let ret = tcgetattr(fd, &tty)

guard ret == 0 else {
close(fd)
perror("Couldn't get terminal attributes")
continue
}

let ret = tcgetattr(fd, &tty)
return
}

guard ret == 0 else {
perror("Couldn't get terminal attributes")
abort()
}
return nil
}

public convenience init?(_ uartId: String) {
self.init([uartId])
}

public func configureInterface(speed: UARTSpeed, bitsPerChar: CharSize, stopBits: StopBits, parity: ParityType) {
Expand Down