# i2c – I²C-transaktioner

> Utför I²C-transaktioner — läs, skriv och skanna bussen efter enheter — direkt från skalet.

_Källa: E-Sharp (esharp.se) · Accordion Shell (CLI)_


## Description

The `i2c` command performs I2C bus transactions including reading, writing, and scanning for devices. It provides low-level I2C communication capabilities.

## Syntax

```bash
i2c --channel <channel_name> [options]
```

## Required Options

| Option | Short | Description |
| --- | --- | --- |
| `--channel` | `-c` | I2C channel name to use for the transaction |

## Optional Options

| Option | Short | Description |
| --- | --- | --- |
| `--address` | `-a` | 7-bit I2C device address (prefix with 0x for hex) |
| `--write` | `-w` | Perform write operation |
| `--read` | `-r` | Perform read operation |
| `--scan` | `-s` | Scan I2C bus for devices |
| `--length` | `-l` | Number of bytes to read (default: 1) |
| `--data` | `-d` | Data bytes to send (space-separated) |

## Examples

```bash
i2c --channel "I2C_BUS_1" --scan
i2c -c "I2C_BUS_1" -a 0x50 -w -d 0x00
i2c -c "I2C_BUS_1" -a 0x50 -w -d 0x00 0xFF 0xAA 0x55
i2c -c "I2C_BUS_1" -a 0x50 -r -l 16
i2c -c "I2C_BUS_1" -a 0x50 -w -r -l 4 -d 0x00
```

## Operation Types

| Operation | Flags | Description |
| --- | --- | --- |
| Scan | `--scan` | Detect all devices on bus |
| Send | `--write` | Write data only |
| Receive | `--read` | Read data only |
| SendReceive | `--write --read` | Write then read (restart) |

## Output Format

```
Sent: 0x00 0xFF
Received: 0x12 0x34 0x56 0x78
Transaction took 3.45 ms
```

## Address & Data Format

Addresses may be hex (`0x50`) or decimal (`80`) — always 7-bit (not 8-bit with R/W bit). Data bytes may be hex, decimal or mixed (`0xFF 0 0xAA`).

## Use Cases

### Read EEPROM

```bash
i2c -c "I2C_MAIN" -a 0x50 -w -d 0x00 0x00
i2c -c "I2C_MAIN" -a 0x50 -r -l 256
```

### Detect Devices

```bash
i2c -c "I2C_MAIN" -s
```

## Error Handling

- **No option supplied**: Must specify scan, read, or write
- **Address required**: Read/write operations need device address
- **NACK**: Device didn't acknowledge (wrong address or device error)
- **Timeout**: Bus or device not responding
- **Not connected**: Run `init` command first

## Notes

- Requires an active connection (use `init` first)
- Speed depends on hardware (typically 100 kHz or 400 kHz)
- Address range 0x08–0x77 (7-bit); 0x00–0x07 and 0x78–0x7F reserved
