# Architecture

Complete system overview showing FreeRTOS, sensor polling loop, protobuf encoding, UDP transmission, and memory layout. Includes initialization sequence and error handling strategy.

## Main Loop Operation

```c
while (1) {
    // STEP 1: System Health Check
    // - Check heap (critical: <8KB)
    // - Toggle status LEDs
    // - Send raw Ethernet beacon
    
    // STEP 2: Initialize Diagnostics Message
    SensorBoardDiagnostics diagnostics_msg;
    
    // STEP 3: Poll All Sensors
    // - pH Sensor
    // - GPS Sensor
    // - IMU Sensor
    // - Load Cells (x2)
    // - Pressure Sensors (x2)
    
    // STEP 4: Encode & Transmit
    // - Encode diagnostics to Protobuf
    // - Send UDP broadcast (192.168.0.255:7)
    
    // STEP 5: Wait
    osDelay(5000); // 5 seconds
}
```

## Error Handling Strategy

### Polling Error States

```c
if (poll_result == RESULT_ERR_UNIMPLEMENTED) {
    sensor.state = SENSOR_IDLE;
    sensor.error_code = COMMUNICATION_FAILURE;

} else if (poll_result == RESULT_ERR_COMMS) {
    sensor.state = SENSOR_ERROR;
    sensor.error_code = COMMUNICATION_FAILURE;

} else if (poll_result == RESULT_OK) {
    if (validate_sensor_data(value) == RESULT_OK) {
        sensor.state = SENSOR_OPERATING;
        sensor.error_code = NO_ERROR;
    } else {
        sensor.state = SENSOR_ERROR;
        sensor.error_code = INVALID_DATA;
    }
}
```

### Sensor Status Codes

<table id="bkmrk-codemeaningsensor_id"><colgroup><col></col><col></col></colgroup><tbody><tr><th>Code

</th><th>Meaning

</th></tr><tr><td>`<span class="editor-theme-code">SENSOR_IDLE</span>`

</td><td>Not connected or not implemented

</td></tr><tr><td>`<span class="editor-theme-code">SENSOR_OPERATING</span>`

</td><td>Normal operation, valid data

</td></tr><tr><td>`<span class="editor-theme-code">SENSOR_ERROR</span>`

</td><td>Communication failure or invalid data

</td></tr></tbody></table>

## Initialization Sequence

### Phase 1: Hardware Setup

```
1. MPU/Cache configuration
2. HAL initialization
3. System clock → 480 MHz
4. RTOS kernel init
5. GPIO initialization
6. Timer initialization (TIM1)
```

### Phase 2: Communication Setup

```
1. UART initialization (115200 baud)
2. Logging system init
3. Ethernet PHY init (LAN8742)
4. MAC address filtering
5. ARP table setup
```

### Phase 3: Application Setup

```
1. Packet dispatcher registration
2. Sensor initialization
3. UDP queue creation
4. UDP callback registration
```

### Phase 4: Main Loop

```
1. LED initialization
2. Sensor polling starts
3. Continuous 5-second cycles
```

```

```