Skip to content

Commit c5a464f

Browse files
committed
Switch to FspTimer API.
1 parent 5d580e2 commit c5a464f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/renesas/Servo.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "Servo.h"
2727
#include "ServoTimers.h"
2828
#include "math.h"
29+
#include "FspTimer.h"
2930

3031
#define SERVO_MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
3132
#define SERVO_INVALID_INDEX (255)
@@ -52,33 +53,36 @@ typedef struct {
5253
// Keep track of the total number of servos attached.
5354
static size_t n_servos=0;
5455
static ra_servo_t ra_servos[SERVO_MAX_SERVOS];
56+
57+
static FspTimer servo_timer;
5558
static bool servo_timer_started = false;
59+
void servo_timer_callback(timer_callback_args_t *args);
5660

5761
static int servo_timer_config(uint32_t period_us)
5862
{
5963
static bool configured = false;
60-
6164
if (configured == false) {
6265
// Configure and enable the servo timer.
63-
uint32_t clksrc_div = servo_timer_cfg.source_div;
64-
uint32_t freq_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKB) >> clksrc_div;
65-
uint32_t period_counts = freq_hz / (1000000 / period_us);
66-
if (R_AGT_Open(&servo_timer_ctrl, &servo_timer_cfg) != FSP_SUCCESS ||
67-
R_AGT_PeriodSet(&servo_timer_ctrl, period_counts) != FSP_SUCCESS ||
68-
R_AGT_Enable(&servo_timer_ctrl) != FSP_SUCCESS) {
69-
return -1;
66+
uint8_t type = 0;
67+
int8_t channel = FspTimer::get_available_timer(type);
68+
if (channel != -1) {
69+
servo_timer.begin(TIMER_MODE_PERIODIC, type, channel,
70+
1000000.0f/period_us, 50.0f, servo_timer_callback, nullptr);
71+
servo_timer.setup_overflow_irq();
72+
servo_timer.open();
73+
servo_timer.stop();
74+
configured = true;
7075
}
71-
configured = true;
7276
}
73-
return 0;
77+
return configured ? 0 : -1;
7478
}
7579

7680
static int servo_timer_start()
7781
{
7882
// Start the timer if it's not started
7983
if (servo_timer_started == false &&
80-
R_AGT_Start(&servo_timer_ctrl) != FSP_SUCCESS) {
81-
return 0;
84+
servo_timer.start() == false) {
85+
return -1;
8286
}
8387
servo_timer_started = true;
8488
return 0;
@@ -88,8 +92,8 @@ static int servo_timer_stop()
8892
{
8993
// Start the timer if it's not started
9094
if (servo_timer_started == true &&
91-
R_AGT_Stop(&servo_timer_ctrl) != FSP_SUCCESS) {
92-
return 0;
95+
servo_timer.stop() == false) {
96+
return -1;
9397
}
9498
servo_timer_started = false;
9599
return 0;
@@ -101,7 +105,7 @@ void servo_timer_callback(timer_callback_args_t *args)
101105
ra_servo_t *servo = &ra_servos[i];
102106
if (servo->period_us) {
103107
servo->period_count += SERVO_TIMER_TICK_US;
104-
if (servo->period_count < servo->period_us) {
108+
if (servo->period_count <= servo->period_us) {
105109
*servo->io_port = (uint32_t) servo->io_mask;
106110
} else {
107111
*servo->io_port = (uint32_t) (servo->io_mask << 16);

0 commit comments

Comments
 (0)