26
26
#include " Servo.h"
27
27
#include " ServoTimers.h"
28
28
#include " math.h"
29
+ #include " FspTimer.h"
29
30
30
31
#define SERVO_MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
31
32
#define SERVO_INVALID_INDEX (255 )
@@ -52,33 +53,36 @@ typedef struct {
52
53
// Keep track of the total number of servos attached.
53
54
static size_t n_servos=0 ;
54
55
static ra_servo_t ra_servos[SERVO_MAX_SERVOS];
56
+
57
+ static FspTimer servo_timer;
55
58
static bool servo_timer_started = false ;
59
+ void servo_timer_callback (timer_callback_args_t *args);
56
60
57
61
static int servo_timer_config (uint32_t period_us)
58
62
{
59
63
static bool configured = false ;
60
-
61
64
if (configured == false ) {
62
65
// 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 ;
70
75
}
71
- configured = true ;
72
76
}
73
- return 0 ;
77
+ return configured ? 0 : - 1 ;
74
78
}
75
79
76
80
static int servo_timer_start ()
77
81
{
78
82
// Start the timer if it's not started
79
83
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 ;
82
86
}
83
87
servo_timer_started = true ;
84
88
return 0 ;
@@ -88,8 +92,8 @@ static int servo_timer_stop()
88
92
{
89
93
// Start the timer if it's not started
90
94
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 ;
93
97
}
94
98
servo_timer_started = false ;
95
99
return 0 ;
@@ -101,7 +105,7 @@ void servo_timer_callback(timer_callback_args_t *args)
101
105
ra_servo_t *servo = &ra_servos[i];
102
106
if (servo->period_us ) {
103
107
servo->period_count += SERVO_TIMER_TICK_US;
104
- if (servo->period_count < servo->period_us ) {
108
+ if (servo->period_count <= servo->period_us ) {
105
109
*servo->io_port = (uint32_t ) servo->io_mask ;
106
110
} else {
107
111
*servo->io_port = (uint32_t ) (servo->io_mask << 16 );
0 commit comments