Skip to content

Commit 2c8a3bc

Browse files
committed
Add method to allow the GPT Timer period not to be buffered
During code review of changes I made to the Servo library: arduino-libraries/Servo#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.
1 parent 8911a05 commit 2c8a3bc

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cores/arduino/FspTimer.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bool FspTimer::force_pwm_reserved = false;
1111
TimerAvail_t FspTimer::gpt_used_channel[GPT_HOWMANY] = { TIMER_FREE };
1212
TimerAvail_t FspTimer::agt_used_channel[AGT_HOWMANY] = { TIMER_FREE };
1313

14-
FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER) {
14+
FspTimer::FspTimer(): init_ok(false), agt_timer(nullptr), gpt_timer(nullptr), type(GPT_TIMER), _buffer_period(true) {
1515
// AGT0 is always used for timekeeping (millis() and micros())
1616
// agt_used_channel[0] = TIMER_USED;
1717
timer_cfg.cycle_end_irq = FSP_INVALID_VECTOR;
@@ -453,9 +453,15 @@ bool FspTimer::set_period(uint32_t p) {
453453
/* -------------------------------------------------------------------------- */
454454

455455
if(type == GPT_TIMER && gpt_timer != nullptr) {
456+
if (_buffer_period) {
456457
if (R_GPT_PeriodSet(&(gpt_timer->ctrl), p) != FSP_SUCCESS) {
457458
return false;
458459
}
460+
}
461+
else {
462+
// Not buffered set it directl
463+
gpt_timer->ctrl.p_reg->GTPR = p;
464+
}
459465
}
460466
else if(type == AGT_TIMER && agt_timer != nullptr) {
461467
if (R_AGT_PeriodSet(&(agt_timer->ctrl), p) != FSP_SUCCESS) {
@@ -470,6 +476,37 @@ bool FspTimer::set_period(uint32_t p) {
470476

471477

472478

479+
/* -------------------------------------------------------------------------- */
480+
bool FspTimer::use_period_buffer(bool buffer_period) {
481+
/* -------------------------------------------------------------------------- */
482+
483+
if (_buffer_period == (uint8_t)buffer_period) {
484+
return true;
485+
}
486+
487+
_buffer_period = (uint8_t)buffer_period;
488+
if(type == GPT_TIMER && gpt_timer != nullptr) {
489+
490+
if (buffer_period) {
491+
gpt_timer->ctrl.p_reg->GTBER_b.PR = 1;
492+
gpt_timer->ctrl.p_reg->GTBER_b.BD1 = 0;
493+
}
494+
else {
495+
gpt_timer->ctrl.p_reg->GTBER_b.PR = 0;
496+
gpt_timer->ctrl.p_reg->GTBER_b.BD1 = 1;
497+
}
498+
}
499+
else if(type == AGT_TIMER && agt_timer != nullptr) {
500+
// not buffered..
501+
}
502+
else {
503+
return false;
504+
}
505+
return true;
506+
}
507+
508+
509+
473510
/* -------------------------------------------------------------------------- */
474511
bool FspTimer::open() {
475512
/* -------------------------------------------------------------------------- */

cores/arduino/FspTimer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class FspTimer {
9595
uint32_t _duty_cycle_counts;
9696
timer_source_div_t _sd;
9797
uint8_t type;
98+
uint8_t _buffer_period;
9899
void set_period_counts(uint8_t tp, float period, uint32_t max);
99100
TimerIrqCfg_t get_cfg_for_irq();
100101
static bool force_pwm_reserved;
@@ -111,6 +112,8 @@ class FspTimer {
111112
bool reset();
112113
bool set_duty_cycle(uint32_t const duty_cycle_counts, TimerPWMChannel_t pwm_ch);
113114
bool set_period(uint32_t p);
115+
bool use_period_buffer(bool buffer_period);
116+
114117
bool close();
115118
void enable_pwm_channel(TimerPWMChannel_t pwm_channel);
116119
uint32_t get_counter();

0 commit comments

Comments
 (0)