Merge branch 'development' of https://github.com/borisbstyle/betaflight into fix_naze...
[betaflight.git] / src / main / drivers / timer.h
blobd193bf274dfcb7269c70498d294f5898b79bffe4
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include "io.h"
21 #include "rcc.h"
23 #if !defined(USABLE_TIMER_CHANNEL_COUNT)
24 #define USABLE_TIMER_CHANNEL_COUNT 14
25 #endif
27 typedef uint16_t captureCompare_t; // 16 bit on both 103 and 303, just register access must be 32bit sometimes (use timCCR_t)
29 #if defined(STM32F4)
30 typedef uint32_t timCCR_t;
31 typedef uint32_t timCCER_t;
32 typedef uint32_t timSR_t;
33 typedef uint32_t timCNT_t;
34 #elif defined(STM32F3)
35 typedef uint32_t timCCR_t;
36 typedef uint32_t timCCER_t;
37 typedef uint32_t timSR_t;
38 typedef uint32_t timCNT_t;
39 #elif defined(STM32F1)
40 typedef uint16_t timCCR_t;
41 typedef uint16_t timCCER_t;
42 typedef uint16_t timSR_t;
43 typedef uint16_t timCNT_t;
44 #elif defined(UNIT_TEST)
45 typedef uint32_t timCCR_t;
46 typedef uint32_t timCCER_t;
47 typedef uint32_t timSR_t;
48 typedef uint32_t timCNT_t;
49 #else
50 # error "Unknown CPU defined"
51 #endif
53 // use different types from capture and overflow - multiple overflow handlers are implemented as linked list
54 struct timerCCHandlerRec_s;
55 struct timerOvrHandlerRec_s;
56 typedef void timerCCHandlerCallback(struct timerCCHandlerRec_s* self, uint16_t capture);
57 typedef void timerOvrHandlerCallback(struct timerOvrHandlerRec_s* self, uint16_t capture);
59 typedef struct timerCCHandlerRec_s {
60 timerCCHandlerCallback* fn;
61 } timerCCHandlerRec_t;
63 typedef struct timerOvrHandlerRec_s {
64 timerOvrHandlerCallback* fn;
65 struct timerOvrHandlerRec_s* next;
66 } timerOvrHandlerRec_t;
68 typedef struct timerDef_s {
69 TIM_TypeDef *TIMx;
70 rccPeriphTag_t rcc;
71 } timerDef_t;
73 typedef struct {
74 TIM_TypeDef *tim;
75 ioTag_t pin;
76 uint8_t channel;
77 uint8_t irq;
78 uint8_t outputEnable;
79 ioConfig_t ioMode;
80 #if defined(STM32F3) || defined(STM32F4)
81 uint8_t alternateFunction;
82 #endif
83 uint8_t outputInverted;
84 } timerHardware_t;
86 #ifdef STM32F1
87 #if defined(STM32F10X_XL) || defined(STM32F10X_HD_VL)
88 #define HARDWARE_TIMER_DEFINITION_COUNT 14
89 #elif defined(STM32F10X_HD) || defined(STM32F10X_CL)
90 #define HARDWARE_TIMER_DEFINITION_COUNT 7
91 #else
92 #define HARDWARE_TIMER_DEFINITION_COUNT 4
93 #endif
94 #elif defined(STM32F3)
95 #define HARDWARE_TIMER_DEFINITION_COUNT 10
96 #elif defined(STM32F4)
97 #define HARDWARE_TIMER_DEFINITION_COUNT 14
98 #endif
99 extern const timerHardware_t timerHardware[];
100 extern const timerDef_t timerDefinitions[];
102 typedef enum {
103 TYPE_FREE,
104 TYPE_PWMINPUT,
105 TYPE_PPMINPUT,
106 TYPE_PWMOUTPUT_MOTOR,
107 TYPE_PWMOUTPUT_FAST,
108 TYPE_PWMOUTPUT_SERVO,
109 TYPE_SOFTSERIAL_RX,
110 TYPE_SOFTSERIAL_TX,
111 TYPE_SOFTSERIAL_RXTX, // bidirectional pin for softserial
112 TYPE_SOFTSERIAL_AUXTIMER, // timer channel is used for softserial. No IO function on pin
113 TYPE_ADC,
114 TYPE_SERIAL_RX,
115 TYPE_SERIAL_TX,
116 TYPE_SERIAL_RXTX,
117 TYPE_TIMER
118 } channelType_t;
120 void timerConfigure(const timerHardware_t *timHw, uint16_t period, uint8_t mhz); // This interface should be replaced.
122 void timerChConfigIC(const timerHardware_t *timHw, bool polarityRising, unsigned inputFilterSamples);
123 void timerChConfigICDual(const timerHardware_t* timHw, bool polarityRising, unsigned inputFilterSamples);
124 void timerChICPolarity(const timerHardware_t *timHw, bool polarityRising);
125 volatile timCCR_t* timerChCCR(const timerHardware_t* timHw);
126 volatile timCCR_t* timerChCCRLo(const timerHardware_t* timHw);
127 volatile timCCR_t* timerChCCRHi(const timerHardware_t* timHw);
128 void timerChConfigOC(const timerHardware_t* timHw, bool outEnable, bool stateHigh);
129 void timerChConfigGPIO(const timerHardware_t* timHw, ioConfig_t mode);
131 void timerChCCHandlerInit(timerCCHandlerRec_t *self, timerCCHandlerCallback *fn);
132 void timerChOvrHandlerInit(timerOvrHandlerRec_t *self, timerOvrHandlerCallback *fn);
133 void timerChConfigCallbacks(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallback, timerOvrHandlerRec_t *overflowCallback);
134 void timerChConfigCallbacksDual(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallbackLo, timerCCHandlerRec_t *edgeCallbackHi, timerOvrHandlerRec_t *overflowCallback);
135 void timerChITConfigDualLo(const timerHardware_t* timHw, FunctionalState newState);
136 void timerChITConfig(const timerHardware_t* timHw, FunctionalState newState);
137 void timerChClearCCFlag(const timerHardware_t* timHw);
139 void timerChInit(const timerHardware_t *timHw, channelType_t type, int irqPriority);
141 void timerInit(void);
142 void timerStart(void);
143 void timerForceOverflow(TIM_TypeDef *tim);
145 void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration
147 rccPeriphTag_t timerRCC(TIM_TypeDef *tim);