summaryrefslogtreecommitdiffhomepage
path: root/digital/io-hub/src/common-cc/servo.stm32.cc
blob: 016128ed0547b1b625837b5c5f4324409f4fc232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// io-hub - Modular Input/Output. {{{
//
// Copyright (C) 2013 Maxime Hadjinlian
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "servo.hh"

#include <libopencm3/stm32/f4/gpio.h>
#include <libopencm3/stm32/f4/rcc.h>
#include <libopencm3/stm32/f4/timer.h>

Servo::Servo ()
{
}

void
Servo::enable ()
{
    rcc_peripheral_enable_clock (&RCC_APB2ENR, RCC_APB2ENR_TIM8EN);
    timer_reset(TIM8);

    timer_set_mode(TIM8,
            TIM_CR1_CKD_CK_INT,
            TIM_CR1_CMS_EDGE,
            TIM_CR1_DIR_UP);

    timer_set_prescaler(TIM8, 40);
    // 66Mhz which seems to be a pretty good fit for servos
    timer_set_period(TIM8, 43290);
    timer_set_repetition_counter(TIM8, 0);
    timer_continuous_mode(TIM8);

    // Cake servo 1
    gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO7);
    gpio_set_af(GPIOC, GPIO_AF3, GPIO7);

    // Cherry servo 2
    gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8);
    gpio_set_af(GPIOC, GPIO_AF3, GPIO8);

    // Cake servo 1
    timer_disable_oc_output(TIM8, TIM_OC2);
    timer_set_oc_mode(TIM8, TIM_OC2, TIM_OCM_PWM1);
    timer_enable_oc_output(TIM8, TIM_OC2);

    // Cherry servo 2
    timer_disable_oc_output(TIM8, TIM_OC3);
    timer_set_oc_mode(TIM8, TIM_OC3, TIM_OCM_PWM1);
    timer_enable_oc_output(TIM8, TIM_OC3);

    timer_enable_counter(TIM8);
    timer_enable_break_main_output(TIM8);

    // Set null position, so the servo doesn't move.
    set_position(SERVO_CAKE, 0);
    set_position(SERVO_CHERRY, 0);
}

void
Servo::set_position(int servo, int pos)
{
    if (servo == SERVO_CAKE)
    {
        timer_set_oc_value(TIM8, TIM_OC2, pos);
    }
    else if (servo == SERVO_CHERRY)
    {
        timer_set_oc_value(TIM8, TIM_OC3, pos);
    }
}