From 1e043463d69f1ae9f772b023c3d9862649c6802b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Foucher?= <cfoucher@laas.fr>
Date: Tue, 7 Feb 2023 11:10:21 +0100
Subject: [PATCH] HRTIM module : - Add a function to obtain current repetition
 value. - Separate Periodic Event enable from its configuration, that will be
 required in future changes.

---
 .../zephyr/public_api/hrtim.h                 | 39 ++++++++++++++++---
 .../zephyr/src/hrtim_common.c                 | 10 ++++-
 .../src/uninterruptible_synchronous_task.cpp  |  3 +-
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/zephyr/modules/owntech_hrtim_driver/zephyr/public_api/hrtim.h b/zephyr/modules/owntech_hrtim_driver/zephyr/public_api/hrtim.h
index c459aa8..3075af4 100644
--- a/zephyr/modules/owntech_hrtim_driver/zephyr/public_api/hrtim.h
+++ b/zephyr/modules/owntech_hrtim_driver/zephyr/public_api/hrtim.h
@@ -97,7 +97,7 @@ void hrtim_init_voltage_leg1_boost_leg2_buck_center_aligned(hrtim_tu_t leg1_tu,
 void hrtim_update_adc_trig_interleaved(uint16_t new_trig, hrtim_tu_t leg1_tu, hrtim_tu_t leg2_tu);
 
 /**
- * @brief Enable interrupt on repetition counter for the chosen timing unit
+ * @brief Configure interrupt on repetition counter for the chosen timing unit
  * @param tu_src timing unit which will be the source for the ISR:
  *         @arg @ref MSTR
  *         @arg @ref TIMA
@@ -107,12 +107,26 @@ void hrtim_update_adc_trig_interleaved(uint16_t new_trig, hrtim_tu_t leg1_tu, hr
  *         @arg @ref TIME
  *         @arg @ref TIMF
  * @param repetition value between 1 and 256 for the repetition counter:
- * period of the event wrt. periods of the HRTIM.
- * E.g. when set to 10, one event will be triggered every 10 HRTIM period.
+ *        period of the event wrt. periods of the HRTIM.
+ *        E.g. when set to 10, one event will be triggered every 10 HRTIM period.
  * @param callback Pointer to a void(void) function that will be called
- * when the event is triggerred.
+ *        when the event is triggerred.
+ */
+void hrtim_PeriodicEvent_configure(hrtim_tu_t tu_src, uint32_t repetition, hrtim_callback_t callback);
+
+/**
+ * @brief Enable interrupt on repetition counter for the chosen timing unit.
+ *        The periodic event configuration must have been done previously.
+ * @param tu_src timing unit which will be the source for the ISR:
+ *         @arg @ref MSTR
+ *         @arg @ref TIMA
+ *         @arg @ref TIMB
+ *         @arg @ref TIMC
+ *         @arg @ref TIMD
+ *         @arg @ref TIME
+ *         @arg @ref TIMF
  */
-void hrtim_PeriodicEvent_en(hrtim_tu_t tu_src, uint32_t repetition, hrtim_callback_t callback);
+void hrtim_PeriodicEvent_en(hrtim_tu_t tu_src);
 
 /**
  * @brief Disable interrupt on repetition counter for the chosen timing unit
@@ -143,6 +157,21 @@ void hrtim_PeriodicEvent_dis(hrtim_tu_t tu_src);
  */
 void hrtim_PeriodicEvent_SetRep(hrtim_tu_t tu_src, uint32_t repetition);
 
+/**
+ * @brief Get the current value of the repetition counter.
+ * @param tu_src timing unit which will be the source for the ISR:
+ *         @arg @ref MSTR
+ *         @arg @ref TIMA
+ *         @arg @ref TIMB
+ *         @arg @ref TIMC
+ *         @arg @ref TIMD
+ *         @arg @ref TIME
+ *         @arg @ref TIMF
+ * @return Value between 1 and 256 for the repetition counter:
+ * period of the event wrt. periods of the HRTIM.
+ */
+uint32_t hrtim_PeriodicEvent_GetRep(hrtim_tu_t tu_src);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/zephyr/modules/owntech_hrtim_driver/zephyr/src/hrtim_common.c b/zephyr/modules/owntech_hrtim_driver/zephyr/src/hrtim_common.c
index 40d30a9..b724b24 100644
--- a/zephyr/modules/owntech_hrtim_driver/zephyr/src/hrtim_common.c
+++ b/zephyr/modules/owntech_hrtim_driver/zephyr/src/hrtim_common.c
@@ -93,7 +93,7 @@ void hrtim_update_adc_trig_interleaved(uint16_t new_trig, hrtim_tu_t leg1_tu, hr
 	}
 }
 
-void hrtim_PeriodicEvent_en(hrtim_tu_t tu_src, uint32_t repetition, hrtim_callback_t callback)
+void hrtim_PeriodicEvent_configure(hrtim_tu_t tu_src, uint32_t repetition, hrtim_callback_t callback)
 {
 	/* Memorize user callback */
 	user_callback = callback;
@@ -102,7 +102,10 @@ void hrtim_PeriodicEvent_en(hrtim_tu_t tu_src, uint32_t repetition, hrtim_callba
 	 * is triggered every "repetition" number of periods.
 	 */
 	LL_HRTIM_TIM_SetRepetition(HRTIM1, tu_src, repetition-1);
+}
 
+void hrtim_PeriodicEvent_en(hrtim_tu_t tu_src)
+{
 	LL_HRTIM_EnableIT_REP(HRTIM1, tu_src);         /* Enabling the interrupt on repetition counter event*/
 
 	IRQ_CONNECT(HRTIM_IRQ_NUMBER, HRTIM_IRQ_PRIO, _hrtim_callback, NULL, HRTIM_IRQ_FLAGS);
@@ -123,6 +126,11 @@ void hrtim_PeriodicEvent_SetRep(hrtim_tu_t tu_src, uint32_t repetition)
 	LL_HRTIM_TIM_SetRepetition(HRTIM1, tu_src, repetition-1);
 }
 
+uint32_t hrtim_PeriodicEvent_GetRep(hrtim_tu_t tu_src)
+{
+	return LL_HRTIM_TIM_GetRepetition(HRTIM1, tu_src)+1;
+}
+
 void hrtim_init_current()
 {
 	hrtim_cm_init();
diff --git a/zephyr/modules/owntech_scheduling/zephyr/src/uninterruptible_synchronous_task.cpp b/zephyr/modules/owntech_scheduling/zephyr/src/uninterruptible_synchronous_task.cpp
index 58c0f1d..39cb10f 100644
--- a/zephyr/modules/owntech_scheduling/zephyr/src/uninterruptible_synchronous_task.cpp
+++ b/zephyr/modules/owntech_scheduling/zephyr/src/uninterruptible_synchronous_task.cpp
@@ -124,7 +124,8 @@ void scheduling_start_uninterruptible_synchronous_task()
 		if ( (repetition == 0) || (user_periodic_task == NULL) )
 			return;
 
-		hrtim_PeriodicEvent_en(MSTR, repetition, user_periodic_task);
+		hrtim_PeriodicEvent_configure(MSTR, repetition, user_periodic_task);
+		hrtim_PeriodicEvent_en(MSTR);
 
 		uninterruptibleTaskStatus = task_status_t::running;
 	}
-- 
GitLab