Skip to content

Commit

Permalink
Add method to allow the GPT Timer period not to be buffered
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KurtE committed Sep 10, 2023
1 parent 8911a05 commit 2c8a3bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 38 additions & 1 deletion cores/arduino/FspTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
/* -------------------------------------------------------------------------- */
Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/FspTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 2c8a3bc

Please sign in to comment.