Files @ d651f75c8fbc
Branch filter:

Location: HydroBot/protomodule-firmware/Src/gpio.c - annotation

matthewreed
Added ability to read ph meter from analog input
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
a6cb9f350919
#include "gpio.h"

TIM_HandleTypeDef htim1;

uint8_t led_brightness = 0;

gpio_led_t led1 = {GPIO_PIN_10, GPIOA, &htim1, TIM_CHANNEL_3};
gpio_led_t led2 = {GPIO_PIN_9, GPIOA, &htim1, TIM_CHANNEL_2};
gpio_led_t led3 = {GPIO_PIN_8, GPIOA, &htim1, TIM_CHANNEL_1};

gpio_led_t* leds[GPIO_NUM_LEDS] = {&led1, &led2, &led3};

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

void gpio_init(void)
{
    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

//    /*Configure GPIO pin Output Level */
//    HAL_GPIO_WritePin(GPIOA, LED3_Pin|LED2_Pin|LED1_Pin, GPIO_PIN_RESET);
//
//    /*Configure GPIO pins : PAPin PAPin PAPin */
//    GPIO_InitStruct.Pin = LED3_Pin|LED2_Pin|LED1_Pin;
//    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//    GPIO_InitStruct.Pull = GPIO_NOPULL;
//    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    TIM_ClockConfigTypeDef sClockSourceConfig;
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim1.Instance = TIM1;
    htim1.Init.Prescaler = 0;
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 0xFFF;
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim1.Init.RepetitionCounter = 0;
    HAL_TIM_Base_Init(&htim1);

    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);

    HAL_TIM_PWM_Init(&htim1);

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

    sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
    sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
    sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
    sBreakDeadTimeConfig.DeadTime = 0;
    sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
    sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
    sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
    HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
    HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);

    HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);

    HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);

    HAL_TIM_MspPostInit(&htim1);

    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
}



bool gpio_set_led(led_name_t led, bool value)
{
    bool result = true;

    __HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, (uint16_t)(led_brightness * 40.95 * value));

    return result;
}

bool gpio_toggle_led(led_name_t led)
{
    bool result = true;

    if (__HAL_TIM_GET_COMPARE(leds[led]->timer, leds[led]->channel))
    {
        __HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, 0);
    }
    else
    {
        __HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, (uint16_t)(led_brightness * 40.95));
    }

    return result;
}

bool gpio_set_led_brightness(uint8_t brightness)
{
    bool result = true;

    led_brightness = brightness;

    for (uint8_t i = 0; i < GPIO_NUM_LEDS; i++)
    {
        gpio_led_t* led = leds[i];
        if (__HAL_TIM_GET_COMPARE(led->timer, led->channel))
        {
            __HAL_TIM_SET_COMPARE(led->timer, led->channel, (uint16_t)(led_brightness * 40.95));
        }
    }

    return result;
}