diff --git a/Src/periphery/led/led.hpp b/Src/periphery/led/led.hpp index 93361c1..7d3fb98 100644 --- a/Src/periphery/led/led.hpp +++ b/Src/periphery/led/led.hpp @@ -1,6 +1,8 @@ -/// This software is distributed under the terms of the MIT License. -/// Copyright (c) 2022-2023 Dmitry Ponomarev. -/// Author: Dmitry Ponomarev +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + */ #ifndef SRC_APPLICATION_PERIPHERY_LED_LED_HPP_ #define SRC_APPLICATION_PERIPHERY_LED_LED_HPP_ diff --git a/Src/periphery/pwm/pwm.hpp b/Src/periphery/pwm/pwm.hpp index cf7e5f2..f2e98a7 100644 --- a/Src/periphery/pwm/pwm.hpp +++ b/Src/periphery/pwm/pwm.hpp @@ -1,6 +1,8 @@ -/// This software is distributed under the terms of the MIT License. -/// Copyright (c) 2023 Dmitry Ponomarev. -/// Author: Dmitry Ponomarev +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + */ #ifndef SRC_APPLICATION_PERIPHERY_PWM_HPP_ #define SRC_APPLICATION_PERIPHERY_PWM_HPP_ diff --git a/Src/platform/stm32f103/led.cpp b/Src/platform/stm32f103/led.cpp index cab6d66..62ec5d2 100644 --- a/Src/platform/stm32f103/led.cpp +++ b/Src/platform/stm32f103/led.cpp @@ -1,41 +1,33 @@ -/// This software is distributed under the terms of the MIT License. -/// Copyright (c) 2022-2023 Dmitry Ponomarev. -/// Author: Dmitry Ponomarev +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + */ #include "periphery/led/led.hpp" #include "main.h" +static void write_red(GPIO_PinState state) { + HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, state); +} +static void write_green(GPIO_PinState state) { + HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, state); +} +static void write_blue(GPIO_PinState state) { + HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, state); +} void LedPeriphery::reset() { - HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET); + write_red(GPIO_PIN_SET); + write_green(GPIO_PIN_SET); + write_blue(GPIO_PIN_SET); } -void LedPeriphery::toggle(LedColor led_color) { +void LedPeriphery::toggle(LedColor color) { auto crnt_time_ms = HAL_GetTick(); GPIO_PinState state = (crnt_time_ms % 1000 > 500) ? GPIO_PIN_SET : GPIO_PIN_RESET; - switch (led_color) { - case LedColor::RED_COLOR: - HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, state); - HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET); - break; - - case LedColor::GREEN_COLOR: - HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, state); - HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET); - break; - - case LedColor::BLUE_COLOR: - HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, state); - break; - - default: - break; - } + write_red(color == LedColor::RED_COLOR ? state : GPIO_PIN_SET); + write_green(color == LedColor::BLUE_COLOR ? state : GPIO_PIN_SET); + write_blue(color == LedColor::GREEN_COLOR ? state : GPIO_PIN_SET); } diff --git a/Src/platform/stm32f103/pwm.cpp b/Src/platform/stm32f103/pwm.cpp index dfd0d12..a0492f4 100644 --- a/Src/platform/stm32f103/pwm.cpp +++ b/Src/platform/stm32f103/pwm.cpp @@ -1,6 +1,8 @@ -/// This software is distributed under the terms of the MIT License. -/// Copyright (c) 2022-2023 Dmitry Ponomarev. -/// Author: Dmitry Ponomarev +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + */ #include "periphery/pwm/pwm.hpp" #include "main.h" @@ -8,90 +10,42 @@ extern TIM_HandleTypeDef htim3; extern TIM_HandleTypeDef htim4; -int8_t PwmPeriphery::init(PwmPin pwm_pin) { - switch (pwm_pin) { - case PwmPin::PWM_1: - if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2)) { - return -1; - } - TIM4->CCR2 = 1000; - break; - - case PwmPin::PWM_2: - if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1)) { - return -1; - } - TIM4->CCR1 = 1000; - break; +struct PwmPinInfo { + TIM_HandleTypeDef& htim; + uint32_t channel; + volatile uint32_t& ccr; +}; - case PwmPin::PWM_3: - if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1)) { - return -1; - } - TIM3->CCR1 = 1000; - break; +const static PwmPinInfo info[static_cast(PwmPin::PWM_AMOUNT)] = { + {.htim = htim4, .channel = TIM_CHANNEL_2, .ccr = TIM4->CCR2}, // PB7 + {.htim = htim4, .channel = TIM_CHANNEL_1, .ccr = TIM4->CCR1}, // PB6 + {.htim = htim3, .channel = TIM_CHANNEL_1, .ccr = TIM3->CCR1}, // PB4 + {.htim = htim3, .channel = TIM_CHANNEL_2, .ccr = TIM3->CCR2}, // PB5 +}; - case PwmPin::PWM_4: - if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2)) { - return -1; - } - TIM3->CCR2 = 1000; - break; - - default: - return -1; +int8_t PwmPeriphery::init(PwmPin pwm_pin) { + if (pwm_pin > PwmPin::PWM_AMOUNT) { + return -1; } - return 0; + auto& pwm_pin_info = info[static_cast(pwm_pin)]; + return HAL_OK == HAL_TIM_PWM_Start(&pwm_pin_info.htim, pwm_pin_info.channel) ? 0 : -1; } void PwmPeriphery::set_duration(const PwmPin pwm_pin, uint32_t duration_us) { - switch (pwm_pin) { - case PwmPin::PWM_1: - TIM4->CCR2 = duration_us; - break; - - case PwmPin::PWM_2: - TIM4->CCR1 = duration_us; - break; - - case PwmPin::PWM_3: - TIM3->CCR1 = duration_us; - break; - - case PwmPin::PWM_4: - TIM3->CCR2 = duration_us; - break; - - default: - break; + if (pwm_pin > PwmPin::PWM_AMOUNT) { + return; } + + auto& pwm_pin_info = info[static_cast(pwm_pin)]; + pwm_pin_info.ccr = duration_us; } uint32_t PwmPeriphery::get_duration(PwmPin pwm_pin) { - uint32_t pwm_duration; - - switch (pwm_pin) { - case PwmPin::PWM_1: - pwm_duration = TIM4->CCR2; - break; - - case PwmPin::PWM_2: - pwm_duration = TIM4->CCR1; - break; - - case PwmPin::PWM_3: - pwm_duration = TIM3->CCR1; - break; - - case PwmPin::PWM_4: - pwm_duration = TIM3->CCR2; - break; - - default: - pwm_duration = 0; - break; + if (pwm_pin > PwmPin::PWM_AMOUNT) { + return 0; } - return pwm_duration; + auto& pwm_pin_info = info[static_cast(pwm_pin)]; + return pwm_pin_info.ccr; }