Skip to content

Commit 1231345

Browse files
KurtEiabdalkader
andauthored
Add method to allow the GPT Timer period not to be buffered. (#131)
* 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 period (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. Co-authored-by: Ibrahim Abdelkader <[email protected]>
1 parent 8911a05 commit 1231345

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Diff for: cores/arduino/FspTimer.cpp

+38-1
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), _period_buffer(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 (_period_buffer) {
456457
if (R_GPT_PeriodSet(&(gpt_timer->ctrl), p) != FSP_SUCCESS) {
457458
return false;
458459
}
460+
}
461+
else {
462+
// Not buffered set it directly
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::set_period_buffer(bool period_buffer) {
481+
/* -------------------------------------------------------------------------- */
482+
483+
if (_period_buffer == (uint8_t)period_buffer) {
484+
return true;
485+
}
486+
487+
_period_buffer = (uint8_t)period_buffer;
488+
if(type == GPT_TIMER && gpt_timer != nullptr) {
489+
490+
if (period_buffer) {
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
/* -------------------------------------------------------------------------- */

Diff for: cores/arduino/FspTimer.h

+2
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 _period_buffer;
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,7 @@ 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 set_period_buffer(bool period_buffer);
114116
bool close();
115117
void enable_pwm_channel(TimerPWMChannel_t pwm_channel);
116118
uint32_t get_counter();

0 commit comments

Comments
 (0)