Skip to content

Peripherals

Ashish Jaiswal edited this page Jul 21, 2026 · 3 revisions

Peripherals.h

Hardware peripheral interfaces for GPIO, ADC, and PWM. Peripheral_Init, Peripheral_Read, and Peripheral_Write are overloaded: the pin-enum type you pass (GPIO / ADC / PWM) selects which peripheral you are driving.

Enumerations

peripheral_gpio_pin_e

typedef enum peripheral_gpio {
    GPIO_1, GPIO_2, GPIO_3, GPIO_4, GPIO_5,
    GPIO_6, GPIO_7, GPIO_8, GPIO_9, GPIO_10,
    GPIO_11, GPIO_12, GPIO_13, GPIO_14, GPIO_15,
    GPIO_16, GPIO_17, GPIO_18,
    GPIO_COUNT   // total count (not a usable pin)
} peripheral_gpio_pin_e;

GPIO_Mode_e

typedef enum gpio_mode {
    INPUT,              // Floating input
    INPUT_PULL_UP,      // Input with pull-up resistor
    INPUT_PULL_DOWN,    // Input with pull-down resistor
    OUTPUT              // Push-pull output
} GPIO_Mode_e;

GPIO_State_e

typedef enum gpio_state {
    STATE_LOW,      // Logic low (0V)
    STATE_HIGH,     // Logic high (3.3V)
    STATE_TOGGLE    // Toggle between low and high
} GPIO_State_e;

peripheral_adc_pin

typedef enum peripheral_adc {
    ADC_1, ADC_2, ADC_3, ADC_4, ADC_5,
    ADC_6, ADC_7, ADC_8, ADC_9
} peripheral_adc_pin;

peripheral_pwm_pin_e

typedef enum peripheral_pwm {
    PWM_1, PWM_2, PWM_3, PWM_4, PWM_5,
    PWM_6, PWM_7, PWM_8, PWM_9, PWM_10
} peripheral_pwm_pin_e;

GPIO Functions

void Peripheral_Init(peripheral_gpio_pin_e _gpio_pin, GPIO_Mode_e _mode)

Configures a GPIO pin as input or output. Do it once at startup.

void plutoInit ( void ) {
    Peripheral_Init ( GPIO_1, OUTPUT );          // LED output
    Peripheral_Init ( GPIO_2, INPUT_PULL_UP );   // button input
}

bool Peripheral_Read(peripheral_gpio_pin_e _gpio_pin)

Returns true if the pin is high, false if low or invalid. Poll it in the loop.

void plutoLoop ( void ) {
    if ( Peripheral_Read ( GPIO_2 ) ) {
        Monitor_Println ( "Button high" );
    }
}

void Peripheral_Write(peripheral_gpio_pin_e _gpio_pin, GPIO_State_e _state)

Drives a GPIO pin STATE_LOW, STATE_HIGH, or STATE_TOGGLE.

void plutoLoop ( void ) {
    Peripheral_Write ( GPIO_1, STATE_TOGGLE );   // blink the LED each loop
}

ADC Functions

void Peripheral_Init(peripheral_adc_pin _pin)

Initializes an ADC pin for analog reads. Do it once at startup. (The ADC DMA channel is claimed lazily on first init of a pin on that ADC.)

void plutoInit ( void ) {
    Peripheral_Init ( ADC_1 );
}

uint16_t Peripheral_Read(peripheral_adc_pin _adc_pin)

Returns the latest 12-bit ADC value (0 - 4095), or 0 for an invalid pin.

void plutoLoop ( void ) {
    uint16_t raw = Peripheral_Read ( ADC_1 );
    float voltage = ( raw * 3.3f ) / 4095.0f;    // convert to volts
    Monitor_Print ( "ADC: ", raw );
}

PWM Functions

void Peripheral_Init(peripheral_pwm_pin_e _pin, uint16_t pwmRate = 50)

Initializes a PWM pin at the given frequency in Hz (default 50). Do it once at startup.

void plutoInit ( void ) {
    Peripheral_Init ( PWM_1, 50 );      // 50 Hz (servo)
    Peripheral_Init ( PWM_2, 1000 );    // 1 kHz (LED dimming)
}

void Peripheral_Write(peripheral_pwm_pin_e _pwm_pin, uint16_t _pwm_value)

Sets the duty cycle as a percentage, 0 to 100.

void plutoLoop ( void ) {
    Peripheral_Write ( PWM_2, 50 );   // 50% duty cycle
}

void Servo_Write(peripheral_pwm_pin_e _pwm_pin, uint16_t _pwm_value)

Writes a servo position. The value is constrained to 1000 - 2000 and mapped to a 5% - 10% duty cycle. Use this (not Peripheral_Write) for servos.

void plutoLoop ( void ) {
    Servo_Write ( PWM_1, 1500 );   // center the servo
}

void APIAdcInit(void)

Initializes the ADC subsystem for analog peripheral reads.

void plutoInit ( void ) {
    APIAdcInit();
}

Usage Example

Initialize pins at startup, then read/drive them in the loop.

#include "PlutoPilot.h"

// Power-up: configure GPIO, ADC, and PWM pins.
void plutoInit ( void ) {
    Peripheral_Init ( GPIO_1, OUTPUT );          // LED
    Peripheral_Init ( GPIO_2, INPUT_PULL_UP );   // button
    Peripheral_Init ( ADC_1 );                   // analog sensor
    Peripheral_Init ( PWM_1, 50 );               // servo at 50 Hz
}

// While active: read inputs and drive outputs.
void plutoLoop ( void ) {
    // Mirror the button to the LED.
    Peripheral_Write ( GPIO_1, Peripheral_Read ( GPIO_2 ) ? STATE_HIGH : STATE_LOW );

    // Read an analog sensor (0 - 4095).
    uint16_t raw = Peripheral_Read ( ADC_1 );
    Monitor_Print ( "ADC: ", raw );

    // Center a servo.
    Servo_Write ( PWM_1, 1500 );
}

Clone this wiki locally