Skip to content

Commit 0091d6d

Browse files
KurtEiabdalkader
andauthored
Apply suggestions from code review
Co-authored-by: Ibrahim Abdelkader <[email protected]>
1 parent fc280bb commit 0091d6d

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/renesas/Servo.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
#define SERVO_MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
3232
#define SERVO_INVALID_INDEX (255)
3333
// Lower the timer ticks for finer resolution.
34-
#define SERVO_TIMER_TICK_US (100)
3534
#define SERVO_US_PER_CYCLE (20000)
3635
#define SERVO_IO_PORT_ADDR(pn) &((R_PORT0 + ((uint32_t) (R_PORT1 - R_PORT0) * (pn)))->PCNTR3)
37-
#define MIN_CYCLE_OFF_US 50
36+
#define SERVO_MIN_CYCLE_OFF_US 50
3837

3938
// Internal Servo sturct to keep track of RA configuration.
4039
typedef struct {
@@ -65,28 +64,25 @@ static uint32_t active_servos_mask = 0;
6564
static uint32_t active_servos_mask_refresh = 0;
6665

6766

68-
static uint32_t usToticks(uint32_t time_us) {
69-
return (float(servo_ticks_per_cycle) / float(SERVO_US_PER_CYCLE)) * time_us;
67+
static uint32_t us_to_ticks(uint32_t time_us) {
68+
return ((float) servo_ticks_per_cycle / (float) SERVO_US_PER_CYCLE) * time_us;
7069
}
7170

72-
7371
static int servo_timer_config(uint32_t period_us)
7472
{
7573
static bool configured = false;
7674
if (configured == false) {
77-
// Configure and enable the servo timer, for full 20ms
75+
// Configure and enable the servo timer.
7876
uint8_t type = 0;
7977
int8_t channel = FspTimer::get_available_timer(type);
8078
if (channel != -1) {
81-
// lets initially configure the servo to 50ms
8279
servo_timer.begin(TIMER_MODE_PERIODIC, type, channel,
8380
1000000.0f/period_us, 50.0f, servo_timer_callback, nullptr);
8481
servo_timer.set_period_buffer(false); // disable period buffering
8582
servo_timer.setup_overflow_irq(10);
8683
servo_timer.open();
8784
servo_timer.stop();
88-
89-
// Now lets see what the period;
85+
// Read the timer's period count.
9086
servo_ticks_per_cycle = servo_timer.get_period_raw();
9187
min_servo_cycle_low = usToticks(MIN_CYCLE_OFF_US);
9288

@@ -118,11 +114,10 @@ static int servo_timer_stop()
118114
return 0;
119115
}
120116

121-
inline static void updateClockPeriod(uint32_t period) {
117+
inline static void servo_timer_set_period(uint32_t period) {
122118
servo_timer.set_period(period);
123119
}
124120

125-
126121
void servo_timer_callback(timer_callback_args_t *args)
127122
{
128123
(void)args; // remove warning
@@ -132,28 +127,32 @@ void servo_timer_callback(timer_callback_args_t *args)
132127

133128
// See if we need to set a servo back low
134129
if (channel_pin_set_high != 0xff) {
135-
*ra_servos[channel_pin_set_high].io_port = (uint32_t)(ra_servos[channel_pin_set_high].io_mask << 16);
130+
*ra_servos[channel_pin_set_high].io_port = ra_servos[channel_pin_set_high].io_mask << 16;
136131
}
137132

138133
// Find the next servo to set high
139134
while (active_servos_mask_refresh) {
140135
channel = __builtin_ctz(active_servos_mask_refresh);
141136
if (ra_servos[channel].period_us) {
142-
*ra_servos[channel].io_port = (uint32_t)ra_servos[channel].io_mask;
143-
updateClockPeriod(ra_servos[channel].period_ticks);
137+
*ra_servos[channel].io_port = ra_servos[channel].io_mask;
138+
servo_timer_set_period(ra_servos[channel].period_ticks);
144139
channel_pin_set_high = channel;
145-
ticks_accum += ra_servos[channel_pin_set_high].period_ticks;
140+
ticks_accum += ra_servos[channel].period_ticks;
146141
active_servos_mask_refresh &= ~(1 << channel);
147142
return;
148143
}
149144
active_servos_mask_refresh &= ~(1 << channel);
150145
}
151-
152-
// Got to hear we finished processing all servos, now delay to start of next pass.
146+
// Finished processing all servos, now delay to start of next pass.
153147
ticks_accum += min_servo_cycle_low;
154-
uint32_t time_to_next_cycle = (servo_ticks_per_cycle > ticks_accum)? servo_ticks_per_cycle - ticks_accum : min_servo_cycle_low;
148+
uint32_t time_to_next_cycle;
149+
if (servo_ticks_per_cycle > ticks_accum) {
150+
time_to_next_cycle = servo_ticks_per_cycle - ticks_accum;
151+
} else {
152+
time_to_next_cycle = min_servo_cycle_low;
153+
}
155154
ticks_accum = 0;
156-
updateClockPeriod(time_to_next_cycle);
155+
servo_timer_set_period(time_to_next_cycle);
157156
channel_pin_set_high = 0xff;
158157
active_servos_mask_refresh = active_servos_mask;
159158
}
@@ -180,7 +179,7 @@ uint8_t Servo::attach(int pin, int min, int max)
180179
return 0;
181180
}
182181

183-
// Configure and the timer
182+
// Configure the servo timer.
184183
if (servo_timer_config(SERVO_US_PER_CYCLE) != 0) {
185184
return 0;
186185
}
@@ -211,7 +210,7 @@ uint8_t Servo::attach(int pin, int min, int max)
211210
R_IOPORT_PinCfg(&g_ioport_ctrl, io_pin,
212211
IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PORT_OUTPUT_HIGH);
213212

214-
// start the timer if it's not started.
213+
// Start the timer if it's not started.
215214
if (servo_timer_start() != 0) {
216215
return 0;
217216
}
@@ -255,8 +254,7 @@ void Servo::writeMicroseconds(int us)
255254
{
256255
if (servoIndex != SERVO_INVALID_INDEX) {
257256
ra_servo_t *servo = &ra_servos[servoIndex];
258-
//servo->period_count = 0;
259-
servo->period_us = constrain(us, (int)servo->period_min, (int)servo->period_max);
257+
servo->period_us = constrain(us, servo->period_min, servo->period_max);
260258
servo->period_ticks = usToticks(servo->period_us);
261259
}
262260
}

0 commit comments

Comments
 (0)