Skip to content

Commit 3b6e6a3

Browse files
committed
add clock source options
1 parent 2716695 commit 3b6e6a3

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

examples/simple.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
int main()
99
{
1010
stdio_init_all();
11-
11+
1212
servo_init();
13+
servo_clock_auto();
14+
1315
servo_attach(A);
1416
servo_attach(B);
1517

include/pico_servo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "pico/stdlib.h"
55

66
int servo_init();
7+
int servo_clock_auto();
8+
int servo_clock_source(uint);
9+
int servo_clock_manual(uint);
710
int servo_attach(uint);
811
int servo_enable();
912
int servo_move_to(uint, uint);

src/pico_servo.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,40 @@ int servo_init()
4949
memset(servo_pos, 0, 32 * sizeof(uint));
5050
memset(servo_pos_buf, 0, 16 * sizeof(uint));
5151

52-
clkdiv = (float)frequency_count_khz(CLOCKS_FC0_SRC_VALUE_PLL_SYS_CLKSRC_PRIMARY) * 1000.f / (FREQ * WRAP);
52+
irq_set_exclusive_handler(PWM_IRQ_WRAP, wrap_cb);
53+
54+
return 0;
55+
}
56+
57+
int servo_clock_auto()
58+
{
59+
return servo_clock_source(CLOCKS_FC0_SRC_VALUE_PLL_SYS_CLKSRC_PRIMARY);
60+
}
61+
62+
int servo_clock_source(uint src)
63+
{
64+
clkdiv = (float)frequency_count_khz(src) * 1000.f / (FREQ * WRAP);
65+
if (clkdiv == 0)
66+
{
67+
return 1;
68+
}
5369
min = 0.025f * WRAP;
5470
max = 0.125f * WRAP;
5571

56-
irq_set_exclusive_handler(PWM_IRQ_WRAP, wrap_cb);
72+
return 0;
73+
}
74+
75+
int servo_clock_manual(uint freq)
76+
{
77+
clkdiv = (float)freq * 1000.f / (FREQ * WRAP);
78+
if (clkdiv == 0)
79+
{
80+
return 1;
81+
}
82+
min = 0.025f * WRAP;
83+
max = 0.125f * WRAP;
84+
85+
return 0;
5786
}
5887

5988
int servo_attach(uint pin)

0 commit comments

Comments
 (0)