From 2c8a3bc38002be1eb176fb63302cc6688fe3e5c4 Mon Sep 17 00:00:00 2001
From: kurte <kurte@rockisland.com>
Date: Sun, 10 Sep 2023 07:15:19 -0700
Subject: [PATCH 1/3] Add method to allow the GPT Timer period not to be
 buffered

During code review of changes I made to the Servo library:
https://github.com/arduino-libraries/Servo/pull/116

@iabdalkader requested that I move all of the platform specific timer changes into the core instead of being in the hardware specific sub-directory of the Servo library.

This change adds a method to tell the GPT timer, that when I make a change to the pseriod (set_period(p) ) that I want the change to happen now and not be buffered.

And updated the set_period method to check for this and directly set the not buffered register with the new period.
---
 cores/arduino/FspTimer.cpp | 39 +++++++++++++++++++++++++++++++++++++-
 cores/arduino/FspTimer.h   |  3 +++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/cores/arduino/FspTimer.cpp b/cores/arduino/FspTimer.cpp
index f613d5dcd..2345707cb 100644
--- a/cores/arduino/FspTimer.cpp
+++ b/cores/arduino/FspTimer.cpp
@@ -11,7 +11,7 @@ bool FspTimer::force_pwm_reserved = false;
 TimerAvail_t FspTimer::gpt_used_channel[GPT_HOWMANY] = { TIMER_FREE };
 TimerAvail_t FspTimer::agt_used_channel[AGT_HOWMANY] = { TIMER_FREE };
 
-FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER) {
+FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER), _buffer_period(true) {
     // AGT0 is always used for timekeeping (millis() and micros())
     // agt_used_channel[0] = TIMER_USED;
     timer_cfg.cycle_end_irq = FSP_INVALID_VECTOR;
@@ -453,9 +453,15 @@ bool FspTimer::set_period(uint32_t p) {
 /* -------------------------------------------------------------------------- */    
 
     if(type == GPT_TIMER && gpt_timer != nullptr) {
+        if (_buffer_period) {
         if (R_GPT_PeriodSet(&(gpt_timer->ctrl), p) != FSP_SUCCESS) {
             return false;
         }
+    }
+        else {
+            // Not buffered set it directl
+            gpt_timer->ctrl.p_reg->GTPR = p;
+        }
     }
     else if(type == AGT_TIMER && agt_timer != nullptr) {
         if (R_AGT_PeriodSet(&(agt_timer->ctrl), p) != FSP_SUCCESS) {
@@ -470,6 +476,37 @@ bool FspTimer::set_period(uint32_t p) {
 
 
 
+/* -------------------------------------------------------------------------- */
+bool FspTimer::use_period_buffer(bool buffer_period) {
+/* -------------------------------------------------------------------------- */    
+
+    if (_buffer_period == (uint8_t)buffer_period) {
+        return true;
+    }
+
+    _buffer_period = (uint8_t)buffer_period;
+    if(type == GPT_TIMER && gpt_timer != nullptr) {
+
+        if (buffer_period) {
+            gpt_timer->ctrl.p_reg->GTBER_b.PR = 1;
+            gpt_timer->ctrl.p_reg->GTBER_b.BD1 = 0;
+        }
+        else {
+            gpt_timer->ctrl.p_reg->GTBER_b.PR = 0;
+            gpt_timer->ctrl.p_reg->GTBER_b.BD1 = 1;
+        } 
+    }
+    else if(type == AGT_TIMER && agt_timer != nullptr) {
+        // not buffered..
+    }
+    else {
+        return false;
+    }
+    return true;
+}
+
+
+
 /* -------------------------------------------------------------------------- */
 bool FspTimer::open() {
 /* -------------------------------------------------------------------------- */    
diff --git a/cores/arduino/FspTimer.h b/cores/arduino/FspTimer.h
index ffd2c801c..2de778375 100644
--- a/cores/arduino/FspTimer.h
+++ b/cores/arduino/FspTimer.h
@@ -95,6 +95,7 @@ class FspTimer {
     uint32_t _duty_cycle_counts;
     timer_source_div_t _sd;
     uint8_t type;
+    uint8_t _buffer_period;
     void set_period_counts(uint8_t tp, float period, uint32_t max);
     TimerIrqCfg_t get_cfg_for_irq();
     static bool force_pwm_reserved;
@@ -111,6 +112,8 @@ class FspTimer {
     bool reset();
     bool set_duty_cycle(uint32_t const duty_cycle_counts, TimerPWMChannel_t pwm_ch);
     bool set_period(uint32_t p);
+    bool use_period_buffer(bool buffer_period);
+
     bool close();
     void enable_pwm_channel(TimerPWMChannel_t pwm_channel);
     uint32_t get_counter();

From c4f005f54245149de61e4bb22e00ee45bd20cbe4 Mon Sep 17 00:00:00 2001
From: kurte <kurte@rockisland.com>
Date: Tue, 12 Sep 2023 05:08:38 -0700
Subject: [PATCH 2/3] Code review changes

Plus - changes then required to make things match up.

Note: in the header file, I personally believe there should be a blank line above every public API, plus a comment line or lines describing each
of the public APIs, such that they are picked up by context sensitive help.

But that is beyond scope of this PR
---
 cores/arduino/FspTimer.cpp | 12 ++++++------
 cores/arduino/FspTimer.h   |  5 ++---
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/cores/arduino/FspTimer.cpp b/cores/arduino/FspTimer.cpp
index 2345707cb..59f3fb1bf 100644
--- a/cores/arduino/FspTimer.cpp
+++ b/cores/arduino/FspTimer.cpp
@@ -11,7 +11,7 @@ bool FspTimer::force_pwm_reserved = false;
 TimerAvail_t FspTimer::gpt_used_channel[GPT_HOWMANY] = { TIMER_FREE };
 TimerAvail_t FspTimer::agt_used_channel[AGT_HOWMANY] = { TIMER_FREE };
 
-FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER), _buffer_period(true) {
+FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER), _period_buffer(true) {
     // AGT0 is always used for timekeeping (millis() and micros())
     // agt_used_channel[0] = TIMER_USED;
     timer_cfg.cycle_end_irq = FSP_INVALID_VECTOR;
@@ -453,7 +453,7 @@ bool FspTimer::set_period(uint32_t p) {
 /* -------------------------------------------------------------------------- */    
 
     if(type == GPT_TIMER && gpt_timer != nullptr) {
-        if (_buffer_period) {
+        if (_period_buffer) {
         if (R_GPT_PeriodSet(&(gpt_timer->ctrl), p) != FSP_SUCCESS) {
             return false;
         }
@@ -477,17 +477,17 @@ bool FspTimer::set_period(uint32_t p) {
 
 
 /* -------------------------------------------------------------------------- */
-bool FspTimer::use_period_buffer(bool buffer_period) {
+bool FspTimer::set_period_buffer(bool period_buffer) {
 /* -------------------------------------------------------------------------- */    
 
-    if (_buffer_period == (uint8_t)buffer_period) {
+    if (_period_buffer == (uint8_t)period_buffer) {
         return true;
     }
 
-    _buffer_period = (uint8_t)buffer_period;
+    _period_buffer = (uint8_t)period_buffer;
     if(type == GPT_TIMER && gpt_timer != nullptr) {
 
-        if (buffer_period) {
+        if (period_buffer) {
             gpt_timer->ctrl.p_reg->GTBER_b.PR = 1;
             gpt_timer->ctrl.p_reg->GTBER_b.BD1 = 0;
         }
diff --git a/cores/arduino/FspTimer.h b/cores/arduino/FspTimer.h
index 2de778375..c42ddd240 100644
--- a/cores/arduino/FspTimer.h
+++ b/cores/arduino/FspTimer.h
@@ -95,7 +95,7 @@ class FspTimer {
     uint32_t _duty_cycle_counts;
     timer_source_div_t _sd;
     uint8_t type;
-    uint8_t _buffer_period;
+    uint8_t _period_buffer;
     void set_period_counts(uint8_t tp, float period, uint32_t max);
     TimerIrqCfg_t get_cfg_for_irq();
     static bool force_pwm_reserved;
@@ -112,8 +112,7 @@ class FspTimer {
     bool reset();
     bool set_duty_cycle(uint32_t const duty_cycle_counts, TimerPWMChannel_t pwm_ch);
     bool set_period(uint32_t p);
-    bool use_period_buffer(bool buffer_period);
-
+    bool set_period_buffer(bool period_buffer);
     bool close();
     void enable_pwm_channel(TimerPWMChannel_t pwm_channel);
     uint32_t get_counter();

From d8c7ed6792c2446ae5a9fa6a55a7ec3270170838 Mon Sep 17 00:00:00 2001
From: KurtE <kurte@rockisland.com>
Date: Wed, 13 Sep 2023 04:53:28 -0700
Subject: [PATCH 3/3] Update cores/arduino/FspTimer.cpp

Fix typo

Co-authored-by: Ibrahim Abdelkader <i.abdalkader@gmail.com>
---
 cores/arduino/FspTimer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cores/arduino/FspTimer.cpp b/cores/arduino/FspTimer.cpp
index 59f3fb1bf..3e30a1c5f 100644
--- a/cores/arduino/FspTimer.cpp
+++ b/cores/arduino/FspTimer.cpp
@@ -459,7 +459,7 @@ bool FspTimer::set_period(uint32_t p) {
         }
     }
         else {
-            // Not buffered set it directl
+            // Not buffered set it directly
             gpt_timer->ctrl.p_reg->GTPR = p;
         }
     }