summaryrefslogtreecommitdiff
path: root/ucoo/hal/timer/timer.stm32.hh
blob: 6b2f2ddbf9cc9b3ba330c10a026e9bc7a9b81e52 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef ucoo_hal_timer_timer_stm32_hh
#define ucoo_hal_timer_timer_stm32_hh
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/common.hh"

namespace ucoo {

/// Available instances.
enum class TimerInstance
{
    TIM1,
    TIM2,
    TIM3,
    TIM4,
    TIM5,
    TIM10,
    TIM11,
};

template<TimerInstance inst>
struct TimerHardware { };

/// STM32 timer very basic support.
template<TimerInstance inst>
class TimerHard
{
  public:
    /// Maximum timer value.
    static const unsigned int max;
  public:
    /// Destructor, disable.
    ~TimerHard ();
    /// Enable timer.
    template <typename... Options>
    void enable (int freq_hz, bool start = true);
    /// Disable timer.
    void disable ();
    /// Manually start timer.
    void start ();
    /// Set reload value.
    void set_reload (unsigned int value);
    /// Set output compare value.
    void set_output_compare (int ch, unsigned int value);
    /// Read timer current value.
    static unsigned int get_value ();
    /// Read input capture value.
    unsigned int get_input_capture (int ch) const;
    /// Wait until update event.
    void wait_update () const;
    /// Wait until a new input capture value is available.
    void wait_input_capture (int ch) const;
    /// Get timer frequency.
    int get_freq_hz () const { return freq_hz_; }
    /// Enable interrupts on update event.
    void enable_interrupt ();
    /// Disable interrupts on update event.
    void disable_interrupt ();
    /// Clear update event interrupt flag.
    void clear_interrupt ();
  private:
    /// Enable updates (reload value, output compare values...).
    void enable_updates ();
    /// Disable updates.
    void disable_updates ();
  public:
    /// Filter for timer input.
    enum class Filter { OFF, CK_INT_N_2, CK_INT_N_4, CK_INT_N_8,
        DTF_DIV_2_N_6, DTF_DIV_2_N_8, DTF_DIV_4_N_6, DTF_DIV_4_N_8,
        DTF_DIV_8_N_6, DTF_DIV_8_N_8, DTF_DIV_16_N_5, DTF_DIV_16_N_6,
        DTF_DIV_16_N_8, DTF_DIV_32_N_5, DTF_DIV_32_N_6, DTF_DIV_32_N_8 };
    /// Mapping from timer input to input capture.
    enum class Map {
        SAME = 1, // Same timer input (for example TI1 for IC1).
        OTHER = 2, // Other timer input (for example TI2 for IC1).
        TRC = 3, // Internal trigger.
    };
    /// Polarity for input capture or output compare.  Edge is only used for
    /// input.
    enum class Polarity {
        NON_INVERTED = 1,
        INVERTED = 3,
        RISING = NON_INVERTED,
        FALLING = INVERTED,
        BOTH = 11,
    };
    /// Temporarily disable updates.
    class UpdateDisabled
    {
      public:
        /// Disable updates.
        UpdateDisabled (TimerHard<inst> &timer);
        /// Enable updates.
        ~UpdateDisabled ();
      private:
        TimerHard<inst> &timer_;
    };
    friend class UpdateDisabled;
    /// Base class for options.
    struct Option;
    /// Set the automatic reload value.
    template<unsigned int value>
    struct OptionReloadValue;
    /// Set the timer in one pulse mode.  You may want to unset start
    /// parameter.
    struct OptionOnePulse;
    /// Set timer trigger input.
    template<int timer_input>
    struct OptionTrigger;
    /// Set input capture options.
    template<int channel, Filter filter = Filter::OFF, Map map = Map::SAME,
        Polarity polarity = Polarity::NON_INVERTED>
    struct OptionInputCapture;
    /// Set output compare options.  Setup PWM mode 1.
    template<int channel, Polarity polarity = Polarity::NON_INVERTED>
    struct OptionOutputCompare;
    /// Set output compare value.
    template<int channel, unsigned int value>
    struct OptionOutputCompareValue;
  private:
    /// Helper to aggregate all options.
    template<typename... Options>
    struct CombinedOptions;
  private:
    /// When enabled, programmed frequency.
    int freq_hz_ = 0;
    /// Hardware characteristics.
    using Hard = TimerHardware<inst>;
};

} // namespace ucoo

#include "ucoo/hal/timer/timer.stm32.tcc"

#endif // ucoo_hal_timer_timer_stm32_hh