diff --git a/Src/platform/stm32/pwm/pwm.cpp b/Src/platform/stm32/pwm/pwm.cpp
new file mode 100644
index 0000000..d0d2b7c
--- /dev/null
+++ b/Src/platform/stm32/pwm/pwm.cpp
@@ -0,0 +1,60 @@
+/**
+ * This program is free software under the GNU General Public License v3.
+ * See for details.
+ * Author: Dmitry Ponomarev
+ */
+
+#include "pwm.hpp"
+
+extern TIM_HandleTypeDef htim4;
+
+namespace HAL {
+
+int8_t Pwm::init(PwmPin pwm_pin) {
+ if (pwm_pin > PwmPin::PWM_AMOUNT) {
+ return -1;
+ }
+
+ auto& pwm = pwms[static_cast(pwm_pin)];
+ return HAL_OK == HAL_TIM_PWM_Start(&pwm.htim, pwm.channel) ? 0 : -1;
+}
+
+void Pwm::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
+ if (pwm_pin > PwmPin::PWM_AMOUNT) {
+ return;
+ }
+
+ pwms[static_cast(pwm_pin)].ccr = duration_us;
+}
+
+uint32_t Pwm::get_duration(PwmPin pwm_pin) {
+ if (pwm_pin > PwmPin::PWM_AMOUNT) {
+ return 0;
+ }
+
+ return pwms[static_cast(pwm_pin)].ccr;
+}
+
+void Pwm::set_frequency(PwmPin pwm_pin, uint32_t frequency_hz) {
+ if (pwm_pin > PwmPin::PWM_AMOUNT) {
+ return;
+ }
+
+ auto& pwm = pwms[static_cast(pwm_pin)];
+ volatile uint32_t* arr_reg = &(pwm.htim.Instance->ARR);
+ uint16_t period_us = 1000000 / frequency_hz;
+ *arr_reg = period_us;
+}
+
+uint32_t Pwm::get_frequency(PwmPin pwm_pin) {
+ if (pwm_pin > PwmPin::PWM_AMOUNT) {
+ return -1;
+ }
+
+ auto& pwm = pwms[static_cast(pwm_pin)];
+ volatile uint32_t* arr_reg = &(pwm.htim.Instance->ARR);
+ uint32_t frequency = 1000000 / *arr_reg;
+ return frequency;
+}
+
+} // namespace HAL
diff --git a/Src/platform/stm32/pwm/pwm.hpp b/Src/platform/stm32/pwm/pwm.hpp
new file mode 100644
index 0000000..fddcb86
--- /dev/null
+++ b/Src/platform/stm32/pwm/pwm.hpp
@@ -0,0 +1,27 @@
+/**
+ * This program is free software under the GNU General Public License v3.
+ * See for details.
+ * Author: Dmitry Ponomarev
+ */
+
+#ifndef SRC_PLATFORM_STM32_PWM_HPP_
+#define SRC_PLATFORM_STM32_PWM_HPP_
+
+#include
+#include
+#include "periphery/pwm/pwm.hpp"
+#include "main.h"
+
+namespace HAL {
+
+struct PwmPinInfo {
+ TIM_HandleTypeDef& htim;
+ uint32_t channel;
+ volatile uint32_t& ccr;
+};
+
+extern const std::array(PwmPin::PWM_AMOUNT)> pwms;
+
+} // namespace HAL
+
+#endif // SRC_PLATFORM_STM32_PWM_HPP_
diff --git a/Src/platform/stm32f103/CMakeLists.txt b/Src/platform/stm32f103/CMakeLists.txt
index 4e541a8..deca5c6 100644
--- a/Src/platform/stm32f103/CMakeLists.txt
+++ b/Src/platform/stm32f103/CMakeLists.txt
@@ -11,6 +11,7 @@ add_executable(${EXECUTABLE}
${ROOT_DIR}/Src/periphery/adc/circuit_periphery.cpp
${CMAKE_CURRENT_LIST_DIR}/adc.cpp
+ ${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp
${CMAKE_CURRENT_LIST_DIR}/pwm.cpp
${CMAKE_CURRENT_LIST_DIR}/iwdg.cpp
${CMAKE_CURRENT_LIST_DIR}/led.cpp
diff --git a/Src/platform/stm32f103/pwm.cpp b/Src/platform/stm32f103/pwm.cpp
index 0199961..4657a23 100644
--- a/Src/platform/stm32f103/pwm.cpp
+++ b/Src/platform/stm32f103/pwm.cpp
@@ -5,71 +5,18 @@
*/
#include "periphery/pwm/pwm.hpp"
-#include "main.h"
+#include "platform/stm32/pwm/pwm.hpp"
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
namespace HAL {
-struct PwmPinInfo {
- TIM_HandleTypeDef& htim;
- uint32_t channel;
- volatile uint32_t& ccr;
-};
-
-static const PwmPinInfo info[static_cast(PwmPin::PWM_AMOUNT)] = {
+const std::array(PwmPin::PWM_AMOUNT)> pwms = {{
{.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
-};
-
-int8_t Pwm::init(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return -1;
- }
-
- 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 Pwm::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return;
- }
-
- info[static_cast(pwm_pin)].ccr = duration_us;
-}
-
-uint32_t Pwm::get_duration(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return 0;
- }
-
- return info[static_cast(pwm_pin)].ccr;
-}
-
-void Pwm::set_frequency(PwmPin pwm_pin, uint32_t frequency_hz) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return;
- }
-
- auto& pwm_pin_info = info[static_cast(pwm_pin)];
- volatile uint32_t* arr_reg = &(pwm_pin_info.htim.Instance->ARR);
- uint16_t period_us = 1000000 / frequency_hz;
- *arr_reg = period_us;
-}
-
-uint32_t Pwm::get_frequency(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return -1;
- }
-
- auto& pwm_pin_info = info[static_cast(pwm_pin)];
- volatile uint32_t* arr_reg = &(pwm_pin_info.htim.Instance->ARR);
- uint32_t frequency = 1000000 / *arr_reg;
- return frequency;
-}
+}};
} // namespace HAL
diff --git a/Src/platform/stm32g0b1/CMakeLists.txt b/Src/platform/stm32g0b1/CMakeLists.txt
index d364432..54d48fa 100644
--- a/Src/platform/stm32g0b1/CMakeLists.txt
+++ b/Src/platform/stm32g0b1/CMakeLists.txt
@@ -15,6 +15,7 @@ add_executable(${EXECUTABLE}
${ROOT_DIR}/Src/common/module.cpp
${ROOT_DIR}/Src/periphery/adc/circuit_periphery.cpp
+ ${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp
${PLATFORM_DIR}/stm32f103/adc.cpp
${PLATFORM_DIR}/stm32g0b1/pwm.cpp
${PLATFORM_DIR}/stm32f103/iwdg.cpp
diff --git a/Src/platform/stm32g0b1/pwm.cpp b/Src/platform/stm32g0b1/pwm.cpp
index 5964b34..d7fa101 100644
--- a/Src/platform/stm32g0b1/pwm.cpp
+++ b/Src/platform/stm32g0b1/pwm.cpp
@@ -5,70 +5,18 @@
*/
#include "periphery/pwm/pwm.hpp"
-#include "main.h"
+#include "platform/stm32/pwm/pwm.hpp"
extern TIM_HandleTypeDef htim4;
-namespace HAL {
-struct PwmPinInfo {
- TIM_HandleTypeDef& htim;
- uint32_t channel;
- volatile uint32_t& ccr;
-};
+namespace HAL {
-static const PwmPinInfo info[static_cast(PwmPin::PWM_AMOUNT)] = {
+const std::array(PwmPin::PWM_AMOUNT)> pwms = {{
{.htim = htim4, .channel = TIM_CHANNEL_2, .ccr = TIM4->CCR2}, // PB7
{.htim = htim4, .channel = TIM_CHANNEL_1, .ccr = TIM4->CCR1}, // PB6
{.htim = htim4, .channel = TIM_CHANNEL_4, .ccr = TIM3->CCR4}, // PB9
{.htim = htim4, .channel = TIM_CHANNEL_3, .ccr = TIM3->CCR3}, // PB8
-};
-
-int8_t Pwm::init(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return -1;
- }
-
- 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 Pwm::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return;
- }
-
- info[static_cast(pwm_pin)].ccr = duration_us;
-}
-
-uint32_t Pwm::get_duration(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return 0;
- }
-
- return info[static_cast(pwm_pin)].ccr;
-}
-
-void Pwm::set_frequency(PwmPin pwm_pin, uint32_t frequency_hz) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return;
- }
-
- auto& pwm_pin_info = info[static_cast(pwm_pin)];
- volatile uint32_t* arr_reg = &(pwm_pin_info.htim.Instance->ARR);
- uint16_t period_us = 1000000 / frequency_hz;
- *arr_reg = period_us;
-}
-
-uint32_t Pwm::get_frequency(PwmPin pwm_pin) {
- if (pwm_pin > PwmPin::PWM_AMOUNT) {
- return -1;
- }
-
- auto& pwm_pin_info = info[static_cast(pwm_pin)];
- volatile uint32_t* arr_reg = &(pwm_pin_info.htim.Instance->ARR);
- uint32_t frequency = 1000000 / *arr_reg;
- return frequency;
-}
+}};
} // namespace HAL