From ead06db6a21548d24e3004a0027ae19a501bbaf1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu>
Date: Wed, 22 Jan 2025 19:20:49 +0100
Subject: [PATCH] [BSP][RIGv2] Enable up to 12 servos for the Orion project

* Remove usage of TIM5 (used by Miosix)
* Introduce usage of TIM2 (previously used by TimestampTimer, not the case anymore since the update to Miosix 2.7)
* Repurpose unused sensor CS pins to additional timers

Some servos will now share the same timer (but different channels), meaning the same frequency must be used for such cases.
---
 .../interfaces-impl/bsp.cpp                   | 12 ++-
 .../interfaces-impl/hwmapping.h               | 98 ++++++++++++-------
 2 files changed, 73 insertions(+), 37 deletions(-)

diff --git a/src/bsps/stm32f767zi_rig_v2/interfaces-impl/bsp.cpp b/src/bsps/stm32f767zi_rig_v2/interfaces-impl/bsp.cpp
index cbf61373e..02d1867e6 100644
--- a/src/bsps/stm32f767zi_rig_v2/interfaces-impl/bsp.cpp
+++ b/src/bsps/stm32f767zi_rig_v2/interfaces-impl/bsp.cpp
@@ -313,12 +313,16 @@ void IRQbspInit()
 
     interfaces::timers::tim1ch1::alternateFunction(1);
     interfaces::timers::tim1ch1::mode(Mode::ALTERNATE);
+    interfaces::timers::tim2ch3::alternateFunction(1);
+    interfaces::timers::tim2ch3::mode(Mode::ALTERNATE);
+    interfaces::timers::tim2ch4::alternateFunction(1);
+    interfaces::timers::tim2ch4::mode(Mode::ALTERNATE);
     interfaces::timers::tim3ch3::alternateFunction(2);
     interfaces::timers::tim3ch3::mode(Mode::ALTERNATE);
     interfaces::timers::tim4ch1::alternateFunction(2);
     interfaces::timers::tim4ch1::mode(Mode::ALTERNATE);
-    interfaces::timers::tim5ch3::alternateFunction(2);
-    interfaces::timers::tim5ch3::mode(Mode::ALTERNATE);
+    interfaces::timers::tim4ch2::alternateFunction(2);
+    interfaces::timers::tim4ch2::mode(Mode::ALTERNATE);
     interfaces::timers::tim8ch1::alternateFunction(3);
     interfaces::timers::tim8ch1::mode(Mode::ALTERNATE);
     interfaces::timers::tim8ch2::alternateFunction(3);
@@ -346,6 +350,10 @@ void IRQbspInit()
     interfaces::relays::relay4::high();
 
     /* Do not enable Flavio's bullshit
+
+    NOTE: some of the following pins are used for servo timers, ensure there's
+    no conflicts before enabling these.
+
     sensors::H3LIS::cs::mode(Mode::OUTPUT);
     sensors::H3LIS::cs::high();
 
diff --git a/src/bsps/stm32f767zi_rig_v2/interfaces-impl/hwmapping.h b/src/bsps/stm32f767zi_rig_v2/interfaces-impl/hwmapping.h
index 0560321ff..fb082226b 100644
--- a/src/bsps/stm32f767zi_rig_v2/interfaces-impl/hwmapping.h
+++ b/src/bsps/stm32f767zi_rig_v2/interfaces-impl/hwmapping.h
@@ -38,27 +38,6 @@
 #define MIOSIX_RADIO_DIO3_IRQ EXTI9_IRQHandlerImpl
 #define MIOSIX_RADIO_SPI SPI6
 
-#define MIOSIX_SERVOS_1_TIM TIM8
-#define MIOSIX_SERVOS_1_CHANNEL CHANNEL_2
-#define MIOSIX_SERVOS_2_TIM TIM1
-#define MIOSIX_SERVOS_2_CHANNEL CHANNEL_1
-#define MIOSIX_SERVOS_3_TIM TIM10
-#define MIOSIX_SERVOS_3_CHANNEL CHANNEL_1
-#define MIOSIX_SERVOS_4_TIM TIM11
-#define MIOSIX_SERVOS_4_CHANNEL CHANNEL_1
-#define MIOSIX_SERVOS_5_TIM TIM5
-#define MIOSIX_SERVOS_5_CHANNEL CHANNEL_3
-#define MIOSIX_SERVOS_6_TIM TIM3
-#define MIOSIX_SERVOS_6_CHANNEL CHANNEL_3
-#define MIOSIX_SERVOS_7_TIM TIM4
-#define MIOSIX_SERVOS_7_CHANNEL CHANNEL_1
-#define MIOSIX_SERVOS_8_TIM TIM8
-#define MIOSIX_SERVOS_8_CHANNEL CHANNEL_1
-#define MIOSIX_SERVOS_9_TIM TIM12
-#define MIOSIX_SERVOS_9_CHANNEL CHANNEL_2
-#define MIOSIX_SERVOS_10_TIM TIM9
-#define MIOSIX_SERVOS_10_CHANNEL CHANNEL_2
-
 namespace miosix
 {
 
@@ -128,11 +107,13 @@ using rx = Gpio<GPIOB_BASE, 12>;
 namespace timers
 {
 using tim1ch1  = Gpio<GPIOA_BASE, 8>;
+using tim2ch3  = Gpio<GPIOA_BASE, 2>;
+using tim2ch4  = Gpio<GPIOB_BASE, 11>;
 using tim3ch3  = Gpio<GPIOB_BASE, 0>;
 using tim4ch1  = Gpio<GPIOD_BASE, 12>;
-using tim5ch3  = Gpio<GPIOA_BASE, 2>;
+using tim4ch2  = Gpio<GPIOD_BASE, 13>;
 using tim8ch1  = Gpio<GPIOC_BASE, 6>;
-using tim8ch2  = Gpio<GPIOC_BASE, 7>;  // Extra timer
+using tim8ch2  = Gpio<GPIOC_BASE, 7>;
 using tim9ch2  = Gpio<GPIOA_BASE, 3>;
 using tim10ch1 = Gpio<GPIOB_BASE, 8>;
 using tim11ch1 = Gpio<GPIOB_BASE, 9>;
@@ -167,13 +148,15 @@ using cs = Gpio<GPIOB_BASE, 2>;
 // Flavio, why?
 namespace LPS22
 {
-using cs = Gpio<GPIOB_BASE, 11>;
+// NOTE: also used by tim2ch4, ensure there's no conflict before enabling this
+// using cs = Gpio<GPIOB_BASE, 11>;
 }  // namespace LPS22
 
 // Flavio, why?
 namespace LSM6
 {
-using cs = Gpio<GPIOD_BASE, 13>;
+// NOTE: also used by tim4ch2, ensure there's no conflict before enabling this
+// using cs = Gpio<GPIOD_BASE, 13>;
 }  // namespace LSM6
 
 // Flavio, why?
@@ -214,18 +197,63 @@ using dio1 = Gpio<GPIOE_BASE, 4>;
 using dio3 = Gpio<GPIOG_BASE, 9>;
 }  // namespace radio
 
+/**
+ * @brief Servo PWM control PIN definitions
+ *
+ * @note Some servos share the same timer, so they must use the same frequency:
+ * - Servo OX_FIL     and NITR   (TIM8) must use the same frequency
+ * - Servo N2_REL and UNUSED (TIM2) must use the same frequency
+ * - Servo N2_FIL and MAIN   (TIM4) must use the same frequency
+ */
 namespace servos
 {
-using servo1  = miosix::interfaces::timers::tim8ch2;
-using servo2  = miosix::interfaces::timers::tim1ch1;
-using servo3  = miosix::interfaces::timers::tim10ch1;
-using servo4  = miosix::interfaces::timers::tim11ch1;
-using servo5  = miosix::interfaces::timers::tim5ch3;
-using servo6  = miosix::interfaces::timers::tim3ch3;
-using servo7  = miosix::interfaces::timers::tim4ch1;
-using servo8  = miosix::interfaces::timers::tim8ch1;
-using servo9  = miosix::interfaces::timers::tim12ch2;
-using servo10 = miosix::interfaces::timers::tim9ch2;
+#define MIOSIX_SERVOS_OX_FIL_TIM TIM8
+#define MIOSIX_SERVOS_OX_FIL_CHANNEL CHANNEL_2
+using servo_ox_filling = miosix::interfaces::timers::tim8ch2;
+
+#define MIOSIX_SERVOS_OX_REL_TIM TIM1
+#define MIOSIX_SERVOS_OX_REL_CHANNEL CHANNEL_1
+using servo_ox_release = miosix::interfaces::timers::tim1ch1;
+
+#define MIOSIX_SERVOS_OX_DET_TIM TIM10
+#define MIOSIX_SERVOS_OX_DET_CHANNEL CHANNEL_1
+using servo_ox_detach = miosix::interfaces::timers::tim10ch1;
+
+#define MIOSIX_SERVOS_N2_3W_TIM TIM11
+#define MIOSIX_SERVOS_N2_3W_CHANNEL CHANNEL_1
+using servo_n2_3way = miosix::interfaces::timers::tim11ch1;
+
+#define MIOSIX_SERVOS_N2_FIL_TIM TIM4
+#define MIOSIX_SERVOS_N2_FIL_CHANNEL CHANNEL_1
+using servo_n2_filling = miosix::interfaces::timers::tim4ch1;
+
+#define MIOSIX_SERVOS_N2_REL_TIM TIM2
+#define MIOSIX_SERVOS_N2_REL_CHANNEL CHANNEL_3
+using servo_n2_release = miosix::interfaces::timers::tim2ch3;
+
+#define MIOSIX_SERVOS_N2_DET_TIM TIM3
+#define MIOSIX_SERVOS_N2_DET_CHANNEL CHANNEL_3
+using servo_n2_detach = miosix::interfaces::timers::tim3ch3;
+
+#define MIOSIX_SERVOS_NITR_TIM TIM8
+#define MIOSIX_SERVOS_NITR_CHANNEL CHANNEL_1
+using servo_nitrogen = miosix::interfaces::timers::tim8ch1;
+
+#define MIOSIX_SERVOS_OX_VEN_TIM TIM12
+#define MIOSIX_SERVOS_OX_VEN_CHANNEL CHANNEL_2
+using servo_ox_venting = miosix::interfaces::timers::tim12ch2;
+
+#define MIOSIX_SERVOS_N2_QUE_TIM TIM9
+#define MIOSIX_SERVOS_N2_QUE_CHANNEL CHANNEL_2
+using servo_n2_quenching = miosix::interfaces::timers::tim9ch2;
+
+#define MIOSIX_SERVOS_MAIN_TIM TIM4
+#define MIOSIX_SERVOS_MAIN_CHANNEL CHANNEL_2
+using servo_main = miosix::interfaces::timers::tim4ch2;
+
+#define MIOSIX_SERVOS_UNUSED_TIM TIM2
+#define MIOSIX_SERVOS_UNUSED_CHANNEL CHANNEL_4
+using servo_unused = miosix::interfaces::timers::tim2ch4;
 }  // namespace servos
 
 namespace adcs
-- 
GitLab