diff --git a/cores/arduino/FspTimer.cpp b/cores/arduino/FspTimer.cpp index f613d5dcd..3e30a1c5f 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), _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,9 +453,15 @@ bool FspTimer::set_period(uint32_t p) { /* -------------------------------------------------------------------------- */ if(type == GPT_TIMER && gpt_timer != nullptr) { + if (_period_buffer) { if (R_GPT_PeriodSet(&(gpt_timer->ctrl), p) != FSP_SUCCESS) { return false; } + } + else { + // Not buffered set it directly + 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::set_period_buffer(bool period_buffer) { +/* -------------------------------------------------------------------------- */ + + if (_period_buffer == (uint8_t)period_buffer) { + return true; + } + + _period_buffer = (uint8_t)period_buffer; + if(type == GPT_TIMER && gpt_timer != nullptr) { + + if (period_buffer) { + 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..c42ddd240 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 _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; @@ -111,6 +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 set_period_buffer(bool period_buffer); bool close(); void enable_pwm_channel(TimerPWMChannel_t pwm_channel); uint32_t get_counter();