#pragma once

// IWYU pragma: private

/// @file platforms/arm/sam/pin_sam_native.hpp
/// Native SAM pin system implementation using direct GPIO register access
///
/// Implements Arduino-compatible pin functions using SAM3X8E PIO registers.
/// This provides GPIO control without requiring the Arduino framework.
///
/// SAM3X8E PIO Architecture:
/// - Organized into 4 parallel I/O controllers: PIOA, PIOB, PIOC, PIOD
/// - Each controller manages up to 32 pins
/// - Key registers per controller:
///   - PIO_PER: PIO Enable Register
///   - PIO_OER: Output Enable Register
///   - PIO_SODR: Set Output Data Register
///   - PIO_CODR: Clear Output Data Register
///   - PIO_ODSR: Output Data Status Register
///   - PIO_PDSR: Pin Data Status Register
///
/// Pin Mapping:
/// - Arduino pin numbers must be converted to (PIO controller, pin_bit) pairs
/// - This implementation uses stub functions for non-Arduino builds

#include "platforms/arm/sam/is_sam.h"

#if defined(FL_IS_SAM)

namespace fl {
namespace platforms {

// Make enum types from fl namespace visible
using ::fl::PinMode;
using ::fl::PinValue;
using ::fl::AdcRange;

// ============================================================================
// GPIO Functions - Native SAM Implementation (STUBS)
// ============================================================================

/// Set pin mode (input, output, input_pullup)
/// @param pin Arduino pin number
/// @param mode Pin mode (Input, Output, InputPullup, InputPulldown)
/// @note STUB: Real implementation requires SAM3X8E register access
inline void pinMode(int /*pin*/, PinMode /*mode*/) {
    // STUB: Native SAM pin mode configuration requires:
    // 1. Map Arduino pin to (PIO controller, bit position)
    // 2. Enable PIO control (PIO_PER)
    // 3. Configure direction (PIO_OER for output, PIO_ODR for input)
    // 4. Configure pull-up (PIO_PUER/PIO_PUDR)
    //
    // For now, no-op as stub.
}

/// Write digital output value
/// @param pin Arduino pin number
/// @param val Output value (Low or High)
/// @note STUB: Real implementation requires SAM3X8E register access
inline void digitalWrite(int /*pin*/, PinValue /*val*/) {
    // STUB: Native SAM digital write requires:
    // 1. Map Arduino pin to (PIO controller, bit position)
    // 2. Use PIO_SODR to set high or PIO_CODR to set low
    //
    // For now, no-op as stub.
}

/// Read digital input value
/// @param pin Arduino pin number
/// @return Pin state (Low or High)
/// @note STUB: Real implementation requires SAM3X8E register access
inline PinValue digitalRead(int /*pin*/) {
    // STUB: Native SAM digital read requires:
    // 1. Map Arduino pin to (PIO controller, bit position)
    // 2. Read from PIO_PDSR register
    //
    // For now, return Low as stub.
    return PinValue::Low;
}

/// Read analog input value
/// @param pin Arduino pin number
/// @return ADC value (0-4095 for 12-bit SAM3X8E ADC)
/// @note STUB: Real implementation requires ADC peripheral configuration
inline u16 analogRead(int /*pin*/) {
    // STUB: ADC implementation requires:
    // 1. Enable ADC peripheral clock (PMC)
    // 2. Configure ADC resolution, timing, reference
    // 3. Map pin to ADC channel
    // 4. Start conversion and wait for completion
    // 5. Read ADC result register
    //
    // For now, return 0 as stub.
    return 0;
}

/// Write analog output value (PWM)
/// @param pin Arduino pin number
/// @param val PWM duty cycle (0-255)
/// @note STUB: Real implementation requires PWM peripheral configuration
inline void analogWrite(int /*pin*/, u16 /*val*/) {
    // STUB: PWM implementation requires:
    // 1. Enable PWM peripheral clock (PMC)
    // 2. Configure PWM channel mode, period, prescaler
    // 3. Map pin to PWM output
    // 4. Set duty cycle register
    //
    // For now, no-op as stub.
}

/// Set PWM duty cycle with 16-bit resolution
/// @param pin Arduino pin number
/// @param val PWM duty cycle (0-65535)
/// @note STUB: Real implementation requires PWM peripheral configuration
inline void setPwm16(int /*pin*/, u16 /*val*/) {
    // STUB: 16-bit PWM would configure SAM3X8E PWM controller
    // with CPRD=65535 for full 16-bit resolution
    // For now, no-op as stub.
}

/// Set ADC voltage range
/// @param range Voltage range for analog readings
/// @note Arduino Due doesn't support analogReference - always 3.3V
inline void setAdcRange(AdcRange /*range*/) {
    // Arduino Due analog reference is fixed at 3.3V
    // This function does nothing on the Due platform
}

// ============================================================================
// PWM Frequency Control
// ============================================================================

inline bool needsPwmIsrFallback(int /*pin*/, u32 /*frequency_hz*/) {
    return true;
}

inline int setPwmFrequencyNative(int /*pin*/, u32 /*frequency_hz*/) {
    return -4;
}

inline u32 getPwmFrequencyNative(int /*pin*/) {
    return 0;
}

}  // namespace platforms
}  // namespace fl

#endif  // FL_IS_SAM
