# Example 24: SD Card Read/Write

## Experiment Overview

Read/write SD/TF cards via SPI or SDIO interface for data storage, logging and configuration file management. FastBee has reserved the `SDIO` peripheral type (type 37) and configuration framework, but runtime SD card mounting and file read/write actions are not yet built-in.

> **Note**: This page is a wiring and configuration placeholder. Dedicated runtime mount and file actions are not complete; implement and test them in a custom higher-resource environment before production use.

## Hardware Wiring

### SPI Mode

| Board Label | GPIO Pin | SD Card Pin |
|-------------|----------|-------------|
| MOSI | GPIO23 | DI (CMD) |
| MISO | GPIO19 | DO (DAT0) |
| CLK | GPIO18 | CLK |
| CS | GPIO5 | CS (DAT3) |

### SDIO Mode (Faster)

| Board Label | GPIO Pin | SD Card Pin |
|-------------|----------|-------------|
| CLK | GPIO14 | CLK |
| CMD | GPIO15 | CMD |
| D0 | GPIO2 | DAT0 |
| D1 | GPIO4 | DAT1 |
| D2 | GPIO12 | DAT2 |
| D3 | GPIO13 | DAT3 |

## JSON Configuration Example

### SPI Mode

```json
{
  "peripherals": [
    {
      "id": "sd_spi",
      "name": "SD Card-SPI",
      "type": 37,
      "enabled": false,
      "pins": [23, 19, 18, 5],
      "params": {}
    }
  ]
}
```

### SDIO Mode

```json
{
  "peripherals": [
    {
      "id": "sd_sdio",
      "name": "SD Card-SDIO",
      "type": 37,
      "enabled": false,
      "pins": [14, 15, 2, 4, 12, 13],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

> ⚠️ **Note**: Peripheral execution currently does not have built-in `file_append`/`file_write` file actions. Below is the future expansion direction.

### Future Scenario: Temperature Data Log (Timer Trigger)

**Function**: Read temperature every 60 seconds and write to SD card log file

**Expected Configuration Steps** (pending firmware support):

1. Create rule, name: `Temperature Data Log`
2. Trigger configuration:
   * **Trigger Type**: Select **Timer Trigger**
   * **Timer Mode**: Select **Fixed Interval**
   * **Interval Time**: `60000` (60 seconds)
3. Action configuration:
   * **Action 1**: Read Sensor → `dht_01`
   * **Action 2**: File Append → `/sd/temperature_log.csv`

**Expected Log Format**:

```csv
Timestamp,Temperature(°C),Humidity(%)
2024-01-01 08:00:00,25.3,60
2024-01-01 08:01:00,25.4,59
```

## Notes

1. **Format**: SD card must be formatted as FAT32 (<32GB) or exFAT (≥32GB)
2. **Power**: SD card working current can reach 100mA, ensure stable 3.3V supply
3. **SPI Sharing**: When SD card uses SPI bus, CS pin is used for device selection
4. **GPIO12 Conflict**: In SDIO mode GPIO12 is DAT2, must not be HIGH during boot
5. **Write Frequency**: Avoid too frequent write operations, recommend batch writing
6. **File System**: FastBee uses LittleFS for internal Flash, SD card uses FAT file system
7. **Hot Swap**: Not supported, SD card must be inserted/removed when powered off
8. **Current Status**: Configuration framework ready, runtime driver and file actions pending
