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

Add NetBSD support #178

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Add NetBSD support #178

wants to merge 6 commits into from

Conversation

picohive
Copy link

Test with arduino-cli 0.35.3:

picohive: {/home/rxg} uname -a
NetBSD picohive 9.3 NetBSD 9.3 (GENERIC) #0: Thu Aug 4 15:30:37 UTC 2022 [email protected]:/usr/src/sys/arch/amd64/compile/GENERIC amd64
picohive: {/home/rxg} arduino-cli board list
Port Protocol Type Board Name FQBN Core
/dev/dty00 serial Serial Port Unknown
/dev/dty01 serial Serial Port Unknown
/dev/dty02 serial Serial Port Unknown
/dev/dty03 serial Serial Port Unknown
/dev/dtyU0 serial Serial Port (USB) Unknown
/dev/dtyU1 serial Serial Port (USB) Unknown
/dev/dtyU2 serial Serial Port (USB) Unknown
/dev/dtyU3 serial Serial Port (USB) Unknown
/dev/dtyU4 serial Serial Port (USB) Unknown
/dev/dtyU5 serial Serial Port (USB) Unknown
/dev/dtyU6 serial Serial Port (USB) Unknown
/dev/dtyU7 serial Serial Port (USB) Unknown
/dev/tty00 serial Serial Port Unknown
/dev/tty01 serial Serial Port Unknown
/dev/tty02 serial Serial Port Unknown
/dev/tty03 serial Serial Port Unknown
/dev/ttyU0 serial Serial Port (USB) Unknown
/dev/ttyU1 serial Serial Port (USB) Unknown
/dev/ttyU2 serial Serial Port (USB) Unknown
/dev/ttyU3 serial Serial Port (USB) Unknown
/dev/ttyU4 serial Serial Port (USB) Unknown
/dev/ttyU5 serial Serial Port (USB) Unknown
/dev/ttyU6 serial Serial Port (USB) Unknown
/dev/ttyU7 serial Serial Port (USB) Unknown

picohive: {/home/rxg} arduino-cli monitor -p /dev/ttyU0 -c baudrate=115200
Monitor port settings:
baudrate=115200
Connected to /dev/ttyU0! Press CTRL-C to exit.
Bpm:0,SPO2:90.00
Bpm:0,SPO2:90.00
Bpm:0,SPO2:90.00

Copy link
Member

@cmaglie cmaglie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @picohive thanks for the patch!

Do you actually have 12 serial ports installed?

/dev/tty00 serial Serial Port Unknown
/dev/tty01 serial Serial Port Unknown
/dev/tty02 serial Serial Port Unknown
/dev/tty03 serial Serial Port Unknown
/dev/ttyU0 serial Serial Port (USB) Unknown
/dev/ttyU1 serial Serial Port (USB) Unknown
/dev/ttyU2 serial Serial Port (USB) Unknown
/dev/ttyU3 serial Serial Port (USB) Unknown
/dev/ttyU4 serial Serial Port (USB) Unknown
/dev/ttyU5 serial Serial Port (USB) Unknown
/dev/ttyU6 serial Serial Port (USB) Unknown
/dev/ttyU7 serial Serial Port (USB) Unknown

it looks like those are preallocated devices that are not connected to any actual port (more or less what Linux does with the ttySxx devices). Is there a way to filter those from the list output?

Comment on lines +33 to +40
func nativeGetPortDetails(portPath string) (*PortDetails, error) {
result := &PortDetails{Name: portPath}

if strings.Contains(result.Name, "U") {
result.IsUSB = true
}
return result, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to get the USB VID, PID, and ISerial number is this possible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, they are just available precreated node. There is no sysfs on NetBSD yet, maybe have a better solution in the future.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible, what you need to do is to open /dev/usb0 and issue the DRVLISTDEV ioctl. example code in C
I can send a separate PR to add support for that after the basic support is merged.

import "golang.org/x/sys/unix"

const devFolder = "/dev"
const regexFilter = "(dty|tty|dtyU|ttyU)[0-9]{1,2}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between dty and tty?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From man tty(4) on NetBSD:
The /dev/ttyXX special file is used for dial-in modems and terminals.
The /dev/dtyXX special file is a SunOS-compatible dial-out device.

@picohive picohive closed this Jun 26, 2024
@picohive picohive reopened this Jun 26, 2024
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Please consider using this implementation instead. It supports arbitrary BAUD rates and allows to remove the mapping table.

const tcCMSPAR uint32 = 0 // not supported
const tcIUCLC uint32 = 0  // not supported

const tcCRTSCTS uint32 = unix.CRTSCTS

const ioctlTcgetattr = unix.TIOCGETA
const ioctlTcsetattr = unix.TIOCSETA
const ioctlTcflsh = unix.TIOCFLUSH
const ioctlTioccbrk = unix.TIOCCBRK
const ioctlTiocsbrk = unix.TIOCSBRK

func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) {
	// see https://nxr.netbsd.org/xref/src/lib/libc/termios/cfsetspeed.c
	if speed < 50 || speed > math.MaxInt32 {
		return &PortError{code: InvalidSpeed}, true
	}
	settings.Ispeed = int32(speed)
	settings.Ospeed = int32(speed)
	return nil, false
}

func (port *unixPort) setSpecialBaudrate(speed uint32) error {
	if speed < 50 || speed > math.MaxInt32 {
		return &PortError{code: InvalidSpeed}
	}
	// see https://nxr.netbsd.org/xref/src/lib/libc/termios/tcgetattr.c
	settings, err := unix.IoctlGetTermios(port.handle, ioctlTcgetattr)
	if err != nil {
		return err
	}
	if err, ok := setTermSettingsBaudrate(int(speed), settings); !ok {
		return err
	}
	return unix.IoctlSetTermios(port.handle, ioctlTcsetattr, settings)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants