# gizhw:bus

the buses a board exposes: a serial port's speed (termios2, any rate through BOTHER), GPIO lines (the character device's chardev v2 interface: a line request, then its values), I2C transfers (I2C_RDWR), SPI transfers (SPI_IOC_MESSAGE), USB control transfers (usbfs), and CAN frames (SocketCAN, raw). The ioctl numbers and the structs are the kernel's (termios.h, gpio.h, i2c-dev.h, spidev.h, usbdevice_fs.h, can.h), checked against those headers. Every ioctl is on a Handle of gizhw:device; the CAN socket and the interface lookup are libc calls through libcCall.

```gizlang
import * as Bus from "gizhw:bus";
```

## Types

### `I2cMessage`

```gizlang
type I2cMessage = { address: i64; read: bool; length: i64; data: bytes; }
```

One I2C message: the 7-bit `address`, `read` (true: `length` bytes come back) or the `data` to write.

- `address`: `i64`
- `read`: `bool`
- `length`: `i64`
- `data`: `bytes`

### `CanFrame`

```gizlang
type CanFrame = { id: i64; extended: bool; data: bytes; }
```

One CAN frame: its identifier, whether it is an extended (29-bit) one, and its data (up to 8 bytes).

- `id`: `i64`
- `extended`: `bool`
- `data`: `bytes`

## Functions

### `termiosRate`

```gizlang
function termiosRate(termios: bytes, baud: i64): bytes
```

The serial port's termios2 with the speed set: both directions at `baud`, through BOTHER.

Effects: fail.

### `serialRate`

```gizlang
function serialRate(h: Device.Handle, baud: i64): void throws string
```

Sets the serial port behind `h` to `baud`.

Effects: fail, io.

### `lineRequest`

```gizlang
function lineRequest(offset: i64, flags: i64, consumer: string): bytes
```

The request for GPIO_V2_GET_LINE: one line of the chip (its `offset`), as an input or an output, under `consumer`.

Effects: fail.

### `parseLineFd`

```gizlang
function parseLineFd(reply: bytes): i64
```

The line's file descriptor in the reply to GPIO_V2_GET_LINE (at byte 588).

Effects: fail.

### `valuesRequest`

```gizlang
function valuesRequest(bits: i64, mask: i64): bytes
```

The request for GPIO_V2_LINE_GET/SET_VALUES: the bit values and the mask of the lines it touches (bit 0: the line).

Effects: fail.

### `parseLineValue`

```gizlang
function parseLineValue(reply: bytes): i64
```

The value of the one line the reply to GPIO_V2_LINE_GET_VALUES describes: 0 or 1.

Effects: fail.

### `gpioRead`

```gizlang
function gpioRead(sys: System, chip: Device.Handle, offset: i64): i64 throws string
```

Reads GPIO line `offset` of the chip behind `chip` (its character device, /dev/gpiochipN): a line request, then its value.

Effects: fail, io.

### `gpioWrite`

```gizlang
function gpioWrite(sys: System, chip: Device.Handle, offset: i64, value: i64): void throws string
```

Sets GPIO line `offset` of the chip behind `chip` to `value` (0 or 1), as an output.

Effects: fail, io.

### `i2cMessageBytes`

```gizlang
function i2cMessageBytes(address: i64, flags: i64, length: i64, buffer: u64): bytes
```

One I2C message (struct i2c_msg: 16 bytes): address, flags, length, and the buffer's native address.

Effects: fail.

### `i2cRdwrBytes`

```gizlang
function i2cRdwrBytes(messages: u64, count: i64): bytes
```

The I2C_RDWR argument (struct i2c_rdwr_ioctl_data: 16 bytes): the messages' native address and their count.

Effects: fail.

### `i2c`

```gizlang
function i2c(h: Device.Handle, messages: Vec<I2cMessage>): Vec<bytes> throws string
```

Runs I2C messages on the bus behind `h`, one transaction; gives each read's bytes (empty for a write).

Effects: fail, io.

### `spiTransferBytes`

```gizlang
function spiTransferBytes(tx: u64, rx: u64, length: i64, speed: i64, bits: i64): bytes
```

The SPI transfer (struct spi_ioc_transfer: 32 bytes): the tx and rx buffers, the length, speed and bits per word.

Effects: fail.

### `spi`

```gizlang
function spi(h: Device.Handle, tx: bytes, rxLength: i64, speed: i64): bytes throws string
```

One SPI transfer on the device behind `h`: `tx` out, and `rxLength` bytes back; gives those bytes.

Effects: fail, io.

### `usbCtrlBytes`

```gizlang
function usbCtrlBytes(requestType: i64, request: i64, value: i64, index: i64, length: i64, timeout: i64, data: u64): bytes
```

The usbfs control transfer (struct usbdevfs_ctrltransfer: 24 bytes).

Effects: fail.

### `usbControl`

```gizlang
function usbControl(h: Device.Handle, requestType: i64, request: i64, value: i64, index: i64, data: bytes, readLength: i64, timeout: i64): bytes throws string
```

A control transfer on the device behind `h`. A device-to-host one (requestType with the IN bit) reads `readLength`
bytes back; a host-to-device one sends `data`. Gives the bytes that came back (empty for a send).

Effects: fail, io.

### `canFrameBytes`

```gizlang
function canFrameBytes(frame: CanFrame): bytes
```

A CAN frame as struct can_frame (16 bytes): the identifier (with the extended bit), the data length, and the data.

Effects: fail.

### `parseCanFrame`

```gizlang
function parseCanFrame(data: bytes): CanFrame
```

The CAN frame from its 16 bytes.

Effects: fail.

### `canOpen`

```gizlang
function canOpen(sys: System, name: string): Device.Handle throws string
```

The CAN interface `name` (can0) as a raw SocketCAN handle: the socket is bound to the interface.

Effects: fail, io.

### `canSend`

```gizlang
function canSend(h: Device.Handle, frame: CanFrame): i64 throws string
```

Sends one frame on the CAN handle; gives the bytes written (16).

Effects: fail, io.

### `canReceive`

```gizlang
function canReceive(h: Device.Handle): CanFrame throws string
```

Receives one frame from the CAN handle, waiting for it.

Effects: fail, io.

