Skip to content

Add method to allow the GPT Timer period not to be buffered. #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), _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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 2 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 _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;
Expand All @@ -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();
Expand Down