# OAK4 M8 Manual Control (before 1.49.0)

> **Luxonis OS releases before 1.49.0**
> This page applies only to Luxonis OS releases before 1.49.0. From Luxonis OS 1.49.0, a driver controls the M8 connector, and the `gpioset` and `gpioget` commands on this page fail with `Device or resource busy`. On Luxonis OS 1.49.0 and later, use [OAK4 M8 Control](https://docs.luxonis.com/hardware/platform/deploy/oak4-m8-interface/oak4-m8-control.md). To check the version, run `cat /etc/os-release` on the device and read the `VERSION` line.

The M8 connector on OAK4 devices carries UART, I2C, GPIO, FSYNC, strobe, USB2, and a detect line (DET). Each pin can switch
between some of these functions, and the set differs per board. Luxonis OS releases before 1.49.0 have no driver for the
connector. For each board, this page lists the GPIO lines and levels that select each pin function and the USB2 route. The first
sections show how to set them with libgpiod.

For an overview of the M8 interfaces, see the [OAK4 M8 Interface
Guide](https://docs.luxonis.com/hardware/platform/deploy/oak4-m8-interface.md).

## Before you start

You need a root shell on the device, over `ssh` or `adb shell`. These facts apply to every board:

 * The commands use the libgpiod 1.6 tools `gpioget`, `gpioset`, and `gpiofind`.
 * Pins 1 and 6 switch UART and I2C together. Pin 1 carries RX or SDA, and pin 6 carries TX or SCL.
 * Pins 3 and 4 (USB2 D+ and D-), pin 5 (5 V out), and pin 8 (GND) need no setup.

## Read the board tables

Each board section in the "Board reference" section has a pinout table and a USB2 route table. To find the section for your
device, see the "Find your board" section.

 * In the select and direction columns, `79=1` means that you set line 79 to 1. A cell with two entries, such as `45=1, 44=0`,
   means that you set both lines.
 * A dash in the select or direction column means that there is no line to set. If a row has a dash in both columns, you set no
   line for that function.
 * The direction line sets whether the pin is an input or an output. Only some boards have one. `106=0 (in)` means that the
   function is always an input. `106: 0 in, 1 out` means that you choose the direction. Set the line to 0 for an input or to 1 for
   an output.
 * In a GPIO row, the GPIO line column gives the line that you read or drive. In the DET row, the column gives the DET line.
 * The `stm-util` channel column gives the channel that controls FSYNC or the strobe.

## Configure a pin or the USB2 route

### Set and read a line

Most M8 lines are on the GPIO chip `f000000.pinctrl`. To set a line, give the chip, the line number, and the level. This command
sets line 32 to 1:

```sh
gpioset f000000.pinctrl 32=1
```

The DEV-KIT tables name some lines, such as `SWITCH2`, instead of giving a number. These lines are on an I/O expander. `gpiofind`
prints the chip and line of a name, so this command sets `SWITCH2` to 1:

```sh
gpioset $(gpiofind SWITCH2)=1
```

`gpioget` reads a line and prints 0 or 1. It also switches the line to input. To switch a line to input without printing the
level, send the output to `/dev/null`:

```sh
gpioget f000000.pinctrl 20             # print the level of line 20
gpioget f000000.pinctrl 20 >/dev/null  # switch line 20 to input only
```

### Change a pin function in a safe order

Follow this order so that the connector never drives a pin against the camera or against your device:

 1. If the pin has a GPIO row, switch the GPIO line of that row to input with `gpioget`. Skip this step only if the pin becomes a
    GPIO output.
 2. If the pin becomes an output, set its direction line to 1.
 3. Set the select lines.
 4. If the pin becomes an input, set its direction line to 0.
 5. If the pin is now a GPIO, read its GPIO line with `gpioget` or drive it with `gpioset`.

If the pin has no direction line, skip steps 2 and 4. When pins 1 and 6 change together, do each step on both pins before you
start the next step.

On OAK4-FFC R2, these commands make pin 6 the strobe output:

```sh
gpioget f000000.pinctrl 95 >/dev/null   # step 1: pin 6 GPIO line to input
gpioset f000000.pinctrl 74=1            # step 2: pin 6 direction to output
gpioset f000000.pinctrl 45=1            # step 3: pin 6 select line for strobe
gpioset f000000.pinctrl 44=0            # step 3: pin 6 select line for strobe
```

### Read DET

DET on pin 7 reads 0 when something is attached and 1 when nothing is attached. `agentconfd` holds the DET line, so stop it first.
If the DET row gives a direction line, set it as the row says. Then read the line from the GPIO line column. This example reads
DET on line 123:

```sh
systemctl stop agentconfd
gpioget f000000.pinctrl 123
systemctl start agentconfd
```

The notes of each board show how to read DET without stopping `agentconfd`.

### Start FSYNC or a strobe

The lines in an FSYNC or strobe row only connect the pin to the STM32 microcontroller on the board. The STM32 drives or reads the
signal. You control it with `stm-util` on the channel that the table gives. These commands start FSYNC output at 30 Hz on channel
11, then stop it:

```sh
stm-util write info 1; stm-util write pin 11 30 1 1024
stm-util write pin 11 0 0 0; stm-util write info 0
```

To start a strobe at 30 Hz, run `stm-util write pin <channel> 30 1 205`. To stop it, run `stm-util write pin <channel> 0 0 0`.

### Scan the M8 I2C bus

The M8 I2C bus is `988000.i2c`. Its bus number differs per board, so this command looks the number up and scans the bus:

```sh
i2cdetect -y -r $(basename $(ls -d /sys/bus/platform/devices/988000.i2c/i2c-*) | cut -d- -f2)
```

### Route USB2

The USB2 route table of each board gives the level of the select line for each route. A USB device on M8 pins 3 and 4 needs the
camera to be the USB host. While USB2 is on the M8, a computer on the USB-C port loses its USB connection to the camera.

If the board notes say so, stop `agentconfd` before you route USB2. To route USB2 to M8 pins 3 and 4, first make the camera the
USB host, then set the select line. This example is for a board whose table gives `32=1` for M8 pins 3 and 4:

```sh
echo host > /sys/class/usb_role/a600000.ssusb-role-switch/role
gpioset f000000.pinctrl 32=1
```

To route USB2 back, first set the select line to the level for the USB-C port, then make the camera a USB device again. If you
stopped `agentconfd`, start it again after these commands:

```sh
gpioset f000000.pinctrl 32=0
echo device > /sys/class/usb_role/a600000.ssusb-role-switch/role
```

## Keep a level after gpioset exits

`gpioset` sets a level and exits. libgpiod does not guarantee that the line keeps the level after that. Lines on `f000000.pinctrl`
keep their level and direction after `gpioset` exits, so you can set those lines once. To hold a line for certain, keep `gpioset`
running in the background:

```sh
gpioset -m signal f000000.pinctrl 32=1 &
```

While it runs, other `gpioset` calls on that line fail with `Device or resource busy`. To release the line, kill the process.

## Stop agentconfd when the board notes say so

The system service `agentconfd` watches the M8 connector. It holds the DET line, and on most boards it changes the USB2 route when
something is plugged in. The notes of each board say when to stop it with `systemctl stop agentconfd`.

> **agentconfd stopped**
> While `agentconfd` is stopped, the factory reset button and power monitoring do not work. When you are done, start it again with `systemctl start agentconfd`.

## Find your board

Print the model string of the device:

```sh
cat /proc/device-tree/model
```

The output is `Luxonis, Inc.` followed by the model, for example `Luxonis, Inc. OAK4-S R9`. Find the model in this table and go to
its section under "Board reference".

| Model | Section |
| --- | --- |
| `OAK4-S R0`, `OAK4-S R1`, `OAK4-S R2` | OAK4-S R0, R1, R2 |
| `OAK4-S R5`, `OAK4-S R9` | OAK4-S R5, R9 |
| `OAK4-D R0`, `OAK4-D R1`, `OAK4-D R2`, `OAK4-D R7`, `OAK4-D R9` | OAK4-D R0, R1, R2, R7, R9 |
| `DEV-KIT R3`, `DEV-KIT R4` | DEV-KIT R3, R4 |
| `OAK4-FFC R0` | OAK4-FFC R0 |
| `OAK4-FFC R1` | OAK4-FFC R1 |
| `OAK4-FFC R2` | OAK4-FFC R2 |

## Board reference

### OAK4-S R0, R1, R2

Models: `OAK4-S R0`, `OAK4-S R1`, `OAK4-S R2`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | 79=1 | - | - | - |
| 1 | I2C SDA | 79=0 | - | - | - |
| 1 | GPIO | 79=0 | - | 20 | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | 124=1 | - | - | - |
| 6 | I2C SCL | 124=0 | - | - | - |
| 6 | GPIO | 124=0 | - | 21 | - |
| 7 | DET | - | - | 123 | - |
| 7 | GPIO | - | - | 123 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| M8 pins 3 and 4 | 32=1 |
| USB-C port | 32=0 |

#### Notes for this board

 * `agentconfd` holds DET (line 123) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio123 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.
 * In GPIO mode, pins 1 and 6 use lines 20 and 21. These lines are also the pads of the I2C bus `988000.i2c`. Do not use that I2C
   bus while you use these pins as GPIO. An access to the bus takes the lines away from GPIO until the next `gpioget` or `gpioset`
   on them.
 * On OAK4-S R2, the system exports line 32 through sysfs (as GPIO 333) at boot. Until you release it with `echo 333 >
   /sys/class/gpio/unexport`, `gpioset` on line 32 fails with `Device or resource busy`. Run this command before you route USB2 to
   the M8.

### OAK4-S R5, R9

Models: `OAK4-S R5`, `OAK4-S R9`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | 79=1 | - | - | - |
| 1 | I2C SDA | 79=0 | - | - | - |
| 1 | GPIO | 79=0 | - | 20 | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | 124=1 | - | - | - |
| 6 | I2C SCL | 124=0 | - | - | - |
| 6 | GPIO | 124=0 | - | 21 | - |
| 7 | DET | - | - | 123 | - |
| 7 | GPIO | - | - | 123 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| M8 pins 3 and 4 | 32=0 |
| USB-C port | 32=1 |

#### Notes for this board

 * `agentconfd` holds DET (line 123) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio123 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.
 * In GPIO mode, pins 1 and 6 use lines 20 and 21. These lines are also the pads of the I2C bus `988000.i2c`. Do not use that I2C
   bus while you use these pins as GPIO. An access to the bus takes the lines away from GPIO until the next `gpioget` or `gpioset`
   on them.

### OAK4-D R0, R1, R2, R7, R9

Models: `OAK4-D R0`, `OAK4-D R1`, `OAK4-D R2`, `OAK4-D R7`, `OAK4-D R9`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | 163=1 | - | - | - |
| 1 | I2C SDA | 163=0 | - | - | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | 162=1 | - | - | - |
| 6 | I2C SCL | 162=0 | - | - | - |
| 7 | DET | - | - | 123 | - |
| 7 | GPIO | - | - | 123 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| M8 pins 3 and 4 | 12=1 |
| USB-C port | 12=0 |

#### Notes for this board

 * `agentconfd` holds DET (line 123) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio123 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.

### DEV-KIT R3, R4

Models: `DEV-KIT R3`, `DEV-KIT R4`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | SWITCH2=1 | - | - | - |
| 1 | I2C SDA | SWITCH2=0 | - | - | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | SWITCH1=1 | - | - | - |
| 6 | I2C SCL | SWITCH1=0 | - | - | - |
| 7 | DET | - | - | 125 | - |
| 7 | GPIO | - | - | 125 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| M8 pins 3 and 4 | USB_SEL=1 |
| USB-C port | USB_SEL=0 |

#### Notes for this board

 * `agentconfd` holds DET (line 125) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio125 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` does not change the USB2 route on this board. You can route USB2 without stopping it.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.
 * The select lines of pins 1 and 6 (`SWITCH2` and `SWITCH1`) and the USB2 select line (`USB_SEL`) are on an I/O expander, so the
   tables name them instead of giving a number. `gpiofind SWITCH2` prints the chip and line of `SWITCH2`.

### OAK4-FFC R0

Models: `OAK4-FFC R0`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | - | - | - | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | - | - | - | - |
| 7 | DET | - | - | 125 | - |
| 7 | GPIO | - | - | 125 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| USB-C port | 117=1, fixed |

USB2 cannot reach the M8 on this board. Keep line 117 at 1.

#### Notes for this board

 * `agentconfd` holds DET (line 125) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio125 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.

### OAK4-FFC R1

Models: `OAK4-FFC R1`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | UART RX | 55=0 | - | - | - |
| 1 | GPIO | 55=1 | - | 20 | - |
| 2 | FSYNC | - | - | - | 11 |
| 6 | UART TX | 54=0 | - | - | - |
| 6 | GPIO | 54=1 | - | 21 | - |
| 7 | DET | - | - | 125 | - |
| 7 | GPIO | - | - | 125 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| USB-C port | 68=1, fixed |

USB2 cannot reach the M8 on this board. Keep line 68 at 1.

#### Notes for this board

 * `agentconfd` holds DET (line 125) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio125 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.
 * In GPIO mode, pins 1 and 6 use lines 20 and 21. These lines are also the pads of the I2C bus `988000.i2c`. Do not use that I2C
   bus while you use these pins as GPIO. An access to the bus takes the lines away from GPIO until the next `gpioget` or `gpioset`
   on them.

### OAK4-FFC R2

Models: `OAK4-FFC R2`.

#### Pinout

| Pin | Function | Select lines | Direction line | GPIO line | `stm-util` channel |
| --- | --- | --- | --- | --- | --- |
| 1 | GPIO | 54=0 | 106: 0 in, 1 out | 20 | - |
| 1 | UART RX | 54=1 | 106=0 (in) | - | - |
| 2 | FSYNC | 55=0 | 72: 0 in, 1 out | - | 11 |
| 2 | GPIO | 55=1 | 72: 0 in, 1 out | 21 | - |
| 6 | GPIO | 45=0, 44=0 | 74: 0 in, 1 out | 95 | - |
| 6 | Strobe | 45=1, 44=0 | 74=1 (out) | - | 13 |
| 6 | UART TX | 45=0, 44=1 | 74=1 (out) | - | - |
| 7 | DET | - | 75=0 (in) | 125 | - |
| 7 | GPIO | - | 75: 0 in, 1 out | 125 | - |

#### USB2 routes

| Route | Select line |
| --- | --- |
| M8 pins 3 and 4 | 68=0 |
| USB-C port | 68=1 |

#### Notes for this board

 * `agentconfd` holds DET (line 125) while it runs, so `gpioget` on that line fails with `Device or resource busy`. Stop
   `agentconfd` before you use pin 7. To read DET without stopping it, run `grep -E "^ ?gpio125 " /sys/kernel/debug/gpio`. The
   output shows `in low` when something is attached.
 * `agentconfd` changes the USB2 route when something is plugged in. Stop it before you route USB2 by hand.
 * You do not need to stop `agentconfd` to use pins 1, 2, and 6.
 * In GPIO mode, pins 1 and 2 use lines 20 and 21. These lines are also the pads of the I2C bus `988000.i2c`. Do not use that I2C
   bus while you use these pins as GPIO. An access to the bus takes the lines away from GPIO until the next `gpioget` or `gpioset`
   on them.
 * Other parts of the board can also take some M8 lines. The SPI bus uses lines 44 and 45, the camera control bus uses lines 74
   and 75, and PCIe uses line 95. A `gpioget` or `gpioset` on a line takes it back to GPIO. After you set a function, check that
   lines 20, 21, 44, 45, 74, 75, and 95 show `func0`, the GPIO function: `grep -E "^ ?gpio(20|21|44|45|74|75|95) "
   /sys/kernel/debug/gpio`.

## Lines that the driver holds from Luxonis OS 1.49.0

From Luxonis OS 1.49.0, a driver controls the M8 connector and holds the lines in this table. `gpioset` and `gpioget` on these
lines fail with `Device or resource busy`, so the steps on this page fail on Luxonis OS 1.49.0 and later. On those releases, use
[OAK4 M8 Control](https://docs.luxonis.com/hardware/platform/deploy/oak4-m8-interface/oak4-m8-control.md).

| Board | Lines on `f000000.pinctrl` |
| --- | --- |
| OAK4-S | 20, 21, 32, 79, 123, 124 |
| OAK4-D | 12, 123, 162, 163 |
| DEV-KIT R3, R4 | 125, and `SWITCH1`, `SWITCH2` and `USB_SEL` on the I/O expander |
| OAK4-FFC R0 | 117, 125 |
| OAK4-FFC R1 | 20, 21, 54, 55, 68, 125 |
| OAK4-FFC R2 | 20, 21, 44, 45, 54, 55, 68, 72, 74, 75, 95, 106, 125 |
