Skip to content

Peripherals

Hrisabh Yadav edited this page Nov 14, 2025 · 1 revision

Peripherals.h

Hardware peripheral interfaces for GPIO, ADC, and PWM control.

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
} 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/5V)
    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;

Functions

GPIO Functions

  • void Peripheral_Init(peripheral_gpio_pin_e _gpio_pin, GPIO_Mode_e _mode) - Initialize GPIO pin
  • bool Peripheral_Read(peripheral_gpio_pin_e _gpio_pin) - Read GPIO state
  • void Peripheral_Write(peripheral_gpio_pin_e _gpio_pin, GPIO_State_e _state) - Write GPIO state

ADC Functions

  • void Peripheral_Init(peripheral_adc_pin _pin) - Initialize ADC pin
  • uint16_t Peripheral_Read(peripheral_adc_pin _adc_pin) - Read ADC value (0-4095)

PWM Functions

  • void Peripheral_Init(peripheral_pwm_pin_e _pin, uint16_t pwmRate = 50) - Initialize PWM with frequency
  • void Peripheral_Write(peripheral_pwm_pin_e _pwm_pin, uint16_t _pwm_value) - Set PWM duty cycle

Usage Example

// GPIO control
Peripheral_Init(GPIO_1, OUTPUT);           // LED output
Peripheral_Init(GPIO_2, INPUT_PULL_UP);    // Button input
Peripheral_Write(GPIO_1, STATE_HIGH);      // Turn LED on
bool buttonPressed = Peripheral_Read(GPIO_2);

// ADC reading
Peripheral_Init(ADC_1);  // Battery monitor
uint16_t batteryRaw = Peripheral_Read(ADC_1);
float voltage = (batteryRaw * 3.3f) / 4095.0f;

// PWM control
Peripheral_Init(PWM_1, 50);    // Servo (50Hz)
Peripheral_Init(PWM_2, 1000);  // LED (1kHz)
Peripheral_Write(PWM_1, 1500); // Center servo
Peripheral_Write(PWM_2, 500);  // 50% LED brightness

Clone this wiki locally