system

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

README

system

This document describes some basics for developers.

The gobot package name is a little bit misleading, because also contains /dev/i2c-* usage with syscall for i2c interface and the file system mock for unit testing.

Change available features on TinkerOS (Tinkerboard)

Open the file /boot/config.txt and add lines/remove comment sign. Then save the file, close and reboot.

For PWM on TinkerOS (Tinkerboard)

intf:pwm2=on intf:pwm3=on

For i2c on TinkerOS (Tinkerboard)

intf:i2c1=on intf:i2c4=on

Device tree overlays can also be provided, e.g. "overlay=i2c-100kbit-tinker-overlay".

Create a dts-file "i2c1-100kbit-tinker-overlay.dts" with this content:

// Definitions for i2c1 with 100 kbit
/dts-v1/;
/plugin/;

/ {
  compatible = "rockchip,rk3288-evb-rk808-linux", "rockchip,rk3288";

  fragment@0 {
    target = <&i2c1>;
    __overlay__ {
      status = "okay";
      clock-frequency = <100000>;
    };
  };
};

Compile the tree overlay:

dtc -@ -I dts -O dtb -o i2c1-100kbit-tinker-overlay.dtbo i2c1-100kbit-tinker-overlay.dts

Copy the file to "/boot/overlays/i2c-100kbit-tinker-overlay.dtbo"

Package "device-tree-compiler" needs to be installed for "dtc" command.

Change available features on raspi

Start "raspi-config" and go to the menu "interface options". After change something save and reboot the device.

Change available features on armbian

Install the armbian-config utility (howto is shown after each login) and start it.

For i2c on armbian

Open the related menu entry and activate/deactivate the needed features.

For PWM on armbian

Go to the device tree menu - this opens the device tree for editing.
Looking for the pwm entries. There are some marked with "disabled". Change the entry to "okay" for your needed pwm.

Example for Tinkerboard:

ff680020 => pwm2, pin33 ff680030 => pwm3, pin32

Next steps for developers

  • test gpio
  • test pwm
  • background information for i2c in gobot

Documentation

Overview

Package system provides generic access to Linux gpio, i2c and filesystem.

It is intended to be used while implementing support for a single board Linux computer

Index

Constants

View Source
const (
	// IN gpio direction
	IN = "in"
	// OUT gpio direction
	OUT = "out"
	// HIGH gpio level
	HIGH = 1
	// LOW gpio level
	LOW = 0
)
View Source
const (
	// DigitalPinEventRisingEdge indicates an inactive to active event.
	DigitalPinEventRisingEdge = "rising edge"
	// DigitalPinEventFallingEdge indicates an active to inactive event.
	DigitalPinEventFallingEdge = "falling edge"
)
View Source
const (
	// From  /usr/include/linux/i2c-dev.h:
	// ioctl signals
	I2C_SLAVE = 0x0703
	I2C_FUNCS = 0x0705
	I2C_SMBUS = 0x0720
	// Read/write markers
	I2C_SMBUS_READ  = 1
	I2C_SMBUS_WRITE = 0

	// From  /usr/include/linux/i2c.h:
	// Adapter functionality
	I2C_FUNC_SMBUS_READ_BYTE        = 0x00020000
	I2C_FUNC_SMBUS_WRITE_BYTE       = 0x00040000
	I2C_FUNC_SMBUS_READ_BYTE_DATA   = 0x00080000
	I2C_FUNC_SMBUS_WRITE_BYTE_DATA  = 0x00100000
	I2C_FUNC_SMBUS_READ_WORD_DATA   = 0x00200000
	I2C_FUNC_SMBUS_WRITE_WORD_DATA  = 0x00400000
	I2C_FUNC_SMBUS_READ_BLOCK_DATA  = 0x01000000
	I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = 0x02000000
	I2C_FUNC_SMBUS_READ_I2C_BLOCK   = 0x04000000 // I2C-like block transfer with 1-byte reg. addr.
	I2C_FUNC_SMBUS_WRITE_I2C_BLOCK  = 0x08000000 // I2C-like block transfer with 1-byte reg. addr.
	// Transaction types
	I2C_SMBUS_BYTE             = 1
	I2C_SMBUS_BYTE_DATA        = 2
	I2C_SMBUS_WORD_DATA        = 3
	I2C_SMBUS_PROC_CALL        = 4
	I2C_SMBUS_BLOCK_DATA       = 5
	I2C_SMBUS_I2C_BLOCK_BROKEN = 6
	I2C_SMBUS_BLOCK_PROC_CALL  = 7 /* SMBus 2.0 */
	I2C_SMBUS_I2C_BLOCK_DATA   = 8 /* SMBus 2.0 */
)
View Source
const (
	Syscall_SYS_IOCTL = unix.SYS_IOCTL
	Syscall_EINVAL    = unix.EINVAL
	Syscall_EBUSY     = unix.EBUSY
	Syscall_EFAULT    = unix.EFAULT
)

wrapping for used constants of unix package

Variables

This section is empty.

Functions

func WithDigitalPinGpiodAccess

func WithDigitalPinGpiodAccess() func(Optioner)

WithDigitalPinGpiodAccess can be used to change the default sysfs implementation for digital pins to the character device Kernel ABI. The access is provided by the gpiod package.

func WithPinActiveLow

func WithPinActiveLow() func(gobot.DigitalPinOptioner) bool

WithPinActiveLow initializes the pin with inverse reaction (applies on input and output).

func WithPinDebounce

func WithPinDebounce(period time.Duration) func(gobot.DigitalPinOptioner) bool

WithPinDebounce initializes the input pin to be debounced.

func WithPinDirectionInput

func WithPinDirectionInput() func(gobot.DigitalPinOptioner) bool

WithPinDirectionInput initializes the pin as input.

func WithPinDirectionOutput

func WithPinDirectionOutput(initial int) func(gobot.DigitalPinOptioner) bool

WithPinDirectionOutput initializes the pin as output instead of the default "input".

func WithPinEventOnBothEdges

func WithPinEventOnBothEdges(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32,
	lseqno uint32),
) func(gobot.DigitalPinOptioner) bool

WithPinEventOnBothEdges initializes the input pin for edge detection and call the event handler on all edges.

func WithPinEventOnFallingEdge

func WithPinEventOnFallingEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32,
	lseqno uint32),
) func(gobot.DigitalPinOptioner) bool

WithPinEventOnFallingEdge initializes the input pin for edge detection and call the event handler on falling edges.

func WithPinEventOnRisingEdge

func WithPinEventOnRisingEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32,
	lseqno uint32),
) func(gobot.DigitalPinOptioner) bool

WithPinEventOnRisingEdge initializes the input pin for edge detection and call the event handler on rising edges.

func WithPinLabel

func WithPinLabel(label string) func(gobot.DigitalPinOptioner) bool

WithPinLabel use a pin label, which will replace the default label "gobotio#".

func WithPinOpenDrain

func WithPinOpenDrain() func(gobot.DigitalPinOptioner) bool

WithPinOpenDrain initializes the output pin to be driven with open drain/collector.

func WithPinOpenSource

func WithPinOpenSource() func(gobot.DigitalPinOptioner) bool

WithPinOpenSource initializes the output pin to be driven with open source/emitter.

func WithPinPollForEdgeDetection added in v2.2.0

func WithPinPollForEdgeDetection(
	pollInterval time.Duration,
	pollQuitChan chan struct{},
) func(gobot.DigitalPinOptioner) bool

WithPinPollForEdgeDetection initializes a discrete input pin polling function to use for edge detection.

func WithPinPullDown

func WithPinPullDown() func(gobot.DigitalPinOptioner) bool

WithPinPullDown initializes the pin to be pulled down (high impedance to GND, applies on input and output). This is working since Kernel 5.5.

func WithPinPullUp

func WithPinPullUp() func(gobot.DigitalPinOptioner) bool

WithPinPullUp initializes the pin to be pulled up (high impedance to VDD, applies on input and output). This is working since Kernel 5.5.

func WithSpiGpioAccess

func WithSpiGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, nssPin, mosiPin, misoPin string) func(Optioner)

WithSpiGpioAccess can be used to switch the default SPI implementation to GPIO usage.

Types

type Accesser

type Accesser struct {
	// contains filtered or unexported fields
}

Accesser provides access to system calls, filesystem, implementation for digital pin and SPI

func NewAccesser

func NewAccesser(options ...func(Optioner)) *Accesser

NewAccesser returns a accesser to native system call, native file system and the chosen digital pin access. Digital pin accesser can be empty or "sysfs", otherwise it will be automatically chosen.

func (*Accesser) Find

func (a *Accesser) Find(baseDir string, pattern string) ([]string, error)

Find finds file from native or the mocked file system

func (*Accesser) IsSysfsDigitalPinAccess

func (a *Accesser) IsSysfsDigitalPinAccess() bool

IsSysfsDigitalPinAccess returns whether the used digital pin accesser is a sysfs one.

func (*Accesser) NewAnalogPin added in v2.3.0

func (a *Accesser) NewAnalogPin(path string, r, w bool, readBufLen uint16) gobot.AnalogPinner

func (*Accesser) NewDigitalPin

func (a *Accesser) NewDigitalPin(chip string, pin int,
	o ...func(gobot.DigitalPinOptioner) bool,
) gobot.DigitalPinner

NewDigitalPin returns a new system digital pin, according to the given pin number.

func (*Accesser) NewI2cDevice

func (a *Accesser) NewI2cDevice(location string) (*i2cDevice, error)

NewI2cDevice returns a Linux Kernel access by ioctrl to the given i2c bus location (character device). Important note for "os.ModeExclusive": this is undefined without create the file for character devices, this means a second open will not return an error e.g. due to a busy resource. If this is not wanted, e.g. to minimize count of open fd's this needs to be prevented at caller side by implementing a caching mechanism. Furthermore this behavior can lead to problems with multiple devices on the same bus because the cycle SetAddress()...Read()/Write() etc. can be interrupted when using multiple instances for the same location.

func (*Accesser) NewPWMPin

func (a *Accesser) NewPWMPin(path string, pin int, polNormIdent string, polInvIdent string) gobot.PWMPinner

NewPWMPin returns a new system PWM pin, according to the given pin number.

func (*Accesser) NewSpiDevice

func (a *Accesser) NewSpiDevice(busNum, chipNum, mode, bits int, maxSpeed int64) (gobot.SpiSystemDevicer, error)

NewSpiDevice returns a new connection to SPI with the given parameters.

func (*Accesser) OpenFile

func (a *Accesser) OpenFile(name string, flag int, perm os.FileMode) (File, error)

OpenFile opens file of given name from native or the mocked file system

func (*Accesser) ReadFile

func (a *Accesser) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

func (*Accesser) Stat

func (a *Accesser) Stat(name string) (os.FileInfo, error)

Stat returns a generic FileInfo, if the file with given name exists. It uses the native or the mocked file system.

func (*Accesser) UseDigitalPinAccessWithMockFs

func (a *Accesser) UseDigitalPinAccessWithMockFs(digitalPinAccess string, files []string) digitalPinAccesser

UseDigitalPinAccessWithMockFs sets the digital pin handler accesser to the chosen one. Used only for tests.

func (*Accesser) UseMockFilesystem

func (a *Accesser) UseMockFilesystem(files []string) *MockFilesystem

UseMockFilesystem sets the filesystem implementation of the accesser to the mocked one. Used only for tests.

func (*Accesser) UseMockSpi

func (a *Accesser) UseMockSpi() *MockSpiAccess

UseMockSpi sets the SPI implementation of the accesser to the mocked one. Used only for tests.

func (*Accesser) UseMockSyscall

func (a *Accesser) UseMockSyscall() *mockSyscall

UseMockSyscall sets the Syscall implementation of the accesser to the mocked one. Used only for tests.

type File

type File interface {
	Write(b []byte) (n int, err error)
	WriteString(s string) (ret int, err error)
	Sync() error
	Read(b []byte) (n int, err error)
	ReadAt(b []byte, off int64) (n int, err error)
	Seek(offset int64, whence int) (ret int64, err error)
	Fd() uintptr
	Close() error
}

A File represents basic IO interactions with the underlying file system

type MockFile

type MockFile struct {
	Contents string
	Seq      int // When this file was last written or read.
	Opened   bool
	Closed   bool
	// contains filtered or unexported fields
}

A MockFile represents a mock file that contains a single string. Any write overwrites, and any read returns from the start.

func (*MockFile) Close

func (f *MockFile) Close() error

Close implements the File interface Close function

func (*MockFile) Fd

func (f *MockFile) Fd() uintptr

Fd returns a random uintprt based on the time of the MockFile creation

func (*MockFile) Read

func (f *MockFile) Read(b []byte) (int, error)

Read copies b bytes from f.Contents

func (*MockFile) ReadAt

func (f *MockFile) ReadAt(b []byte, off int64) (int, error)

ReadAt calls MockFile.Read

func (*MockFile) Seek

func (f *MockFile) Seek(offset int64, whence int) (int64, error)

Seek seeks to a specific offset in a file

func (*MockFile) Sync

func (f *MockFile) Sync() error

Sync implements the File interface Sync function

func (*MockFile) Write

func (f *MockFile) Write(b []byte) (int, error)

Write writes string(b) to f.Contents

func (*MockFile) WriteString

func (f *MockFile) WriteString(s string) (int, error)

WriteString writes s to f.Contents

type MockFilesystem

type MockFilesystem struct {
	Seq            int // Increases with each write or read.
	Files          map[string]*MockFile
	WithReadError  bool
	WithWriteError bool
	WithCloseError bool
	// contains filtered or unexported fields
}

MockFilesystem represents a filesystem of mock files.

func (*MockFilesystem) Add

func (fs *MockFilesystem) Add(name string) *MockFile

Add adds a new file to fs.Files given a name, and returns the newly created file

type MockSpiAccess

type MockSpiAccess struct {
	CreateError bool
	// contains filtered or unexported fields
}

MockSpiAccess contains parameters of mocked SPI access

func (*MockSpiAccess) Reset

func (spi *MockSpiAccess) Reset()

Reset resets the last written values.

func (*MockSpiAccess) SetCloseError

func (spi *MockSpiAccess) SetCloseError(val bool)

SetCloseError can be used to simulate a error on Close().

func (*MockSpiAccess) SetReadError

func (spi *MockSpiAccess) SetReadError(val bool)

SetReadError can be used to simulate a read error.

func (*MockSpiAccess) SetSimRead

func (spi *MockSpiAccess) SetSimRead(data []byte)

SetSimRead is used to set the byte stream for next read.

func (*MockSpiAccess) SetWriteError

func (spi *MockSpiAccess) SetWriteError(val bool)

SetWriteError can be used to simulate a write error.

func (*MockSpiAccess) Written

func (spi *MockSpiAccess) Written() []byte

Written returns the byte stream which was last written.

type Optioner

type Optioner interface {
	// contains filtered or unexported methods
}

Optioner is the interface for system options. This provides the possibility for change the systems behavior by the caller/user when creating the system access, e.g. by "NewAccesser()".

type SyscallErrno added in v2.1.1

type SyscallErrno unix.Errno

SyscallErrno wraps the "unix.Errno"

func (SyscallErrno) Error added in v2.1.1

func (e SyscallErrno) Error() string

Error implements the error interface. It wraps the "unix.Errno.Error()".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL